Skip to content

Commit 99b8966

Browse files
committed
loopin: don't publish timeout tx if invoice paid
This action is not needed, since the swap is complete. Wait until the timeout anyway to be able to push the key on every block.
1 parent 70cf2d8 commit 99b8966

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

loopin.go

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
860862
func (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,17 @@ 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+
// Mark the swap successful.
922+
s.setState(loopdb.StateSuccess)
923+
break
924+
}
925+
906926
select {
907927
// If the client requested the swap to be abandoned, we override
908928
// the status in the database.

0 commit comments

Comments
 (0)