mirror of
https://github.com/lightninglabs/lightning-terminal.git
synced 2026-08-13 12:33:36 +02:00
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.
This commit is contained in:
parent
57789b0c61
commit
297313e9c1
11 changed files with 65 additions and 39 deletions
|
|
@ -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",
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -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), "",
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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() {
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue