github.com/decred/dcrlnd@v0.7.6/routing/graph.go (about)

     1  package routing
     2  
     3  import (
     4  	"github.com/decred/dcrlnd/channeldb"
     5  	"github.com/decred/dcrlnd/kvdb"
     6  	"github.com/decred/dcrlnd/lnwire"
     7  	"github.com/decred/dcrlnd/routing/route"
     8  )
     9  
    10  // routingGraph is an abstract interface that provides information about nodes
    11  // and edges to pathfinding.
    12  type routingGraph interface {
    13  	// forEachNodeChannel calls the callback for every channel of the given
    14  	// node.
    15  	forEachNodeChannel(nodePub route.Vertex,
    16  		cb func(channel *channeldb.DirectedChannel) error) error
    17  
    18  	// sourceNode returns the source node of the graph.
    19  	sourceNode() route.Vertex
    20  
    21  	// fetchNodeFeatures returns the features of the given node.
    22  	fetchNodeFeatures(nodePub route.Vertex) (*lnwire.FeatureVector, error)
    23  }
    24  
    25  // CachedGraph is a routingGraph implementation that retrieves from the
    26  // database.
    27  type CachedGraph struct {
    28  	graph  *channeldb.ChannelGraph
    29  	tx     kvdb.RTx
    30  	source route.Vertex
    31  }
    32  
    33  // A compile time assertion to make sure CachedGraph implements the routingGraph
    34  // interface.
    35  var _ routingGraph = (*CachedGraph)(nil)
    36  
    37  // NewCachedGraph instantiates a new db-connected routing graph. It implicitly
    38  // instantiates a new read transaction.
    39  func NewCachedGraph(sourceNode *channeldb.LightningNode,
    40  	graph *channeldb.ChannelGraph) (*CachedGraph, error) {
    41  
    42  	tx, err := graph.NewPathFindTx()
    43  	if err != nil {
    44  		return nil, err
    45  	}
    46  
    47  	return &CachedGraph{
    48  		graph:  graph,
    49  		tx:     tx,
    50  		source: sourceNode.PubKeyBytes,
    51  	}, nil
    52  }
    53  
    54  // close attempts to close the underlying db transaction. This is a no-op in
    55  // case the underlying graph uses an in-memory cache.
    56  func (g *CachedGraph) close() error {
    57  	if g.tx == nil {
    58  		return nil
    59  	}
    60  
    61  	return g.tx.Rollback()
    62  }
    63  
    64  // forEachNodeChannel calls the callback for every channel of the given node.
    65  //
    66  // NOTE: Part of the routingGraph interface.
    67  func (g *CachedGraph) forEachNodeChannel(nodePub route.Vertex,
    68  	cb func(channel *channeldb.DirectedChannel) error) error {
    69  
    70  	return g.graph.ForEachNodeChannel(g.tx, nodePub, cb)
    71  }
    72  
    73  // sourceNode returns the source node of the graph.
    74  //
    75  // NOTE: Part of the routingGraph interface.
    76  func (g *CachedGraph) sourceNode() route.Vertex {
    77  	return g.source
    78  }
    79  
    80  // fetchNodeFeatures returns the features of the given node. If the node is
    81  // unknown, assume no additional features are supported.
    82  //
    83  // NOTE: Part of the routingGraph interface.
    84  func (g *CachedGraph) fetchNodeFeatures(nodePub route.Vertex) (
    85  	*lnwire.FeatureVector, error) {
    86  
    87  	return g.graph.FetchNodeFeatures(nodePub)
    88  }