From fa30af04757d8dead9f11d146e503faead6ca8fe Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Sun, 9 Dec 2018 19:31:34 -0800 Subject: [PATCH] channeldb: when fetching/inserting commits check for ChanStatusRestored If the ChanStatusRestored flag is set, then we don't need to write or read the set of commits for a channel as they won't exist. This will be the case when we restore a channel from an SCB. --- channeldb/channel.go | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/channeldb/channel.go b/channeldb/channel.go index 8dd26e57..a68f517c 100644 --- a/channeldb/channel.go +++ b/channeldb/channel.go @@ -972,7 +972,9 @@ func (c *OpenChannel) UpdateCommitment(newCommitment *ChannelCommitment) error { // With the proper bucket fetched, we'll now write toe latest // commitment state to dis for the target party. - err = putChanCommitment(chanBucket, newCommitment, true) + err = putChanCommitment( + chanBucket, newCommitment, true, + ) if err != nil { return fmt.Errorf("unable to store chan "+ "revocations: %v", err) @@ -1540,7 +1542,9 @@ func (c *OpenChannel) AdvanceCommitChainTail(fwdPkg *FwdPkg) error { if err != nil { return err } - err = putChanCommitment(chanBucket, &newCommit.Commitment, false) + err = putChanCommitment( + chanBucket, &newCommit.Commitment, false, + ) if err != nil { return err } @@ -2353,12 +2357,22 @@ func putChanCommitment(chanBucket *bbolt.Bucket, c *ChannelCommitment, } func putChanCommitments(chanBucket *bbolt.Bucket, channel *OpenChannel) error { - err := putChanCommitment(chanBucket, &channel.LocalCommitment, true) + // If this is a restored channel, then we don't have any commitments to + // write. + if channel.hasChanStatus(ChanStatusRestored) { + return nil + } + + err := putChanCommitment( + chanBucket, &channel.LocalCommitment, true, + ) if err != nil { return err } - return putChanCommitment(chanBucket, &channel.RemoteCommitment, false) + return putChanCommitment( + chanBucket, &channel.RemoteCommitment, false, + ) } func putChanRevocationState(chanBucket *bbolt.Bucket, channel *OpenChannel) error { @@ -2473,6 +2487,12 @@ func fetchChanCommitment(chanBucket *bbolt.Bucket, local bool) (ChannelCommitmen func fetchChanCommitments(chanBucket *bbolt.Bucket, channel *OpenChannel) error { var err error + // If this is a restored channel, then we don't have any commitments to + // read. + if channel.hasChanStatus(ChanStatusRestored) { + return nil + } + channel.LocalCommitment, err = fetchChanCommitment(chanBucket, true) if err != nil { return err