Skip to content

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

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

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
@saubyk

saubyk commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator

Appreciate the effort you put into the chainntnfs/btcdnotify code here, @eynhaender.

As laid out in our guidelines for new contributors, PRs from first-time contributors are not prioritized for review. Rather than leave this open indefinitely, we are going to close it for now.

If you're confident this fixes a real problem in the chainntnfs/btcdnotify code, the better route is a detailed issue: how to reproduce it, the lnd version and backend you were running, and why the current behavior is wrong. Maintainers can then decide whether a fix is warranted and may take it over or ask you to resubmit.

If you'd like to contribute going forward, reviewing open PRs and helping triage issues is the path we recommend. It's a stronger signal of understanding than a first patch, and it makes it a lot easier for us to prioritize your PRs later.

An issue describing the btcd rescan deprecation and the libbitcoin-server backend use case would be a good starting point here.

@saubyk saubyk closed this Nov 23, 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.

2 participants