@@ -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,14 @@ 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 we are not waiting for funds
890+ // from HTLC, e.g. if the invoice was settled.
891+ if s .state != loopdb .StateHtlcPublished {
892+ return 0 , nil
893+ }
894+
887895 if s .height >= s .LoopInContract .CltvExpiry {
888896 return s .publishTimeoutTx (ctx , htlcOutpoint , htlcValue )
889897 }
@@ -902,7 +910,18 @@ func (s *loopInSwap) waitForSwapComplete(ctx context.Context,
902910 htlcSpend := false
903911 invoiceFinalized := false
904912 htlcKeyRevealed := false
905- for ! htlcSpend || ! invoiceFinalized {
913+ for {
914+ // Check stop conditions.
915+ if htlcSpend {
916+ break
917+ }
918+ if s .state == loopdb .StateInvoiceSettled {
919+ if s .height >= s .LoopInContract .CltvExpiry {
920+ s .setState (loopdb .StateSuccess )
921+ break
922+ }
923+ }
924+
906925 select {
907926 // If the client requested the swap to be abandoned, we override
908927 // the status in the database.
0 commit comments