graph/db: thread context through to SetSourceNode

This commit is contained in:
Elle Mouton committed 2025-06-19 14:47:40 +02:00
1 parent e0a9705d59
commit 65049ddd02
11 files changed
+49 -32

No files matched your search

+2 -2
View File
@@ -1445,7 +1445,7 @@ func parseTestGraph(t *testing.T, useCache bool, path string) (
if source != nil {
// Set the selected source node
if err := graph.SetSourceNode(source); err != nil {
if err := graph.SetSourceNode(ctx, source); err != nil {
return nil, err
}
}
@@ -1785,7 +1785,7 @@ func createTestGraphFromChannels(t *testing.T, useCache bool,
return nil, err
}
if err = graph.SetSourceNode(dbNode); err != nil {
if err = graph.SetSourceNode(ctx, dbNode); err != nil {
return nil, err
}
+16 -12
View File
@@ -363,6 +363,7 @@ func TestAliasLookup(t *testing.T) {
// TestSourceNode tests the source node functionality of the graph store.
func TestSourceNode(t *testing.T) {
t.Parallel()
ctx := context.Background()
graph := MakeTestGraphNew(t)
@@ -377,7 +378,7 @@ func TestSourceNode(t *testing.T) {
// Set the source node, this should insert the node into the
// database in a special way indicating it's the source node.
require.NoError(t, graph.SetSourceNode(testNode))
require.NoError(t, graph.SetSourceNode(ctx, testNode))
// Retrieve the source node from the database, it should exactly match
// the one we set above.
@@ -511,11 +512,12 @@ func createEdge(height, txIndex uint32, txPosition uint16, outPointIndex uint32,
// database is what we expect after calling DisconnectBlockAtHeight.
func TestDisconnectBlockAtHeight(t *testing.T) {
t.Parallel()
ctx := context.Background()
graph := MakeTestGraph(t)
sourceNode := createTestVertex(t)
if err := graph.SetSourceNode(sourceNode); err != nil {
if err := graph.SetSourceNode(ctx, sourceNode); err != nil {
t.Fatalf("unable to set source node: %v", err)
}
@@ -1183,12 +1185,13 @@ func TestAddEdgeProof(t *testing.T) {
// correctly iterates through the channels of the set source node.
func TestForEachSourceNodeChannel(t *testing.T) {
t.Parallel()
ctx := context.Background()
graph := MakeTestGraphNew(t)
// Create a source node (A) and set it as such in the DB.
nodeA := createTestVertex(t)
require.NoError(t, graph.SetSourceNode(nodeA))
require.NoError(t, graph.SetSourceNode(ctx, nodeA))
// Now, create a few more nodes (B, C, D) along with some channels
// between them. We'll create the following graph:
@@ -1685,7 +1688,7 @@ func TestGraphPruning(t *testing.T) {
graph := MakeTestGraph(t)
sourceNode := createTestVertex(t)
if err := graph.SetSourceNode(sourceNode); err != nil {
if err := graph.SetSourceNode(ctx, sourceNode); err != nil {
t.Fatalf("unable to set source node: %v", err)
}
@@ -2444,7 +2447,7 @@ func TestStressTestChannelGraphAPI(t *testing.T) {
node2 := createTestVertex(t)
require.NoError(t, graph.AddLightningNode(ctx, node2))
require.NoError(t, graph.SetSourceNode(node1))
require.NoError(t, graph.SetSourceNode(ctx, node1))
type chanInfo struct {
info models.ChannelEdgeInfo
@@ -3146,7 +3149,7 @@ func TestChannelEdgePruningUpdateIndexDeletion(t *testing.T) {
}
sourceNode := createTestVertex(t)
if err := graph.SetSourceNode(sourceNode); err != nil {
if err := graph.SetSourceNode(ctx, sourceNode); err != nil {
t.Fatalf("unable to set source node: %v", err)
}
@@ -3290,7 +3293,7 @@ func TestPruneGraphNodes(t *testing.T) {
// We'll start off by inserting our source node, to ensure that it's
// the only node left after we prune the graph.
sourceNode := createTestVertex(t)
if err := graph.SetSourceNode(sourceNode); err != nil {
if err := graph.SetSourceNode(ctx, sourceNode); err != nil {
t.Fatalf("unable to set source node: %v", err)
}
@@ -3356,7 +3359,7 @@ func TestAddChannelEdgeShellNodes(t *testing.T) {
// To start, we'll create two nodes, and only add one of them to the
// channel graph.
node1 := createTestVertex(t)
require.NoError(t, graph.SetSourceNode(node1))
require.NoError(t, graph.SetSourceNode(ctx, node1))
node2 := createTestVertex(t)
// We'll now create an edge between the two nodes, as a result, node2
@@ -3447,19 +3450,19 @@ func TestNodeIsPublic(t *testing.T) {
// some graphs but not others, etc.).
aliceGraph := MakeTestGraph(t)
aliceNode := createTestVertex(t)
if err := aliceGraph.SetSourceNode(aliceNode); err != nil {
if err := aliceGraph.SetSourceNode(ctx, aliceNode); err != nil {
t.Fatalf("unable to set source node: %v", err)
}
bobGraph := MakeTestGraph(t)
bobNode := createTestVertex(t)
if err := bobGraph.SetSourceNode(bobNode); err != nil {
if err := bobGraph.SetSourceNode(ctx, bobNode); err != nil {
t.Fatalf("unable to set source node: %v", err)
}
carolGraph := MakeTestGraph(t)
carolNode := createTestVertex(t)
if err := carolGraph.SetSourceNode(carolNode); err != nil {
if err := carolGraph.SetSourceNode(ctx, carolNode); err != nil {
t.Fatalf("unable to set source node: %v", err)
}
@@ -3982,11 +3985,12 @@ func TestComputeFee(t *testing.T) {
// executes multiple AddChannelEdge requests in a single txn.
func TestBatchedAddChannelEdge(t *testing.T) {
t.Parallel()
ctx := context.Background()
graph := MakeTestGraph(t)
sourceNode := createTestVertex(t)
require.Nil(t, graph.SetSourceNode(sourceNode))
require.Nil(t, graph.SetSourceNode(ctx, sourceNode))
// We'd like to test the insertion/deletion of edges, so we create two
// vertexes to connect.
+2 -1
View File
@@ -344,7 +344,8 @@ type V1Store interface { //nolint:interfacebloat
// SetSourceNode sets the source node within the graph database. The
// source node is to be used as the center of a star-graph within path
// finding algorithms.
SetSourceNode(node *models.LightningNode) error
SetSourceNode(ctx context.Context,
node *models.LightningNode) error
// PruneTip returns the block height and hash of the latest block that
// has been used to prune channels in the graph. Knowing the "prune tip"
+3 -1
View File
@@ -926,7 +926,9 @@ func (c *KVStore) sourceNode(nodes kvdb.RBucket) (*models.LightningNode,
// SetSourceNode sets the source node within the graph database. The source
// node is to be used as the center of a star-graph within path finding
// algorithms.
func (c *KVStore) SetSourceNode(node *models.LightningNode) error {
func (c *KVStore) SetSourceNode(_ context.Context,
node *models.LightningNode) error {
nodePubBytes := node.PubKeyBytes[:]
return kvdb.Update(c.db, func(tx kvdb.RwTx) error {
+2 -2
View File
@@ -421,8 +421,8 @@ func (s *SQLStore) SourceNode() (*models.LightningNode, error) {
// algorithms.
//
// NOTE: part of the V1Store interface.
func (s *SQLStore) SetSourceNode(node *models.LightningNode) error {
ctx := context.TODO()
func (s *SQLStore) SetSourceNode(ctx context.Context,
node *models.LightningNode) error {
return s.db.ExecTx(ctx, sqldb.WriteTxOpt(), func(db SQLQueries) error {
id, err := upsertNode(ctx, db, node)
+2 -1
View File
@@ -1061,7 +1061,8 @@ func createTestCtxSingleNode(t *testing.T,
sourceNode := createTestNode(t)
require.NoError(t,
graph.SetSourceNode(sourceNode), "failed to set source node",
graph.SetSourceNode(context.Background(), sourceNode),
"failed to set source node",
)
graphInstance := &testGraphInstance{
+3 -1
View File
@@ -4,6 +4,7 @@
package peersrpc
import (
"context"
"net"
"github.com/lightningnetwork/lnd/lnwire"
@@ -27,6 +28,7 @@ type Config struct {
// setting the feature vector provided and applying the
// NodeAnnModifiers. If no feature updates are required, a nil feature
// vector should be provided.
UpdateNodeAnnouncement func(features *lnwire.RawFeatureVector,
UpdateNodeAnnouncement func(ctx context.Context,
features *lnwire.RawFeatureVector,
mods ...netann.NodeAnnModifier) error
}
+2 -2
View File
@@ -306,7 +306,7 @@ func (s *Server) updateFeatures(currentfeatures *lnwire.RawFeatureVector,
// UpdateNodeAnnouncement allows the caller to update the node parameters
// and broadcasts a new version of the node announcement to its peers.
func (s *Server) UpdateNodeAnnouncement(_ context.Context,
func (s *Server) UpdateNodeAnnouncement(ctx context.Context,
req *NodeAnnouncementUpdateRequest) (
*NodeAnnouncementUpdateResponse, error) {
@@ -393,7 +393,7 @@ func (s *Server) UpdateNodeAnnouncement(_ context.Context,
}
if err := s.cfg.UpdateNodeAnnouncement(
nodeAnnFeatures, nodeModifiers...,
ctx, nodeAnnFeatures, nodeModifiers...,
); err != nil {
return nil, err
}
+2 -2
View File
@@ -300,7 +300,7 @@ func parseTestGraph(t *testing.T, useCache bool, path string) (
if source != nil {
// Set the selected source node
if err := graph.SetSourceNode(source); err != nil {
if err := graph.SetSourceNode(ctx, source); err != nil {
return nil, err
}
}
@@ -590,7 +590,7 @@ func createTestGraphFromChannels(t *testing.T, useCache bool,
)
require.NoError(t, err)
if err = graph.SetSourceNode(dbNode); err != nil {
if err = graph.SetSourceNode(ctx, dbNode); err != nil {
return nil, err
}
+12 -7
View File
@@ -970,7 +970,7 @@ func newServer(ctx context.Context, cfg *Config, listenAddrs []net.Addr,
// Finally, we'll update the representation on disk, and update our
// cached in-memory version as well.
if err := dbs.GraphDB.SetSourceNode(selfNode); err != nil {
if err := dbs.GraphDB.SetSourceNode(ctx, selfNode); err != nil {
return nil, fmt.Errorf("can't set self node: %w", err)
}
s.currentNodeAnn = nodeAnn
@@ -2073,7 +2073,11 @@ func (s *server) createLivenessMonitor(cfg *Config, cc *chainreg.ChainControl,
func() error {
return healthcheck.CheckTorServiceStatus(
s.torController,
s.createNewHiddenService,
func() error {
return s.createNewHiddenService(
context.TODO(),
)
},
)
},
cfg.HealthChecks.TorConnection.Interval,
@@ -2467,7 +2471,7 @@ func (s *server) Start(ctx context.Context) error {
if s.torController != nil {
cleanup = cleanup.add(s.torController.Stop)
if err := s.createNewHiddenService(); err != nil {
if err := s.createNewHiddenService(ctx); err != nil {
startErr = err
return
}
@@ -3325,7 +3329,7 @@ func (s *server) initialPeerBootstrap(ctx context.Context,
// createNewHiddenService automatically sets up a v2 or v3 onion service in
// order to listen for inbound connections over Tor.
func (s *server) createNewHiddenService() error {
func (s *server) createNewHiddenService(ctx context.Context) error {
// Determine the different ports the server is listening on. The onion
// service's virtual port will map to these ports and one will be picked
// at random when the onion service is being accessed.
@@ -3390,7 +3394,7 @@ func (s *server) createNewHiddenService() error {
AuthSigBytes: newNodeAnn.Signature.ToSignatureBytes(),
}
copy(selfNode.PubKeyBytes[:], s.identityECDH.PubKey().SerializeCompressed())
if err := s.graphDB.SetSourceNode(selfNode); err != nil {
if err := s.graphDB.SetSourceNode(ctx, selfNode); err != nil {
return fmt.Errorf("can't set self node: %w", err)
}
@@ -3485,7 +3489,8 @@ func (s *server) genNodeAnnouncement(features *lnwire.RawFeatureVector,
// applying the giving modifiers and updating the time stamp
// to ensure it propagates through the network. Then it broadcasts
// it to the network.
func (s *server) updateAndBroadcastSelfNode(features *lnwire.RawFeatureVector,
func (s *server) updateAndBroadcastSelfNode(ctx context.Context,
features *lnwire.RawFeatureVector,
modifiers ...netann.NodeAnnModifier) error {
newNodeAnn, err := s.genNodeAnnouncement(features, modifiers...)
@@ -3512,7 +3517,7 @@ func (s *server) updateAndBroadcastSelfNode(features *lnwire.RawFeatureVector,
copy(selfNode.PubKeyBytes[:], s.identityECDH.PubKey().SerializeCompressed())
if err := s.graphDB.SetSourceNode(selfNode); err != nil {
if err := s.graphDB.SetSourceNode(ctx, selfNode); err != nil {
return fmt.Errorf("can't set self node: %w", err)
}
+3 -1
View File
@@ -1,6 +1,7 @@
package lnd
import (
"context"
"fmt"
"net"
"reflect"
@@ -122,7 +123,8 @@ func (s *subRPCServerConfigs) PopulateDependencies(cfg *Config,
genInvoiceFeatures func() *lnwire.FeatureVector,
genAmpInvoiceFeatures func() *lnwire.FeatureVector,
getNodeAnnouncement func() lnwire.NodeAnnouncement,
updateNodeAnnouncement func(features *lnwire.RawFeatureVector,
updateNodeAnnouncement func(ctx context.Context,
features *lnwire.RawFeatureVector,
modifiers ...netann.NodeAnnModifier) error,
parseAddr func(addr string) (net.Addr, error),
rpcLogger btclog.Logger, aliasMgr *aliasmgr.Manager,