From 23107563077cd8139d6b271d97f24d32d1da5303 Mon Sep 17 00:00:00 2001 From: Elle Mouton Date: Thu, 26 Jun 2025 13:37:30 +0200 Subject: [PATCH] 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. --- graph/db/kv_store.go | 24 +++++++++++++----------- graph/db/sql_store.go | 2 +- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/graph/db/kv_store.go b/graph/db/kv_store.go index d0892b48d..eef4f7ec5 100644 --- a/graph/db/kv_store.go +++ b/graph/db/kv_store.go @@ -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 } diff --git a/graph/db/sql_store.go b/graph/db/sql_store.go index 067a044d3..c26312f0d 100644 --- a/graph/db/sql_store.go +++ b/graph/db/sql_store.go @@ -3699,7 +3699,7 @@ type srcNodeInfo struct { 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. func (s *SQLStore) getSourceNode(ctx context.Context, db SQLQueries, version ProtocolVersion) (int64, route.Vertex, error) {