Skip to content

Commit 4c26aef

Browse files
committed
sweepbatcher: fix height hint for confirmations
Previously, we passed the current best block height as the height hint to RegisterConfirmationsNtfn. If Loop was shut down when a sweep was confirmed and restarted later, it would use the current best height as the hint. This caused the confirmation to be missed, since it had already occurred before that height. This commit fixes the issue by passing the initiation height of the swap as the height hint instead. This is consistent with what we do in other places where we use RegisterConfirmationsNtfn. Since LND caches previously passed height hints for RegisterConfirmationsNtfn and uses maximum passed value, to actually recover in such a condition, one has to restart LND passing the option --height-hint-cache-query-disable and then restart Loop applying this fix. If a pruned bitcoind backend is used, this might not work.
1 parent ec2bf44 commit 4c26aef

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

sweepbatcher/sweep_batch.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1707,13 +1707,20 @@ func (b *batch) monitorSpend(ctx context.Context, primarySweep sweep) error {
17071707

17081708
// monitorConfirmations monitors the batch transaction for confirmations.
17091709
func (b *batch) monitorConfirmations(ctx context.Context) error {
1710+
// Find initiationHeight.
1711+
primarySweep, ok := b.sweeps[b.primarySweepID]
1712+
if !ok {
1713+
return fmt.Errorf("can't find primarySweep")
1714+
}
1715+
17101716
reorgChan := make(chan struct{})
17111717

17121718
confCtx, cancel := context.WithCancel(ctx)
17131719

17141720
confChan, errChan, err := b.chainNotifier.RegisterConfirmationsNtfn(
17151721
confCtx, b.batchTxid, b.batchPkScript, batchConfHeight,
1716-
b.currentHeight, lndclient.WithReOrgChan(reorgChan),
1722+
primarySweep.initiationHeight,
1723+
lndclient.WithReOrgChan(reorgChan),
17171724
)
17181725
if err != nil {
17191726
cancel()

sweepbatcher/sweep_batcher_test.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -781,12 +781,15 @@ func testSweepBatcherSimpleLifecycle(t *testing.T, store testStore,
781781
Notifier: &dummyNotifier,
782782
}
783783

784+
const initiationHeight = 550
785+
784786
swap1 := &loopdb.LoopOutContract{
785787
SwapContract: loopdb.SwapContract{
786-
CltvExpiry: 111,
787-
AmountRequested: 111,
788-
ProtocolVersion: loopdb.ProtocolVersionMuSig2,
789-
HtlcKeys: htlcKeys,
788+
CltvExpiry: 111,
789+
AmountRequested: 111,
790+
ProtocolVersion: loopdb.ProtocolVersionMuSig2,
791+
HtlcKeys: htlcKeys,
792+
InitiationHeight: initiationHeight,
790793
},
791794

792795
DestAddr: destAddr,
@@ -871,14 +874,17 @@ func testSweepBatcherSimpleLifecycle(t *testing.T, store testStore,
871874
SpendingTx: spendingTx,
872875
SpenderTxHash: &spendingTxHash,
873876
SpenderInputIndex: 0,
874-
SpendingHeight: 601,
875877
}
876878

877879
// We notify the spend.
878880
lnd.SpendChannel <- spendDetail
879881

880882
// After receiving the spend, the batch is now monitoring for confs.
881-
<-lnd.RegisterConfChannel
883+
confReg := <-lnd.RegisterConfChannel
884+
885+
// Make sure the confirmation has proper height hint. It should pass
886+
// the swap initiation height, not the current height.
887+
require.Equal(t, int32(initiationHeight), confReg.HeightHint)
882888

883889
// The batch should eventually read the spend notification and progress
884890
// its state to closed.

0 commit comments

Comments
 (0)