From 291a8e4effb5f6c12247d0135c1b8cfd61e99406 Mon Sep 17 00:00:00 2001 From: yyforyongyu Date: Wed, 6 Apr 2022 20:20:31 +0800 Subject: [PATCH] contractcourt: move breach handling into its own function This commit refactors the breach handling logic into its own function so that the related code can be unit tested. --- contractcourt/chain_watcher.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/contractcourt/chain_watcher.go b/contractcourt/chain_watcher.go index 58d83d297..c9b5e7ef9 100644 --- a/contractcourt/chain_watcher.go +++ b/contractcourt/chain_watcher.go @@ -729,7 +729,6 @@ func (c *chainWatcher) handleKnownRemoteState( commitTxBroadcast := commitSpend.SpendingTx commitHash := commitTxBroadcast.TxHash() - spendHeight := uint32(commitSpend.SpendingHeight) switch { // If the spending transaction matches the current latest state, then @@ -780,8 +779,19 @@ func (c *chainWatcher) handleKnownRemoteState( return true, nil } + // This is neither a remote force close or a "future" commitment, we + // now check whether it's a remote breach and properly handle it. + return c.handlePossibleBreach(commitSpend, broadcastStateNum, chainSet) +} + +// handlePossibleBreach checks whether the remote has breached and dispatches a +// breach resolution to claim funds. +func (c *chainWatcher) handlePossibleBreach(commitSpend *chainntnfs.SpendDetail, + broadcastStateNum uint64, chainSet *chainSet) (bool, error) { + // We check if we have a revoked state at this state num that matches // the spend transaction. + spendHeight := uint32(commitSpend.SpendingHeight) retribution, err := lnwallet.NewBreachRetribution( c.cfg.chanState, broadcastStateNum, spendHeight, ) @@ -801,6 +811,7 @@ func (c *chainWatcher) handleKnownRemoteState( // We found a revoked state at this height, but it could still be our // own broadcasted state we are looking at. Therefore check that the // commit matches before assuming it was a breach. + commitHash := commitSpend.SpendingTx.TxHash() if retribution.BreachTransaction.TxHash() != commitHash { return false, nil }