From 372883ab816293d3311553cc4ae3cfc85b6472c4 Mon Sep 17 00:00:00 2001 From: Elle Mouton Date: Tue, 12 Nov 2024 08:49:34 +0200 Subject: [PATCH] lnd+graph: add GraphBootstrapper to the GraphSource interface So that LND can use a different GraphSource for network bootstrapping and does not need to rely on its local graph db. --- graph/sources/chan_graph.go | 14 ++++++++++++++ graph/sources/interfaces.go | 6 ++++++ server.go | 4 ++-- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/graph/sources/chan_graph.go b/graph/sources/chan_graph.go index ee43b06d4..f5a880323 100644 --- a/graph/sources/chan_graph.go +++ b/graph/sources/chan_graph.go @@ -8,6 +8,8 @@ import ( "github.com/btcsuite/btcd/btcec/v2" "github.com/btcsuite/btcd/wire" + "github.com/lightningnetwork/lnd/autopilot" + "github.com/lightningnetwork/lnd/discovery" graphdb "github.com/lightningnetwork/lnd/graph/db" "github.com/lightningnetwork/lnd/graph/db/models" "github.com/lightningnetwork/lnd/graph/session" @@ -217,6 +219,18 @@ func (s *DBSource) FetchLightningNode(_ context.Context, return s.db.FetchLightningNode(nodePub) } +// GraphBootstrapper returns a NetworkPeerBootstrapper instance backed by the +// ChannelGraph instance. +// +// NOTE: this is part of the GraphSource interface. +func (s *DBSource) GraphBootstrapper(_ context.Context) ( + discovery.NetworkPeerBootstrapper, error) { + + chanGraph := autopilot.ChannelGraphFromDatabase(s.db) + + return discovery.NewGraphBootstrapper(chanGraph) +} + // kvdbRTx is an implementation of graphdb.RTx backed by a KVDB database read // transaction. type kvdbRTx struct { diff --git a/graph/sources/interfaces.go b/graph/sources/interfaces.go index 2279a2059..44fe79fce 100644 --- a/graph/sources/interfaces.go +++ b/graph/sources/interfaces.go @@ -6,6 +6,7 @@ import ( "github.com/btcsuite/btcd/btcec/v2" "github.com/lightningnetwork/lnd/channeldb" + "github.com/lightningnetwork/lnd/discovery" "github.com/lightningnetwork/lnd/graph/db/models" "github.com/lightningnetwork/lnd/graph/session" "github.com/lightningnetwork/lnd/lnrpc/invoicesrpc" @@ -68,4 +69,9 @@ type GraphSource interface { // graphdb.ErrGraphNodeNotFound is returned. FetchLightningNode(ctx context.Context, nodePub route.Vertex) ( *models.LightningNode, error) + + // GraphBootstrapper returns a network peer bootstrapper that can be + // used to discover new peers to connect to. + GraphBootstrapper(ctx context.Context) ( + discovery.NetworkPeerBootstrapper, error) } diff --git a/server.go b/server.go index 2890f2734..a6416b4ce 100644 --- a/server.go +++ b/server.go @@ -2817,6 +2817,7 @@ out: // based on the server, and currently active bootstrap mechanisms as defined // within the current configuration. func initNetworkBootstrappers(s *server) ([]discovery.NetworkPeerBootstrapper, error) { + ctx := context.TODO() srvrLog.Infof("Initializing peer network bootstrappers!") var bootStrappers []discovery.NetworkPeerBootstrapper @@ -2824,8 +2825,7 @@ func initNetworkBootstrappers(s *server) ([]discovery.NetworkPeerBootstrapper, e // First, we'll create an instance of the ChannelGraphBootstrapper as // this can be used by default if we've already partially seeded the // network. - chanGraph := autopilot.ChannelGraphFromDatabase(s.graphDB) - graphBootstrapper, err := discovery.NewGraphBootstrapper(chanGraph) + graphBootstrapper, err := s.graphSource.GraphBootstrapper(ctx) if err != nil { return nil, err }