From ef5b3d810d3f7fb3800a969983d1d485d3bf4509 Mon Sep 17 00:00:00 2001 From: Paul Lamb Date: Thu, 19 Nov 2026 17:37:19 -0600 Subject: [PATCH] chainntnfs: mark a set complete above the tip only with no scan pending From lightningnetwork/lnd#11247 as re-pushed on 2026-11-17 (head 62e50afd8): the early returns for a start height above the notifier's tip leave a set pending while another scan for it is outstanding. In this tree the case is already handled earlier: a prefix that starts above the tip also starts above the prefix's end, so the prefix branch returns before this check and the set stays pending. The guard keeps the two trees alike, and upstream's TestTxNotifierPendingScanAfterDeepReorg covers the path here. --- chainntnfs/txnotifier.go | 18 +++--- chainntnfs/txnotifier_hint_origin_test.go | 78 +++++++++++++++++++++++ 2 files changed, 88 insertions(+), 8 deletions(-) diff --git a/chainntnfs/txnotifier.go b/chainntnfs/txnotifier.go index b8ae25b9095..1605fba5e16 100644 --- a/chainntnfs/txnotifier.go +++ b/chainntnfs/txnotifier.go @@ -931,10 +931,11 @@ func (n *TxNotifier) RegisterConf(txid *chainhash.Hash, pkScript []byte, "dispatching historical confirmation rescan for %v", ntfn.ConfRequest) - // Set the rescan status to complete, which will allow the - // notifier to start delivering messages for this set - // immediately. - confSet.rescanStatus = rescanComplete + // If no other scan is pending, mark the set complete so the + // notifier can start delivering messages for it immediately. + if confSet.pendingRescans == 0 { + confSet.rescanStatus = rescanComplete + } return &ConfRegistration{ Event: ntfn.Event, HistoricalDispatch: nil, @@ -1437,10 +1438,11 @@ func (n *TxNotifier) RegisterSpend(outpoint *wire.OutPoint, pkScript []byte, Log.Debugf("Spend hint of %d for %v is above current height %d", startHeight, ntfn.SpendRequest, n.currentHeight) - // We'll also set the rescan status as complete to ensure that - // spend hints for this request get updated upon - // connected/disconnected blocks. - spendSet.rescanStatus = rescanComplete + // If no other scan is pending, mark the set complete so spend + // hints for it are updated upon connected/disconnected blocks. + if spendSet.pendingRescans == 0 { + spendSet.rescanStatus = rescanComplete + } return &SpendRegistration{ Event: ntfn.Event, HistoricalDispatch: nil, diff --git a/chainntnfs/txnotifier_hint_origin_test.go b/chainntnfs/txnotifier_hint_origin_test.go index e5975973d8b..d8417103d1e 100644 --- a/chainntnfs/txnotifier_hint_origin_test.go +++ b/chainntnfs/txnotifier_hint_origin_test.go @@ -468,3 +468,81 @@ func TestTxNotifierPrefixScanUsesCachedHint(t *testing.T) { ) }) } + +// TestTxNotifierPendingScanAfterDeepReorg asserts that a prefix which lies +// entirely above a reorged tip does not mark a set complete while its original +// historical scan remains outstanding. A pending set must not advance its +// cached hint when the next block connects. +func TestTxNotifierPendingScanAfterDeepReorg(t *testing.T) { + const ( + startHeight = uint32(100) + reorgTip = uint32(50) + earlierHint = uint32(80) + ) + + disconnect := func(t *testing.T, n *chainntnfs.TxNotifier) { + t.Helper() + for height := startHeight; height > reorgTip; height-- { + require.NoError(t, n.DisconnectTip(height)) + } + } + + t.Run("confirmation", func(t *testing.T) { + cache := newMockHintCache() + n := chainntnfs.NewTxNotifier( + startHeight, chainntnfs.ReorgSafetyLimit, cache, cache, + ) + t.Cleanup(n.TearDown) + + script, tx := staleHintTx("pending-deep-reorg-conf") + txid := tx.TxHash() + initial, err := n.RegisterConf( + &txid, script, 1, startHeight, + ) + require.NoError(t, err) + require.NotNil(t, initial.HistoricalDispatch) + request := initial.HistoricalDispatch.ConfRequest + + disconnect(t, n) + earlier, err := n.RegisterConf(&txid, script, 1, earlierHint) + require.NoError(t, err) + require.Nil(t, earlier.HistoricalDispatch) + + block := btcutil.NewBlock(&wire.MsgBlock{}) + require.NoError(t, n.ConnectTip(block, reorgTip+1)) + require.NoError(t, n.NotifyHeight(reorgTip+1)) + _, err = cache.QueryConfirmHint(request) + require.ErrorIs(t, err, chainntnfs.ErrConfirmHintNotFound) + }) + + t.Run("spend", func(t *testing.T) { + cache := newMockHintCache() + n := chainntnfs.NewTxNotifier( + startHeight, chainntnfs.ReorgSafetyLimit, cache, cache, + ) + t.Cleanup(n.TearDown) + + outpoint, script, _ := staleHintSpend( + "pending-deep-reorg-spend", + ) + initial, err := n.RegisterSpend( + &outpoint, script, startHeight, + ) + require.NoError(t, err) + require.NotNil(t, initial.HistoricalDispatch) + request := initial.HistoricalDispatch.SpendRequest + + disconnect(t, n) + earlier, err := n.RegisterSpend( + &outpoint, script, earlierHint, + ) + require.NoError(t, err) + require.Nil(t, earlier.HistoricalDispatch) + + block := btcutil.NewBlock(&wire.MsgBlock{}) + require.NoError(t, n.ConnectTip(block, reorgTip+1)) + require.NoError(t, n.NotifyHeight(reorgTip+1)) + _, err = cache.QuerySpendHint(request) + require.ErrorIs(t, err, chainntnfs.ErrSpendHintNotFound) + }) +}