multi: let FetchLightningNode take an optional tx

In preparation for the next commit which will remove the
`*LightningNode` from the `ChannelEdgePolicy` struct,
`FetchLightningNode` is modified to take in an optional transaction so
that it can be utilised in places where a transaction exists.
This commit is contained in:
Elle Mouton 2023-10-23 14:21:24 +02:00
parent 9a62a46aa3
commit 6c76d31e89
No known key found for this signature in database
GPG Key ID: D7D916376026F177
9 changed files with 38 additions and 24 deletions

View File

@ -158,7 +158,7 @@ func (d *databaseChannelGraph) addRandChannel(node1, node2 *btcec.PublicKey,
return nil, err return nil, err
} }
dbNode, err := d.db.FetchLightningNode(vertex) dbNode, err := d.db.FetchLightningNode(nil, vertex)
switch { switch {
case err == channeldb.ErrGraphNodeNotFound: case err == channeldb.ErrGraphNodeNotFound:
fallthrough fallthrough

View File

@ -1351,7 +1351,7 @@ func (d *DB) AddrsForNode(nodePub *btcec.PublicKey) ([]net.Addr,
if err != nil { if err != nil {
return nil, err return nil, err
} }
graphNode, err := d.graph.FetchLightningNode(pubKey) graphNode, err := d.graph.FetchLightningNode(nil, pubKey)
if err != nil && err != ErrGraphNodeNotFound { if err != nil && err != ErrGraphNodeNotFound {
return nil, err return nil, err
} else if err == ErrGraphNodeNotFound { } else if err == ErrGraphNodeNotFound {

View File

@ -519,7 +519,7 @@ func (c *ChannelGraph) FetchNodeFeatures(
} }
// Fallback that uses the database. // Fallback that uses the database.
targetNode, err := c.FetchLightningNode(node) targetNode, err := c.FetchLightningNode(nil, node)
switch err { switch err {
// If the node exists and has features, return them directly. // If the node exists and has features, return them directly.
case nil: case nil:
@ -2779,12 +2779,13 @@ func (c *ChannelGraph) isPublic(tx kvdb.RTx, nodePub route.Vertex,
// FetchLightningNode attempts to look up a target node by its identity public // FetchLightningNode attempts to look up a target node by its identity public
// key. If the node isn't found in the database, then ErrGraphNodeNotFound is // key. If the node isn't found in the database, then ErrGraphNodeNotFound is
// returned. // returned. An optional transaction may be provided. If none is provided, then
func (c *ChannelGraph) FetchLightningNode(nodePub route.Vertex) ( // a new one will be created.
func (c *ChannelGraph) FetchLightningNode(tx kvdb.RTx, nodePub route.Vertex) (
*LightningNode, error) { *LightningNode, error) {
var node *LightningNode var node *LightningNode
err := kvdb.View(c.db, func(tx kvdb.RTx) error { fetch := 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)
@ -2810,9 +2811,22 @@ func (c *ChannelGraph) FetchLightningNode(nodePub route.Vertex) (
node = &n node = &n
return nil return nil
}, func() { }
if tx == nil {
err := kvdb.View(
c.db, fetch, func() {
node = nil node = nil
}) },
)
if err != nil {
return nil, err
}
return node, nil
}
err := fetch(tx)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -139,7 +139,7 @@ func TestNodeInsertionAndDeletion(t *testing.T) {
// Next, fetch the node from the database to ensure everything was // Next, fetch the node from the database to ensure everything was
// serialized properly. // serialized properly.
dbNode, err := graph.FetchLightningNode(testPub) dbNode, err := graph.FetchLightningNode(nil, testPub)
require.NoError(t, err, "unable to locate node") require.NoError(t, err, "unable to locate node")
if _, exists, err := graph.HasLightningNode(dbNode.PubKeyBytes); err != nil { if _, exists, err := graph.HasLightningNode(dbNode.PubKeyBytes); err != nil {
@ -162,7 +162,7 @@ func TestNodeInsertionAndDeletion(t *testing.T) {
// Finally, attempt to fetch the node again. This should fail as the // Finally, attempt to fetch the node again. This should fail as the
// node should have been deleted from the database. // node should have been deleted from the database.
_, err = graph.FetchLightningNode(testPub) _, err = graph.FetchLightningNode(nil, testPub)
if err != ErrGraphNodeNotFound { if err != ErrGraphNodeNotFound {
t.Fatalf("fetch after delete should fail!") t.Fatalf("fetch after delete should fail!")
} }
@ -190,7 +190,7 @@ func TestPartialNode(t *testing.T) {
// Next, fetch the node from the database to ensure everything was // Next, fetch the node from the database to ensure everything was
// serialized properly. // serialized properly.
dbNode, err := graph.FetchLightningNode(testPub) dbNode, err := graph.FetchLightningNode(nil, testPub)
require.NoError(t, err, "unable to locate node") require.NoError(t, err, "unable to locate node")
if _, exists, err := graph.HasLightningNode(dbNode.PubKeyBytes); err != nil { if _, exists, err := graph.HasLightningNode(dbNode.PubKeyBytes); err != nil {
@ -220,7 +220,7 @@ func TestPartialNode(t *testing.T) {
// Finally, attempt to fetch the node again. This should fail as the // Finally, attempt to fetch the node again. This should fail as the
// node should have been deleted from the database. // node should have been deleted from the database.
_, err = graph.FetchLightningNode(testPub) _, err = graph.FetchLightningNode(nil, testPub)
if err != ErrGraphNodeNotFound { if err != ErrGraphNodeNotFound {
t.Fatalf("fetch after delete should fail!") t.Fatalf("fetch after delete should fail!")
} }
@ -2560,7 +2560,7 @@ func TestPruneGraphNodes(t *testing.T) {
// Finally, we'll ensure that node3, the only fully unconnected node as // Finally, we'll ensure that node3, the only fully unconnected node as
// properly deleted from the graph and not another node in its place. // properly deleted from the graph and not another node in its place.
_, err = graph.FetchLightningNode(node3.PubKeyBytes) _, err = graph.FetchLightningNode(nil, node3.PubKeyBytes)
if err == nil { if err == nil {
t.Fatalf("node 3 should have been deleted!") t.Fatalf("node 3 should have been deleted!")
} }
@ -2594,13 +2594,13 @@ func TestAddChannelEdgeShellNodes(t *testing.T) {
// Ensure that node1 was inserted as a full node, while node2 only has // Ensure that node1 was inserted as a full node, while node2 only has
// a shell node present. // a shell node present.
node1, err = graph.FetchLightningNode(node1.PubKeyBytes) node1, err = graph.FetchLightningNode(nil, node1.PubKeyBytes)
require.NoError(t, err, "unable to fetch node1") require.NoError(t, err, "unable to fetch node1")
if !node1.HaveNodeAnnouncement { if !node1.HaveNodeAnnouncement {
t.Fatalf("have shell announcement for node1, shouldn't") t.Fatalf("have shell announcement for node1, shouldn't")
} }
node2, err = graph.FetchLightningNode(node2.PubKeyBytes) node2, err = graph.FetchLightningNode(nil, node2.PubKeyBytes)
require.NoError(t, err, "unable to fetch node2") require.NoError(t, err, "unable to fetch node2")
if node2.HaveNodeAnnouncement { if node2.HaveNodeAnnouncement {
t.Fatalf("should have shell announcement for node2, but is full") t.Fatalf("should have shell announcement for node2, but is full")

View File

@ -2283,7 +2283,7 @@ func TestPathFindSpecExample(t *testing.T) {
// Carol, so we set "B" as the source node so path finding starts from // Carol, so we set "B" as the source node so path finding starts from
// Bob. // Bob.
bob := ctx.aliases["B"] bob := ctx.aliases["B"]
bobNode, err := ctx.graph.FetchLightningNode(bob) bobNode, err := ctx.graph.FetchLightningNode(nil, bob)
require.NoError(t, err, "unable to find bob") require.NoError(t, err, "unable to find bob")
if err := ctx.graph.SetSourceNode(bobNode); err != nil { if err := ctx.graph.SetSourceNode(bobNode); err != nil {
t.Fatalf("unable to set source node: %v", err) t.Fatalf("unable to set source node: %v", err)
@ -2332,7 +2332,7 @@ func TestPathFindSpecExample(t *testing.T) {
// Next, we'll set A as the source node so we can assert that we create // Next, we'll set A as the source node so we can assert that we create
// the proper route for any queries starting with Alice. // the proper route for any queries starting with Alice.
alice := ctx.aliases["A"] alice := ctx.aliases["A"]
aliceNode, err := ctx.graph.FetchLightningNode(alice) aliceNode, err := ctx.graph.FetchLightningNode(nil, alice)
require.NoError(t, err, "unable to find alice") require.NoError(t, err, "unable to find alice")
if err := ctx.graph.SetSourceNode(aliceNode); err != nil { if err := ctx.graph.SetSourceNode(aliceNode); err != nil {
t.Fatalf("unable to set source node: %v", err) t.Fatalf("unable to set source node: %v", err)

View File

@ -2802,7 +2802,7 @@ func (r *ChannelRouter) GetChannelByID(chanID lnwire.ShortChannelID) (
func (r *ChannelRouter) FetchLightningNode( func (r *ChannelRouter) FetchLightningNode(
node route.Vertex) (*channeldb.LightningNode, error) { node route.Vertex) (*channeldb.LightningNode, error) {
return r.cfg.Graph.FetchLightningNode(node) return r.cfg.Graph.FetchLightningNode(nil, node)
} }
// ForEachNode is used to iterate over every node in router topology. // ForEachNode is used to iterate over every node in router topology.

View File

@ -1613,14 +1613,14 @@ func TestAddEdgeUnknownVertexes(t *testing.T) {
_, _, err = ctx.router.FindRoute(req) _, _, err = ctx.router.FindRoute(req)
require.NoError(t, err, "unable to find any routes") require.NoError(t, err, "unable to find any routes")
copy1, err := ctx.graph.FetchLightningNode(pub1) copy1, err := ctx.graph.FetchLightningNode(nil, pub1)
require.NoError(t, err, "unable to fetch node") require.NoError(t, err, "unable to fetch node")
if copy1.Alias != n1.Alias { if copy1.Alias != n1.Alias {
t.Fatalf("fetched node not equal to original") t.Fatalf("fetched node not equal to original")
} }
copy2, err := ctx.graph.FetchLightningNode(pub2) copy2, err := ctx.graph.FetchLightningNode(nil, pub2)
require.NoError(t, err, "unable to fetch node") require.NoError(t, err, "unable to fetch node")
if copy2.Alias != n2.Alias { if copy2.Alias != n2.Alias {

View File

@ -6135,7 +6135,7 @@ func (r *rpcServer) GetNodeInfo(ctx context.Context,
// With the public key decoded, attempt to fetch the node corresponding // With the public key decoded, attempt to fetch the node corresponding
// to this public key. If the node cannot be found, then an error will // to this public key. If the node cannot be found, then an error will
// be returned. // be returned.
node, err := graph.FetchLightningNode(pubKey) node, err := graph.FetchLightningNode(nil, pubKey)
switch { switch {
case err == channeldb.ErrGraphNodeNotFound: case err == channeldb.ErrGraphNodeNotFound:
return nil, status.Error(codes.NotFound, err.Error()) return nil, status.Error(codes.NotFound, err.Error())
@ -7107,7 +7107,7 @@ func (r *rpcServer) ForwardingHistory(ctx context.Context,
return "", err return "", err
} }
peer, err := r.server.graphDB.FetchLightningNode(vertex) peer, err := r.server.graphDB.FetchLightningNode(nil, vertex)
if err != nil { if err != nil {
return "", err return "", err
} }

View File

@ -4596,7 +4596,7 @@ func (s *server) fetchNodeAdvertisedAddrs(pub *btcec.PublicKey) ([]net.Addr, err
return nil, err return nil, err
} }
node, err := s.graphDB.FetchLightningNode(vertex) node, err := s.graphDB.FetchLightningNode(nil, vertex)
if err != nil { if err != nil {
return nil, err return nil, err
} }