Skip to content

Commit 3555d3d

Browse files
committed
channeldb: convert all Update calls to use Batch
In this commit, we convert all the `Update` calls which are serial, to use `Batch` calls which are optimistically batched together for concurrent writers. This should increase performance slightly during the initial graph sync, and also updates at tip as we can coalesce more of these individual transactions into a single transaction.
1 parent abda447 commit 3555d3d

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

channeldb/graph.go

+14-10
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ func (c *ChannelGraph) sourceNode(nodes *bbolt.Bucket) (*LightningNode, error) {
326326
func (c *ChannelGraph) SetSourceNode(node *LightningNode) error {
327327
nodePubBytes := node.PubKeyBytes[:]
328328

329-
return c.db.Update(func(tx *bbolt.Tx) error {
329+
return c.db.Batch(func(tx *bbolt.Tx) error {
330330
// First grab the nodes bucket which stores the mapping from
331331
// pubKey to node information.
332332
nodes, err := tx.CreateBucketIfNotExists(nodeBucket)
@@ -355,7 +355,7 @@ func (c *ChannelGraph) SetSourceNode(node *LightningNode) error {
355355
//
356356
// TODO(roasbeef): also need sig of announcement
357357
func (c *ChannelGraph) AddLightningNode(node *LightningNode) error {
358-
return c.db.Update(func(tx *bbolt.Tx) error {
358+
return c.db.Batch(func(tx *bbolt.Tx) error {
359359
return addLightningNode(tx, node)
360360
})
361361
}
@@ -419,7 +419,7 @@ func (c *ChannelGraph) LookupAlias(pub *btcec.PublicKey) (string, error) {
419419
// from the database according to the node's public key.
420420
func (c *ChannelGraph) DeleteLightningNode(nodePub *btcec.PublicKey) error {
421421
// TODO(roasbeef): ensure dangling edges are removed...
422-
return c.db.Update(func(tx *bbolt.Tx) error {
422+
return c.db.Batch(func(tx *bbolt.Tx) error {
423423
nodes := tx.Bucket(nodeBucket)
424424
if nodes == nil {
425425
return ErrGraphNodeNotFound
@@ -483,7 +483,7 @@ func (c *ChannelGraph) deleteLightningNode(nodes *bbolt.Bucket,
483483
// the channel supports. The chanPoint and chanID are used to uniquely identify
484484
// the edge globally within the database.
485485
func (c *ChannelGraph) AddChannelEdge(edge *ChannelEdgeInfo) error {
486-
return c.db.Update(func(tx *bbolt.Tx) error {
486+
return c.db.Batch(func(tx *bbolt.Tx) error {
487487
return c.addChannelEdge(tx, edge)
488488
})
489489
}
@@ -657,7 +657,7 @@ func (c *ChannelGraph) UpdateChannelEdge(edge *ChannelEdgeInfo) error {
657657
var chanKey [8]byte
658658
binary.BigEndian.PutUint64(chanKey[:], edge.ChannelID)
659659

660-
return c.db.Update(func(tx *bbolt.Tx) error {
660+
return c.db.Batch(func(tx *bbolt.Tx) error {
661661
edges := tx.Bucket(edgeBucket)
662662
if edge == nil {
663663
return ErrEdgeNotFound
@@ -697,7 +697,9 @@ func (c *ChannelGraph) PruneGraph(spentOutputs []*wire.OutPoint,
697697

698698
var chansClosed []*ChannelEdgeInfo
699699

700-
err := c.db.Update(func(tx *bbolt.Tx) error {
700+
err := c.db.Batch(func(tx *bbolt.Tx) error {
701+
chansClosed = nil
702+
701703
// First grab the edges bucket which houses the information
702704
// we'd like to delete
703705
edges, err := tx.CreateBucketIfNotExists(edgeBucket)
@@ -801,7 +803,7 @@ func (c *ChannelGraph) PruneGraph(spentOutputs []*wire.OutPoint,
801803
// that we only maintain a graph of reachable nodes. In the event that a pruned
802804
// node gains more channels, it will be re-added back to the graph.
803805
func (c *ChannelGraph) PruneGraphNodes() error {
804-
return c.db.Update(func(tx *bbolt.Tx) error {
806+
return c.db.Batch(func(tx *bbolt.Tx) error {
805807
nodes := tx.Bucket(nodeBucket)
806808
if nodes == nil {
807809
return ErrGraphNodesNotFound
@@ -946,7 +948,9 @@ func (c *ChannelGraph) DisconnectBlockAtHeight(height uint32) ([]*ChannelEdgeInf
946948
// Keep track of the channels that are removed from the graph.
947949
var removedChans []*ChannelEdgeInfo
948950

949-
if err := c.db.Update(func(tx *bbolt.Tx) error {
951+
if err := c.db.Batch(func(tx *bbolt.Tx) error {
952+
removedChans = nil
953+
950954
edges, err := tx.CreateBucketIfNotExists(edgeBucket)
951955
if err != nil {
952956
return err
@@ -1070,7 +1074,7 @@ func (c *ChannelGraph) DeleteChannelEdge(chanPoint *wire.OutPoint) error {
10701074
// channels
10711075
// TODO(roasbeef): don't delete both edges?
10721076

1073-
return c.db.Update(func(tx *bbolt.Tx) error {
1077+
return c.db.Batch(func(tx *bbolt.Tx) error {
10741078
// First grab the edges bucket which houses the information
10751079
// we'd like to delete
10761080
edges := tx.Bucket(edgeBucket)
@@ -1642,7 +1646,7 @@ func delChannelByEdge(edges *bbolt.Bucket, edgeIndex *bbolt.Bucket,
16421646
// determined by the lexicographical ordering of the identity public keys of
16431647
// the nodes on either side of the channel.
16441648
func (c *ChannelGraph) UpdateEdgePolicy(edge *ChannelEdgePolicy) error {
1645-
return c.db.Update(func(tx *bbolt.Tx) error {
1649+
return c.db.Batch(func(tx *bbolt.Tx) error {
16461650
return updateEdgePolicy(tx, edge)
16471651
})
16481652
}

0 commit comments

Comments
 (0)