From cbd26b35e0551cfa835f7fa16f84ddc723526eb8 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Mon, 19 Dec 2016 16:49:42 -0800 Subject: [PATCH] channeldb: deleting a channel twice no longer triggers an error MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit adds an additional check within CloseChannel to ensure that sub-systems attempting to delete the channel one after the other (in the event of any sort of closure) doesn’t result in an extraneous error. To fix this, we now check if the channel exists before attempting a deletion. If the channel doesn’t exist, then we simply exit early with a nil error. --- channeldb/channel.go | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/channeldb/channel.go b/channeldb/channel.go index 7bc83c4d..df7bea29 100644 --- a/channeldb/channel.go +++ b/channeldb/channel.go @@ -568,6 +568,11 @@ func (c *OpenChannel) FindPreviousState(updateNum uint64) (*ChannelDelta, error) // purposes. // TODO(roasbeef): delete on-disk set of HTLC's func (c *OpenChannel) CloseChannel() error { + var b bytes.Buffer + if err := writeOutpoint(&b, c.ChanID); err != nil { + return err + } + return c.Db.Update(func(tx *bolt.Tx) error { // First fetch the top level bucket which stores all data related to // current, active channels. @@ -589,12 +594,19 @@ func (c *OpenChannel) CloseChannel() error { if chanIndexBucket == nil { return ErrNoActiveChannels } - var b bytes.Buffer - if err := writeOutpoint(&b, c.ChanID); err != nil { - return err - } + + // If this channel isn't found within the channel index bucket, + // then it has already been deleted. So we can exit early as + // there isn't any more work for us to do here. outPointBytes := b.Bytes() - if err := chanIndexBucket.Delete(b.Bytes()); err != nil { + if chanIndexBucket.Get(outPointBytes) == nil { + return nil + } + + // Otherwise, we can safely delete the channel from the index + // without running into any boltdb related errors by repeated + // deletion attempts. + if err := chanIndexBucket.Delete(outPointBytes); err != nil { return err } @@ -988,7 +1000,7 @@ func fetchChanTheirDustLimit(openChanBucket *bolt.Bucket, channel *OpenChannel) return nil } -func fetchChanOurDustLimit(openChanBucket *bolt.Bucket, channel *OpenChannel)error { +func fetchChanOurDustLimit(openChanBucket *bolt.Bucket, channel *OpenChannel) error { var b bytes.Buffer if err := writeOutpoint(&b, channel.ChanID); err != nil { return err