From 3db464955d0641878d7c62c9ca760ca6cbeb87ed Mon Sep 17 00:00:00 2001 From: Slyghtning Date: Fri, 12 Jan 2024 11:36:03 +0100 Subject: [PATCH] loopin: sweep incorrect htlc amount after timeout --- loopin.go | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/loopin.go b/loopin.go index f2e495e..5aecb67 100644 --- a/loopin.go +++ b/loopin.go @@ -623,10 +623,18 @@ func (s *loopInSwap) executeSwap(globalCtx context.Context) error { } // Verify that the confirmed (external) htlc value matches the swap - // amount. Otherwise, fail the swap immediately. - if htlcValue != s.LoopInContract.AmountRequested { + // amount. If the amounts mismatch we update the swap state to indicate + // this, but end processing the swap. Instead, we continue to wait for + // the htlc to expire and publish a timeout tx to reclaim the funds. We + // skip this part if the swap was recovered from this state. + if s.state != loopdb.StateFailIncorrectHtlcAmt && + htlcValue != s.LoopInContract.AmountRequested { + s.setState(loopdb.StateFailIncorrectHtlcAmt) - return s.persistAndAnnounceState(globalCtx) + err = s.persistAndAnnounceState(globalCtx) + if err != nil { + log.Errorf("Error persisting state: %v", err) + } } // The server is expected to see the htlc on-chain and know that it can @@ -1032,7 +1040,16 @@ func (s *loopInSwap) processHtlcSpend(ctx context.Context, // We needed another on chain tx to sweep the timeout clause, // which we now include in our costs. s.cost.Onchain += sweepFee - s.setState(loopdb.StateFailTimeout) + + // If the swap is in state StateFailIncorrectHtlcAmt we know + // that the deposited htlc amount wasn't equal to the contract + // amount. We can finalize the swap by setting an appropriate + // state. + if s.state == loopdb.StateFailIncorrectHtlcAmt { + s.setState(loopdb.StateFailIncorrectHtlcAmtSwept) + } else { + s.setState(loopdb.StateFailTimeout) + } // Now that the timeout tx confirmed, we can safely cancel the // swap invoice. We still need to query the final invoice state.