From a6bf76a0b7c9d3e6a3cff0251c24698f98b8d922 Mon Sep 17 00:00:00 2001 From: Elle Mouton Date: Mon, 2 Sep 2024 12:10:13 +0200 Subject: [PATCH] discovery+lnwallet: add fetchPKScript helper to gossiper This commit makes an lnwallet.BlockChainIO available to the gossiper and uses it to construct a helper that can be used to fetch the pk script for a given SCID. This will be used for channel announcement verification in an upcoming commit. --- discovery/gossiper.go | 11 +++++++++++ lnwallet/interface.go | 24 ++++++++++++++++++++++++ server.go | 1 + 3 files changed, 36 insertions(+) diff --git a/discovery/gossiper.go b/discovery/gossiper.go index a9ce801a0..a2a215d0d 100644 --- a/discovery/gossiper.go +++ b/discovery/gossiper.go @@ -171,6 +171,10 @@ type Config struct { // order to be included in the LN graph. Graph graph.ChannelGraphSource + // ChainIO represents an abstraction over a source that can query the + // blockchain. + ChainIO lnwallet.BlockChainIO + // ChanSeries is an interfaces that provides access to a time series // view of the current known channel graph. Each GossipSyncer enabled // peer will utilize this in order to create and respond to channel @@ -1930,6 +1934,13 @@ func (d *AuthenticatedGossiper) processRejectedEdge( return announcements, nil } +// fetchPKScript fetches the output script for the given SCID. +func (d *AuthenticatedGossiper) fetchPKScript(chanID *lnwire.ShortChannelID) ( + []byte, error) { + + return lnwallet.FetchPKScriptWithQuit(d.cfg.ChainIO, chanID, d.quit) +} + // addNode processes the given node announcement, and adds it to our channel // graph. func (d *AuthenticatedGossiper) addNode(msg *lnwire.NodeAnnouncement, diff --git a/lnwallet/interface.go b/lnwallet/interface.go index 3c59313f7..2a4462462 100644 --- a/lnwallet/interface.go +++ b/lnwallet/interface.go @@ -21,6 +21,7 @@ import ( "github.com/lightningnetwork/lnd/fn" "github.com/lightningnetwork/lnd/keychain" "github.com/lightningnetwork/lnd/lnwallet/chainfee" + "github.com/lightningnetwork/lnd/lnwallet/chanvalidate" "github.com/lightningnetwork/lnd/lnwire" ) @@ -737,3 +738,26 @@ func FetchFundingTx(chain BlockChainIO, return fundingBlock.Transactions[chanID.TxIndex].Copy(), nil } + +// FetchPKScriptWithQuit fetches the output script for the given SCID and exits +// early with an error if the provided quit channel is closed before +// completion. +func FetchPKScriptWithQuit(chain BlockChainIO, chanID *lnwire.ShortChannelID, + quit chan struct{}) ([]byte, error) { + + tx, err := FetchFundingTxWrapper(chain, chanID, quit) + if err != nil { + return nil, err + } + + outputLocator := chanvalidate.ShortChanIDChanLocator{ + ID: *chanID, + } + + output, _, err := outputLocator.Locate(tx) + if err != nil { + return nil, err + } + + return output.PkScript, nil +} diff --git a/server.go b/server.go index a188f6269..695375400 100644 --- a/server.go +++ b/server.go @@ -1042,6 +1042,7 @@ func newServer(cfg *Config, listenAddrs []net.Addr, s.authGossiper = discovery.New(discovery.Config{ Graph: s.graphBuilder, + ChainIO: s.cc.ChainIO, Notifier: s.cc.ChainNotifier, ChainHash: *s.cfg.ActiveNetParams.GenesisHash, Broadcast: s.BroadcastMessage,