Merge pull request #4280 from cfromknecht/read-for-each-node

channeldb: remove Tx arg from ForEachNode
This commit is contained in:
Conner Fromknecht 2020-05-13 16:37:10 -07:00 committed by GitHub
commit 2f84d1a819
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 8 additions and 21 deletions

@ -121,7 +121,7 @@ func (d dbNode) ForEachChannel(cb func(ChannelEdge) error) error {
// //
// NOTE: Part of the autopilot.ChannelGraph interface. // NOTE: Part of the autopilot.ChannelGraph interface.
func (d *databaseChannelGraph) ForEachNode(cb func(Node) error) error { func (d *databaseChannelGraph) ForEachNode(cb func(Node) error) error {
return d.db.ForEachNode(nil, func(tx kvdb.ReadTx, n *channeldb.LightningNode) error { return d.db.ForEachNode(func(tx kvdb.ReadTx, n *channeldb.LightningNode) error {
// We'll skip over any node that doesn't have any advertised // We'll skip over any node that doesn't have any advertised
// addresses. As we won't be able to reach them to actually // addresses. As we won't be able to reach them to actually

@ -321,14 +321,9 @@ func (c *ChannelGraph) DisabledChannelIDs() ([]uint64, error) {
// returns an error, then the transaction is aborted and the iteration stops // returns an error, then the transaction is aborted and the iteration stops
// early. // early.
// //
// If the caller wishes to re-use an existing boltdb transaction, then it
// should be passed as the first argument. Otherwise the first argument should
// be nil and a fresh transaction will be created to execute the graph
// traversal
//
// TODO(roasbeef): add iterator interface to allow for memory efficient graph // TODO(roasbeef): add iterator interface to allow for memory efficient graph
// traversal when graph gets mega // traversal when graph gets mega
func (c *ChannelGraph) ForEachNode(tx kvdb.RwTx, cb func(kvdb.ReadTx, *LightningNode) error) error { // nolint:interfacer func (c *ChannelGraph) ForEachNode(cb func(kvdb.ReadTx, *LightningNode) error) error { // nolint:interfacer
traversal := func(tx kvdb.ReadTx) error { traversal := func(tx kvdb.ReadTx) 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.
@ -358,15 +353,7 @@ func (c *ChannelGraph) ForEachNode(tx kvdb.RwTx, cb func(kvdb.ReadTx, *Lightning
}) })
} }
// If no transaction was provided, then we'll create a new transaction return kvdb.View(c.db, traversal)
// to execute the transaction within.
if tx == nil {
return kvdb.View(c.db, traversal)
}
// Otherwise, we re-use the existing transaction to execute the graph
// traversal.
return traversal(tx)
} }
// SourceNode returns the source node of the graph. The source node is treated // SourceNode returns the source node of the graph. The source node is treated

@ -882,7 +882,7 @@ func TestGraphTraversal(t *testing.T) {
// Iterate over each node as returned by the graph, if all nodes are // Iterate over each node as returned by the graph, if all nodes are
// reached, then the map created above should be empty. // reached, then the map created above should be empty.
err = graph.ForEachNode(nil, func(_ kvdb.ReadTx, node *LightningNode) error { err = graph.ForEachNode(func(_ kvdb.ReadTx, node *LightningNode) error {
delete(nodeIndex, node.Alias) delete(nodeIndex, node.Alias)
return nil return nil
}) })
@ -1051,7 +1051,7 @@ func assertNumChans(t *testing.T, graph *ChannelGraph, n int) {
func assertNumNodes(t *testing.T, graph *ChannelGraph, n int) { func assertNumNodes(t *testing.T, graph *ChannelGraph, n int) {
numNodes := 0 numNodes := 0
err := graph.ForEachNode(nil, func(_ kvdb.ReadTx, _ *LightningNode) error { err := graph.ForEachNode(func(_ kvdb.ReadTx, _ *LightningNode) error {
numNodes++ numNodes++
return nil return nil
}) })

@ -2186,7 +2186,7 @@ func (r *ChannelRouter) FetchLightningNode(node route.Vertex) (*channeldb.Lightn
// //
// NOTE: This method is part of the ChannelGraphSource interface. // NOTE: This method is part of the ChannelGraphSource interface.
func (r *ChannelRouter) ForEachNode(cb func(*channeldb.LightningNode) error) error { func (r *ChannelRouter) ForEachNode(cb func(*channeldb.LightningNode) error) error {
return r.cfg.Graph.ForEachNode(nil, func(_ kvdb.ReadTx, n *channeldb.LightningNode) error { return r.cfg.Graph.ForEachNode(func(_ kvdb.ReadTx, n *channeldb.LightningNode) error {
return cb(n) return cb(n)
}) })
} }

@ -4599,7 +4599,7 @@ func (r *rpcServer) DescribeGraph(ctx context.Context,
// First iterate through all the known nodes (connected or unconnected // First iterate through all the known nodes (connected or unconnected
// within the graph), collating their current state into the RPC // within the graph), collating their current state into the RPC
// response. // response.
err := graph.ForEachNode(nil, func(_ kvdb.ReadTx, node *channeldb.LightningNode) error { err := graph.ForEachNode(func(_ kvdb.ReadTx, node *channeldb.LightningNode) error {
nodeAddrs := make([]*lnrpc.NodeAddress, 0) nodeAddrs := make([]*lnrpc.NodeAddress, 0)
for _, addr := range node.Addresses { for _, addr := range node.Addresses {
nodeAddr := &lnrpc.NodeAddress{ nodeAddr := &lnrpc.NodeAddress{
@ -4914,7 +4914,7 @@ func (r *rpcServer) GetNetworkInfo(ctx context.Context,
// network, tallying up the total number of nodes, and also gathering // network, tallying up the total number of nodes, and also gathering
// each node so we can measure the graph diameter and degree stats // each node so we can measure the graph diameter and degree stats
// below. // below.
if err := graph.ForEachNode(nil, func(tx kvdb.ReadTx, node *channeldb.LightningNode) error { if err := graph.ForEachNode(func(tx kvdb.ReadTx, node *channeldb.LightningNode) error {
// Increment the total number of nodes with each iteration. // Increment the total number of nodes with each iteration.
numNodes++ numNodes++