From 27329ed9dbccd8787722931e93aa6adb0ab17be8 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Thu, 4 May 2017 16:08:56 -0700 Subject: [PATCH] breacharbiter: mark channel as fully closed upon detection of unilateral close This commit modifies the breachArbiter to properly mark a channel as _fully_ closed once the transaction which force closed the channel has been confirmed within the chain. --- breacharbiter.go | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/breacharbiter.go b/breacharbiter.go index 833393dd..6f3c8777 100644 --- a/breacharbiter.go +++ b/breacharbiter.go @@ -92,7 +92,7 @@ func (b *breachArbiter) Start() error { // it to watch for channel breaches. activeChannels, err := b.db.FetchAllChannels() if err != nil && err != channeldb.ErrNoActiveChannels { - brarLog.Errorf("unable to fetch active channels") + brarLog.Errorf("unable to fetch active channels: %v", err) return err } @@ -109,7 +109,8 @@ func (b *breachArbiter) Start() error { channel, err := lnwallet.NewLightningChannel(nil, b.notifier, chanState) if err != nil { - brarLog.Errorf("unable to load channel from disk") + brarLog.Errorf("unable to load channel from "+ + "disk: %v", err) return err } @@ -360,6 +361,30 @@ func (b *breachArbiter) breachObserver(contract *lnwallet.LightningChannel, contract.Stop() return + // The channel has been closed by a normal means: force closing with + // the latest commitment transaction. + case closeInfo := <-contract.UnilateralClose: + // Launch a goroutine to cancel out this contract within the + // breachArbiter's main goroutine. + go func() { + b.settledContracts <- chanPoint + }() + + // Next, we'll launch a goroutine to wait until the closing + // transaction has been confirmed so we can mark the contract + // as resolved in the database. + // + // TODO(roasbeef): also notify utxoNursery, might've had + // outbound HTLC's in flight + go waitForChanToClose(b.notifier, nil, chanPoint, closeInfo.SpenderTxHash, func() { + brarLog.Infof("Force closed ChannelPoint(%v) is "+ + "fully closed, updating DB", chanPoint) + + if err := b.db.MarkChanFullyClosed(chanPoint); err != nil { + brarLog.Errorf("unable to mark chan as closed: %v", err) + } + }) + // A read from this channel indicates that a channel breach has been // detected! So we notify the main coordination goroutine with the // information needed to bring the counterparty to justice.