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) + }) +}