Skip to content

chainntnfs/btcdnotify: scan the chain for historical spends - #11583

Open
eynhaender wants to merge 3 commits into
lightningnetwork:masterfrom
eynhaender:fix/btcdnotify-historical-spend
Open

eynhaender wants to merge 3 commits into
lightningnetwork:masterfrom
eynhaender:fix/btcdnotify-historical-spend

Conversation

@eynhaender

Copy link
Copy Markdown

Description

RegisterSpendNtfn of the btcd notifier used the deprecated btcd rescan command (RescanAsync) for historical spends. The historical spend dispatch now runs in the notification dispatcher, as the bitcoind notifier does: it scans the blocks of the range from the end height down (getblockhash, getblock) and matches each transaction with SpendRequest.MatchesTx. The outpoint shortcuts are kept: an unspent output completes the request at once,
and a spent output starts the scan at its funding height.

The dispatch now always updates the spend details, also when no spend was found. Before, an output script request without a spend stayed pending, so its spend hint was never committed and the scan repeated from the original height
hint on every restart, also with btcd.

This lets btcd-compatible servers without rescan, such as libbitcoin-server, serve as a chain backend. There, every historical spend dispatch failed and the spend was never notified. lnd no longer uses rescan.

Related: btcsuite/btcwallet#1515 removes the wallet's use of rescan. The two changes are independent.

Verification

  • TestHistoricalSpendDetails: unspent, spent and out-of-range requests, by outpoint and by script.
  • TestHistoricalSpendDispatchUnspent: the spend hint of an unspent output script is committed. Fails on master against btcd, passes with this change.
  • chainntnfs interface tests (btcd): 30/30, same as master.
  • make itest backend=btcd (384 cases): identical to master, 545 passed, 9 skipped. 812 historical spend scans completed, 99 found a spend, none failed.
  • lnd v0.21.6 with this change against a libbitcoin-server mainnet node: recent spends are notified in under a second, spends about 900 blocks deep in 74 s (outpoint) and 90 s (script). As with the bitcoind notifier, a script with several spends returns the most recent one.

RegisterSpendNtfn issued the btcd rescan command, which rpcclient marks
as deprecated, to find a spend below the current height. Dispatch the
historical spend request to the notification dispatcher instead, which
scans the blocks of the requested range from the end height down to
the start height and matches each transaction against the request, as
the bitcoind notifier does. The outpoint case keeps its existing
shortcuts: an unspent output completes the request at once, and a
spent output starts the scan at the height of its funding block.

The dispatcher now always updates the spend details once the scan
finishes, including when no spend was found. Previously an output
script request whose rescan found no spend left the request pending,
so its spend hint was never committed and the historical dispatch was
repeated from the original height hint on every restart.

This also allows btcd-compatible servers that do not implement the
deprecated rescan command, such as libbitcoin-server, to serve as a
chain backend.
@github-actions github-actions Bot added the ci-hold CI is held; remove this label to run CI label Nov 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci-hold CI is held; remove this label to run CI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant