diff --git a/autopilot/graph.go b/autopilot/graph.go index 0d98bebd2..c8b54082a 100644 --- a/autopilot/graph.go +++ b/autopilot/graph.go @@ -31,7 +31,7 @@ var ( // // TODO(roasbeef): move inmpl to main package? type databaseChannelGraph struct { - db *graphdb.ChannelGraph + db GraphSource } // A compile time assertion to ensure databaseChannelGraph meets the @@ -39,8 +39,8 @@ type databaseChannelGraph struct { var _ ChannelGraph = (*databaseChannelGraph)(nil) // ChannelGraphFromDatabase returns an instance of the autopilot.ChannelGraph -// backed by a live, open channeldb instance. -func ChannelGraphFromDatabase(db *graphdb.ChannelGraph) ChannelGraph { +// backed by a GraphSource. +func ChannelGraphFromDatabase(db GraphSource) ChannelGraph { return &databaseChannelGraph{ db: db, } @@ -136,7 +136,7 @@ func (d *databaseChannelGraph) ForEachNode(cb func(Node) error) error { // databaseChannelGraphCached wraps a channeldb.ChannelGraph instance with the // necessary API to properly implement the autopilot.ChannelGraph interface. type databaseChannelGraphCached struct { - db *graphdb.ChannelGraph + db GraphSource } // A compile time assertion to ensure databaseChannelGraphCached meets the @@ -145,7 +145,7 @@ var _ ChannelGraph = (*databaseChannelGraphCached)(nil) // ChannelGraphFromCachedDatabase returns an instance of the // autopilot.ChannelGraph backed by a live, open channeldb instance. -func ChannelGraphFromCachedDatabase(db *graphdb.ChannelGraph) ChannelGraph { +func ChannelGraphFromCachedDatabase(db GraphSource) ChannelGraph { return &databaseChannelGraphCached{ db: db, } diff --git a/autopilot/interface.go b/autopilot/interface.go index 671d34332..35182a760 100644 --- a/autopilot/interface.go +++ b/autopilot/interface.go @@ -6,7 +6,9 @@ import ( "github.com/btcsuite/btcd/btcec/v2" "github.com/btcsuite/btcd/btcutil" "github.com/btcsuite/btcd/wire" + graphdb "github.com/lightningnetwork/lnd/graph/db" "github.com/lightningnetwork/lnd/lnwire" + "github.com/lightningnetwork/lnd/routing/route" ) // DefaultConfTarget is the default confirmation target for autopilot channels. @@ -216,3 +218,20 @@ type ChannelController interface { // TODO(roasbeef): add force option? CloseChannel(chanPoint *wire.OutPoint) error } + +// GraphSource represents read access to the channel graph. +type GraphSource interface { + // ForEachNode iterates through all the stored vertices/nodes in the + // graph, executing the passed callback with each node encountered. If + // the callback returns an error, then the transaction is aborted and + // the iteration stops early. Any operations performed on the NodeTx + // passed to the call-back are executed under the same read transaction. + ForEachNode(func(graphdb.NodeRTx) error) error + + // ForEachNodeCached is similar to ForEachNode, but it utilizes the + // channel graph cache if one is available. It is less consistent than + // ForEachNode since any further calls are made across multiple + // transactions. + ForEachNodeCached(cb func(node route.Vertex, + chans map[uint64]*graphdb.DirectedChannel) error) error +}