From 498a18d0285406b61181dc2572ccb4e29497e25c Mon Sep 17 00:00:00 2001 From: Elle Mouton Date: Sat, 24 May 2025 15:41:32 +0200 Subject: [PATCH] graph/db: provide SQLStore with chainhash In this commit, we introduce a SQLStoreConfig struct which for the time being only has the ChainHash of the genesis block of the chain this node is running on. This is used to reconstruct lnwire messages from what we have persisted in the DB. This means we dont need need to persist the chain-hash of gossip messages since we know it will always be the same for a given node. If a node were to be started with a different network, the lnwire messages it reconstructs for gossip will be invalid. --- graph/db/sql_store.go | 14 ++++++++++++-- graph/db/test_postgres.go | 7 ++++++- graph/db/test_sqlite.go | 7 ++++++- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/graph/db/sql_store.go b/graph/db/sql_store.go index 1d296d2d2..bc1a92e76 100644 --- a/graph/db/sql_store.go +++ b/graph/db/sql_store.go @@ -14,6 +14,7 @@ import ( "time" "github.com/btcsuite/btcd/btcec/v2" + "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/lightningnetwork/lnd/batch" "github.com/lightningnetwork/lnd/graph/db/models" "github.com/lightningnetwork/lnd/lnwire" @@ -96,7 +97,8 @@ type BatchedSQLQueries interface { // implemented, things will fall back to the KVStore. This is ONLY the case // for the time being while this struct is purely used in unit tests only. type SQLStore struct { - db BatchedSQLQueries + cfg *SQLStoreConfig + db BatchedSQLQueries // cacheMu guards all caches (rejectCache and chanCache). If // this mutex will be acquired at the same time as the DB mutex then @@ -117,9 +119,16 @@ type SQLStore struct { // interface. var _ V1Store = (*SQLStore)(nil) +// SQLStoreConfig holds the configuration for the SQLStore. +type SQLStoreConfig struct { + // ChainHash is the genesis hash for the chain that all the gossip + // messages in this store are aimed at. + ChainHash chainhash.Hash +} + // NewSQLStore creates a new SQLStore instance given an open BatchedSQLQueries // storage backend. -func NewSQLStore(db BatchedSQLQueries, kvStore *KVStore, +func NewSQLStore(cfg *SQLStoreConfig, db BatchedSQLQueries, kvStore *KVStore, options ...StoreOptionModifier) (*SQLStore, error) { opts := DefaultOptions() @@ -133,6 +142,7 @@ func NewSQLStore(db BatchedSQLQueries, kvStore *KVStore, } s := &SQLStore{ + cfg: cfg, db: db, KVStore: kvStore, rejectCache: newRejectCache(opts.RejectCacheSize), diff --git a/graph/db/test_postgres.go b/graph/db/test_postgres.go index 2e38505f3..d5cd1f8ff 100644 --- a/graph/db/test_postgres.go +++ b/graph/db/test_postgres.go @@ -6,6 +6,7 @@ import ( "database/sql" "testing" + "github.com/btcsuite/btcd/chaincfg" "github.com/lightningnetwork/lnd/kvdb" "github.com/lightningnetwork/lnd/sqldb" "github.com/stretchr/testify/require" @@ -38,7 +39,11 @@ func NewTestDB(t testing.TB) V1Store { }, ) - store, err := NewSQLStore(executor, graphStore) + store, err := NewSQLStore( + &SQLStoreConfig{ + ChainHash: *chaincfg.MainNetParams.GenesisHash, + }, executor, graphStore, + ) require.NoError(t, err) return store diff --git a/graph/db/test_sqlite.go b/graph/db/test_sqlite.go index 61373bfdd..c19300ce2 100644 --- a/graph/db/test_sqlite.go +++ b/graph/db/test_sqlite.go @@ -6,6 +6,7 @@ import ( "database/sql" "testing" + "github.com/btcsuite/btcd/chaincfg" "github.com/lightningnetwork/lnd/kvdb" "github.com/lightningnetwork/lnd/sqldb" "github.com/stretchr/testify/require" @@ -31,7 +32,11 @@ func NewTestDB(t testing.TB) V1Store { }, ) - store, err := NewSQLStore(executor, graphStore) + store, err := NewSQLStore( + &SQLStoreConfig{ + ChainHash: *chaincfg.MainNetParams.GenesisHash, + }, executor, graphStore, + ) require.NoError(t, err) return store