Skip to content

Commit 297313e

Browse files
committed
firewalldb: add clock.Clock to firewalldb DB impls
In preparation for using the clock to get an Action's AttemptedAt time in an upcoming commit, we let both the bbolt and SQL impls of the firewalldb take a clock.
1 parent 57789b0 commit 297313e

11 files changed

+65
-39
lines changed

config_dev.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func NewStores(cfg *Config, clock clock.Clock) (*stores, error) {
108108

109109
acctStore := accounts.NewSQLStore(sqlStore.BaseDB, clock)
110110
sessStore := session.NewSQLStore(sqlStore.BaseDB, clock)
111-
firewallStore := firewalldb.NewSQLDB(sqlStore.BaseDB)
111+
firewallStore := firewalldb.NewSQLDB(sqlStore.BaseDB, clock)
112112

113113
stores.accounts = acctStore
114114
stores.sessions = sessStore
@@ -123,7 +123,7 @@ func NewStores(cfg *Config, clock clock.Clock) (*stores, error) {
123123

124124
acctStore := accounts.NewSQLStore(sqlStore.BaseDB, clock)
125125
sessStore := session.NewSQLStore(sqlStore.BaseDB, clock)
126-
firewallStore := firewalldb.NewSQLDB(sqlStore.BaseDB)
126+
firewallStore := firewalldb.NewSQLDB(sqlStore.BaseDB, clock)
127127

128128
stores.accounts = acctStore
129129
stores.sessions = sessStore
@@ -154,7 +154,7 @@ func NewStores(cfg *Config, clock clock.Clock) (*stores, error) {
154154
}
155155

156156
firewallBoltDB, err := firewalldb.NewBoltDB(
157-
networkDir, firewalldb.DBFilename, stores.sessions,
157+
networkDir, firewalldb.DBFilename, stores.sessions, clock,
158158
)
159159
if err != nil {
160160
return stores, fmt.Errorf("error creating firewall BoltDB: %v",

config_prod.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func NewStores(cfg *Config, clock clock.Clock) (*stores, error) {
5656
stores.closeFns["sessions"] = sessStore.Close
5757

5858
firewallDB, err := firewalldb.NewBoltDB(
59-
networkDir, firewalldb.DBFilename, sessStore,
59+
networkDir, firewalldb.DBFilename, sessStore, clock,
6060
)
6161
if err != nil {
6262
return stores, fmt.Errorf("error creating firewall DB: %v", err)

firewalldb/actions_test.go

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

9+
"github.com/lightningnetwork/lnd/clock"
910
"github.com/stretchr/testify/require"
1011
)
1112

@@ -44,7 +45,7 @@ func TestActionStorage(t *testing.T) {
4445
tmpDir := t.TempDir()
4546
ctx := context.Background()
4647

47-
db, err := NewBoltDB(tmpDir, "test.db", nil)
48+
db, err := NewBoltDB(tmpDir, "test.db", nil, clock.NewDefaultClock())
4849
require.NoError(t, err)
4950
t.Cleanup(func() {
5051
_ = db.Close()
@@ -141,7 +142,7 @@ func TestListActions(t *testing.T) {
141142
tmpDir := t.TempDir()
142143
ctx := context.Background()
143144

144-
db, err := NewBoltDB(tmpDir, "test.db", nil)
145+
db, err := NewBoltDB(tmpDir, "test.db", nil, clock.NewDefaultClock())
145146
require.NoError(t, err)
146147
t.Cleanup(func() {
147148
_ = db.Close()
@@ -343,7 +344,9 @@ func TestListGroupActions(t *testing.T) {
343344
index.AddPair(sessionID1, group1)
344345
index.AddPair(sessionID2, group1)
345346

346-
db, err := NewBoltDB(t.TempDir(), "test.db", index)
347+
db, err := NewBoltDB(
348+
t.TempDir(), "test.db", index, clock.NewDefaultClock(),
349+
)
347350
require.NoError(t, err)
348351
t.Cleanup(func() {
349352
_ = db.Close()

firewalldb/kvdb_store.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"path/filepath"
99
"time"
1010

11+
"github.com/lightningnetwork/lnd/clock"
1112
"go.etcd.io/bbolt"
1213
)
1314

@@ -37,13 +38,15 @@ var (
3738
type BoltDB struct {
3839
*bbolt.DB
3940

41+
clock clock.Clock
42+
4043
sessionIDIndex SessionDB
4144
}
4245

4346
// NewBoltDB creates a new bolt database that can be found at the given
4447
// directory.
45-
func NewBoltDB(dir, fileName string, sessionIDIndex SessionDB) (*BoltDB,
46-
error) {
48+
func NewBoltDB(dir, fileName string, sessionIDIndex SessionDB,
49+
clock clock.Clock) (*BoltDB, error) {
4750

4851
firstInit := false
4952
path := filepath.Join(dir, fileName)
@@ -70,6 +73,7 @@ func NewBoltDB(dir, fileName string, sessionIDIndex SessionDB) (*BoltDB,
7073
return &BoltDB{
7174
DB: db,
7275
sessionIDIndex: sessionIDIndex,
76+
clock: clock,
7377
}, nil
7478
}
7579

firewalldb/kvstores_test.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func TestKVStoreTxs(t *testing.T) {
1919
t.Parallel()
2020

2121
ctx := context.Background()
22-
db := NewTestDB(t)
22+
db := NewTestDB(t, clock.NewDefaultClock())
2323
store := db.GetKVStores("AutoFees", [4]byte{1, 1, 1, 1}, "auto-fees")
2424

2525
// Test that if an action fails midway through the transaction, then
@@ -79,14 +79,15 @@ func TestTempAndPermStores(t *testing.T) {
7979
// session level KV stores.
8080
func testTempAndPermStores(t *testing.T, featureSpecificStore bool) {
8181
ctx := context.Background()
82+
clock := clock.NewDefaultClock()
8283

8384
var featureName string
8485
if featureSpecificStore {
8586
featureName = "auto-fees"
8687
}
8788

88-
sessions := session.NewTestDB(t, clock.NewDefaultClock())
89-
store := NewTestDBWithSessions(t, sessions)
89+
sessions := session.NewTestDB(t, clock)
90+
store := NewTestDBWithSessions(t, sessions, clock)
9091
db := NewDB(store)
9192
require.NoError(t, db.Start(ctx))
9293

@@ -172,9 +173,10 @@ func testTempAndPermStores(t *testing.T, featureSpecificStore bool) {
172173
func TestKVStoreNameSpaces(t *testing.T) {
173174
t.Parallel()
174175
ctx := context.Background()
176+
clock := clock.NewDefaultClock()
175177

176-
sessions := session.NewTestDB(t, clock.NewDefaultClock())
177-
db := NewTestDBWithSessions(t, sessions)
178+
sessions := session.NewTestDB(t, clock)
179+
db := NewTestDBWithSessions(t, sessions, clock)
178180

179181
// Create 2 sessions that we can reference.
180182
sess1, err := sessions.NewSession(
@@ -397,9 +399,10 @@ func TestKVStoreNameSpaces(t *testing.T) {
397399
func TestKVStoreSessionCoupling(t *testing.T) {
398400
t.Parallel()
399401
ctx := context.Background()
402+
clock := clock.NewDefaultClock()
400403

401-
sessions := session.NewTestDB(t, clock.NewDefaultClock())
402-
db := NewTestDBWithSessions(t, sessions)
404+
sessions := session.NewTestDB(t, clock)
405+
db := NewTestDBWithSessions(t, sessions, clock)
403406

404407
// Get a kvstore namespaced by a session ID for a session that does
405408
// not exist.

firewalldb/privacy_mapper_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ import (
1515
func TestPrivacyMapStorage(t *testing.T) {
1616
t.Parallel()
1717
ctx := context.Background()
18+
clock := clock.NewDefaultClock()
1819

19-
sessions := session.NewTestDB(t, clock.NewDefaultClock())
20-
db := NewTestDBWithSessions(t, sessions)
20+
sessions := session.NewTestDB(t, clock)
21+
db := NewTestDBWithSessions(t, sessions, clock)
2122

2223
// First up, let's test that the correct error is returned if an
2324
// attempt is made to write to a privacy map that is not linked to
@@ -221,9 +222,10 @@ func TestPrivacyMapStorage(t *testing.T) {
221222
func TestPrivacyMapTxs(t *testing.T) {
222223
t.Parallel()
223224
ctx := context.Background()
225+
clock := clock.NewDefaultClock()
224226

225-
sessions := session.NewTestDB(t, clock.NewDefaultClock())
226-
db := NewTestDBWithSessions(t, sessions)
227+
sessions := session.NewTestDB(t, clock)
228+
db := NewTestDBWithSessions(t, sessions, clock)
227229

228230
sess, err := sessions.NewSession(
229231
ctx, "test", session.TypeAutopilot, time.Unix(1000, 0), "",

firewalldb/sql_store.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"database/sql"
66

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

1011
// SQLQueries is a subset of the sqlc.Queries interface that can be used to
@@ -30,6 +31,8 @@ type SQLDB struct {
3031

3132
// BaseDB represents the underlying database connection.
3233
*db.BaseDB
34+
35+
clock clock.Clock
3336
}
3437

3538
// A compile-time assertion to ensure that SQLDB implements the RulesDB
@@ -38,7 +41,7 @@ var _ RulesDB = (*SQLDB)(nil)
3841

3942
// NewSQLDB creates a new SQLStore instance given an open SQLQueries
4043
// storage backend.
41-
func NewSQLDB(sqlDB *db.BaseDB) *SQLDB {
44+
func NewSQLDB(sqlDB *db.BaseDB, clock clock.Clock) *SQLDB {
4245
executor := db.NewTransactionExecutor(
4346
sqlDB, func(tx *sql.Tx) SQLQueries {
4447
return sqlDB.WithTx(tx)
@@ -48,6 +51,7 @@ func NewSQLDB(sqlDB *db.BaseDB) *SQLDB {
4851
return &SQLDB{
4952
db: executor,
5053
BaseDB: sqlDB,
54+
clock: clock,
5155
}
5256
}
5357

firewalldb/test_kvdb.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,33 @@ import (
66
"testing"
77

88
"github.com/lightninglabs/lightning-terminal/session"
9+
"github.com/lightningnetwork/lnd/clock"
910
"github.com/stretchr/testify/require"
1011
)
1112

1213
// NewTestDB is a helper function that creates an BBolt database for testing.
13-
func NewTestDB(t *testing.T) *BoltDB {
14-
return NewTestDBFromPath(t, t.TempDir())
14+
func NewTestDB(t *testing.T, clock clock.Clock) *BoltDB {
15+
return NewTestDBFromPath(t, t.TempDir(), clock)
1516
}
1617

1718
// NewTestDBFromPath is a helper function that creates a new BoltStore with a
1819
// connection to an existing BBolt database for testing.
19-
func NewTestDBFromPath(t *testing.T, dbPath string) *BoltDB {
20-
return newDBFromPathWithSessions(t, dbPath, nil)
20+
func NewTestDBFromPath(t *testing.T, dbPath string, clock clock.Clock) *BoltDB {
21+
return newDBFromPathWithSessions(t, dbPath, nil, clock)
2122
}
2223

2324
// NewTestDBWithSessions creates a new test BoltDB Store with access to an
2425
// existing sessions DB.
25-
func NewTestDBWithSessions(t *testing.T, sessStore session.Store) *BoltDB {
26-
return newDBFromPathWithSessions(t, t.TempDir(), sessStore)
26+
func NewTestDBWithSessions(t *testing.T, sessStore session.Store,
27+
clock clock.Clock) *BoltDB {
28+
29+
return newDBFromPathWithSessions(t, t.TempDir(), sessStore, clock)
2730
}
2831

2932
func newDBFromPathWithSessions(t *testing.T, dbPath string,
30-
sessStore session.Store) *BoltDB {
33+
sessStore session.Store, clock clock.Clock) *BoltDB {
3134

32-
store, err := NewBoltDB(dbPath, DBFilename, sessStore)
35+
store, err := NewBoltDB(dbPath, DBFilename, sessStore, clock)
3336
require.NoError(t, err)
3437

3538
t.Cleanup(func() {

firewalldb/test_postgres.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@ import (
66
"testing"
77

88
"github.com/lightninglabs/lightning-terminal/db"
9+
"github.com/lightningnetwork/lnd/clock"
910
)
1011

1112
// NewTestDB is a helper function that creates an BBolt database for testing.
12-
func NewTestDB(t *testing.T) *SQLDB {
13-
return NewSQLDB(db.NewTestPostgresDB(t).BaseDB)
13+
func NewTestDB(t *testing.T, clock clock.Clock) *SQLDB {
14+
return NewSQLDB(db.NewTestPostgresDB(t).BaseDB, clock)
1415
}
1516

1617
// NewTestDBFromPath is a helper function that creates a new BoltStore with a
1718
// connection to an existing BBolt database for testing.
18-
func NewTestDBFromPath(t *testing.T, _ string) *SQLDB {
19-
return NewSQLDB(db.NewTestPostgresDB(t).BaseDB)
19+
func NewTestDBFromPath(t *testing.T, _ string, clock clock.Clock) *SQLDB {
20+
return NewSQLDB(db.NewTestPostgresDB(t).BaseDB, clock)
2021
}

firewalldb/test_sql.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@ import (
66
"testing"
77

88
"github.com/lightninglabs/lightning-terminal/session"
9+
"github.com/lightningnetwork/lnd/clock"
910
"github.com/stretchr/testify/require"
1011
)
1112

1213
// NewTestDBWithSessions creates a new test SQLDB Store with access to an
1314
// existing sessions DB.
14-
func NewTestDBWithSessions(t *testing.T, sessionStore session.Store) *SQLDB {
15+
func NewTestDBWithSessions(t *testing.T, sessionStore session.Store,
16+
clock clock.Clock) *SQLDB {
17+
1518
sessions, ok := sessionStore.(*session.SQLStore)
1619
require.True(t, ok)
1720

18-
return NewSQLDB(sessions.BaseDB)
21+
return NewSQLDB(sessions.BaseDB, clock)
1922
}

0 commit comments

Comments
 (0)