Skip to content

Commit e765baa

Browse files
committed
loopd: check loop in timeout already before a new block comes in
1 parent b5d2fb3 commit e765baa

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

loopin.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,22 @@ func (s *loopInSwap) waitForSwapComplete(ctx context.Context,
441441
return fmt.Errorf("subscribe to swap invoice: %v", err)
442442
}
443443

444+
// checkTimeout publishes the timeout tx if the contract has expired.
445+
checkTimeout := func() error {
446+
if s.height >= s.LoopInContract.CltvExpiry {
447+
return s.publishTimeoutTx(ctx, htlc)
448+
}
449+
450+
return nil
451+
}
452+
453+
// Check timeout at current height. After a restart we may want to
454+
// publish the tx immediately.
455+
err = checkTimeout()
456+
if err != nil {
457+
return err
458+
}
459+
444460
htlcSpend := false
445461
invoiceFinalized := false
446462
for !htlcSpend || !invoiceFinalized {
@@ -454,11 +470,9 @@ func (s *loopInSwap) waitForSwapComplete(ctx context.Context,
454470
case notification := <-s.blockEpochChan:
455471
s.height = notification.(int32)
456472

457-
if s.height >= s.LoopInContract.CltvExpiry {
458-
err := s.publishTimeoutTx(ctx, htlc)
459-
if err != nil {
460-
return err
461-
}
473+
err := checkTimeout()
474+
if err != nil {
475+
return err
462476
}
463477

464478
// The htlc spend is confirmed. Inspect the spending tx to

0 commit comments

Comments
 (0)