graph/db: refactor source node fetching logic
Small refactor to introduce a re-usable getSourceNode function that we can then make use of in the upcoming kvdb->SQL migration logic.
This commit is contained in:
2 files changed
+14
-12
No files matched your search
+13
-11
@@ -881,11 +881,15 @@ func (c *KVStore) ForEachNodeCacheable(cb func(route.Vertex,
|
|||||||
// as the center node within a star-graph. This method may be used to kick off
|
// as the center node within a star-graph. This method may be used to kick off
|
||||||
// a path finding algorithm in order to explore the reachability of another
|
// a path finding algorithm in order to explore the reachability of another
|
||||||
// node based off the source node.
|
// node based off the source node.
|
||||||
func (c *KVStore) SourceNode(_ context.Context) (*models.LightningNode,
|
func (c *KVStore) SourceNode(_ context.Context) (*models.LightningNode, error) {
|
||||||
error) {
|
return sourceNode(c.db)
|
||||||
|
}
|
||||||
|
|
||||||
|
// sourceNode fetches the source node of the graph. The source node is treated
|
||||||
|
// as the center node within a star-graph.
|
||||||
|
func sourceNode(db kvdb.Backend) (*models.LightningNode, error) {
|
||||||
var source *models.LightningNode
|
var source *models.LightningNode
|
||||||
err := kvdb.View(c.db, func(tx kvdb.RTx) error {
|
err := kvdb.View(db, func(tx kvdb.RTx) error {
|
||||||
// First grab the nodes bucket which stores the mapping from
|
// First grab the nodes bucket which stores the mapping from
|
||||||
// pubKey to node information.
|
// pubKey to node information.
|
||||||
nodes := tx.ReadBucket(nodeBucket)
|
nodes := tx.ReadBucket(nodeBucket)
|
||||||
@@ -893,7 +897,7 @@ func (c *KVStore) SourceNode(_ context.Context) (*models.LightningNode,
|
|||||||
return ErrGraphNotFound
|
return ErrGraphNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
node, err := c.sourceNode(nodes)
|
node, err := sourceNodeWithTx(nodes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -910,13 +914,11 @@ func (c *KVStore) SourceNode(_ context.Context) (*models.LightningNode,
|
|||||||
return source, nil
|
return source, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// sourceNode uses an existing database transaction and returns the source node
|
// sourceNodeWithTx uses an existing database transaction and returns the source
|
||||||
// of the graph. The source node is treated as the center node within a
|
// node of the graph. The source node is treated as the center node within a
|
||||||
// star-graph. This method may be used to kick off a path finding algorithm in
|
// star-graph. This method may be used to kick off a path finding algorithm in
|
||||||
// order to explore the reachability of another node based off the source node.
|
// order to explore the reachability of another node based off the source node.
|
||||||
func (c *KVStore) sourceNode(nodes kvdb.RBucket) (*models.LightningNode,
|
func sourceNodeWithTx(nodes kvdb.RBucket) (*models.LightningNode, error) {
|
||||||
error) {
|
|
||||||
|
|
||||||
selfPub := nodes.Get(sourceKey)
|
selfPub := nodes.Get(sourceKey)
|
||||||
if selfPub == nil {
|
if selfPub == nil {
|
||||||
return nil, ErrSourceNodeNotSet
|
return nil, ErrSourceNodeNotSet
|
||||||
@@ -1569,7 +1571,7 @@ func (c *KVStore) pruneGraphNodes(nodes kvdb.RwBucket,
|
|||||||
|
|
||||||
// We'll retrieve the graph's source node to ensure we don't remove it
|
// We'll retrieve the graph's source node to ensure we don't remove it
|
||||||
// even if it no longer has any open channels.
|
// even if it no longer has any open channels.
|
||||||
sourceNode, err := c.sourceNode(nodes)
|
sourceNode, err := sourceNodeWithTx(nodes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -3255,7 +3257,7 @@ func (c *KVStore) ForEachSourceNodeChannel(cb func(chanPoint wire.OutPoint,
|
|||||||
return ErrGraphNotFound
|
return ErrGraphNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
node, err := c.sourceNode(nodes)
|
node, err := sourceNodeWithTx(nodes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3699,7 +3699,7 @@ type srcNodeInfo struct {
|
|||||||
pub route.Vertex
|
pub route.Vertex
|
||||||
}
|
}
|
||||||
|
|
||||||
// getSourceNode returns the DB node ID and pub key of the source node for the
|
// sourceNode returns the DB node ID and pub key of the source node for the
|
||||||
// specified protocol version.
|
// specified protocol version.
|
||||||
func (s *SQLStore) getSourceNode(ctx context.Context, db SQLQueries,
|
func (s *SQLStore) getSourceNode(ctx context.Context, db SQLQueries,
|
||||||
version ProtocolVersion) (int64, route.Vertex, error) {
|
version ProtocolVersion) (int64, route.Vertex, error) {
|
||||||
|
|||||||
Reference in new issue
Block a user