From e765baa1f984530646f09ade3532813cc425bdcf Mon Sep 17 00:00:00 2001 From: Joost Jager Date: Wed, 3 Apr 2019 14:03:32 +0200 Subject: [PATCH] loopd: check loop in timeout already before a new block comes in --- loopin.go | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/loopin.go b/loopin.go index c301726..53ae6e6 100644 --- a/loopin.go +++ b/loopin.go @@ -441,6 +441,22 @@ func (s *loopInSwap) waitForSwapComplete(ctx context.Context, return fmt.Errorf("subscribe to swap invoice: %v", err) } + // checkTimeout publishes the timeout tx if the contract has expired. + checkTimeout := func() error { + if s.height >= s.LoopInContract.CltvExpiry { + return s.publishTimeoutTx(ctx, htlc) + } + + return nil + } + + // Check timeout at current height. After a restart we may want to + // publish the tx immediately. + err = checkTimeout() + if err != nil { + return err + } + htlcSpend := false invoiceFinalized := false for !htlcSpend || !invoiceFinalized { @@ -454,11 +470,9 @@ func (s *loopInSwap) waitForSwapComplete(ctx context.Context, case notification := <-s.blockEpochChan: s.height = notification.(int32) - if s.height >= s.LoopInContract.CltvExpiry { - err := s.publishTimeoutTx(ctx, htlc) - if err != nil { - return err - } + err := checkTimeout() + if err != nil { + return err } // The htlc spend is confirmed. Inspect the spending tx to