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 committed 2023-11-08 14:50:34 +02:00
1 parent 9a62a46aa3
commit 6c76d31e89
9 files changed
+38 -24

No files matched your search

+1 -1
View File
@@ -1351,7 +1351,7 @@ func (d *DB) AddrsForNode(nodePub *btcec.PublicKey) ([]net.Addr,
if err != nil {
return nil, err
}
graphNode, err := d.graph.FetchLightningNode(pubKey)
graphNode, err := d.graph.FetchLightningNode(nil, pubKey)
if err != nil && err != ErrGraphNodeNotFound {
return nil, err
} else if err == ErrGraphNodeNotFound {
+21 -7
View File
@@ -519,7 +519,7 @@ func (c *ChannelGraph) FetchNodeFeatures(
}
// Fallback that uses the database.
targetNode, err := c.FetchLightningNode(node)
targetNode, err := c.FetchLightningNode(nil, node)
switch err {
// If the node exists and has features, return them directly.
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
// key. If the node isn't found in the database, then ErrGraphNodeNotFound is
// returned.
func (c *ChannelGraph) FetchLightningNode(nodePub route.Vertex) (
// returned. An optional transaction may be provided. If none is provided, then
// a new one will be created.
func (c *ChannelGraph) FetchLightningNode(tx kvdb.RTx, nodePub route.Vertex) (
*LightningNode, error) {
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
// pubKey to node information.
nodes := tx.ReadBucket(nodeBucket)
@@ -2810,9 +2811,22 @@ func (c *ChannelGraph) FetchLightningNode(nodePub route.Vertex) (
node = &n
return nil
}, func() {
node = nil
})
}
if tx == nil {
err := kvdb.View(
c.db, fetch, func() {
node = nil
},
)
if err != nil {
return nil, err
}
return node, nil
}
err := fetch(tx)
if err != nil {
return nil, err
}
+7 -7
View File
@@ -139,7 +139,7 @@ func TestNodeInsertionAndDeletion(t *testing.T) {
// Next, fetch the node from the database to ensure everything was
// serialized properly.
dbNode, err := graph.FetchLightningNode(testPub)
dbNode, err := graph.FetchLightningNode(nil, testPub)
require.NoError(t, err, "unable to locate node")
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
// node should have been deleted from the database.
_, err = graph.FetchLightningNode(testPub)
_, err = graph.FetchLightningNode(nil, testPub)
if err != ErrGraphNodeNotFound {
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
// serialized properly.
dbNode, err := graph.FetchLightningNode(testPub)
dbNode, err := graph.FetchLightningNode(nil, testPub)
require.NoError(t, err, "unable to locate node")
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
// node should have been deleted from the database.
_, err = graph.FetchLightningNode(testPub)
_, err = graph.FetchLightningNode(nil, testPub)
if err != ErrGraphNodeNotFound {
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
// 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 {
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
// 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")
if !node1.HaveNodeAnnouncement {
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")
if node2.HaveNodeAnnouncement {
t.Fatalf("should have shell announcement for node2, but is full")