From 083d3c9d7c54927fb07f9ccf714f1162d9c48f03 Mon Sep 17 00:00:00 2001 From: Elle Mouton Date: Tue, 22 Oct 2024 14:02:43 +0200 Subject: [PATCH] channeldb: define a single AddrSource interface Our aim is to completely remove the `channeldb.DB`'s direct dependence on the `graphdb.ChannelGraph` pointer. The only place where it still depends on this pointer is in the `(DB).AddrsForNode(..)` method where it queries both the channel DB and the graph db for the known addresses for the node in question and then combines the results. So, to separate these out, we will define an AddrsForNodes interface in this commit which we will later let both the ChannelGraph and channeldb.DB both implement and we will merge these results outside of the channeldb package. All this commit does is to unify the `AddrSource` interface since this has been defined separately in a couple of places. --- chanbackup/backup.go | 16 +++------------- channel_notifier.go | 13 +------------ channeldb/addr_source.go | 15 +++++++++++++++ server.go | 2 +- 4 files changed, 20 insertions(+), 26 deletions(-) create mode 100644 channeldb/addr_source.go diff --git a/chanbackup/backup.go b/chanbackup/backup.go index 5d9d769e8..a5ba49748 100644 --- a/chanbackup/backup.go +++ b/chanbackup/backup.go @@ -2,9 +2,7 @@ package chanbackup import ( "fmt" - "net" - "github.com/btcsuite/btcd/btcec/v2" "github.com/btcsuite/btcd/wire" "github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/fn" @@ -24,19 +22,11 @@ type LiveChannelSource interface { *channeldb.OpenChannel, error) } -// AddressSource is an interface that allows us to query for the set of -// addresses a node can be connected to. -type AddressSource interface { - // AddrsForNode returns all known addresses for the target node public - // key. - AddrsForNode(nodePub *btcec.PublicKey) ([]net.Addr, error) -} - // assembleChanBackup attempts to assemble a static channel backup for the // passed open channel. The backup includes all information required to restore // the channel, as well as addressing information so we can find the peer and // reconnect to them to initiate the protocol. -func assembleChanBackup(addrSource AddressSource, +func assembleChanBackup(addrSource channeldb.AddrSource, openChan *channeldb.OpenChannel) (*Single, error) { log.Debugf("Crafting backup for ChannelPoint(%v)", @@ -100,7 +90,7 @@ func buildCloseTxInputs( // the target channel identified by its channel point. If we're unable to find // the target channel, then an error will be returned. func FetchBackupForChan(chanPoint wire.OutPoint, chanSource LiveChannelSource, - addrSource AddressSource) (*Single, error) { + addrSource channeldb.AddrSource) (*Single, error) { // First, we'll query the channel source to see if the channel is known // and open within the database. @@ -124,7 +114,7 @@ func FetchBackupForChan(chanPoint wire.OutPoint, chanSource LiveChannelSource, // FetchStaticChanBackups will return a plaintext static channel back up for // all known active/open channels within the passed channel source. func FetchStaticChanBackups(chanSource LiveChannelSource, - addrSource AddressSource) ([]Single, error) { + addrSource channeldb.AddrSource) ([]Single, error) { // First, we'll query the backup source for information concerning all // currently open and available channels. diff --git a/channel_notifier.go b/channel_notifier.go index 5995eb409..6d08620ef 100644 --- a/channel_notifier.go +++ b/channel_notifier.go @@ -2,24 +2,13 @@ package lnd import ( "fmt" - "net" - "github.com/btcsuite/btcd/btcec/v2" "github.com/btcsuite/btcd/wire" "github.com/lightningnetwork/lnd/chanbackup" "github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/channelnotifier" ) -// addrSource is an interface that allow us to get the addresses for a target -// node. We'll need this in order to be able to properly proxy the -// notifications to create SCBs. -type addrSource interface { - // AddrsForNode returns all known addresses for the target node public - // key. - AddrsForNode(nodePub *btcec.PublicKey) ([]net.Addr, error) -} - // channelNotifier is an implementation of the chanbackup.ChannelNotifier // interface using the existing channelnotifier.ChannelNotifier struct. This // implementation allows us to satisfy all the dependencies of the @@ -32,7 +21,7 @@ type channelNotifier struct { // addrs is an implementation of the addrSource interface that allows // us to get the latest set of addresses for a given node. We'll need // this to be able to create an SCB for new channels. - addrs addrSource + addrs channeldb.AddrSource } // SubscribeChans requests a new channel subscription relative to the initial diff --git a/channeldb/addr_source.go b/channeldb/addr_source.go new file mode 100644 index 000000000..7fc0f8134 --- /dev/null +++ b/channeldb/addr_source.go @@ -0,0 +1,15 @@ +package channeldb + +import ( + "net" + + "github.com/btcsuite/btcd/btcec/v2" +) + +// AddrSource is an interface that allow us to get the addresses for a target +// node. It may combine the results of multiple address sources. +type AddrSource interface { + // AddrsForNode returns all known addresses for the target node public + // key. + AddrsForNode(nodePub *btcec.PublicKey) ([]net.Addr, error) +} diff --git a/server.go b/server.go index 6369c7ed8..70b7d68f8 100644 --- a/server.go +++ b/server.go @@ -263,7 +263,7 @@ type server struct { chanStateDB *channeldb.ChannelStateDB - addrSource chanbackup.AddressSource + addrSource channeldb.AddrSource // miscDB is the DB that contains all "other" databases within the main // channel DB that haven't been separated out yet.