From 00432e46f312d942341150679fb34bc81d910bf2 Mon Sep 17 00:00:00 2001 From: Elle Mouton Date: Tue, 18 Feb 2025 13:48:11 -0300 Subject: [PATCH] multi: add ChannelGraph Config struct And use this struct to pass NewChannelGraph anything it needs to be able to init the KVStore that it houses. This will allow us to add ChannelGraph specific options. --- autopilot/prefattach_test.go | 2 +- config_builder.go | 7 ++++--- graph/db/graph.go | 18 ++++++++++++++---- graph/db/graph_test.go | 4 ++-- graph/db/kv_store.go | 5 ++++- graph/notifications_test.go | 9 ++++++--- peer/test_utils.go | 4 +++- routing/pathfind_test.go | 9 ++++++--- 8 files changed, 40 insertions(+), 18 deletions(-) diff --git a/autopilot/prefattach_test.go b/autopilot/prefattach_test.go index 784d1a0f8..f553482bb 100644 --- a/autopilot/prefattach_test.go +++ b/autopilot/prefattach_test.go @@ -46,7 +46,7 @@ func newDiskChanGraph(t *testing.T) (testGraph, error) { }) require.NoError(t, err) - graphDB, err := graphdb.NewChannelGraph(backend) + graphDB, err := graphdb.NewChannelGraph(&graphdb.Config{KVDB: backend}) require.NoError(t, err) return &testDBGraph{ diff --git a/config_builder.go b/config_builder.go index aef188b53..9b33520fd 100644 --- a/config_builder.go +++ b/config_builder.go @@ -1043,9 +1043,10 @@ func (d *DefaultDatabaseBuilder) BuildDatabase( ) } - dbs.GraphDB, err = graphdb.NewChannelGraph( - databaseBackends.GraphDB, graphDBOptions..., - ) + dbs.GraphDB, err = graphdb.NewChannelGraph(&graphdb.Config{ + KVDB: databaseBackends.GraphDB, + KVStoreOpts: graphDBOptions, + }) if err != nil { cleanUp() diff --git a/graph/db/graph.go b/graph/db/graph.go index ac52a0306..b8140a9d6 100644 --- a/graph/db/graph.go +++ b/graph/db/graph.go @@ -2,6 +2,18 @@ package graphdb import "github.com/lightningnetwork/lnd/kvdb" +// 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 @@ -12,10 +24,8 @@ type ChannelGraph struct { } // NewChannelGraph creates a new ChannelGraph instance with the given backend. -func NewChannelGraph(db kvdb.Backend, options ...KVStoreOptionModifier) ( - *ChannelGraph, error) { - - store, err := NewKVStore(db, options...) +func NewChannelGraph(cfg *Config) (*ChannelGraph, error) { + store, err := NewKVStore(cfg.KVDB, cfg.KVStoreOpts...) if err != nil { return nil, err } diff --git a/graph/db/graph_test.go b/graph/db/graph_test.go index 62b9cd4e1..e3681f4b0 100644 --- a/graph/db/graph_test.go +++ b/graph/db/graph_test.go @@ -4005,7 +4005,7 @@ func TestGraphLoading(t *testing.T) { defer backend.Close() defer backendCleanup() - graph, err := NewChannelGraph(backend) + graph, err := NewChannelGraph(&Config{KVDB: backend}) require.NoError(t, err) // Populate the graph with test data. @@ -4015,7 +4015,7 @@ func TestGraphLoading(t *testing.T) { // Recreate the graph. This should cause the graph cache to be // populated. - graphReloaded, err := NewChannelGraph(backend) + graphReloaded, err := NewChannelGraph(&Config{KVDB: backend}) require.NoError(t, err) // Assert that the cache content is identical. diff --git a/graph/db/kv_store.go b/graph/db/kv_store.go index 8ddafc179..96babbec9 100644 --- a/graph/db/kv_store.go +++ b/graph/db/kv_store.go @@ -4843,7 +4843,10 @@ func MakeTestGraph(t testing.TB, modifiers ...KVStoreOptionModifier) ( return nil, err } - graph, err := NewChannelGraph(backend) + graph, err := NewChannelGraph(&Config{ + KVDB: backend, + KVStoreOpts: modifiers, + }) if err != nil { backendCleanup() diff --git a/graph/notifications_test.go b/graph/notifications_test.go index 4049c9f81..6846be15e 100644 --- a/graph/notifications_test.go +++ b/graph/notifications_test.go @@ -1093,9 +1093,12 @@ func makeTestGraph(t *testing.T, useCache bool) (*graphdb.ChannelGraph, t.Cleanup(backendCleanup) - graph, err := graphdb.NewChannelGraph( - backend, graphdb.WithUseGraphCache(useCache), - ) + graph, err := graphdb.NewChannelGraph(&graphdb.Config{ + KVDB: backend, + KVStoreOpts: []graphdb.KVStoreOptionModifier{ + graphdb.WithUseGraphCache(useCache), + }, + }) if err != nil { return nil, nil, err } diff --git a/peer/test_utils.go b/peer/test_utils.go index bcde83bf9..7d3c5ca37 100644 --- a/peer/test_utils.go +++ b/peer/test_utils.go @@ -615,7 +615,9 @@ func createTestPeer(t *testing.T) *peerTestCtx { }) require.NoError(t, err) - dbAliceGraph, err := graphdb.NewChannelGraph(graphBackend) + dbAliceGraph, err := graphdb.NewChannelGraph(&graphdb.Config{ + KVDB: graphBackend, + }) require.NoError(t, err) dbAliceChannel := channeldb.OpenForTesting(t, dbPath) diff --git a/routing/pathfind_test.go b/routing/pathfind_test.go index fd92ed934..6aba44a4f 100644 --- a/routing/pathfind_test.go +++ b/routing/pathfind_test.go @@ -166,9 +166,12 @@ func makeTestGraph(t *testing.T, useCache bool) (*graphdb.ChannelGraph, t.Cleanup(backendCleanup) - graph, err := graphdb.NewChannelGraph( - backend, graphdb.WithUseGraphCache(useCache), - ) + graph, err := graphdb.NewChannelGraph(&graphdb.Config{ + KVDB: backend, + KVStoreOpts: []graphdb.KVStoreOptionModifier{ + graphdb.WithUseGraphCache(useCache), + }, + }) if err != nil { return nil, nil, err }