From 526fb7f1818fc1e9ee13473d0f8666404008a6c2 Mon Sep 17 00:00:00 2001 From: Elle Mouton Date: Sun, 30 Mar 2025 11:42:22 +0200 Subject: [PATCH] graph/db: init KVStore outside of ChannelGraph So that we can pass in the abstract V1Store in preparation for adding a SQL implementation of the KVStore. --- autopilot/prefattach_test.go | 5 ++++- config_builder.go | 12 ++++++++---- graph/db/graph.go | 24 +++--------------------- graph/db/graph_test.go | 7 +++++-- graph/db/kv_store.go | 8 ++++---- graph/notifications_test.go | 6 ++++-- peer/test_utils.go | 7 ++++--- routing/pathfind_test.go | 6 ++++-- 8 files changed, 36 insertions(+), 39 deletions(-) diff --git a/autopilot/prefattach_test.go b/autopilot/prefattach_test.go index 2e3b22ff3..de7b6a7d6 100644 --- a/autopilot/prefattach_test.go +++ b/autopilot/prefattach_test.go @@ -47,7 +47,10 @@ func newDiskChanGraph(t *testing.T) (testGraph, error) { }) require.NoError(t, err) - graphDB, err := graphdb.NewChannelGraph(&graphdb.Config{KVDB: backend}) + graphStore, err := graphdb.NewKVStore(backend) + require.NoError(t, err) + + graphDB, err := graphdb.NewChannelGraph(graphStore) require.NoError(t, err) require.NoError(t, graphDB.Start()) diff --git a/config_builder.go b/config_builder.go index 2214250e7..708e79d8f 100644 --- a/config_builder.go +++ b/config_builder.go @@ -1046,10 +1046,14 @@ func (d *DefaultDatabaseBuilder) BuildDatabase( ) } - dbs.GraphDB, err = graphdb.NewChannelGraph(&graphdb.Config{ - KVDB: databaseBackends.GraphDB, - KVStoreOpts: graphDBOptions, - }, chanGraphOpts...) + graphStore, err := graphdb.NewKVStore( + databaseBackends.GraphDB, graphDBOptions..., + ) + if err != nil { + return nil, nil, err + } + + dbs.GraphDB, err = graphdb.NewChannelGraph(graphStore, chanGraphOpts...) if err != nil { cleanUp() diff --git a/graph/db/graph.go b/graph/db/graph.go index 5af3ee1b8..6203b2edb 100644 --- a/graph/db/graph.go +++ b/graph/db/graph.go @@ -11,7 +11,6 @@ import ( "github.com/btcsuite/btcd/wire" "github.com/lightningnetwork/lnd/batch" "github.com/lightningnetwork/lnd/graph/db/models" - "github.com/lightningnetwork/lnd/kvdb" "github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/routing/route" ) @@ -20,18 +19,6 @@ import ( // busy shutting down. var ErrChanGraphShuttingDown = fmt.Errorf("ChannelGraph shutting down") -// Config is a struct that holds all the necessary dependencies for a -// ChannelGraph. -type Config struct { - // KVDB is the kvdb.Backend that will be used for initializing the - // KVStore CRUD layer. - KVDB kvdb.Backend - - // KVStoreOpts is a list of functional options that will be used when - // initializing the KVStore. - KVStoreOpts []KVStoreOptionModifier -} - // ChannelGraph is a layer above the graph's CRUD layer. // // NOTE: currently, this is purely a pass-through layer directly to the backing @@ -51,21 +38,16 @@ type ChannelGraph struct { } // NewChannelGraph creates a new ChannelGraph instance with the given backend. -func NewChannelGraph(cfg *Config, options ...ChanGraphOption) (*ChannelGraph, - error) { +func NewChannelGraph(v1Store V1Store, + options ...ChanGraphOption) (*ChannelGraph, error) { opts := defaultChanGraphOptions() for _, o := range options { o(opts) } - store, err := NewKVStore(cfg.KVDB, cfg.KVStoreOpts...) - if err != nil { - return nil, err - } - g := &ChannelGraph{ - V1Store: store, + V1Store: v1Store, topologyManager: newTopologyManager(), quit: make(chan struct{}), } diff --git a/graph/db/graph_test.go b/graph/db/graph_test.go index 1818ef7b7..4551a29c5 100644 --- a/graph/db/graph_test.go +++ b/graph/db/graph_test.go @@ -4084,7 +4084,10 @@ func TestGraphLoading(t *testing.T) { defer backend.Close() defer backendCleanup() - graph, err := NewChannelGraph(&Config{KVDB: backend}) + graphStore, err := NewKVStore(backend) + require.NoError(t, err) + + graph, err := NewChannelGraph(graphStore) require.NoError(t, err) require.NoError(t, graph.Start()) t.Cleanup(func() { @@ -4098,7 +4101,7 @@ func TestGraphLoading(t *testing.T) { // Recreate the graph. This should cause the graph cache to be // populated. - graphReloaded, err := NewChannelGraph(&Config{KVDB: backend}) + graphReloaded, err := NewChannelGraph(graphStore) require.NoError(t, err) require.NoError(t, graphReloaded.Start()) t.Cleanup(func() { diff --git a/graph/db/kv_store.go b/graph/db/kv_store.go index 6c283cb81..36da985cf 100644 --- a/graph/db/kv_store.go +++ b/graph/db/kv_store.go @@ -4725,10 +4725,10 @@ func MakeTestGraph(t testing.TB, modifiers ...KVStoreOptionModifier) ( return nil, err } - graph, err := NewChannelGraph(&Config{ - KVDB: backend, - KVStoreOpts: modifiers, - }) + graphStore, err := NewKVStore(backend, modifiers...) + require.NoError(t, err) + + graph, err := NewChannelGraph(graphStore) if err != nil { backendCleanup() diff --git a/graph/notifications_test.go b/graph/notifications_test.go index ace578376..33bac64dd 100644 --- a/graph/notifications_test.go +++ b/graph/notifications_test.go @@ -1093,9 +1093,11 @@ func makeTestGraph(t *testing.T, useCache bool) *graphdb.ChannelGraph { t.Cleanup(backendCleanup) + graphStore, err := graphdb.NewKVStore(backend) + require.NoError(t, err) + graph, err := graphdb.NewChannelGraph( - &graphdb.Config{KVDB: backend}, - graphdb.WithUseGraphCache(useCache), + graphStore, graphdb.WithUseGraphCache(useCache), ) require.NoError(t, err) require.NoError(t, graph.Start()) diff --git a/peer/test_utils.go b/peer/test_utils.go index 87a80712f..4d2bae8c7 100644 --- a/peer/test_utils.go +++ b/peer/test_utils.go @@ -615,9 +615,10 @@ func createTestPeer(t *testing.T) *peerTestCtx { }) require.NoError(t, err) - dbAliceGraph, err := graphdb.NewChannelGraph(&graphdb.Config{ - KVDB: graphBackend, - }) + graphStore, err := graphdb.NewKVStore(graphBackend) + require.NoError(t, err) + + dbAliceGraph, err := graphdb.NewChannelGraph(graphStore) require.NoError(t, err) require.NoError(t, dbAliceGraph.Start()) t.Cleanup(func() { diff --git a/routing/pathfind_test.go b/routing/pathfind_test.go index 8a0280686..363858a45 100644 --- a/routing/pathfind_test.go +++ b/routing/pathfind_test.go @@ -166,9 +166,11 @@ func makeTestGraph(t *testing.T, useCache bool) (*graphdb.ChannelGraph, t.Cleanup(backendCleanup) + graphStore, err := graphdb.NewKVStore(backend) + require.NoError(t, err) + graph, err := graphdb.NewChannelGraph( - &graphdb.Config{KVDB: backend}, - graphdb.WithUseGraphCache(useCache), + graphStore, graphdb.WithUseGraphCache(useCache), ) if err != nil { return nil, nil, err