From 297313e9c14a479ba814c44b93cb5cab07a59685 Mon Sep 17 00:00:00 2001 From: Elle Mouton Date: Mon, 12 May 2025 13:37:53 +0200 Subject: [PATCH] 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. --- config_dev.go | 6 +++--- config_prod.go | 2 +- firewalldb/actions_test.go | 9 ++++++--- firewalldb/kvdb_store.go | 8 ++++++-- firewalldb/kvstores_test.go | 17 ++++++++++------- firewalldb/privacy_mapper_test.go | 10 ++++++---- firewalldb/sql_store.go | 6 +++++- firewalldb/test_kvdb.go | 19 +++++++++++-------- firewalldb/test_postgres.go | 9 +++++---- firewalldb/test_sql.go | 7 +++++-- firewalldb/test_sqlite.go | 11 +++++++---- 11 files changed, 65 insertions(+), 39 deletions(-) diff --git a/config_dev.go b/config_dev.go index 2dd937b9..ae7d1897 100644 --- a/config_dev.go +++ b/config_dev.go @@ -108,7 +108,7 @@ func NewStores(cfg *Config, clock clock.Clock) (*stores, error) { acctStore := accounts.NewSQLStore(sqlStore.BaseDB, clock) sessStore := session.NewSQLStore(sqlStore.BaseDB, clock) - firewallStore := firewalldb.NewSQLDB(sqlStore.BaseDB) + firewallStore := firewalldb.NewSQLDB(sqlStore.BaseDB, clock) stores.accounts = acctStore stores.sessions = sessStore @@ -123,7 +123,7 @@ func NewStores(cfg *Config, clock clock.Clock) (*stores, error) { acctStore := accounts.NewSQLStore(sqlStore.BaseDB, clock) sessStore := session.NewSQLStore(sqlStore.BaseDB, clock) - firewallStore := firewalldb.NewSQLDB(sqlStore.BaseDB) + firewallStore := firewalldb.NewSQLDB(sqlStore.BaseDB, clock) stores.accounts = acctStore stores.sessions = sessStore @@ -154,7 +154,7 @@ func NewStores(cfg *Config, clock clock.Clock) (*stores, error) { } firewallBoltDB, err := firewalldb.NewBoltDB( - networkDir, firewalldb.DBFilename, stores.sessions, + networkDir, firewalldb.DBFilename, stores.sessions, clock, ) if err != nil { return stores, fmt.Errorf("error creating firewall BoltDB: %v", diff --git a/config_prod.go b/config_prod.go index d10f0adb..5ea897fc 100644 --- a/config_prod.go +++ b/config_prod.go @@ -56,7 +56,7 @@ func NewStores(cfg *Config, clock clock.Clock) (*stores, error) { stores.closeFns["sessions"] = sessStore.Close firewallDB, err := firewalldb.NewBoltDB( - networkDir, firewalldb.DBFilename, sessStore, + networkDir, firewalldb.DBFilename, sessStore, clock, ) if err != nil { return stores, fmt.Errorf("error creating firewall DB: %v", err) diff --git a/firewalldb/actions_test.go b/firewalldb/actions_test.go index 26f33596..57e9d8c5 100644 --- a/firewalldb/actions_test.go +++ b/firewalldb/actions_test.go @@ -6,6 +6,7 @@ import ( "testing" "time" + "github.com/lightningnetwork/lnd/clock" "github.com/stretchr/testify/require" ) @@ -44,7 +45,7 @@ func TestActionStorage(t *testing.T) { tmpDir := t.TempDir() ctx := context.Background() - db, err := NewBoltDB(tmpDir, "test.db", nil) + db, err := NewBoltDB(tmpDir, "test.db", nil, clock.NewDefaultClock()) require.NoError(t, err) t.Cleanup(func() { _ = db.Close() @@ -141,7 +142,7 @@ func TestListActions(t *testing.T) { tmpDir := t.TempDir() ctx := context.Background() - db, err := NewBoltDB(tmpDir, "test.db", nil) + db, err := NewBoltDB(tmpDir, "test.db", nil, clock.NewDefaultClock()) require.NoError(t, err) t.Cleanup(func() { _ = db.Close() @@ -343,7 +344,9 @@ func TestListGroupActions(t *testing.T) { index.AddPair(sessionID1, group1) index.AddPair(sessionID2, group1) - db, err := NewBoltDB(t.TempDir(), "test.db", index) + db, err := NewBoltDB( + t.TempDir(), "test.db", index, clock.NewDefaultClock(), + ) require.NoError(t, err) t.Cleanup(func() { _ = db.Close() diff --git a/firewalldb/kvdb_store.go b/firewalldb/kvdb_store.go index 99497a27..edef36a1 100644 --- a/firewalldb/kvdb_store.go +++ b/firewalldb/kvdb_store.go @@ -8,6 +8,7 @@ import ( "path/filepath" "time" + "github.com/lightningnetwork/lnd/clock" "go.etcd.io/bbolt" ) @@ -37,13 +38,15 @@ var ( type BoltDB struct { *bbolt.DB + clock clock.Clock + sessionIDIndex SessionDB } // NewBoltDB creates a new bolt database that can be found at the given // directory. -func NewBoltDB(dir, fileName string, sessionIDIndex SessionDB) (*BoltDB, - error) { +func NewBoltDB(dir, fileName string, sessionIDIndex SessionDB, + clock clock.Clock) (*BoltDB, error) { firstInit := false path := filepath.Join(dir, fileName) @@ -70,6 +73,7 @@ func NewBoltDB(dir, fileName string, sessionIDIndex SessionDB) (*BoltDB, return &BoltDB{ DB: db, sessionIDIndex: sessionIDIndex, + clock: clock, }, nil } diff --git a/firewalldb/kvstores_test.go b/firewalldb/kvstores_test.go index 592188c7..20f6ec0c 100644 --- a/firewalldb/kvstores_test.go +++ b/firewalldb/kvstores_test.go @@ -19,7 +19,7 @@ func TestKVStoreTxs(t *testing.T) { t.Parallel() ctx := context.Background() - db := NewTestDB(t) + db := NewTestDB(t, clock.NewDefaultClock()) store := db.GetKVStores("AutoFees", [4]byte{1, 1, 1, 1}, "auto-fees") // Test that if an action fails midway through the transaction, then @@ -79,14 +79,15 @@ func TestTempAndPermStores(t *testing.T) { // session level KV stores. func testTempAndPermStores(t *testing.T, featureSpecificStore bool) { ctx := context.Background() + clock := clock.NewDefaultClock() var featureName string if featureSpecificStore { featureName = "auto-fees" } - sessions := session.NewTestDB(t, clock.NewDefaultClock()) - store := NewTestDBWithSessions(t, sessions) + sessions := session.NewTestDB(t, clock) + store := NewTestDBWithSessions(t, sessions, clock) db := NewDB(store) require.NoError(t, db.Start(ctx)) @@ -172,9 +173,10 @@ func testTempAndPermStores(t *testing.T, featureSpecificStore bool) { func TestKVStoreNameSpaces(t *testing.T) { t.Parallel() ctx := context.Background() + clock := clock.NewDefaultClock() - sessions := session.NewTestDB(t, clock.NewDefaultClock()) - db := NewTestDBWithSessions(t, sessions) + sessions := session.NewTestDB(t, clock) + db := NewTestDBWithSessions(t, sessions, clock) // Create 2 sessions that we can reference. sess1, err := sessions.NewSession( @@ -397,9 +399,10 @@ func TestKVStoreNameSpaces(t *testing.T) { func TestKVStoreSessionCoupling(t *testing.T) { t.Parallel() ctx := context.Background() + clock := clock.NewDefaultClock() - sessions := session.NewTestDB(t, clock.NewDefaultClock()) - db := NewTestDBWithSessions(t, sessions) + sessions := session.NewTestDB(t, clock) + db := NewTestDBWithSessions(t, sessions, clock) // Get a kvstore namespaced by a session ID for a session that does // not exist. diff --git a/firewalldb/privacy_mapper_test.go b/firewalldb/privacy_mapper_test.go index fbdf880f..9ba6a5b1 100644 --- a/firewalldb/privacy_mapper_test.go +++ b/firewalldb/privacy_mapper_test.go @@ -15,9 +15,10 @@ import ( func TestPrivacyMapStorage(t *testing.T) { t.Parallel() ctx := context.Background() + clock := clock.NewDefaultClock() - sessions := session.NewTestDB(t, clock.NewDefaultClock()) - db := NewTestDBWithSessions(t, sessions) + sessions := session.NewTestDB(t, clock) + db := NewTestDBWithSessions(t, sessions, clock) // First up, let's test that the correct error is returned if an // attempt is made to write to a privacy map that is not linked to @@ -221,9 +222,10 @@ func TestPrivacyMapStorage(t *testing.T) { func TestPrivacyMapTxs(t *testing.T) { t.Parallel() ctx := context.Background() + clock := clock.NewDefaultClock() - sessions := session.NewTestDB(t, clock.NewDefaultClock()) - db := NewTestDBWithSessions(t, sessions) + sessions := session.NewTestDB(t, clock) + db := NewTestDBWithSessions(t, sessions, clock) sess, err := sessions.NewSession( ctx, "test", session.TypeAutopilot, time.Unix(1000, 0), "", diff --git a/firewalldb/sql_store.go b/firewalldb/sql_store.go index acca60ce..369920d6 100644 --- a/firewalldb/sql_store.go +++ b/firewalldb/sql_store.go @@ -5,6 +5,7 @@ import ( "database/sql" "github.com/lightninglabs/lightning-terminal/db" + "github.com/lightningnetwork/lnd/clock" ) // SQLQueries is a subset of the sqlc.Queries interface that can be used to @@ -30,6 +31,8 @@ type SQLDB struct { // BaseDB represents the underlying database connection. *db.BaseDB + + clock clock.Clock } // A compile-time assertion to ensure that SQLDB implements the RulesDB @@ -38,7 +41,7 @@ var _ RulesDB = (*SQLDB)(nil) // NewSQLDB creates a new SQLStore instance given an open SQLQueries // storage backend. -func NewSQLDB(sqlDB *db.BaseDB) *SQLDB { +func NewSQLDB(sqlDB *db.BaseDB, clock clock.Clock) *SQLDB { executor := db.NewTransactionExecutor( sqlDB, func(tx *sql.Tx) SQLQueries { return sqlDB.WithTx(tx) @@ -48,6 +51,7 @@ func NewSQLDB(sqlDB *db.BaseDB) *SQLDB { return &SQLDB{ db: executor, BaseDB: sqlDB, + clock: clock, } } diff --git a/firewalldb/test_kvdb.go b/firewalldb/test_kvdb.go index 91ea130b..2c0ad66c 100644 --- a/firewalldb/test_kvdb.go +++ b/firewalldb/test_kvdb.go @@ -6,30 +6,33 @@ import ( "testing" "github.com/lightninglabs/lightning-terminal/session" + "github.com/lightningnetwork/lnd/clock" "github.com/stretchr/testify/require" ) // NewTestDB is a helper function that creates an BBolt database for testing. -func NewTestDB(t *testing.T) *BoltDB { - return NewTestDBFromPath(t, t.TempDir()) +func NewTestDB(t *testing.T, clock clock.Clock) *BoltDB { + return NewTestDBFromPath(t, t.TempDir(), clock) } // NewTestDBFromPath is a helper function that creates a new BoltStore with a // connection to an existing BBolt database for testing. -func NewTestDBFromPath(t *testing.T, dbPath string) *BoltDB { - return newDBFromPathWithSessions(t, dbPath, nil) +func NewTestDBFromPath(t *testing.T, dbPath string, clock clock.Clock) *BoltDB { + return newDBFromPathWithSessions(t, dbPath, nil, clock) } // NewTestDBWithSessions creates a new test BoltDB Store with access to an // existing sessions DB. -func NewTestDBWithSessions(t *testing.T, sessStore session.Store) *BoltDB { - return newDBFromPathWithSessions(t, t.TempDir(), sessStore) +func NewTestDBWithSessions(t *testing.T, sessStore session.Store, + clock clock.Clock) *BoltDB { + + return newDBFromPathWithSessions(t, t.TempDir(), sessStore, clock) } func newDBFromPathWithSessions(t *testing.T, dbPath string, - sessStore session.Store) *BoltDB { + sessStore session.Store, clock clock.Clock) *BoltDB { - store, err := NewBoltDB(dbPath, DBFilename, sessStore) + store, err := NewBoltDB(dbPath, DBFilename, sessStore, clock) require.NoError(t, err) t.Cleanup(func() { diff --git a/firewalldb/test_postgres.go b/firewalldb/test_postgres.go index aeb01235..f5777e4c 100644 --- a/firewalldb/test_postgres.go +++ b/firewalldb/test_postgres.go @@ -6,15 +6,16 @@ import ( "testing" "github.com/lightninglabs/lightning-terminal/db" + "github.com/lightningnetwork/lnd/clock" ) // NewTestDB is a helper function that creates an BBolt database for testing. -func NewTestDB(t *testing.T) *SQLDB { - return NewSQLDB(db.NewTestPostgresDB(t).BaseDB) +func NewTestDB(t *testing.T, clock clock.Clock) *SQLDB { + return NewSQLDB(db.NewTestPostgresDB(t).BaseDB, clock) } // NewTestDBFromPath is a helper function that creates a new BoltStore with a // connection to an existing BBolt database for testing. -func NewTestDBFromPath(t *testing.T, _ string) *SQLDB { - return NewSQLDB(db.NewTestPostgresDB(t).BaseDB) +func NewTestDBFromPath(t *testing.T, _ string, clock clock.Clock) *SQLDB { + return NewSQLDB(db.NewTestPostgresDB(t).BaseDB, clock) } diff --git a/firewalldb/test_sql.go b/firewalldb/test_sql.go index d256480f..947ff149 100644 --- a/firewalldb/test_sql.go +++ b/firewalldb/test_sql.go @@ -6,14 +6,17 @@ import ( "testing" "github.com/lightninglabs/lightning-terminal/session" + "github.com/lightningnetwork/lnd/clock" "github.com/stretchr/testify/require" ) // NewTestDBWithSessions creates a new test SQLDB Store with access to an // existing sessions DB. -func NewTestDBWithSessions(t *testing.T, sessionStore session.Store) *SQLDB { +func NewTestDBWithSessions(t *testing.T, sessionStore session.Store, + clock clock.Clock) *SQLDB { + sessions, ok := sessionStore.(*session.SQLStore) require.True(t, ok) - return NewSQLDB(sessions.BaseDB) + return NewSQLDB(sessions.BaseDB, clock) } diff --git a/firewalldb/test_sqlite.go b/firewalldb/test_sqlite.go index 2497584d..5496cb20 100644 --- a/firewalldb/test_sqlite.go +++ b/firewalldb/test_sqlite.go @@ -6,15 +6,18 @@ import ( "testing" "github.com/lightninglabs/lightning-terminal/db" + "github.com/lightningnetwork/lnd/clock" ) // NewTestDB is a helper function that creates an BBolt database for testing. -func NewTestDB(t *testing.T) *SQLDB { - return NewSQLDB(db.NewTestSqliteDB(t).BaseDB) +func NewTestDB(t *testing.T, clock clock.Clock) *SQLDB { + return NewSQLDB(db.NewTestSqliteDB(t).BaseDB, clock) } // NewTestDBFromPath is a helper function that creates a new BoltStore with a // connection to an existing BBolt database for testing. -func NewTestDBFromPath(t *testing.T, dbPath string) *SQLDB { - return NewSQLDB(db.NewTestSqliteDbHandleFromPath(t, dbPath).BaseDB) +func NewTestDBFromPath(t *testing.T, dbPath string, clock clock.Clock) *SQLDB { + return NewSQLDB( + db.NewTestSqliteDbHandleFromPath(t, dbPath).BaseDB, clock, + ) }