Skip to content

Commit bd704c4

Browse files
authored
Merge pull request #1078 from lightninglabs/sql40
[sql-40] firewalldb: action store prep commits
2 parents d0eaa3d + ed96f93 commit bd704c4

File tree

5 files changed

+66
-26
lines changed

5 files changed

+66
-26
lines changed

firewall/request_logger.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/lightninglabs/lightning-terminal/firewalldb"
1010
mid "github.com/lightninglabs/lightning-terminal/rpcmiddleware"
1111
"github.com/lightninglabs/lightning-terminal/session"
12+
"github.com/lightningnetwork/lnd/fn"
1213
"github.com/lightningnetwork/lnd/lnrpc"
1314
"github.com/lightningnetwork/lnd/macaroons"
1415
)
@@ -181,16 +182,15 @@ func (r *RequestLogger) Intercept(ctx context.Context,
181182
func (r *RequestLogger) addNewAction(ctx context.Context, ri *RequestInfo,
182183
withPayloadData bool) error {
183184

184-
// If no macaroon is provided, then an empty 4-byte array is used as the
185-
// macaroon ID. Otherwise, the last 4 bytes of the macaroon's root key
186-
// ID are used.
187-
var macaroonID [4]byte
185+
var macaroonID fn.Option[[4]byte]
188186
if ri.Macaroon != nil {
189187
var err error
190-
macaroonID, err = session.IDFromMacaroon(ri.Macaroon)
188+
macID, err := session.IDFromMacaroon(ri.Macaroon)
191189
if err != nil {
192190
return fmt.Errorf("could not extract ID from macaroon")
193191
}
192+
193+
macaroonID = fn.Some([4]byte(macID))
194194
}
195195

196196
actionReq := &firewalldb.AddActionReq{

firewalldb/actions.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ const (
3636
type AddActionReq struct {
3737
// MacaroonIdentifier is a 4 byte identifier created from the last 4
3838
// bytes of the root key ID of the macaroon used to perform the action.
39-
MacaroonIdentifier [4]byte
39+
// If no macaroon was used for the action, then this will not be set.
40+
MacaroonIdentifier fn.Option[[4]byte]
4041

4142
// SessionID holds the optional session ID of the session that this
4243
// action was performed with.

firewalldb/actions_kvdb.go

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ var (
5858
func (db *BoltDB) AddAction(ctx context.Context,
5959
req *AddActionReq) (ActionLocator, error) {
6060

61+
// If no macaroon is provided, then an empty 4-byte array is used as the
62+
// macaroon ID.
63+
var macaroonID [4]byte
64+
req.MacaroonIdentifier.WhenSome(func(id [4]byte) {
65+
macaroonID = id
66+
})
67+
6168
// If the new action links to a session, the session must exist.
6269
// For the bbolt impl of the store, this is our best effort attempt
6370
// at ensuring each action links to a session. If the session is
@@ -105,7 +112,7 @@ func (db *BoltDB) AddAction(ctx context.Context,
105112
}
106113

107114
sessBucket, err := actionsBucket.CreateBucketIfNotExists(
108-
action.MacaroonIdentifier[:],
115+
macaroonID[:],
109116
)
110117
if err != nil {
111118
return err
@@ -134,7 +141,7 @@ func (db *BoltDB) AddAction(ctx context.Context,
134141
}
135142

136143
locator = kvdbActionLocator{
137-
sessionID: action.MacaroonIdentifier,
144+
sessionID: macaroonID,
138145
actionID: nextActionIndex,
139146
}
140147

@@ -323,7 +330,13 @@ func (db *BoltDB) ListActions(ctx context.Context, query *ListActionsQuery,
323330
)
324331
}
325332
if opts.groupID != session.EmptyID {
326-
actions, err := db.listGroupActions(ctx, opts.groupID, filterFn)
333+
var reversed bool
334+
if query != nil {
335+
reversed = query.Reversed
336+
}
337+
actions, err := db.listGroupActions(
338+
ctx, opts.groupID, filterFn, reversed,
339+
)
327340
if err != nil {
328341
return nil, 0, 0, err
329342
}
@@ -432,11 +445,11 @@ func (db *BoltDB) listSessionActions(sessionID session.ID,
432445
//
433446
// TODO: update to allow for pagination.
434447
func (db *BoltDB) listGroupActions(ctx context.Context, groupID session.ID,
435-
filterFn listActionsFilterFn) ([]*Action, error) {
448+
filterFn listActionsFilterFn, reversed bool) ([]*Action, error) {
436449

437450
if filterFn == nil {
438451
filterFn = func(a *Action, reversed bool) (bool, bool) {
439-
return true, true
452+
return true, reversed
440453
}
441454
}
442455

@@ -475,9 +488,18 @@ func (db *BoltDB) listGroupActions(ctx context.Context, groupID session.ID,
475488
return err
476489
}
477490

478-
include, cont := filterFn(action, false)
491+
include, cont := filterFn(action, reversed)
479492
if include {
480-
actions = append(actions, action)
493+
if !reversed {
494+
actions = append(
495+
actions, action,
496+
)
497+
} else {
498+
actions = append(
499+
[]*Action{action},
500+
actions...,
501+
)
502+
}
481503
}
482504

483505
if !cont {
@@ -574,7 +596,7 @@ func DeserializeAction(r io.Reader, sessionID session.ID) (*Action, error) {
574596
return nil, err
575597
}
576598

577-
action.MacaroonIdentifier = sessionID
599+
action.MacaroonIdentifier = fn.Some([4]byte(sessionID))
578600
action.SessionID = fn.Some(sessionID)
579601
action.ActorName = string(actor)
580602
action.FeatureName = string(featureName)

firewalldb/actions_test.go

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import (
1414
)
1515

1616
var (
17-
testTime1 = time.Unix(32100, 0)
18-
testTime2 = time.Unix(12300, 0)
17+
testTime1 = time.Unix(12300, 0)
18+
testTime2 = time.Unix(32100, 0)
1919
)
2020

2121
// TestActionStorage tests that the ActionsListDB CRUD logic.
@@ -67,7 +67,7 @@ func TestActionStorage(t *testing.T) {
6767
action1Req := &AddActionReq{
6868
SessionID: fn.Some(sess1.ID),
6969
AccountID: fn.Some(acct1.ID),
70-
MacaroonIdentifier: sess1.ID,
70+
MacaroonIdentifier: fn.Some([4]byte(sess1.ID)),
7171
ActorName: "Autopilot",
7272
FeatureName: "auto-fees",
7373
Trigger: "fee too low",
@@ -85,7 +85,7 @@ func TestActionStorage(t *testing.T) {
8585

8686
action2Req := &AddActionReq{
8787
SessionID: fn.Some(sess2.ID),
88-
MacaroonIdentifier: sess2.ID,
88+
MacaroonIdentifier: fn.Some([4]byte(sess2.ID)),
8989
ActorName: "Autopilot",
9090
FeatureName: "rebalancer",
9191
Trigger: "channels not balanced",
@@ -161,7 +161,7 @@ func TestActionStorage(t *testing.T) {
161161

162162
// Check that providing no session id and no filter function returns
163163
// all the actions.
164-
actions, _, _, err = db.ListActions(nil, &ListActionsQuery{
164+
actions, _, _, err = db.ListActions(ctx, &ListActionsQuery{
165165
IndexOffset: 0,
166166
MaxNum: 100,
167167
Reversed: false,
@@ -223,7 +223,7 @@ func TestListActions(t *testing.T) {
223223
actionIds++
224224

225225
actionReq := &AddActionReq{
226-
MacaroonIdentifier: sessionID,
226+
MacaroonIdentifier: fn.Some(sessionID),
227227
ActorName: "Autopilot",
228228
FeatureName: fmt.Sprintf("%d", actionIds),
229229
Trigger: "fee too low",
@@ -245,9 +245,11 @@ func TestListActions(t *testing.T) {
245245
assertActions := func(dbActions []*Action, al []*action) {
246246
require.Len(t, dbActions, len(al))
247247
for i, a := range al {
248-
require.EqualValues(
249-
t, a.sessionID, dbActions[i].MacaroonIdentifier,
248+
mID, err := dbActions[i].MacaroonIdentifier.UnwrapOrErr(
249+
fmt.Errorf("macaroon identifier is none"),
250250
)
251+
require.NoError(t, err)
252+
require.EqualValues(t, a.sessionID, mID)
251253
require.Equal(t, a.actionID, dbActions[i].FeatureName)
252254
}
253255
}
@@ -433,7 +435,7 @@ func TestListGroupActions(t *testing.T) {
433435

434436
action1Req := &AddActionReq{
435437
SessionID: fn.Some(sess1.ID),
436-
MacaroonIdentifier: sess1.ID,
438+
MacaroonIdentifier: fn.Some([4]byte(sess1.ID)),
437439
ActorName: "Autopilot",
438440
FeatureName: "auto-fees",
439441
Trigger: "fee too low",
@@ -451,7 +453,7 @@ func TestListGroupActions(t *testing.T) {
451453

452454
action2Req := &AddActionReq{
453455
SessionID: fn.Some(sess2.ID),
454-
MacaroonIdentifier: sess2.ID,
456+
MacaroonIdentifier: fn.Some([4]byte(sess2.ID)),
455457
ActorName: "Autopilot",
456458
FeatureName: "rebalancer",
457459
Trigger: "channels not balanced",
@@ -495,12 +497,22 @@ func TestListGroupActions(t *testing.T) {
495497
_, err = db.AddAction(ctx, action2Req)
496498
require.NoError(t, err)
497499

498-
// There should now be actions in the group.
500+
// There should now be two actions in the group.
499501
al, _, _, err = db.ListActions(ctx, nil, WithActionGroupID(group1))
500502
require.NoError(t, err)
501503
require.Len(t, al, 2)
502504
assertEqualActions(t, action1, al[0])
503505
assertEqualActions(t, action2, al[1])
506+
507+
// Try the reversed query too.
508+
al, _, _, err = db.ListActions(
509+
ctx, &ListActionsQuery{Reversed: true},
510+
WithActionGroupID(group1),
511+
)
512+
require.NoError(t, err)
513+
require.Len(t, al, 2)
514+
assertEqualActions(t, action2, al[0])
515+
assertEqualActions(t, action1, al[1])
504516
}
505517

506518
func assertEqualActions(t *testing.T, expected, got *Action) {

session_rpcserver.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -806,9 +806,14 @@ func (s *sessionRpcServer) ListActions(ctx context.Context,
806806
sessionID = id
807807
})
808808

809+
var macID [4]byte
810+
a.MacaroonIdentifier.WhenSome(func(id [4]byte) {
811+
macID = id
812+
})
813+
809814
resp[i] = &litrpc.Action{
810815
SessionId: sessionID[:],
811-
MacaroonIdentifier: a.MacaroonIdentifier[:],
816+
MacaroonIdentifier: macID[:],
812817
ActorName: a.ActorName,
813818
FeatureName: a.FeatureName,
814819
Trigger: a.Trigger,

0 commit comments

Comments
 (0)