Skip to content

Commit 8f7312f

Browse files
committed
firewalldb: introduce AddActionReq
Instead of passing an `Action` to the AddAction method, we introduce an `AddActionReq` type which only holds the fields that are needed to create a new Action. The rest of the info is determined by the DB layer.
1 parent 297313e commit 8f7312f

File tree

5 files changed

+61
-37
lines changed

5 files changed

+61
-37
lines changed

firewall/request_logger.go

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"strings"
77
"sync"
8-
"time"
98

109
"github.com/lightninglabs/lightning-terminal/firewalldb"
1110
mid "github.com/lightninglabs/lightning-terminal/rpcmiddleware"
@@ -193,11 +192,9 @@ func (r *RequestLogger) addNewAction(ctx context.Context, ri *RequestInfo,
193192
}
194193
}
195194

196-
action := &firewalldb.Action{
197-
SessionID: sessionID,
198-
RPCMethod: ri.URI,
199-
AttemptedAt: time.Now(),
200-
State: firewalldb.ActionStateInit,
195+
actionReq := &firewalldb.AddActionReq{
196+
SessionID: sessionID,
197+
RPCMethod: ri.URI,
201198
}
202199

203200
if withPayloadData {
@@ -211,19 +208,19 @@ func (r *RequestLogger) addNewAction(ctx context.Context, ri *RequestInfo,
211208
return fmt.Errorf("unable to decode response: %v", err)
212209
}
213210

214-
action.RPCParamsJson = jsonBytes
211+
actionReq.RPCParamsJson = jsonBytes
215212

216213
meta := ri.MetaInfo
217214
if meta != nil {
218-
action.ActorName = meta.ActorName
219-
action.FeatureName = meta.Feature
220-
action.Trigger = meta.Trigger
221-
action.Intent = meta.Intent
222-
action.StructuredJsonData = meta.StructuredJsonData
215+
actionReq.ActorName = meta.ActorName
216+
actionReq.FeatureName = meta.Feature
217+
actionReq.Trigger = meta.Trigger
218+
actionReq.Intent = meta.Intent
219+
actionReq.StructuredJsonData = meta.StructuredJsonData
223220
}
224221
}
225222

226-
locator, err := r.actionsDB.AddAction(ctx, action)
223+
locator, err := r.actionsDB.AddAction(ctx, actionReq)
227224
if err != nil {
228225
return err
229226
}

firewalldb/actions.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ const (
2828
ActionStateError ActionState = 3
2929
)
3030

31-
// Action represents an RPC call made through the firewall.
32-
type Action struct {
31+
// AddActionReq is the request that is used to add a new Action to the database.
32+
// It contains all the information that is needed to create a new Action in the
33+
// ActionStateInit State.
34+
type AddActionReq struct {
3335
// SessionID is the ID of the session that this action belongs to.
3436
// Note that this is not serialized on persistence since the action is
3537
// already stored under a bucket identified by the session ID.
@@ -59,6 +61,11 @@ type Action struct {
5961

6062
// RPCParams is the method parameters of the request in JSON form.
6163
RPCParamsJson []byte
64+
}
65+
66+
// Action represents an RPC call made through the firewall.
67+
type Action struct {
68+
AddActionReq
6269

6370
// AttemptedAt is the time at which this action was created.
6471
AttemptedAt time.Time
@@ -181,7 +188,7 @@ func WithActionState(state ActionState) ListActionOption {
181188
// ActionsWriteDB is an abstraction over the Actions DB that will allow a
182189
// caller to add new actions as well as change the values of an existing action.
183190
type ActionsWriteDB interface {
184-
AddAction(ctx context.Context, action *Action) (ActionLocator, error)
191+
AddAction(ctx context.Context, req *AddActionReq) (ActionLocator, error)
185192
SetActionState(ctx context.Context, al ActionLocator,
186193
state ActionState, errReason string) error
187194
}

firewalldb/actions_kvdb.go

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

5555
// AddAction serialises and adds an Action to the DB under the given sessionID.
56-
func (db *BoltDB) AddAction(_ context.Context, action *Action) (ActionLocator,
57-
error) {
56+
func (db *BoltDB) AddAction(_ context.Context,
57+
req *AddActionReq) (ActionLocator, error) {
58+
59+
action := &Action{
60+
AddActionReq: *req,
61+
AttemptedAt: db.clock.Now().UTC(),
62+
State: ActionStateInit,
63+
}
5864

5965
var buf bytes.Buffer
6066
if err := SerializeAction(&buf, action); err != nil {

firewalldb/actions_test.go

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@ import (
1111
)
1212

1313
var (
14+
testTime1 = time.Unix(32100, 0)
15+
testTime2 = time.Unix(12300, 0)
16+
1417
sessionID1 = intToSessionID(1)
1518
sessionID2 = intToSessionID(2)
1619

17-
action1 = &Action{
20+
action1Req = &AddActionReq{
1821
SessionID: sessionID1,
1922
ActorName: "Autopilot",
2023
FeatureName: "auto-fees",
@@ -23,29 +26,37 @@ var (
2326
StructuredJsonData: "{\"something\":\"nothing\"}",
2427
RPCMethod: "UpdateChanPolicy",
2528
RPCParamsJson: []byte("new fee"),
26-
AttemptedAt: time.Unix(32100, 0),
27-
State: ActionStateDone,
2829
}
2930

30-
action2 = &Action{
31+
action1 = &Action{
32+
AddActionReq: *action1Req,
33+
AttemptedAt: testTime1,
34+
State: ActionStateDone,
35+
}
36+
37+
action2Req = &AddActionReq{
3138
SessionID: sessionID2,
3239
ActorName: "Autopilot",
3340
FeatureName: "rebalancer",
3441
Trigger: "channels not balanced",
3542
Intent: "balance",
3643
RPCMethod: "SendToRoute",
3744
RPCParamsJson: []byte("hops, amount"),
38-
AttemptedAt: time.Unix(12300, 0),
39-
State: ActionStateInit,
45+
}
46+
47+
action2 = &Action{
48+
AddActionReq: *action2Req,
49+
AttemptedAt: testTime2,
50+
State: ActionStateInit,
4051
}
4152
)
4253

4354
// TestActionStorage tests that the ActionsListDB CRUD logic.
4455
func TestActionStorage(t *testing.T) {
45-
tmpDir := t.TempDir()
4656
ctx := context.Background()
57+
clock := clock.NewTestClock(testTime1)
4758

48-
db, err := NewBoltDB(tmpDir, "test.db", nil, clock.NewDefaultClock())
59+
db, err := NewBoltDB(t.TempDir(), "test.db", nil, clock)
4960
require.NoError(t, err)
5061
t.Cleanup(func() {
5162
_ = db.Close()
@@ -67,10 +78,14 @@ func TestActionStorage(t *testing.T) {
6778
require.NoError(t, err)
6879
require.Len(t, actions, 0)
6980

70-
_, err = db.AddAction(ctx, action1)
81+
locator1, err := db.AddAction(ctx, action1Req)
7182
require.NoError(t, err)
83+
err = db.SetActionState(ctx, locator1, ActionStateDone, "")
84+
require.NoError(t, err)
85+
86+
clock.SetTime(testTime2)
7287

73-
locator2, err := db.AddAction(ctx, action2)
88+
locator2, err := db.AddAction(ctx, action2Req)
7489
require.NoError(t, err)
7590

7691
actions, _, _, err = db.ListActions(
@@ -103,7 +118,7 @@ func TestActionStorage(t *testing.T) {
103118
action2.State = ActionStateDone
104119
assertEqualActions(t, action2, actions[0])
105120

106-
_, err = db.AddAction(ctx, action1)
121+
_, err = db.AddAction(ctx, action1Req)
107122
require.NoError(t, err)
108123

109124
// Check that providing no session id and no filter function returns
@@ -154,7 +169,8 @@ func TestListActions(t *testing.T) {
154169
actionIds := 0
155170
addAction := func(sessionID [4]byte) {
156171
actionIds++
157-
action := &Action{
172+
173+
actionReq := &AddActionReq{
158174
SessionID: sessionID,
159175
ActorName: "Autopilot",
160176
FeatureName: fmt.Sprintf("%d", actionIds),
@@ -163,11 +179,9 @@ func TestListActions(t *testing.T) {
163179
StructuredJsonData: "{\"something\":\"nothing\"}",
164180
RPCMethod: "UpdateChanPolicy",
165181
RPCParamsJson: []byte("new fee"),
166-
AttemptedAt: time.Unix(32100, 0),
167-
State: ActionStateDone,
168182
}
169183

170-
_, err := db.AddAction(ctx, action)
184+
_, err := db.AddAction(ctx, actionReq)
171185
require.NoError(t, err)
172186
}
173187

@@ -358,7 +372,7 @@ func TestListGroupActions(t *testing.T) {
358372
require.Empty(t, al)
359373

360374
// Add an action under session 1.
361-
_, err = db.AddAction(ctx, action1)
375+
_, err = db.AddAction(ctx, action1Req)
362376
require.NoError(t, err)
363377

364378
// There should now be one action in the group.
@@ -368,7 +382,7 @@ func TestListGroupActions(t *testing.T) {
368382
require.Equal(t, sessionID1, al[0].SessionID)
369383

370384
// Add an action under session 2.
371-
_, err = db.AddAction(ctx, action2)
385+
_, err = db.AddAction(ctx, action2Req)
372386
require.NoError(t, err)
373387

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

firewalldb/interface.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ type PrivacyMapper interface {
104104
// ActionDB is an interface that abstracts the database operations needed for
105105
// the Action persistence and querying.
106106
type ActionDB interface {
107-
// AddAction persists the given action to the database.
108-
AddAction(ctx context.Context, action *Action) (ActionLocator, error)
107+
// AddAction persists a new action to the database.
108+
AddAction(ctx context.Context, req *AddActionReq) (ActionLocator, error)
109109

110110
// SetActionState finds the action specified by the ActionLocator and
111111
// sets its state to the given state.

0 commit comments

Comments
 (0)