8
8
"fmt"
9
9
"time"
10
10
11
+ "github.com/btcsuite/btcd/chaincfg/chainhash"
11
12
"github.com/btcsuite/btcd/wire"
12
13
"github.com/btcsuite/btcutil"
13
14
"github.com/lightninglabs/lndclient"
@@ -56,6 +57,9 @@ type loopOutSwap struct {
56
57
57
58
htlc * swap.Htlc
58
59
60
+ // htlcTxHash is the confirmed htlc tx id.
61
+ htlcTxHash * chainhash.Hash
62
+
59
63
swapPaymentChan chan lndclient.PaymentResult
60
64
prePaymentChan chan lndclient.PaymentResult
61
65
}
@@ -210,6 +214,7 @@ func resumeLoopOutSwap(reqContext context.Context, cfg *swapConfig,
210
214
} else {
211
215
swap .state = lastUpdate .State
212
216
swap .lastUpdateTime = lastUpdate .Time
217
+ swap .htlcTxHash = lastUpdate .HtlcTxHash
213
218
}
214
219
215
220
return swap , nil
@@ -376,6 +381,7 @@ func (s *loopOutSwap) executeSwap(globalCtx context.Context) error {
376
381
377
382
// Try to spend htlc and continue (rbf) until a spend has confirmed.
378
383
spendDetails , err := s .waitForHtlcSpendConfirmed (globalCtx ,
384
+ * htlcOutpoint ,
379
385
func () error {
380
386
return s .sweep (globalCtx , * htlcOutpoint , htlcValue )
381
387
},
@@ -419,8 +425,9 @@ func (s *loopOutSwap) persistState(ctx context.Context) error {
419
425
err := s .store .UpdateLoopOut (
420
426
s .hash , updateTime ,
421
427
loopdb.SwapStateData {
422
- State : s .state ,
423
- Cost : s .cost ,
428
+ State : s .state ,
429
+ Cost : s .cost ,
430
+ HtlcTxHash : s .htlcTxHash ,
424
431
},
425
432
)
426
433
if err != nil {
@@ -563,11 +570,21 @@ func (s *loopOutSwap) waitForConfirmedHtlc(globalCtx context.Context) (
563
570
s .InitiationHeight ,
564
571
)
565
572
573
+ // If we've revealed the preimage in a previous run, we expect to have
574
+ // recorded the htlc tx hash. We use this to re-register for
575
+ // confirmation, to be sure that we'll keep tracking the same htlc. For
576
+ // older swaps, this field may not be populated even though the preimage
577
+ // has already been revealed.
578
+ if s .state == loopdb .StatePreimageRevealed && s .htlcTxHash == nil {
579
+ s .log .Warnf ("No htlc tx hash available, registering with " +
580
+ "just the pkscript" )
581
+ }
582
+
566
583
ctx , cancel := context .WithCancel (globalCtx )
567
584
defer cancel ()
568
585
htlcConfChan , htlcErrChan , err :=
569
586
s .lnd .ChainNotifier .RegisterConfirmationsNtfn (
570
- ctx , nil , s .htlc .PkScript , 1 ,
587
+ ctx , s . htlcTxHash , s .htlc .PkScript , 1 ,
571
588
s .InitiationHeight ,
572
589
)
573
590
if err != nil {
@@ -680,8 +697,10 @@ func (s *loopOutSwap) waitForConfirmedHtlc(globalCtx context.Context) (
680
697
}
681
698
}
682
699
683
- s .log .Infof ("Htlc tx %v at height %v" , txConf .Tx .TxHash (),
684
- txConf .BlockHeight )
700
+ htlcTxHash := txConf .Tx .TxHash ()
701
+ s .log .Infof ("Htlc tx %v at height %v" , htlcTxHash , txConf .BlockHeight )
702
+
703
+ s .htlcTxHash = & htlcTxHash
685
704
686
705
return txConf , nil
687
706
}
@@ -694,13 +713,14 @@ func (s *loopOutSwap) waitForConfirmedHtlc(globalCtx context.Context) (
694
713
// sweep offchain. So we must make sure we sweep successfully before on-chain
695
714
// timeout.
696
715
func (s * loopOutSwap ) waitForHtlcSpendConfirmed (globalCtx context.Context ,
697
- spendFunc func () error ) (* chainntnfs.SpendDetail , error ) {
716
+ htlc wire.OutPoint , spendFunc func () error ) (* chainntnfs.SpendDetail ,
717
+ error ) {
698
718
699
719
// Register the htlc spend notification.
700
720
ctx , cancel := context .WithCancel (globalCtx )
701
721
defer cancel ()
702
722
spendChan , spendErr , err := s .lnd .ChainNotifier .RegisterSpendNtfn (
703
- ctx , nil , s .htlc .PkScript , s .InitiationHeight ,
723
+ ctx , & htlc , s .htlc .PkScript , s .InitiationHeight ,
704
724
)
705
725
if err != nil {
706
726
return nil , fmt .Errorf ("register spend ntfn: %v" , err )
0 commit comments