From c1d7a9d2e75b9f0f30fade57ffb05660bb844bbe Mon Sep 17 00:00:00 2001 From: Elle Mouton Date: Fri, 14 Jun 2024 20:05:20 -0400 Subject: [PATCH] multi: move ChannelGraphSource interface ... to the new `graph` package in preparation for the implementation of the interface being moved to this new package. --- discovery/gossiper.go | 3 +- discovery/gossiper_test.go | 3 +- graph/interfaces.go | 90 ++++++++++++++++++++++++++++++++++++++ routing/router.go | 79 +-------------------------------- 4 files changed, 96 insertions(+), 79 deletions(-) create mode 100644 graph/interfaces.go diff --git a/discovery/gossiper.go b/discovery/gossiper.go index 8a1c30136..6786b9db0 100644 --- a/discovery/gossiper.go +++ b/discovery/gossiper.go @@ -20,6 +20,7 @@ import ( "github.com/lightningnetwork/lnd/chainntnfs" "github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/channeldb/models" + "github.com/lightningnetwork/lnd/graph" "github.com/lightningnetwork/lnd/keychain" "github.com/lightningnetwork/lnd/kvdb" "github.com/lightningnetwork/lnd/lnpeer" @@ -169,7 +170,7 @@ type Config struct { // topology of lightning network. After incoming channel, node, channel // updates announcements are validated they are sent to the router in // order to be included in the LN graph. - Router routing.ChannelGraphSource + Router graph.ChannelGraphSource // ChanSeries is an interfaces that provides access to a time series // view of the current known channel graph. Each GossipSyncer enabled diff --git a/discovery/gossiper_test.go b/discovery/gossiper_test.go index a7b850529..4971e980a 100644 --- a/discovery/gossiper_test.go +++ b/discovery/gossiper_test.go @@ -25,6 +25,7 @@ import ( "github.com/lightningnetwork/lnd/chainntnfs" "github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/channeldb/models" + "github.com/lightningnetwork/lnd/graph" "github.com/lightningnetwork/lnd/keychain" "github.com/lightningnetwork/lnd/kvdb" "github.com/lightningnetwork/lnd/lnpeer" @@ -108,7 +109,7 @@ func newMockRouter(height uint32) *mockGraphSource { } } -var _ routing.ChannelGraphSource = (*mockGraphSource)(nil) +var _ graph.ChannelGraphSource = (*mockGraphSource)(nil) func (r *mockGraphSource) AddNode(node *channeldb.LightningNode, _ ...batch.SchedulerOption) error { diff --git a/graph/interfaces.go b/graph/interfaces.go new file mode 100644 index 000000000..b49a07af9 --- /dev/null +++ b/graph/interfaces.go @@ -0,0 +1,90 @@ +package graph + +import ( + "time" + + "github.com/lightningnetwork/lnd/batch" + "github.com/lightningnetwork/lnd/channeldb" + "github.com/lightningnetwork/lnd/channeldb/models" + "github.com/lightningnetwork/lnd/kvdb" + "github.com/lightningnetwork/lnd/lnwire" + "github.com/lightningnetwork/lnd/routing/route" +) + +// ChannelGraphSource represents the source of information about the topology +// of the lightning network. It's responsible for the addition of nodes, edges, +// applying edge updates, and returning the current block height with which the +// topology is synchronized. +// +//nolint:interfacebloat +type ChannelGraphSource interface { + // AddNode is used to add information about a node to the router + // database. If the node with this pubkey is not present in an existing + // channel, it will be ignored. + AddNode(node *channeldb.LightningNode, + op ...batch.SchedulerOption) error + + // AddEdge is used to add edge/channel to the topology of the router, + // after all information about channel will be gathered this + // edge/channel might be used in construction of payment path. + AddEdge(edge *models.ChannelEdgeInfo, + op ...batch.SchedulerOption) error + + // AddProof updates the channel edge info with proof which is needed to + // properly announce the edge to the rest of the network. + AddProof(chanID lnwire.ShortChannelID, + proof *models.ChannelAuthProof) error + + // UpdateEdge is used to update edge information, without this message + // edge considered as not fully constructed. + UpdateEdge(policy *models.ChannelEdgePolicy, + op ...batch.SchedulerOption) error + + // IsStaleNode returns true if the graph source has a node announcement + // for the target node with a more recent timestamp. This method will + // also return true if we don't have an active channel announcement for + // the target node. + IsStaleNode(node route.Vertex, timestamp time.Time) bool + + // IsPublicNode determines whether the given vertex is seen as a public + // node in the graph from the graph's source node's point of view. + IsPublicNode(node route.Vertex) (bool, error) + + // IsKnownEdge returns true if the graph source already knows of the + // passed channel ID either as a live or zombie edge. + IsKnownEdge(chanID lnwire.ShortChannelID) bool + + // IsStaleEdgePolicy returns true if the graph source has a channel + // edge for the passed channel ID (and flags) that have a more recent + // timestamp. + IsStaleEdgePolicy(chanID lnwire.ShortChannelID, timestamp time.Time, + flags lnwire.ChanUpdateChanFlags) bool + + // MarkEdgeLive clears an edge from our zombie index, deeming it as + // live. + MarkEdgeLive(chanID lnwire.ShortChannelID) error + + // ForAllOutgoingChannels is used to iterate over all channels + // emanating from the "source" node which is the center of the + // star-graph. + ForAllOutgoingChannels(cb func(tx kvdb.RTx, + c *models.ChannelEdgeInfo, + e *models.ChannelEdgePolicy) error) error + + // CurrentBlockHeight returns the block height from POV of the router + // subsystem. + CurrentBlockHeight() (uint32, error) + + // GetChannelByID return the channel by the channel id. + GetChannelByID(chanID lnwire.ShortChannelID) ( + *models.ChannelEdgeInfo, *models.ChannelEdgePolicy, + *models.ChannelEdgePolicy, error) + + // FetchLightningNode attempts to look up a target node by its identity + // public key. channeldb.ErrGraphNodeNotFound is returned if the node + // doesn't exist within the graph. + FetchLightningNode(route.Vertex) (*channeldb.LightningNode, error) + + // ForEachNode is used to iterate over every node in the known graph. + ForEachNode(func(node *channeldb.LightningNode) error) error +} diff --git a/routing/router.go b/routing/router.go index 33f5a7814..f722c2664 100644 --- a/routing/router.go +++ b/routing/router.go @@ -24,6 +24,7 @@ import ( "github.com/lightningnetwork/lnd/channeldb/models" "github.com/lightningnetwork/lnd/clock" "github.com/lightningnetwork/lnd/fn" + "github.com/lightningnetwork/lnd/graph" "github.com/lightningnetwork/lnd/htlcswitch" "github.com/lightningnetwork/lnd/input" "github.com/lightningnetwork/lnd/kvdb" @@ -127,82 +128,6 @@ var ( ErrSkipTempErr = errors.New("cannot skip temp error for non-MPP") ) -// ChannelGraphSource represents the source of information about the topology -// of the lightning network. It's responsible for the addition of nodes, edges, -// applying edge updates, and returning the current block height with which the -// topology is synchronized. -type ChannelGraphSource interface { - // AddNode is used to add information about a node to the router - // database. If the node with this pubkey is not present in an existing - // channel, it will be ignored. - AddNode(node *channeldb.LightningNode, - op ...batch.SchedulerOption) error - - // AddEdge is used to add edge/channel to the topology of the router, - // after all information about channel will be gathered this - // edge/channel might be used in construction of payment path. - AddEdge(edge *models.ChannelEdgeInfo, - op ...batch.SchedulerOption) error - - // AddProof updates the channel edge info with proof which is needed to - // properly announce the edge to the rest of the network. - AddProof(chanID lnwire.ShortChannelID, - proof *models.ChannelAuthProof) error - - // UpdateEdge is used to update edge information, without this message - // edge considered as not fully constructed. - UpdateEdge(policy *models.ChannelEdgePolicy, - op ...batch.SchedulerOption) error - - // IsStaleNode returns true if the graph source has a node announcement - // for the target node with a more recent timestamp. This method will - // also return true if we don't have an active channel announcement for - // the target node. - IsStaleNode(node route.Vertex, timestamp time.Time) bool - - // IsPublicNode determines whether the given vertex is seen as a public - // node in the graph from the graph's source node's point of view. - IsPublicNode(node route.Vertex) (bool, error) - - // IsKnownEdge returns true if the graph source already knows of the - // passed channel ID either as a live or zombie edge. - IsKnownEdge(chanID lnwire.ShortChannelID) bool - - // IsStaleEdgePolicy returns true if the graph source has a channel - // edge for the passed channel ID (and flags) that have a more recent - // timestamp. - IsStaleEdgePolicy(chanID lnwire.ShortChannelID, timestamp time.Time, - flags lnwire.ChanUpdateChanFlags) bool - - // MarkEdgeLive clears an edge from our zombie index, deeming it as - // live. - MarkEdgeLive(chanID lnwire.ShortChannelID) error - - // ForAllOutgoingChannels is used to iterate over all channels - // emanating from the "source" node which is the center of the - // star-graph. - ForAllOutgoingChannels(cb func(tx kvdb.RTx, - c *models.ChannelEdgeInfo, - e *models.ChannelEdgePolicy) error) error - - // CurrentBlockHeight returns the block height from POV of the router - // subsystem. - CurrentBlockHeight() (uint32, error) - - // GetChannelByID return the channel by the channel id. - GetChannelByID(chanID lnwire.ShortChannelID) ( - *models.ChannelEdgeInfo, *models.ChannelEdgePolicy, - *models.ChannelEdgePolicy, error) - - // FetchLightningNode attempts to look up a target node by its identity - // public key. channeldb.ErrGraphNodeNotFound is returned if the node - // doesn't exist within the graph. - FetchLightningNode(route.Vertex) (*channeldb.LightningNode, error) - - // ForEachNode is used to iterate over every node in the known graph. - ForEachNode(func(node *channeldb.LightningNode) error) error -} - // PaymentAttemptDispatcher is used by the router to send payment attempts onto // the network, and receive their results. type PaymentAttemptDispatcher interface { @@ -499,7 +424,7 @@ type ChannelRouter struct { // A compile time check to ensure ChannelRouter implements the // ChannelGraphSource interface. -var _ ChannelGraphSource = (*ChannelRouter)(nil) +var _ graph.ChannelGraphSource = (*ChannelRouter)(nil) // New creates a new instance of the ChannelRouter with the specified // configuration parameters. As part of initialization, if the router detects