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:
Elle Mouton
2025-06-26 13:37:30 +02:00
parent 016fd9bb4d
commit 2310756307
2 changed files with 14 additions and 12 deletions

View File

@@ -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
// a path finding algorithm in order to explore the reachability of another
// node based off the source node.
func (c *KVStore) SourceNode(_ context.Context) (*models.LightningNode,
error) {
func (c *KVStore) SourceNode(_ context.Context) (*models.LightningNode, 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
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
// pubKey to node information.
nodes := tx.ReadBucket(nodeBucket)
@@ -893,7 +897,7 @@ func (c *KVStore) SourceNode(_ context.Context) (*models.LightningNode,
return ErrGraphNotFound
}
node, err := c.sourceNode(nodes)
node, err := sourceNodeWithTx(nodes)
if err != nil {
return err
}
@@ -910,13 +914,11 @@ func (c *KVStore) SourceNode(_ context.Context) (*models.LightningNode,
return source, nil
}
// sourceNode uses an existing database transaction and returns the source node
// of the graph. The source node is treated as the center node within a
// sourceNodeWithTx uses an existing database transaction and returns the source
// 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
// order to explore the reachability of another node based off the source node.
func (c *KVStore) sourceNode(nodes kvdb.RBucket) (*models.LightningNode,
error) {
func sourceNodeWithTx(nodes kvdb.RBucket) (*models.LightningNode, error) {
selfPub := nodes.Get(sourceKey)
if selfPub == nil {
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
// even if it no longer has any open channels.
sourceNode, err := c.sourceNode(nodes)
sourceNode, err := sourceNodeWithTx(nodes)
if err != nil {
return nil, err
}
@@ -3255,7 +3257,7 @@ func (c *KVStore) ForEachSourceNodeChannel(cb func(chanPoint wire.OutPoint,
return ErrGraphNotFound
}
node, err := c.sourceNode(nodes)
node, err := sourceNodeWithTx(nodes)
if err != nil {
return err
}