Skip to content

Commit 498a18d

Browse files
committed
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.
1 parent 4e2750e commit 498a18d

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

graph/db/sql_store.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"time"
1515

1616
"github.com/btcsuite/btcd/btcec/v2"
17+
"github.com/btcsuite/btcd/chaincfg/chainhash"
1718
"github.com/lightningnetwork/lnd/batch"
1819
"github.com/lightningnetwork/lnd/graph/db/models"
1920
"github.com/lightningnetwork/lnd/lnwire"
@@ -96,7 +97,8 @@ type BatchedSQLQueries interface {
9697
// implemented, things will fall back to the KVStore. This is ONLY the case
9798
// for the time being while this struct is purely used in unit tests only.
9899
type SQLStore struct {
99-
db BatchedSQLQueries
100+
cfg *SQLStoreConfig
101+
db BatchedSQLQueries
100102

101103
// cacheMu guards all caches (rejectCache and chanCache). If
102104
// this mutex will be acquired at the same time as the DB mutex then
@@ -117,9 +119,16 @@ type SQLStore struct {
117119
// interface.
118120
var _ V1Store = (*SQLStore)(nil)
119121

122+
// SQLStoreConfig holds the configuration for the SQLStore.
123+
type SQLStoreConfig struct {
124+
// ChainHash is the genesis hash for the chain that all the gossip
125+
// messages in this store are aimed at.
126+
ChainHash chainhash.Hash
127+
}
128+
120129
// NewSQLStore creates a new SQLStore instance given an open BatchedSQLQueries
121130
// storage backend.
122-
func NewSQLStore(db BatchedSQLQueries, kvStore *KVStore,
131+
func NewSQLStore(cfg *SQLStoreConfig, db BatchedSQLQueries, kvStore *KVStore,
123132
options ...StoreOptionModifier) (*SQLStore, error) {
124133

125134
opts := DefaultOptions()
@@ -133,6 +142,7 @@ func NewSQLStore(db BatchedSQLQueries, kvStore *KVStore,
133142
}
134143

135144
s := &SQLStore{
145+
cfg: cfg,
136146
db: db,
137147
KVStore: kvStore,
138148
rejectCache: newRejectCache(opts.RejectCacheSize),

graph/db/test_postgres.go

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

9+
"github.com/btcsuite/btcd/chaincfg"
910
"github.com/lightningnetwork/lnd/kvdb"
1011
"github.com/lightningnetwork/lnd/sqldb"
1112
"github.com/stretchr/testify/require"
@@ -38,7 +39,11 @@ func NewTestDB(t testing.TB) V1Store {
3839
},
3940
)
4041

41-
store, err := NewSQLStore(executor, graphStore)
42+
store, err := NewSQLStore(
43+
&SQLStoreConfig{
44+
ChainHash: *chaincfg.MainNetParams.GenesisHash,
45+
}, executor, graphStore,
46+
)
4247
require.NoError(t, err)
4348

4449
return store

graph/db/test_sqlite.go

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

9+
"github.com/btcsuite/btcd/chaincfg"
910
"github.com/lightningnetwork/lnd/kvdb"
1011
"github.com/lightningnetwork/lnd/sqldb"
1112
"github.com/stretchr/testify/require"
@@ -31,7 +32,11 @@ func NewTestDB(t testing.TB) V1Store {
3132
},
3233
)
3334

34-
store, err := NewSQLStore(executor, graphStore)
35+
store, err := NewSQLStore(
36+
&SQLStoreConfig{
37+
ChainHash: *chaincfg.MainNetParams.GenesisHash,
38+
}, executor, graphStore,
39+
)
3540
require.NoError(t, err)
3641

3742
return store

0 commit comments

Comments
 (0)