@@ -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
@@ -882,8 +884,18 @@ func (s *loopInSwap) waitForSwapComplete(ctx context.Context,
882884 }
883885
884886 // publishTxOnTimeout publishes the timeout tx if the contract has
885- // expired.
887+ // expired and invoice has not been settled .
886888 publishTxOnTimeout := func () (btcutil.Amount , error ) {
889+ // Don't publish the timeout tx if the invoice was settled.
890+ if s .state == loopdb .StateInvoiceSettled {
891+ return 0 , nil
892+ }
893+
894+ // Don't publish the timeout tx if the swap succeeded.
895+ if s .state == loopdb .StateSuccess {
896+ return 0 , nil
897+ }
898+
887899 if s .height >= s .LoopInContract .CltvExpiry {
888900 return s .publishTimeoutTx (ctx , htlcOutpoint , htlcValue )
889901 }
@@ -902,7 +914,18 @@ func (s *loopInSwap) waitForSwapComplete(ctx context.Context,
902914 htlcSpend := false
903915 invoiceFinalized := false
904916 htlcKeyRevealed := false
905- for ! htlcSpend || ! invoiceFinalized {
917+ for {
918+ // Check stop conditions.
919+ if htlcSpend && invoiceFinalized {
920+ break
921+ }
922+ if s .state == loopdb .StateInvoiceSettled {
923+ if s .height >= s .LoopInContract .CltvExpiry {
924+ s .setState (loopdb .StateSuccess )
925+ break
926+ }
927+ }
928+
906929 select {
907930 // If the client requested the swap to be abandoned, we override
908931 // the status in the database.
0 commit comments