channeldb/graph: reuse nodes bucket in deleteLightningNode

Lets us avoid passing the tx from pruneGraphNodes
This commit is contained in:
Johan T. Halseth 2018-12-04 11:25:05 +01:00
parent 1b9ca06563
commit 9d467d3534
No known key found for this signature in database
GPG Key ID: 15BAADA29DA20D26

@ -420,20 +420,22 @@ func (c *ChannelGraph) LookupAlias(pub *btcec.PublicKey) (string, error) {
func (c *ChannelGraph) DeleteLightningNode(nodePub *btcec.PublicKey) error { func (c *ChannelGraph) DeleteLightningNode(nodePub *btcec.PublicKey) error {
// TODO(roasbeef): ensure dangling edges are removed... // TODO(roasbeef): ensure dangling edges are removed...
return c.db.Update(func(tx *bbolt.Tx) error { return c.db.Update(func(tx *bbolt.Tx) error {
return c.deleteLightningNode(tx, nodePub.SerializeCompressed()) nodes := tx.Bucket(nodeBucket)
if nodes == nil {
return ErrGraphNodeNotFound
}
return c.deleteLightningNode(
nodes, nodePub.SerializeCompressed(),
)
}) })
} }
// deleteLightningNode uses an existing database transaction to remove a // deleteLightningNode uses an existing database transaction to remove a
// vertex/node from the database according to the node's public key. // vertex/node from the database according to the node's public key.
func (c *ChannelGraph) deleteLightningNode(tx *bbolt.Tx, func (c *ChannelGraph) deleteLightningNode(nodes *bbolt.Bucket,
compressedPubKey []byte) error { compressedPubKey []byte) error {
nodes := tx.Bucket(nodeBucket)
if nodes == nil {
return ErrGraphNodesNotFound
}
aliases := nodes.Bucket(aliasIndexBucket) aliases := nodes.Bucket(aliasIndexBucket)
if aliases == nil { if aliases == nil {
return ErrGraphNodesNotFound return ErrGraphNodesNotFound
@ -781,7 +783,7 @@ func (c *ChannelGraph) 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(tx, nodes, edgeIndex) return c.pruneGraphNodes(nodes, edgeIndex)
}) })
if err != nil { if err != nil {
return nil, err return nil, err
@ -809,14 +811,14 @@ func (c *ChannelGraph) PruneGraphNodes() error {
return ErrGraphNoEdgesFound return ErrGraphNoEdgesFound
} }
return c.pruneGraphNodes(tx, nodes, edgeIndex) return c.pruneGraphNodes(nodes, edgeIndex)
}) })
} }
// pruneGraphNodes attempts to remove any nodes from the graph who have had a // pruneGraphNodes attempts to remove any nodes from the graph who have had a
// 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 *ChannelGraph) pruneGraphNodes(tx *bbolt.Tx, nodes *bbolt.Bucket, func (c *ChannelGraph) pruneGraphNodes(nodes *bbolt.Bucket,
edgeIndex *bbolt.Bucket) error { edgeIndex *bbolt.Bucket) error {
log.Trace("Pruning nodes from graph with no open channels") log.Trace("Pruning nodes from graph with no open channels")
@ -889,7 +891,7 @@ func (c *ChannelGraph) pruneGraphNodes(tx *bbolt.Tx, nodes *bbolt.Bucket,
// 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.
if err := c.deleteLightningNode(tx, nodePubKey[:]); err != nil { if err := c.deleteLightningNode(nodes, nodePubKey[:]); err != nil {
log.Warnf("Unable to prune node %x from the "+ log.Warnf("Unable to prune node %x from the "+
"graph: %v", nodePubKey, err) "graph: %v", nodePubKey, err)
continue continue