Skip to content

Commit c1ee884

Browse files
committed
multi: let most ActionDB methods take a context
1 parent ec3b38f commit c1ee884

File tree

7 files changed

+37
-31
lines changed

7 files changed

+37
-31
lines changed

firewall/request_logger.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ func (r *RequestLogger) CustomCaveatName() string {
128128

129129
// Intercept processes an RPC middleware interception request and returns the
130130
// interception result which either accepts or rejects the intercepted message.
131-
func (r *RequestLogger) Intercept(_ context.Context,
131+
func (r *RequestLogger) Intercept(ctx context.Context,
132132
req *lnrpc.RPCMiddlewareRequest) (*lnrpc.RPCMiddlewareResponse, error) {
133133

134134
ri, err := NewInfoFromRequest(req)
@@ -156,7 +156,7 @@ func (r *RequestLogger) Intercept(_ context.Context,
156156

157157
// Parse incoming requests and act on them.
158158
case MWRequestTypeRequest:
159-
return mid.RPCErr(req, r.addNewAction(ri, withPayloadData))
159+
return mid.RPCErr(req, r.addNewAction(ctx, ri, withPayloadData))
160160

161161
// Parse and possibly manipulate outgoing responses.
162162
case MWRequestTypeResponse:
@@ -170,7 +170,7 @@ func (r *RequestLogger) Intercept(_ context.Context,
170170
}
171171

172172
return mid.RPCErr(
173-
req, r.MarkAction(ri.RequestID, state, errReason),
173+
req, r.MarkAction(ctx, ri.RequestID, state, errReason),
174174
)
175175

176176
default:
@@ -179,7 +179,7 @@ func (r *RequestLogger) Intercept(_ context.Context,
179179
}
180180

181181
// addNewAction persists the new action to the db.
182-
func (r *RequestLogger) addNewAction(ri *RequestInfo,
182+
func (r *RequestLogger) addNewAction(ctx context.Context, ri *RequestInfo,
183183
withPayloadData bool) error {
184184

185185
// If no macaroon is provided, then an empty 4-byte array is used as the
@@ -223,7 +223,7 @@ func (r *RequestLogger) addNewAction(ri *RequestInfo,
223223
}
224224
}
225225

226-
id, err := r.actionsDB.AddAction(action)
226+
id, err := r.actionsDB.AddAction(ctx, action)
227227
if err != nil {
228228
return err
229229
}
@@ -240,7 +240,7 @@ func (r *RequestLogger) addNewAction(ri *RequestInfo,
240240

241241
// MarkAction can be used to set the state of an action identified by the given
242242
// requestID.
243-
func (r *RequestLogger) MarkAction(reqID uint64,
243+
func (r *RequestLogger) MarkAction(ctx context.Context, reqID uint64,
244244
state firewalldb.ActionState, errReason string) error {
245245

246246
r.mu.Lock()
@@ -252,5 +252,5 @@ func (r *RequestLogger) MarkAction(reqID uint64,
252252
}
253253
delete(r.reqIDToAction, reqID)
254254

255-
return r.actionsDB.SetActionState(actionLocator, state, errReason)
255+
return r.actionsDB.SetActionState(ctx, actionLocator, state, errReason)
256256
}

firewall/rule_enforcer.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ type RuleEnforcer struct {
3232
ruleDB firewalldb.RulesDB
3333
actionsDB firewalldb.ActionReadDBGetter
3434
sessionDB firewalldb.SessionDB
35-
markActionErrored func(reqID uint64, reason string) error
36-
privMapDB firewalldb.PrivacyMapper
35+
markActionErrored func(ctx context.Context, reqID uint64,
36+
reason string) error
37+
privMapDB firewalldb.PrivacyMapper
3738

3839
permsMgr *perms.Manager
3940
getFeaturePerms featurePerms
@@ -63,7 +64,8 @@ func NewRuleEnforcer(ruleDB firewalldb.RulesDB,
6364
routerClient lndclient.RouterClient,
6465
lndClient lndclient.LightningClient, lndConnID string,
6566
ruleMgrs rules.ManagerSet,
66-
markActionErrored func(reqID uint64, reason string) error,
67+
markActionErrored func(ctx context.Context, reqID uint64,
68+
reason string) error,
6769
privMap firewalldb.PrivacyMapper) *RuleEnforcer {
6870

6971
return &RuleEnforcer{
@@ -164,7 +166,9 @@ func (r *RuleEnforcer) Intercept(ctx context.Context,
164166

165167
replacement, err := r.handleRequest(ctx, ri)
166168
if err != nil {
167-
dbErr := r.markActionErrored(ri.RequestID, err.Error())
169+
dbErr := r.markActionErrored(
170+
ctx, ri.RequestID, err.Error(),
171+
)
168172
if dbErr != nil {
169173
log.Error("could not mark action for "+
170174
"request ID %d as Errored: %v",

firewalldb/actions.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,9 @@ func WithActionState(state ActionState) ListActionOption {
181181
// ActionsWriteDB is an abstraction over the Actions DB that will allow a
182182
// caller to add new actions as well as change the values of an existing action.
183183
type ActionsWriteDB interface {
184-
AddAction(action *Action) (uint64, error)
185-
SetActionState(al *ActionLocator, state ActionState,
186-
errReason string) error
184+
AddAction(ctx context.Context, action *Action) (uint64, error)
185+
SetActionState(ctx context.Context, al *ActionLocator,
186+
state ActionState, errReason string) error
187187
}
188188

189189
// RuleAction represents a method call that was performed at a certain time at

firewalldb/actions_kvdb.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ var (
5353
)
5454

5555
// AddAction serialises and adds an Action to the DB under the given sessionID.
56-
func (db *BoltDB) AddAction(action *Action) (uint64, error) {
56+
func (db *BoltDB) AddAction(_ context.Context, action *Action) (uint64, error) {
5757
var buf bytes.Buffer
5858
if err := SerializeAction(&buf, action); err != nil {
5959
return 0, err
@@ -167,8 +167,8 @@ func getAction(actionsBkt *bbolt.Bucket, al *ActionLocator) (*Action, error) {
167167

168168
// SetActionState finds the action specified by the ActionLocator and sets its
169169
// state to the given state.
170-
func (db *BoltDB) SetActionState(al *ActionLocator, state ActionState,
171-
errorReason string) error {
170+
func (db *BoltDB) SetActionState(_ context.Context, al *ActionLocator,
171+
state ActionState, errorReason string) error {
172172

173173
if errorReason != "" && state != ActionStateError {
174174
return fmt.Errorf("error reason should only be set for " +

firewalldb/actions_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ func TestActionStorage(t *testing.T) {
6666
require.NoError(t, err)
6767
require.Len(t, actions, 0)
6868

69-
id, err := db.AddAction(action1)
69+
id, err := db.AddAction(ctx, action1)
7070
require.NoError(t, err)
7171
require.Equal(t, uint64(1), id)
7272

73-
id, err = db.AddAction(action2)
73+
id, err = db.AddAction(ctx, action2)
7474
require.NoError(t, err)
7575
require.Equal(t, uint64(1), id)
7676

@@ -92,7 +92,7 @@ func TestActionStorage(t *testing.T) {
9292
require.Len(t, actions, 0)
9393

9494
err = db.SetActionState(
95-
&ActionLocator{
95+
ctx, &ActionLocator{
9696
SessionID: sessionID2,
9797
ActionID: uint64(1),
9898
}, ActionStateDone, "",
@@ -109,7 +109,7 @@ func TestActionStorage(t *testing.T) {
109109
action2.State = ActionStateDone
110110
require.Equal(t, action2, actions[0])
111111

112-
id, err = db.AddAction(action1)
112+
id, err = db.AddAction(ctx, action1)
113113
require.NoError(t, err)
114114
require.Equal(t, uint64(2), id)
115115

@@ -125,7 +125,7 @@ func TestActionStorage(t *testing.T) {
125125

126126
// Try set an error reason for a non Errored state.
127127
err = db.SetActionState(
128-
&ActionLocator{
128+
ctx, &ActionLocator{
129129
SessionID: sessionID2,
130130
ActionID: uint64(1),
131131
}, ActionStateDone, "hello",
@@ -134,7 +134,7 @@ func TestActionStorage(t *testing.T) {
134134

135135
// Now try move the action to errored with a reason.
136136
err = db.SetActionState(
137-
&ActionLocator{
137+
ctx, &ActionLocator{
138138
SessionID: sessionID2,
139139
ActionID: uint64(1),
140140
}, ActionStateError, "fail whale",
@@ -184,7 +184,7 @@ func TestListActions(t *testing.T) {
184184
State: ActionStateDone,
185185
}
186186

187-
_, err := db.AddAction(action)
187+
_, err := db.AddAction(ctx, action)
188188
require.NoError(t, err)
189189
}
190190

@@ -373,7 +373,7 @@ func TestListGroupActions(t *testing.T) {
373373
require.Empty(t, al)
374374

375375
// Add an action under session 1.
376-
_, err = db.AddAction(action1)
376+
_, err = db.AddAction(ctx, action1)
377377
require.NoError(t, err)
378378

379379
// There should now be one action in the group.
@@ -383,7 +383,7 @@ func TestListGroupActions(t *testing.T) {
383383
require.Equal(t, sessionID1, al[0].SessionID)
384384

385385
// Add an action under session 2.
386-
_, err = db.AddAction(action2)
386+
_, err = db.AddAction(ctx, action2)
387387
require.NoError(t, err)
388388

389389
// There should now be actions in the group.

firewalldb/interface.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,12 @@ type PrivacyMapper interface {
105105
// the Action persistence and querying.
106106
type ActionDB interface {
107107
// AddAction persists the given action to the database.
108-
AddAction(action *Action) (uint64, error)
108+
AddAction(ctx context.Context, action *Action) (uint64, error)
109109

110110
// SetActionState finds the action specified by the ActionLocator and
111111
// sets its state to the given state.
112-
SetActionState(al *ActionLocator, state ActionState,
113-
errReason string) error
112+
SetActionState(ctx context.Context, al *ActionLocator,
113+
state ActionState, errReason string) error
114114

115115
// ListActions returns a list of Actions that pass the filterFn
116116
// requirements. The query IndexOffset and MaxNum params can be used to

terminal.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,9 +1118,11 @@ func (g *LightningTerminal) startInternalSubServers(ctx context.Context,
11181118
g.permsMgr, g.lndClient.NodePubkey,
11191119
g.lndClient.Router,
11201120
g.lndClient.Client, g.lndConnID, g.ruleMgrs,
1121-
func(reqID uint64, reason string) error {
1121+
func(ctx context.Context, reqID uint64,
1122+
reason string) error {
1123+
11221124
return requestLogger.MarkAction(
1123-
reqID, firewalldb.ActionStateError,
1125+
ctx, reqID, firewalldb.ActionStateError,
11241126
reason,
11251127
)
11261128
}, g.stores.firewall,

0 commit comments

Comments
 (0)