Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions chainntnfs/txnotifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
78 changes: 78 additions & 0 deletions chainntnfs/txnotifier_hint_origin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
Loading