Skip to content

Commit 2310756

Browse files
committed
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.
1 parent 016fd9b commit 2310756

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

graph/db/kv_store.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -881,19 +881,23 @@ func (c *KVStore) ForEachNodeCacheable(cb func(route.Vertex,
881881
// as the center node within a star-graph. This method may be used to kick off
882882
// a path finding algorithm in order to explore the reachability of another
883883
// node based off the source node.
884-
func (c *KVStore) SourceNode(_ context.Context) (*models.LightningNode,
885-
error) {
884+
func (c *KVStore) SourceNode(_ context.Context) (*models.LightningNode, error) {
885+
return sourceNode(c.db)
886+
}
886887

888+
// sourceNode fetches the source node of the graph. The source node is treated
889+
// as the center node within a star-graph.
890+
func sourceNode(db kvdb.Backend) (*models.LightningNode, error) {
887891
var source *models.LightningNode
888-
err := kvdb.View(c.db, func(tx kvdb.RTx) error {
892+
err := kvdb.View(db, func(tx kvdb.RTx) error {
889893
// First grab the nodes bucket which stores the mapping from
890894
// pubKey to node information.
891895
nodes := tx.ReadBucket(nodeBucket)
892896
if nodes == nil {
893897
return ErrGraphNotFound
894898
}
895899

896-
node, err := c.sourceNode(nodes)
900+
node, err := sourceNodeWithTx(nodes)
897901
if err != nil {
898902
return err
899903
}
@@ -910,13 +914,11 @@ func (c *KVStore) SourceNode(_ context.Context) (*models.LightningNode,
910914
return source, nil
911915
}
912916

913-
// sourceNode uses an existing database transaction and returns the source node
914-
// of the graph. The source node is treated as the center node within a
917+
// sourceNodeWithTx uses an existing database transaction and returns the source
918+
// node of the graph. The source node is treated as the center node within a
915919
// star-graph. This method may be used to kick off a path finding algorithm in
916920
// order to explore the reachability of another node based off the source node.
917-
func (c *KVStore) sourceNode(nodes kvdb.RBucket) (*models.LightningNode,
918-
error) {
919-
921+
func sourceNodeWithTx(nodes kvdb.RBucket) (*models.LightningNode, error) {
920922
selfPub := nodes.Get(sourceKey)
921923
if selfPub == nil {
922924
return nil, ErrSourceNodeNotSet
@@ -1569,7 +1571,7 @@ func (c *KVStore) pruneGraphNodes(nodes kvdb.RwBucket,
15691571

15701572
// We'll retrieve the graph's source node to ensure we don't remove it
15711573
// even if it no longer has any open channels.
1572-
sourceNode, err := c.sourceNode(nodes)
1574+
sourceNode, err := sourceNodeWithTx(nodes)
15731575
if err != nil {
15741576
return nil, err
15751577
}
@@ -3255,7 +3257,7 @@ func (c *KVStore) ForEachSourceNodeChannel(cb func(chanPoint wire.OutPoint,
32553257
return ErrGraphNotFound
32563258
}
32573259

3258-
node, err := c.sourceNode(nodes)
3260+
node, err := sourceNodeWithTx(nodes)
32593261
if err != nil {
32603262
return err
32613263
}

graph/db/sql_store.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3699,7 +3699,7 @@ type srcNodeInfo struct {
36993699
pub route.Vertex
37003700
}
37013701

3702-
// getSourceNode returns the DB node ID and pub key of the source node for the
3702+
// sourceNode returns the DB node ID and pub key of the source node for the
37033703
// specified protocol version.
37043704
func (s *SQLStore) getSourceNode(ctx context.Context, db SQLQueries,
37053705
version ProtocolVersion) (int64, route.Vertex, error) {

0 commit comments

Comments
 (0)