rpc: fail scanblocks instead of silently skipping missing filters #37016

Open
shivansh023023 wants to merge 1 commit from fix/scanblocks-lookup-error into master AGit
shivansh023023 commented 2026-11-30 05:44:57 +00:00 (Migrated from github.com)

Problem

In scanblocks (src/rpc/blockchain.cpp), when BlockFilterIndex::LookupFilterRange returns false, the chunk loop silently skips the failed range, advances start_index, and finishes with "completed": true and to_height set to stop_height.

This causes silent false negatives in two realistic scenarios:

  1. Unsynced index: A user enables -blockfilterindex=1 on an existing node and runs scanblocks start while the index is still catching up. Unindexed chunks are dropped without error, returning an empty or partial relevant_blocks list marked "completed": true.
  2. Missing/corrupt filter files: If a fltr*.dat file is missing or unreadable, scanblocks hides the read failure instead of reporting it (unlike getblockfilter, which throws an error).

Because scanblocks is used to discover wallet history from descriptors, silently returning "completed": true on missing filters can mislead callers into concluding no activity exists.

Solution

Mirror getblockfilter's error-handling pattern in scanblocks:

  • Call index->BlockUntilSyncedToCurrentChain() before acquiring cs_main (preserving the ability to scan already-indexed historical ranges while the index is still catching up).
  • In the chunk loop, if index->LookupFilterRange(...) returns false:
    • Throw RPC_MISC_ERROR ("Block filters are still in the process of being indexed.") if the index is not yet synced.
    • Throw RPC_INTERNAL_ERROR indicating a filter read failure / index corruption otherwise.

BlockFiltersScanReserver is already RAII-managed, so throwing out of the loop cleanly releases the active scan reservation.

Tests

  • Unit test (src/test/rpc_tests.cpp — scanblocks_unsynced_index): Registers an unstarted BlockFilterIndex, verifies scanblocks start throws RPC_MISC_ERROR with the expected message, confirms a second call is not blocked by a stale scan reservation, and checks scanblocks status returns null.
  • Functional test (test/functional/rpc_scanblocks.py): Stops the node, moves fltr00000.dat, restarts, verifies scanblocks start fails with -32603 (RPC_INTERNAL_ERROR), verifies scanblocks status returns None, then restores the file and confirms the scan succeeds again.
  • Both tests fail on master without the src/rpc/blockchain.cpp change and pass with the fix.
### Problem In `scanblocks` (`src/rpc/blockchain.cpp`), when `BlockFilterIndex::LookupFilterRange` returns `false`, the chunk loop silently skips the failed range, advances `start_index`, and finishes with `"completed": true` and `to_height` set to `stop_height`. This causes silent false negatives in two realistic scenarios: 1. **Unsynced index:** A user enables `-blockfilterindex=1` on an existing node and runs `scanblocks start` while the index is still catching up. Unindexed chunks are dropped without error, returning an empty or partial `relevant_blocks` list marked `"completed": true`. 2. **Missing/corrupt filter files:** If a `fltr*.dat` file is missing or unreadable, `scanblocks` hides the read failure instead of reporting it (unlike `getblockfilter`, which throws an error). Because `scanblocks` is used to discover wallet history from descriptors, silently returning `"completed": true` on missing filters can mislead callers into concluding no activity exists. ### Solution Mirror `getblockfilter`'s error-handling pattern in `scanblocks`: * Call `index->BlockUntilSyncedToCurrentChain()` before acquiring `cs_main` (preserving the ability to scan already-indexed historical ranges while the index is still catching up). * In the chunk loop, if `index->LookupFilterRange(...)` returns `false`: * Throw `RPC_MISC_ERROR` (`"Block filters are still in the process of being indexed."`) if the index is not yet synced. * Throw `RPC_INTERNAL_ERROR` indicating a filter read failure / index corruption otherwise. `BlockFiltersScanReserver` is already RAII-managed, so throwing out of the loop cleanly releases the active scan reservation. ### Tests * **Unit test (`src/test/rpc_tests.cpp` — `scanblocks_unsynced_index`):** Registers an unstarted `BlockFilterIndex`, verifies `scanblocks start` throws `RPC_MISC_ERROR` with the expected message, confirms a second call is not blocked by a stale scan reservation, and checks `scanblocks status` returns `null`. * **Functional test (`test/functional/rpc_scanblocks.py`):** Stops the node, moves `fltr00000.dat`, restarts, verifies `scanblocks start` fails with `-32603` (`RPC_INTERNAL_ERROR`), verifies `scanblocks status` returns `None`, then restores the file and confirms the scan succeeds again. * Both tests fail on `master` without the `src/rpc/blockchain.cpp` change and pass with the fix.
DrahtBot commented 2026-11-30 05:45:08 +00:00 (Migrated from github.com)

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

External sites

Reviews

See the guideline and AI policy for information on the review process.
A summary of reviews will appear here.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #36986 (rpc, wallet: Convert walletprocesspsbt to use options parameter by jeanpablojp)
  • #35837 (rpc: fail scanblocks when block filter range is unavailable by MicSm)
  • #35713 (Remove boost as a unit test runner by rustaceanrob)
  • #35229 (refactor: Use CBlockIndex parameters as reference by optout21)
  • #33741 (rpc: Optionally print feerates in sat/vb by polespinasa)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

LLM Linter (✨ experimental)

Possible places where named args for integral literals may be used (e.g. func(x, /*named_arg=*/0) in C++, and func(x, named_arg=0) in Python):

  • src/test/rpc_tests.cpp: MakeIndex<BlockFilterIndex>(interfaces::MakeChain(m_node), BlockFilterType::BASIC, 1_MiB, /*f_memory=*/true)

2026-11-30 05:45:20

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/37016 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/37016.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/37016#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) for information on the review process. A summary of reviews will appear here. <!--174a7506f384e20aa4161008e828411d--> ### Conflicts Reviewers, this pull request conflicts with the following ones: * [#36986](https://github.com/bitcoin/bitcoin/pull/36986) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36986.svg"></sub> (rpc, wallet: Convert walletprocesspsbt to use options parameter by jeanpablojp) * [#35837](https://github.com/bitcoin/bitcoin/pull/35837) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/35837.svg"></sub> (rpc: fail scanblocks when block filter range is unavailable by MicSm) * [#35713](https://github.com/bitcoin/bitcoin/pull/35713) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/35713.svg"></sub> (Remove boost as a unit test runner by rustaceanrob) * [#35229](https://github.com/bitcoin/bitcoin/pull/35229) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/35229.svg"></sub> (refactor: Use CBlockIndex parameters as reference by optout21) * [#33741](https://github.com/bitcoin/bitcoin/pull/33741) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/33741.svg"></sub> (rpc: Optionally print feerates in sat/vb by polespinasa) If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. <!--5faf32d7da4f0f540f40219e4f7537a3--> ### LLM Linter (✨ experimental) Possible places where named args for integral literals may be used (e.g. `func(x, /*named_arg=*/0)` in C++, and `func(x, named_arg=0)` in Python): - `src/test/rpc_tests.cpp`: `MakeIndex<BlockFilterIndex>(interfaces::MakeChain(m_node), BlockFilterType::BASIC, 1_MiB, /*f_memory=*/true)` <sup>2026-11-30 05:45:20</sup>
First-time contributor

🟠 Minor · A new tip can make scanblocks report a healthy index as corrupt

A block arriving between the readiness check and tip selection can make scanblocks misreport indexing lag as corruption; production occurrence of this narrow race is unconfirmed.

Location: src/rpc/blockchain.cpp:2656 (head).

🟠 Minor · scanblocks clears abort requests received during the new synchronization wait

The new queue wait widens a pre-existing cancellation race: scanblocks abort can return true during the wait, then have its flag cleared before scanning; production occurrence is unconfirmed.

Location: src/rpc/blockchain.cpp:2656 (head).

🔵 Suggestion · Catch-up coverage omits successful historical scans

The new tests would also accept rejecting every unsynced scan, leaving the intended ability to scan already-indexed historical ranges during catch-up unprotected.

Location: src/test/rpc_tests.cpp:685 (head).

🔵 Suggestion · The new scanblocks errors lack a release note

scanblocks now returns RPC_MISC_ERROR or RPC_INTERNAL_ERROR for unavailable filters instead of a result marked completed=true, but no release note explains this caller-visible change.

Location: src/rpc/blockchain.cpp:2717 (head).

Review coverage was incomplete.


Reviewed: 7cfd27f4c6fb · Review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:312ddb2095238aa2ca395a9cc39b05ab5a38a7177afbdb0d69098df9c3e0bef9:9a7f127b15c6116c267953d38288a9f17ec7c7bcd3a975fb71a2db8bfd974dbf --> <!-- ralph-base:afedb19cca4c3b40d89514fb0c66b0c20e4f5d62 --> <!-- ralph-head:7cfd27f4c6fbb314aa1a0b04b7a75a32a088d112 --> <!-- ralph-case:9e3c3bfc38abd941b7d5b877f6af527200fa2a0e6ce5e3140c2c0ae9e2715b06 --> **🟠 Minor · A new tip can make `scanblocks` report a healthy index as corrupt** A block arriving between the readiness check and tip selection can make `scanblocks` misreport indexing lag as corruption; production occurrence of this narrow race is unconfirmed. Location: [`src/rpc/blockchain.cpp:2656`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/7cfd27f4c6fbb314aa1a0b04b7a75a32a088d112/src/rpc/blockchain.cpp#L2656>) (head). **🟠 Minor · `scanblocks` clears abort requests received during the new synchronization wait** The new queue wait widens a pre-existing cancellation race: `scanblocks abort` can return `true` during the wait, then have its flag cleared before scanning; production occurrence is unconfirmed. Location: [`src/rpc/blockchain.cpp:2656`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/7cfd27f4c6fbb314aa1a0b04b7a75a32a088d112/src/rpc/blockchain.cpp#L2656>) (head). **🔵 Suggestion · Catch-up coverage omits successful historical scans** The new tests would also accept rejecting every unsynced scan, leaving the intended ability to scan already-indexed historical ranges during catch-up unprotected. Location: [`src/test/rpc_tests.cpp:685`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/7cfd27f4c6fbb314aa1a0b04b7a75a32a088d112/src/test/rpc_tests.cpp#L685>) (head). **🔵 Suggestion · The new `scanblocks` errors lack a release note** `scanblocks` now returns `RPC_MISC_ERROR` or `RPC_INTERNAL_ERROR` for unavailable filters instead of a result marked `completed=true`, but no release note explains this caller-visible change. Location: [`src/rpc/blockchain.cpp:2717`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/7cfd27f4c6fbb314aa1a0b04b7a75a32a088d112/src/rpc/blockchain.cpp#L2717>) (head). Review coverage was incomplete. <!-- Review tier: standard --> --- Reviewed: [`7cfd27f4c6fb`](<https://git.fish.foo/bitcoin/bitcoin/commit/7cfd27f4c6fbb314aa1a0b04b7a75a32a088d112>) · [Review report](<https://review.fish.foo/report/37016-7cfd27f4c6fbb314aa1a0b04b7a75a32a088d112-3316-1.html>) · [Previous reports](<https://review.fish.foo/traces/37016/>) [permalink](<https://review.fish.foo/report/37016-7cfd27f4c6fbb314aa1a0b04b7a75a32a088d112-3316-1.html>)
pinheadmz commented 2026-11-30 11:21:06 +00:00 (Migrated from github.com)

Thanks for your submission! The description and code changes match a pattern we sometimes see with PRs that lean heavily on LLM coding assistants. Because the project has such limited human resources for code review, we require code generated by LLMs to be reviewed by the author who submits them first.

It would help to know why you decided to open this PR (did you encounter a bug? find an open issue in repo?), how you reviewed the generated code, what you personally understand about it, how you tested the code before and after the change, etc.

If you don't understand the changes you submitted, we should close this PR. There are many other more helpful ways to contribute to the project, and we look forward to working with you there!

Please also reconsider the value of the PR given how limited our resources are for code review.

Thanks for your submission! The description and code changes match a pattern we sometimes see with PRs that lean heavily on LLM coding assistants. Because the project has such limited human resources for code review, we require code generated by LLMs to be *reviewed by the author who submits them first*. It would help to know why you decided to open this PR (did you encounter a bug? find an open issue in repo?), how you reviewed the generated code, what you personally understand about it, how you tested the code before and after the change, etc. If you don't understand the changes you submitted, we should close this PR. There are many other more helpful ways to contribute to the project, and we look forward to working with you there! Please also reconsider the value of the PR given how limited our resources are for code review.
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!37016
No description provided.