graph/db: move cache update out of pruneGraphNodes

In preparation for moving the cache write completely out of KVStore, we
move the cache write up one layer.
This commit is contained in:
Elle Mouton committed 2025-03-24 15:05:46 +02:00
1 parent 71e5ab6200
commit 941e7bf6b3
1 file changed
+34 -16
+34 -16
View File
@@ -1427,7 +1427,18 @@ func (c *KVStore) PruneGraph(spentOutputs []*wire.OutPoint,
// Now that the graph has been pruned, we'll also attempt to // Now that the graph has been pruned, we'll also attempt to
// prune any nodes that have had a channel closed within the // prune any nodes that have had a channel closed within the
// latest block. // latest block.
return c.pruneGraphNodes(nodes, edgeIndex) prunedNodes, err := c.pruneGraphNodes(nodes, edgeIndex)
if err != nil {
return err
}
if c.graphCache != nil {
for _, nodePubKey := range prunedNodes {
c.graphCache.RemoveNode(nodePubKey)
}
}
return nil
}, func() { }, func() {
chansClosed = nil chansClosed = nil
}) })
@@ -1467,7 +1478,18 @@ func (c *KVStore) PruneGraphNodes() error {
return ErrGraphNoEdgesFound return ErrGraphNoEdgesFound
} }
return c.pruneGraphNodes(nodes, edgeIndex) prunedNodes, err := c.pruneGraphNodes(nodes, edgeIndex)
if err != nil {
return err
}
if c.graphCache != nil {
for _, nodePubKey := range prunedNodes {
c.graphCache.RemoveNode(nodePubKey)
}
}
return nil
}, func() {}) }, func() {})
} }
@@ -1475,7 +1497,7 @@ func (c *KVStore) PruneGraphNodes() error {
// channel closed within the current block. If the node still has existing // channel closed within the current block. If the node still has existing
// channels in the graph, this will act as a no-op. // channels in the graph, this will act as a no-op.
func (c *KVStore) pruneGraphNodes(nodes kvdb.RwBucket, func (c *KVStore) pruneGraphNodes(nodes kvdb.RwBucket,
edgeIndex kvdb.RwBucket) error { edgeIndex kvdb.RwBucket) ([]route.Vertex, error) {
log.Trace("Pruning nodes from graph with no open channels") log.Trace("Pruning nodes from graph with no open channels")
@@ -1483,7 +1505,7 @@ func (c *KVStore) pruneGraphNodes(nodes kvdb.RwBucket,
// even if it no longer has any open channels. // even if it no longer has any open channels.
sourceNode, err := c.sourceNode(nodes) sourceNode, err := c.sourceNode(nodes)
if err != nil { if err != nil {
return err return nil, err
} }
// We'll use this map to keep count the number of references to a node // We'll use this map to keep count the number of references to a node
@@ -1505,7 +1527,7 @@ func (c *KVStore) pruneGraphNodes(nodes kvdb.RwBucket,
return nil return nil
}) })
if err != nil { if err != nil {
return err return nil, err
} }
// To ensure we never delete the source node, we'll start off by // To ensure we never delete the source node, we'll start off by
@@ -1531,12 +1553,12 @@ func (c *KVStore) pruneGraphNodes(nodes kvdb.RwBucket,
return nil return nil
}) })
if err != nil { if err != nil {
return err return nil, err
} }
// Finally, we'll make a second pass over the set of nodes, and delete // Finally, we'll make a second pass over the set of nodes, and delete
// any nodes that have a ref count of zero. // any nodes that have a ref count of zero.
var numNodesPruned int var pruned []route.Vertex
for nodePubKey, refCount := range nodeRefCounts { for nodePubKey, refCount := range nodeRefCounts {
// If the ref count of the node isn't zero, then we can safely // If the ref count of the node isn't zero, then we can safely
// skip it as it still has edges to or from it within the // skip it as it still has edges to or from it within the
@@ -1545,10 +1567,6 @@ func (c *KVStore) pruneGraphNodes(nodes kvdb.RwBucket,
continue continue
} }
if c.graphCache != nil {
c.graphCache.RemoveNode(nodePubKey)
}
// If we reach this point, then there are no longer any edges // If we reach this point, then there are no longer any edges
// that connect this node, so we can delete it. // that connect this node, so we can delete it.
err := c.deleteLightningNode(nodes, nodePubKey[:]) err := c.deleteLightningNode(nodes, nodePubKey[:])
@@ -1561,21 +1579,21 @@ func (c *KVStore) pruneGraphNodes(nodes kvdb.RwBucket,
continue continue
} }
return err return nil, err
} }
log.Infof("Pruned unconnected node %x from channel graph", log.Infof("Pruned unconnected node %x from channel graph",
nodePubKey[:]) nodePubKey[:])
numNodesPruned++ pruned = append(pruned, nodePubKey)
} }
if numNodesPruned > 0 { if len(pruned) > 0 {
log.Infof("Pruned %v unconnected nodes from the channel graph", log.Infof("Pruned %v unconnected nodes from the channel graph",
numNodesPruned) len(pruned))
} }
return nil return pruned, err
} }
// DisconnectBlockAtHeight is used to indicate that the block specified // DisconnectBlockAtHeight is used to indicate that the block specified