From 83a425b74c6675162362c9538db07b94d238973f Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Thu, 4 May 2017 16:12:47 -0700 Subject: [PATCH] breacharbiter: at startup, watch pending closed channels to mark as fully closed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit adds a start up check to the breachArbiter: it will now watch all channels which are in the “pending closed” state, to ensure that state of the database is up to date at all times. Once any of the closing transactions for these channels have been confirmed, then they will properly be marked as such within the database. --- breacharbiter.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/breacharbiter.go b/breacharbiter.go index 1f142283..37f50018 100644 --- a/breacharbiter.go +++ b/breacharbiter.go @@ -120,6 +120,51 @@ func (b *breachArbiter) Start() error { b.wg.Add(1) go b.contractObserver(channelsToWatch) + // Additionally, we'll also want to retrieve any pending close or force + // close transactions to we can properly mark them as resolved in the + // database. + pendingCloseChans, err := b.db.FetchClosedChannels(true) + if err != nil { + brarLog.Errorf("unable to fetch closing channels: %v", err) + return err + } + for _, pendingClose := range pendingCloseChans { + // If this channel was force closed, and we have a non-zero + // balance, then the utxoNursery is currently watching over it. + // As a result we don't need to watch over it. + if pendingClose.CloseType == channeldb.ForceClose && + pendingClose.OurBalance != 0 { + continue + } + + brarLog.Infof("Watching for the closure of ChannelPoint(%v)", + pendingClose.ChanPoint) + + chanPoint := &pendingClose.ChanPoint + closeTXID := &pendingClose.ClosingTXID + confNtfn, err := b.notifier.RegisterConfirmationsNtfn(closeTXID, 1) + if err != nil { + return err + } + + go func() { + // In the case that the ChainNotifier is shutting down, + // all subscriber notification channels will be closed, + // generating a nil receive. + confInfo, ok := <-confNtfn.Confirmed + if !ok { + return + } + + brarLog.Infof("ChannelPoint(%v) is fully closed, "+ + "at height: %v", chanPoint, confInfo.BlockHeight) + + if err := b.db.MarkChanFullyClosed(chanPoint); err != nil { + brarLog.Errorf("unable to mark chan as closed: %v", err) + } + }() + } + return nil }