Skip to content

Commit 01bc36c

Browse files
committed
multi: remove AddActionReq MacaroonIdentifier
As the `MacaroonRootKeyID` field of the `AddActionReq` struct also contains the 4 bytes of the `MacaroonIdentifier`, we change all call sites to instead use the last 4 bytes of the `MacaroonRootKeyID` field. As the `MacaroonIdentifier` field therefore becomes redundant, we also remove it.
1 parent 651cc67 commit 01bc36c

File tree

6 files changed

+38
-58
lines changed

6 files changed

+38
-58
lines changed

firewall/request_logger.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"github.com/lightninglabs/lightning-terminal/firewalldb"
1010
litmac "github.com/lightninglabs/lightning-terminal/macaroons"
1111
mid "github.com/lightninglabs/lightning-terminal/rpcmiddleware"
12-
"github.com/lightninglabs/lightning-terminal/session"
1312
"github.com/lightningnetwork/lnd/fn"
1413
"github.com/lightningnetwork/lnd/lnrpc"
1514
"github.com/lightningnetwork/lnd/macaroons"
@@ -184,8 +183,7 @@ func (r *RequestLogger) addNewAction(ctx context.Context, ri *RequestInfo,
184183
withPayloadData bool) error {
185184

186185
var (
187-
rootKeyID fn.Option[uint64]
188-
macaroonID fn.Option[[4]byte]
186+
rootKeyID fn.Option[uint64]
189187
)
190188

191189
if ri.Macaroon != nil {
@@ -198,19 +196,14 @@ func (r *RequestLogger) addNewAction(ctx context.Context, ri *RequestInfo,
198196
return fmt.Errorf("could not extract root key ID from "+
199197
"macaroon: %w", err)
200198
}
201-
202-
macID := session.IDFromMacRootKeyID(fullRootKeyID)
203-
204199
rootKeyID = fn.Some(fullRootKeyID)
205-
macaroonID = fn.Some([4]byte(macID))
206200
}
207201

208202
actionReq := &firewalldb.AddActionReq{
209-
SessionID: ri.SessionID,
210-
AccountID: ri.AccountID,
211-
MacaroonIdentifier: macaroonID,
212-
MacaroonRootKeyID: rootKeyID,
213-
RPCMethod: ri.URI,
203+
SessionID: ri.SessionID,
204+
AccountID: ri.AccountID,
205+
MacaroonRootKeyID: rootKeyID,
206+
RPCMethod: ri.URI,
214207
}
215208

216209
if withPayloadData {

firewalldb/actions.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,6 @@ const (
3434
// It contains all the information that is needed to create a new Action in the
3535
// ActionStateInit State.
3636
type AddActionReq struct {
37-
// MacaroonIdentifier is a 4 byte identifier created from the last 4
38-
// bytes of the root key ID of the macaroon used to perform the action.
39-
// If no macaroon was used for the action, then this will not be set.
40-
MacaroonIdentifier fn.Option[[4]byte]
41-
4237
// MacaroonRootKeyID is the uint64 / full 8 bytes of the root key ID of
4338
// the macaroon used to perform the action.
4439
// If no macaroon was used for the action, then this will not be set.
@@ -52,8 +47,8 @@ type AddActionReq struct {
5247
// action was performed with.
5348
//
5449
// NOTE: for our BoltDB impl, this is not persisted in any way, and we
55-
// populate it by casting the macaroon ID to a session.ID and so is not
56-
// guaranteed to be linked to an existing session.
50+
// populate it by casting the MacaroonRootKeyID to a session.ID and so
51+
// is not guaranteed to be linked to an existing session.
5752
SessionID fn.Option[session.ID]
5853

5954
// AccountID holds the optional account ID of the account that this

firewalldb/actions_kvdb.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,11 @@ func (db *BoltDB) AddAction(ctx context.Context,
5959
req *AddActionReq) (ActionLocator, error) {
6060

6161
// If no macaroon is provided, then an empty 4-byte array is used as the
62-
// macaroon ID.
62+
// macaroon ID. Note that the kvdb implementation only stores the last
63+
// 4 bytes of the macaroon root key ID.
6364
var macaroonID [4]byte
64-
req.MacaroonIdentifier.WhenSome(func(id [4]byte) {
65-
macaroonID = id
65+
req.MacaroonRootKeyID.WhenSome(func(rootID uint64) {
66+
macaroonID = session.IDFromMacRootKeyID(rootID)
6667
})
6768

6869
// If the new action links to a session, the session must exist.
@@ -601,7 +602,6 @@ func DeserializeAction(r io.Reader, sessionID session.ID) (*Action, error) {
601602
// the first 4 bytes with zeroes.
602603
rootKeyID := uint64(binary.BigEndian.Uint32(sessionID[:]))
603604

604-
action.MacaroonIdentifier = fn.Some([4]byte(sessionID))
605605
action.MacaroonRootKeyID = fn.Some(rootKeyID)
606606
action.SessionID = fn.Some(sessionID)
607607
action.ActorName = string(actor)

firewalldb/actions_sql.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -400,20 +400,15 @@ func unmarshalAction(ctx context.Context, db SQLActionQueries,
400400
// Note that we export the full 8 byte macaroon root key ID in the sql
401401
// actions DB, while the kvdb version persists and exports stored the
402402
// last 4 bytes only.
403-
var macID fn.Option[[4]byte]
404403
var macRootKeyID fn.Option[uint64]
405-
if len(dbAction.MacaroonIdentifier) >= 4 {
406-
dbMacID := dbAction.MacaroonIdentifier
407-
macID = fn.Some([4]byte(dbMacID[len(dbMacID)-4:]))
408-
409-
if len(dbAction.MacaroonIdentifier) >= 8 {
410-
macRootKeyID = fn.Some(binary.BigEndian.Uint64(dbMacID))
411-
}
404+
if len(dbAction.MacaroonIdentifier) >= 8 {
405+
macRootKeyID = fn.Some(
406+
binary.BigEndian.Uint64(dbAction.MacaroonIdentifier),
407+
)
412408
}
413409

414410
return &Action{
415411
AddActionReq: AddActionReq{
416-
MacaroonIdentifier: macID,
417412
MacaroonRootKeyID: macRootKeyID,
418413
AccountID: legacyAcctID,
419414
SessionID: legacySessID,

firewalldb/actions_test.go

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ func TestActionStorage(t *testing.T) {
6666
action1Req := &AddActionReq{
6767
SessionID: fn.Some(sess1.ID),
6868
AccountID: fn.Some(acct1.ID),
69-
MacaroonIdentifier: fn.Some([4]byte(sess1.ID)),
7069
MacaroonRootKeyID: fn.Some(sess1RootKeyID),
7170
ActorName: "Autopilot",
7271
FeatureName: "auto-fees",
@@ -86,15 +85,14 @@ func TestActionStorage(t *testing.T) {
8685
sess2RootKeyID := litmac.NewSuperMacaroonRootKeyID(sess2.ID)
8786

8887
action2Req := &AddActionReq{
89-
SessionID: fn.Some(sess2.ID),
90-
MacaroonIdentifier: fn.Some([4]byte(sess2.ID)),
91-
MacaroonRootKeyID: fn.Some(sess2RootKeyID),
92-
ActorName: "Autopilot",
93-
FeatureName: "rebalancer",
94-
Trigger: "channels not balanced",
95-
Intent: "balance",
96-
RPCMethod: "SendToRoute",
97-
RPCParamsJson: []byte("hops, amount"),
88+
SessionID: fn.Some(sess2.ID),
89+
MacaroonRootKeyID: fn.Some(sess2RootKeyID),
90+
ActorName: "Autopilot",
91+
FeatureName: "rebalancer",
92+
Trigger: "channels not balanced",
93+
Intent: "balance",
94+
RPCMethod: "SendToRoute",
95+
RPCParamsJson: []byte("hops, amount"),
9896
}
9997

10098
action2 := &Action{
@@ -223,7 +221,6 @@ func TestListActions(t *testing.T) {
223221
sessRootKeyID := litmac.NewSuperMacaroonRootKeyID(sessionID)
224222

225223
actionReq := &AddActionReq{
226-
MacaroonIdentifier: fn.Some(sessionID),
227224
MacaroonRootKeyID: fn.Some(sessRootKeyID),
228225
ActorName: "Autopilot",
229226
FeatureName: fmt.Sprintf("%d", actionIds),
@@ -246,11 +243,13 @@ func TestListActions(t *testing.T) {
246243
assertActions := func(dbActions []*Action, al []*action) {
247244
require.Len(t, dbActions, len(al))
248245
for i, a := range al {
249-
mID, err := dbActions[i].MacaroonIdentifier.UnwrapOrErr(
250-
fmt.Errorf("macaroon identifier is none"),
246+
rID, err := dbActions[i].MacaroonRootKeyID.UnwrapOrErr(
247+
fmt.Errorf("macaroon root key is none"),
251248
)
252249
require.NoError(t, err)
253-
require.EqualValues(t, a.sessionID, mID)
250+
require.EqualValues(
251+
t, a.sessionID, session.IDFromMacRootKeyID(rID),
252+
)
254253
require.Equal(t, a.actionID, dbActions[i].FeatureName)
255254
}
256255
}
@@ -438,7 +437,6 @@ func TestListGroupActions(t *testing.T) {
438437

439438
action1Req := &AddActionReq{
440439
SessionID: fn.Some(sess1.ID),
441-
MacaroonIdentifier: fn.Some([4]byte(sess1.ID)),
442440
MacaroonRootKeyID: fn.Some(sess1RootKeyID),
443441
ActorName: "Autopilot",
444442
FeatureName: "auto-fees",
@@ -458,15 +456,14 @@ func TestListGroupActions(t *testing.T) {
458456
sess2RootKeyID := litmac.NewSuperMacaroonRootKeyID(sess2.ID)
459457

460458
action2Req := &AddActionReq{
461-
SessionID: fn.Some(sess2.ID),
462-
MacaroonIdentifier: fn.Some([4]byte(sess2.ID)),
463-
MacaroonRootKeyID: fn.Some(sess2RootKeyID),
464-
ActorName: "Autopilot",
465-
FeatureName: "rebalancer",
466-
Trigger: "channels not balanced",
467-
Intent: "balance",
468-
RPCMethod: "SendToRoute",
469-
RPCParamsJson: []byte("hops, amount"),
459+
SessionID: fn.Some(sess2.ID),
460+
MacaroonRootKeyID: fn.Some(sess2RootKeyID),
461+
ActorName: "Autopilot",
462+
FeatureName: "rebalancer",
463+
Trigger: "channels not balanced",
464+
Intent: "balance",
465+
RPCMethod: "SendToRoute",
466+
RPCParamsJson: []byte("hops, amount"),
470467
}
471468

472469
action2 := &Action{

session_rpcserver.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -818,8 +818,8 @@ func (s *sessionRpcServer) ListActions(ctx context.Context,
818818
})
819819

820820
var macID [4]byte
821-
a.MacaroonIdentifier.WhenSome(func(id [4]byte) {
822-
macID = id
821+
a.MacaroonRootKeyID.WhenSome(func(rootID uint64) {
822+
macID = session.IDFromMacRootKeyID(rootID)
823823
})
824824

825825
resp[i] = &litrpc.Action{

0 commit comments

Comments
 (0)