@@ -623,10 +623,18 @@ func (s *loopInSwap) executeSwap(globalCtx context.Context) error {
623623 }
624624
625625 // Verify that the confirmed (external) htlc value matches the swap
626- // amount. Otherwise, fail the swap immediately.
627- if htlcValue != s .LoopInContract .AmountRequested {
626+ // amount. If the amounts mismatch we update the swap state to indicate
627+ // this, but end processing the swap. Instead, we continue to wait for
628+ // the htlc to expire and publish a timeout tx to reclaim the funds. We
629+ // skip this part if the swap was recovered from this state.
630+ if s .state != loopdb .StateFailIncorrectHtlcAmt &&
631+ htlcValue != s .LoopInContract .AmountRequested {
632+
628633 s .setState (loopdb .StateFailIncorrectHtlcAmt )
629- return s .persistAndAnnounceState (globalCtx )
634+ err = s .persistAndAnnounceState (globalCtx )
635+ if err != nil {
636+ log .Errorf ("Error persisting state: %v" , err )
637+ }
630638 }
631639
632640 // 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,
10321040 // We needed another on chain tx to sweep the timeout clause,
10331041 // which we now include in our costs.
10341042 s .cost .Onchain += sweepFee
1035- s .setState (loopdb .StateFailTimeout )
1043+
1044+ // If the swap is in state StateFailIncorrectHtlcAmt we know
1045+ // that the deposited htlc amount wasn't equal to the contract
1046+ // amount. We can finalize the swap by setting an appropriate
1047+ // state.
1048+ if s .state == loopdb .StateFailIncorrectHtlcAmt {
1049+ s .setState (loopdb .StateFailIncorrectHtlcAmtSwept )
1050+ } else {
1051+ s .setState (loopdb .StateFailTimeout )
1052+ }
10361053
10371054 // Now that the timeout tx confirmed, we can safely cancel the
10381055 // swap invoice. We still need to query the final invoice state.
0 commit comments