Skip to content

Commit 3e963c0

Browse files
committed
multi: rename Action.SessionID to MacaroonIdentifier
To make it very clear what the data is actually derived from. Then also add an optional Session.ID. Our bbolt db wont store this real session ID and will populate it in a best effort manner by casting the persisted MacaroonIdentifier.
1 parent b60dc29 commit 3e963c0

File tree

5 files changed

+42
-23
lines changed

5 files changed

+42
-23
lines changed

firewall/request_logger.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,19 +182,20 @@ func (r *RequestLogger) addNewAction(ctx context.Context, ri *RequestInfo,
182182
withPayloadData bool) error {
183183

184184
// If no macaroon is provided, then an empty 4-byte array is used as the
185-
// session ID. Otherwise, the macaroon is used to derive a session ID.
186-
var sessionID [4]byte
185+
// macaroon ID. Otherwise, the last 4 bytes of the macaroon's root key
186+
// ID are used.
187+
var macaroonID [4]byte
187188
if ri.Macaroon != nil {
188189
var err error
189-
sessionID, err = session.IDFromMacaroon(ri.Macaroon)
190+
macaroonID, err = session.IDFromMacaroon(ri.Macaroon)
190191
if err != nil {
191192
return fmt.Errorf("could not extract ID from macaroon")
192193
}
193194
}
194195

195196
actionReq := &firewalldb.AddActionReq{
196-
SessionID: sessionID,
197-
RPCMethod: ri.URI,
197+
MacaroonIdentifier: macaroonID,
198+
RPCMethod: ri.URI,
198199
}
199200

200201
if withPayloadData {

firewalldb/actions.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"time"
66

77
"github.com/lightninglabs/lightning-terminal/session"
8+
"github.com/lightningnetwork/lnd/fn"
89
)
910

1011
// ActionState represents the state of an action.
@@ -32,10 +33,17 @@ const (
3233
// It contains all the information that is needed to create a new Action in the
3334
// ActionStateInit State.
3435
type AddActionReq struct {
35-
// SessionID is the ID of the session that this action belongs to.
36-
// Note that this is not serialized on persistence since the action is
37-
// already stored under a bucket identified by the session ID.
38-
SessionID session.ID
36+
// MacaroonIdentifier is a 4 byte identifier created from the last 4
37+
// bytes of the root key ID of the macaroon used to perform the action.
38+
MacaroonIdentifier [4]byte
39+
40+
// SessionID holds the optional session ID of the session that this
41+
// action was performed with.
42+
//
43+
// NOTE: for our BoltDB impl, this is not persisted in any way, and we
44+
// populate it by casting the macaroon ID to a session.ID and so is not
45+
// guaranteed to be linked to an existing session.
46+
SessionID fn.Option[session.ID]
3947

4048
// ActorName is the name of the entity who performed the Action.
4149
ActorName string

firewalldb/actions_kvdb.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"time"
1111

1212
"github.com/lightninglabs/lightning-terminal/session"
13+
"github.com/lightningnetwork/lnd/fn"
1314
"github.com/lightningnetwork/lnd/tlv"
1415
"go.etcd.io/bbolt"
1516
)
@@ -80,7 +81,7 @@ func (db *BoltDB) AddAction(_ context.Context,
8081
}
8182

8283
sessBucket, err := actionsBucket.CreateBucketIfNotExists(
83-
action.SessionID[:],
84+
action.MacaroonIdentifier[:],
8485
)
8586
if err != nil {
8687
return err
@@ -109,7 +110,7 @@ func (db *BoltDB) AddAction(_ context.Context,
109110
}
110111

111112
locator = kvdbActionLocator{
112-
sessionID: action.SessionID,
113+
sessionID: action.MacaroonIdentifier,
113114
actionID: nextActionIndex,
114115
}
115116

@@ -549,7 +550,8 @@ func DeserializeAction(r io.Reader, sessionID session.ID) (*Action, error) {
549550
return nil, err
550551
}
551552

552-
action.SessionID = sessionID
553+
action.MacaroonIdentifier = sessionID
554+
action.SessionID = fn.Some(sessionID)
553555
action.ActorName = string(actor)
554556
action.FeatureName = string(featureName)
555557
action.Trigger = string(trigger)

firewalldb/actions_test.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"time"
88

99
"github.com/lightningnetwork/lnd/clock"
10+
"github.com/lightningnetwork/lnd/fn"
1011
"github.com/stretchr/testify/require"
1112
)
1213

@@ -18,7 +19,8 @@ var (
1819
sessionID2 = intToSessionID(2)
1920

2021
action1Req = &AddActionReq{
21-
SessionID: sessionID1,
22+
SessionID: fn.Some(sessionID1),
23+
MacaroonIdentifier: sessionID1,
2224
ActorName: "Autopilot",
2325
FeatureName: "auto-fees",
2426
Trigger: "fee too low",
@@ -35,13 +37,14 @@ var (
3537
}
3638

3739
action2Req = &AddActionReq{
38-
SessionID: sessionID2,
39-
ActorName: "Autopilot",
40-
FeatureName: "rebalancer",
41-
Trigger: "channels not balanced",
42-
Intent: "balance",
43-
RPCMethod: "SendToRoute",
44-
RPCParamsJson: []byte("hops, amount"),
40+
SessionID: fn.Some(sessionID2),
41+
MacaroonIdentifier: sessionID2,
42+
ActorName: "Autopilot",
43+
FeatureName: "rebalancer",
44+
Trigger: "channels not balanced",
45+
Intent: "balance",
46+
RPCMethod: "SendToRoute",
47+
RPCParamsJson: []byte("hops, amount"),
4548
}
4649

4750
action2 = &Action{
@@ -171,7 +174,7 @@ func TestListActions(t *testing.T) {
171174
actionIds++
172175

173176
actionReq := &AddActionReq{
174-
SessionID: sessionID,
177+
MacaroonIdentifier: sessionID,
175178
ActorName: "Autopilot",
176179
FeatureName: fmt.Sprintf("%d", actionIds),
177180
Trigger: "fee too low",
@@ -194,7 +197,7 @@ func TestListActions(t *testing.T) {
194197
require.Len(t, dbActions, len(al))
195198
for i, a := range al {
196199
require.EqualValues(
197-
t, a.sessionID, dbActions[i].SessionID,
200+
t, a.sessionID, dbActions[i].MacaroonIdentifier,
198201
)
199202
require.Equal(t, a.actionID, dbActions[i].FeatureName)
200203
}

session_rpcserver.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -731,8 +731,13 @@ func (s *sessionRpcServer) ListActions(ctx context.Context,
731731
return nil, err
732732
}
733733

734+
var sessionID session.ID
735+
a.SessionID.WhenSome(func(id session.ID) {
736+
sessionID = id
737+
})
738+
734739
resp[i] = &litrpc.Action{
735-
SessionId: a.SessionID[:],
740+
SessionId: sessionID[:],
736741
ActorName: a.ActorName,
737742
FeatureName: a.FeatureName,
738743
Trigger: a.Trigger,

0 commit comments

Comments
 (0)