@@ -854,9 +854,11 @@ func getTxFee(tx *wire.MsgTx, fee chainfee.SatPerKVByte) btcutil.Amount {
854854 return fee .FeeForVSize (lntypes .VByte (vsize ))
855855}
856856
857- // waitForSwapComplete waits until a spending tx of the htlc gets confirmed and
858- // the swap invoice is either settled or canceled. If the htlc times out, the
859- // timeout tx will be published.
857+ // waitForSwapComplete waits until a spending tx of the HTLC gets confirmed and
858+ // the swap invoice is either settled or canceled. If the HTLC times out and the
859+ // invoice is still not paid, the timeout tx will be published. If the HTLC
860+ // times out and the invoice is paid, the function stops waiting and returns
861+ // clearly.
860862func (s * loopInSwap ) waitForSwapComplete (ctx context.Context ,
861863 htlcOutpoint * wire.OutPoint , htlcValue btcutil.Amount ) error {
862864
@@ -881,9 +883,20 @@ func (s *loopInSwap) waitForSwapComplete(ctx context.Context,
881883 return fmt .Errorf ("subscribe to swap invoice: %v" , err )
882884 }
883885
886+ var (
887+ htlcSpend = false
888+ invoiceFinalized = false
889+ htlcKeyRevealed = false
890+ )
891+
884892 // publishTxOnTimeout publishes the timeout tx if the contract has
885- // expired.
893+ // expired and invoice has not been settled .
886894 publishTxOnTimeout := func () (btcutil.Amount , error ) {
895+ // Don't publish the timeout tx if the invoice was settled.
896+ if invoiceFinalized {
897+ return 0 , nil
898+ }
899+
887900 if s .height >= s .LoopInContract .CltvExpiry {
888901 return s .publishTimeoutTx (ctx , htlcOutpoint , htlcValue )
889902 }
@@ -899,10 +912,15 @@ func (s *loopInSwap) waitForSwapComplete(ctx context.Context,
899912 return err
900913 }
901914
902- htlcSpend := false
903- invoiceFinalized := false
904- htlcKeyRevealed := false
905- for ! htlcSpend || ! invoiceFinalized {
915+ for {
916+ // Check stop conditions.
917+ if htlcSpend {
918+ break
919+ }
920+ if invoiceFinalized && s .height >= s .LoopInContract .CltvExpiry {
921+ break
922+ }
923+
906924 select {
907925 // If the client requested the swap to be abandoned, we override
908926 // the status in the database.
0 commit comments