Skip to content

Commit 6ad8be2

Browse files
authored
Merge pull request #2773 from Roasbeef/graph-batch
channeldb: convert all Update calls to use Batch
2 parents c7ca387 + da76c34 commit 6ad8be2

File tree

3 files changed

+34
-21
lines changed

3 files changed

+34
-21
lines changed

channeldb/channel.go

+11-8
Original file line numberDiff line numberDiff line change
@@ -1027,7 +1027,7 @@ func (c *OpenChannel) UpdateCommitment(newCommitment *ChannelCommitment) error {
10271027
return ErrNoRestoredChannelMutation
10281028
}
10291029

1030-
err := c.Db.Update(func(tx *bbolt.Tx) error {
1030+
err := c.Db.Batch(func(tx *bbolt.Tx) error {
10311031
chanBucket, err := fetchChanBucket(
10321032
tx, c.IdentityPub, &c.FundingOutpoint, c.ChainHash,
10331033
)
@@ -1465,7 +1465,7 @@ func (c *OpenChannel) AppendRemoteCommitChain(diff *CommitDiff) error {
14651465
return ErrNoRestoredChannelMutation
14661466
}
14671467

1468-
return c.Db.Update(func(tx *bbolt.Tx) error {
1468+
return c.Db.Batch(func(tx *bbolt.Tx) error {
14691469
// First, we'll grab the writable bucket where this channel's
14701470
// data resides.
14711471
chanBucket, err := fetchChanBucket(
@@ -1608,7 +1608,9 @@ func (c *OpenChannel) AdvanceCommitChainTail(fwdPkg *FwdPkg) error {
16081608

16091609
var newRemoteCommit *ChannelCommitment
16101610

1611-
err := c.Db.Update(func(tx *bbolt.Tx) error {
1611+
err := c.Db.Batch(func(tx *bbolt.Tx) error {
1612+
newRemoteCommit = nil
1613+
16121614
chanBucket, err := fetchChanBucket(
16131615
tx, c.IdentityPub, &c.FundingOutpoint, c.ChainHash,
16141616
)
@@ -1746,7 +1748,7 @@ func (c *OpenChannel) AckAddHtlcs(addRefs ...AddRef) error {
17461748
c.Lock()
17471749
defer c.Unlock()
17481750

1749-
return c.Db.Update(func(tx *bbolt.Tx) error {
1751+
return c.Db.Batch(func(tx *bbolt.Tx) error {
17501752
return c.Packager.AckAddHtlcs(tx, addRefs...)
17511753
})
17521754
}
@@ -1759,7 +1761,7 @@ func (c *OpenChannel) AckSettleFails(settleFailRefs ...SettleFailRef) error {
17591761
c.Lock()
17601762
defer c.Unlock()
17611763

1762-
return c.Db.Update(func(tx *bbolt.Tx) error {
1764+
return c.Db.Batch(func(tx *bbolt.Tx) error {
17631765
return c.Packager.AckSettleFails(tx, settleFailRefs...)
17641766
})
17651767
}
@@ -1770,7 +1772,7 @@ func (c *OpenChannel) SetFwdFilter(height uint64, fwdFilter *PkgFilter) error {
17701772
c.Lock()
17711773
defer c.Unlock()
17721774

1773-
return c.Db.Update(func(tx *bbolt.Tx) error {
1775+
return c.Db.Batch(func(tx *bbolt.Tx) error {
17741776
return c.Packager.SetFwdFilter(tx, height, fwdFilter)
17751777
})
17761778
}
@@ -1783,14 +1785,15 @@ func (c *OpenChannel) RemoveFwdPkg(height uint64) error {
17831785
c.Lock()
17841786
defer c.Unlock()
17851787

1786-
return c.Db.Update(func(tx *bbolt.Tx) error {
1788+
return c.Db.Batch(func(tx *bbolt.Tx) error {
17871789
return c.Packager.RemovePkg(tx, height)
17881790
})
17891791
}
17901792

17911793
// RevocationLogTail returns the "tail", or the end of the current revocation
17921794
// log. This entry represents the last previous state for the remote node's
1793-
// commitment chain. The ChannelDelta returned by this method will always lag one state behind the most current (unrevoked) state of the remote node's
1795+
// commitment chain. The ChannelDelta returned by this method will always lag
1796+
// one state behind the most current (unrevoked) state of the remote node's
17941797
// commitment chain.
17951798
func (c *OpenChannel) RevocationLogTail() (*ChannelCommitment, error) {
17961799
c.RLock()

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
}

channeldb/invoices.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,9 @@ func (d *DB) AddInvoice(newInvoice *Invoice, paymentHash lntypes.Hash) (
242242
}
243243

244244
var invoiceAddIndex uint64
245-
err := d.Update(func(tx *bbolt.Tx) error {
245+
err := d.Batch(func(tx *bbolt.Tx) error {
246+
invoiceAddIndex = 0
247+
246248
invoices, err := tx.CreateBucketIfNotExists(invoiceBucket)
247249
if err != nil {
248250
return err
@@ -635,7 +637,9 @@ func (d *DB) AcceptOrSettleInvoice(paymentHash [32]byte,
635637
amtPaid lnwire.MilliSatoshi) (*Invoice, error) {
636638

637639
var settledInvoice *Invoice
638-
err := d.Update(func(tx *bbolt.Tx) error {
640+
err := d.Batch(func(tx *bbolt.Tx) error {
641+
settledInvoice = nil
642+
639643
invoices, err := tx.CreateBucketIfNotExists(invoiceBucket)
640644
if err != nil {
641645
return err
@@ -714,7 +718,9 @@ func (d *DB) SettleHoldInvoice(preimage lntypes.Preimage) (*Invoice, error) {
714718
// payment hash.
715719
func (d *DB) CancelInvoice(paymentHash lntypes.Hash) (*Invoice, error) {
716720
var canceledInvoice *Invoice
717-
err := d.Update(func(tx *bbolt.Tx) error {
721+
err := d.Batch(func(tx *bbolt.Tx) error {
722+
canceledInvoice = nil
723+
718724
invoices, err := tx.CreateBucketIfNotExists(invoiceBucket)
719725
if err != nil {
720726
return err

0 commit comments

Comments
 (0)