From d26050f7110cb433613b3354aa5dd498569c4f6c Mon Sep 17 00:00:00 2001 From: Wilmer Paulino Date: Fri, 15 Jun 2018 12:31:00 -0700 Subject: [PATCH] contractcourt: use MarkChannelResolved for coop channel closures In this commit, we alter cooperative channel closures to also use MarkChannelResolved in order to unify the logic for the different types of channel closures. --- contractcourt/chain_arbitrator.go | 33 ----------------------------- contractcourt/chain_watcher.go | 16 -------------- contractcourt/channel_arbitrator.go | 8 ++++++- 3 files changed, 7 insertions(+), 50 deletions(-) diff --git a/contractcourt/chain_arbitrator.go b/contractcourt/chain_arbitrator.go index 0d8b6b5d..fb5f1c97 100644 --- a/contractcourt/chain_arbitrator.go +++ b/contractcourt/chain_arbitrator.go @@ -342,23 +342,6 @@ func (c *ChainArbitrator) Start() error { pCache: c.cfg.PreimageDB, signer: c.cfg.Signer, isOurAddr: c.cfg.IsOurAddress, - notifyChanClosed: func() error { - c.Lock() - delete(c.activeChannels, chanPoint) - - chainWatcher, ok := c.activeWatchers[chanPoint] - if ok { - // Since the chainWatcher is - // calling notifyChanClosed, we - // must stop it in a goroutine - // to not deadlock. - go chainWatcher.Stop() - } - delete(c.activeWatchers, chanPoint) - c.Unlock() - - return nil - }, contractBreach: func(retInfo *lnwallet.BreachRetribution) error { return c.cfg.ContractBreach(chanPoint, retInfo) }, @@ -697,22 +680,6 @@ func (c *ChainArbitrator) WatchNewChannel(newChan *channeldb.OpenChannel) error pCache: c.cfg.PreimageDB, signer: c.cfg.Signer, isOurAddr: c.cfg.IsOurAddress, - notifyChanClosed: func() error { - c.Lock() - delete(c.activeChannels, chanPoint) - - chainWatcher, ok := c.activeWatchers[chanPoint] - if ok { - // Since the chainWatcher is calling - // notifyChanClosed, we must stop it in - // a goroutine to not deadlock. - go chainWatcher.Stop() - } - delete(c.activeWatchers, chanPoint) - c.Unlock() - - return nil - }, contractBreach: func(retInfo *lnwallet.BreachRetribution) error { return c.cfg.ContractBreach(chanPoint, retInfo) }, diff --git a/contractcourt/chain_watcher.go b/contractcourt/chain_watcher.go index 8333b52f..1ab959cb 100644 --- a/contractcourt/chain_watcher.go +++ b/contractcourt/chain_watcher.go @@ -78,13 +78,6 @@ type chainWatcherConfig struct { // machine. signer lnwallet.Signer - // notifyChanClosed is a method that will be called by the watcher when - // it has detected a close on-chain and performed all necessary - // actions, like marking the channel closed in the database and - // notified all its subcribers. It lets the chain arbitrator know that - // the chain watcher chan be stopped. - notifyChanClosed func() error - // contractBreach is a method that will be called by the watcher if it // detects that a contract breach transaction has been confirmed. Only // when this method returns with a non-nil error it will be safe to mark @@ -492,16 +485,7 @@ func (c *chainWatcher) dispatchCooperativeClose(commitSpend *chainntnfs.SpendDet } c.Unlock() - // Now notify the ChainArbitrator that the watcher's job is done, such - // that it can shut it down and clean up. - if err := c.cfg.notifyChanClosed(); err != nil { - log.Errorf("unable to notify channel closed for "+ - "ChannelPoint(%v): %v", - c.cfg.chanState.FundingOutpoint, err) - } - return nil - } // dispatchLocalForceClose processes a unilateral close by us being confirmed. diff --git a/contractcourt/channel_arbitrator.go b/contractcourt/channel_arbitrator.go index b80995b9..3d96d51b 100644 --- a/contractcourt/channel_arbitrator.go +++ b/contractcourt/channel_arbitrator.go @@ -1364,10 +1364,16 @@ func (c *ChannelArbitrator) channelAttendant(bestHeight int32) { ) // We've cooperatively closed the channel, so we're no longer - // needed. + // needed. We'll mark the channel as resolved and exit. case <-c.cfg.ChainEvents.CooperativeClosure: log.Infof("ChannelArbitrator(%v) closing due to co-op "+ "closure", c.cfg.ChanPoint) + + if err := c.cfg.MarkChannelResolved(); err != nil { + log.Errorf("Unable to mark contract "+ + "resolved: %v", err) + } + return // We have broadcasted our commitment, and it is now confirmed