Repository navigation
Conversation
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. External sites
ReviewsSee the guideline and AI policy for information on the review process.
If your review is incorrectly listed, please copy-paste ConflictsReviewers, this pull request conflicts with the following ones:
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. |
Thx for following the projects AI policy, and Concept ACK. However, I think the link to the tool can be removed and this can just say "I found this regression while reviewing the RPC with an AI-assisted tool". The tool is "a layer for agents", but I am sure any vanilla agent will find the same issue with the same prompt. E.g: LLM output |
fjahr
left a comment
There was a problem hiding this comment.
getblockfilter already guards against this state and returns an error;
What this is doing here is different from what getblockfilter does. There we don't fail unconditionally if the index isn't synced. We only fail if we the range isn't available. This makes sense because we may have the range available even if the index isn't synced to the tip yet. I don't see why we shouldn't be able to do it the same way here.
| CHECK_NONFATAL(start_index); | ||
| CHECK_NONFATAL(stop_block); | ||
|
|
||
| // Bail out if the filter index is behind the active chain, as getblockfilter |
There was a problem hiding this comment.
This is incorrect, getblockfilter does not do this:
bitcoin/src/rpc/blockchain.cpp
Line 3008 in 67efced
| "scanblocks skipped an unindexed range but still reported completed=true" | ||
| # Index had already caught up; retry to catch it mid-sync. | ||
| else: | ||
| raise AssertionError("filter index synced too quickly to exercise the unsynced path") |
There was a problem hiding this comment.
the ci fails here:
AssertionError: filter index synced too quickly to exercise the unsynced path
9467446 to
120b8e4
Compare
You're right - thanks. So, I updated to match this: |
120b8e4 to
55abc8b
Compare
done |
fjahr
left a comment
There was a problem hiding this comment.
Concept ACK on handling this but the test needs a lot of work
| node = self.nodes[0] | ||
| wallet = MiniWallet(node) | ||
|
|
||
| def bfi(): |
There was a problem hiding this comment.
This is a bit unusual but ok for me. This is always used for checking "synced" so I would put this into the function. And I would prefer a bit more expessive name like index_synced or so.
There was a problem hiding this comment.
That's already done. The helper is gone in the new version, see the test. The test does not poll for "synced" anymore: since #36618, start_node() and sync_all() already wait for the index to reach the tip. The only check of the index state that is left is a direct assert_equal on getindexinfo.
|
|
||
| self.test_scanblocks_unindexed_range() | ||
|
|
||
| def test_scanblocks_unindexed_range(self): |
There was a problem hiding this comment.
We usually add the implementation of new test cases above run_test, not below.
There was a problem hiding this comment.
Sure, both new test cases (test_unindexed_range and test_unreadable_range) are now above run_test, check it please.
| self.wait_until(lambda: bfi()["synced"]) | ||
|
|
||
| self.stop_node(0) | ||
| self.cleanup_folder(node.chain_path / "indexes" / "blockfilter") |
There was a problem hiding this comment.
This doesn't make sense, you are waiting for the index to be synced just to delete it without doing anything else.
There was a problem hiding this comment.
Agreed, that was pointless, so the new version does not delete the index at all. For the "not indexed yet" case, node2 loads an assumeutxo snapshot, so the index cannot get past the cached chain until the node has a peer. For the "cannot read" case, only the filter file is deleted and the index database is kept, so the node starts with a synced index and the lookup fails every time.
| # A large enough chain that rebuilding the wiped index after a restart | ||
| # leaves the node briefly behind the tip. Missing that window is harmless. | ||
| self.generate(node, 3000, sync_fun=self.no_op) |
There was a problem hiding this comment.
No normal functional test should require mining this many blocks. And we don't leave the actual testing to chance. The test either needs to work deterministically or it is of no use for us, with maybe some very rare exceptions.
There was a problem hiding this comment.
I agree, the new test does not depend on timing anymore, it mines 100 blocks, which is the minimum needed to build the regtest snapshot chain (its hash at height 299 is hardcoded in the chainparams). It is the same recipe as in tool_bitcoin_chainstate.py. The second case does not mine any blocks. Both cases give the same result on every run.
| continue; | ||
| } | ||
| if (!index->LookupFilterRange(start_block, end_range, filters)) { | ||
| // Report the failure like getblockfilter: still indexing vs. corruption. |
There was a problem hiding this comment.
This reference to the other RPC is unnecessary for readers to understand the code and it could get stale too easily, so it should be removed
There was a problem hiding this comment.
Yes, I removed it and the comment now only describes the condition in the line below it and does not mention the other RPC.
| self.start_node(0, extra_args=["-blockfilterindex=1"]) | ||
| tip = node.getblockcount() | ||
|
|
||
| if not bfi()["synced"]: |
There was a problem hiding this comment.
As mentioned above, skipping the actual testing under some race condition makes the test basically useless for our purposes. You should look for a more robust way of testing this or consider removing the test. It seems like the getblockfilter RPC doesn't have coverage for this. Maybe it's possible with a partial sync like we are using in feature_assumeutxo.py.
There was a problem hiding this comment.
Thanks, this was the right hint and the new test does exactly that: node2 loads the regtest snapshot while it has no peers, so its filter index stays at height 199 while the active tip is at 299. A scan to the tip fails with "still being indexed", and a scan up to height 199 succeeds. No timing is involved.
This also showed a problem in my previous version: in this state the index reports "synced", so the old check returned "index corruption" instead of "still being indexed". That is why the error is now chosen by checking if the best block of the index covers the requested range.
Side note: while testing this, I found an pre-existing bug in getblockfilter. In the same state it also returns the wrong error ("index corruption" for a block above the snapshot). It is not related to this change, so I think it belongs in a separate PR. Anyone who wants to pick it up is welcome.
|
|
||
| // Only used to classify a failed range lookup below; the requested range | ||
| // may be available even while the index is behind the tip. | ||
| const bool index_ready = index->BlockUntilSyncedToCurrentChain(); |
There was a problem hiding this comment.
This status may be outdated at the end of a long scan.
There was a problem hiding this comment.
Agreed. The flag is gone. The state of the index is now read at the moment the lookup fails, with GetSummary(), and the error is chosen by checking if the best block of the index covers the chunk that failed. So the check always uses the current state, also at the end of a long scan.
The call to BlockUntilSyncedToCurrentChain() before the loop is still there, but only so that a synced index finishes processing the blocks that were just connected.
Its return value is not used anymore.
| if (!index->LookupFilterRange(start_block, end_range, filters)) { | ||
| // Report the failure like getblockfilter: still indexing vs. corruption. | ||
| if (!index_ready) { | ||
| throw JSONRPCError(RPC_MISC_ERROR, "Block filters are still in the process of being indexed."); |
There was a problem hiding this comment.
Why throw here instead of returning a partial result?
There was a problem hiding this comment.
I thought about it and chose the error for these reasons:
-
"completed" is documented as "true if the scan was not aborted". A caller that never calls abort has no reason to check it. That's exactly why the silent gap was not noticed before. A partial result with completed=false would put the same callers in the same trap.
-
A partial result cannot say why it stopped. We would need a new field for that, and that is an API change with release notes.
-
The result would often be empty anyway. The lookup works in chunks of 10000 blocks, and in the assumeutxo case the first chunk already fails.
-
A caller that only wants the part that is already indexed can scan up to best_block_height from getindexinfo.
An error also matches what getblockfilter does for a missing filter.
If you still prefer a partial result, I can change it, but then I think it should be a separate change with its own field.
| self.log.info("once the index is synced the same scan completes and finds the match") | ||
| self.wait_until(lambda: bfi()["synced"]) | ||
| out = node.scanblocks("start", [f"addr({addr})"]) | ||
| assert_equal(out["completed"], True) | ||
| assert match_hash in out["relevant_blocks"] |
There was a problem hiding this comment.
I don't think the success case test needs to be repeated here at the end. Also you already do the same check in racy part above if the index is already synced.
There was a problem hiding this comment.
I removed it, the success case after the sync is already covered by the existing checks in run_test.
| if not bfi()["synced"]: | ||
| self.log.info("scanning to the not-yet-indexed tip errors instead of skipping silently") | ||
| try: | ||
| out = node.scanblocks("start", [f"addr({addr})"], 0, tip) |
There was a problem hiding this comment.
tip will be used by default so the variable can be removed and the call here simplified.
There was a problem hiding this comment.
Done. The variable is gone. The scan to the tip is called without heights, and the only explicit limit left is passed as stop_height=START_HEIGHT.
|
@MicSm are you still working on this? There has been pending review here for a while. |
yes, I see, working on it |
Sure |
55abc8b to
9d25298
Compare
scanblocks reads block filters over a range with LookupFilterRange. A failed lookup was ignored, so the scan skipped the affected blocks and still returned "completed": true, hiding the gap from the caller. Report the failure instead. It is "still being indexed" unless the synced index already covers the range, in which case it indicates index corruption. A scan over a range that is already indexed keeps working while the index is behind the tip. Add functional test coverage for both errors: a scan past the height the index can reach during assumeutxo background validation, and a scan over filters whose file has been deleted.
9d25298 to
64eca3d
Compare
fjahr
left a comment
There was a problem hiding this comment.
Code review ACK 64eca3d
The RPC code is fine, some comments in the tests aren't ideal IMO but I wouldn't consider it striclty a blocker.
The CI failure seems unrelated, would be good to rerun it so other reviewers are not turned away.
|
|
||
| # Let node2 download the missing blocks and index them | ||
| self.connect_nodes(0, 2) | ||
| self.sync_all() |
There was a problem hiding this comment.
The sync_all doesn't seem necessary, the only reason this is needed at all is because the later mining ops in the test do sync_all and would fail if the nodes are not connected. But here this just blocks the test briefly for no reason. The comment could be updated to reflect the reasoning too.
| ) | ||
|
|
||
| START_HEIGHT = 199 | ||
| # Hardcoded in regtest chainparams |
There was a problem hiding this comment.
nit: That's always the case, the comment doesn't seem necessary to spell out
| """Keep node2 disconnected, so that it cannot download the blocks above | ||
| the cached chain before it has loaded a snapshot.""" |
There was a problem hiding this comment.
I would prefer if in setup_network we don't leak reasoning for doing something in a specific test. A simple "Keep node2 disconnected" above the connect_nodes call seem enough here. Such comments are hard to maintain otherwise.
There was a problem hiding this comment.
I wonder if this should come with a test at all? It seems there are already a few nits/confusions and this will be hard to maintain in the future, given the complexity and the coupling with assumeutxo just to test this race.
It should be sufficient to drop this test somewhere in a hidden comment, so that reviewers can use it if they want, but there is no further maintenance cost in case down the line?
There was a problem hiding this comment.
and the coupling with assumeutxo just to test this race.
I also think that if something like #36598 is gathering conceptual agreement, we shouldn't rush to introduce more assumeutxo-related code.

The issue
scanblocksreads block filters over a range viaLookupFilterRange. When theblock filter index is behind the active chain (e.g. right after startup, while
it is still syncing in the background), the lookup for the not yet indexed range
fails. The old code ignored that failure, advanced
start_indexto the end ofthe chunk, and kept going - so the scan skipped every unindexed block and still
returned
"completed": true.The result is a silent gap: a caller gets a
relevant_blockslist that looksfine but is missing any match in the unindexed range, with no way to
distinguish "no matches" from "range was never scanned".
Note that
"completed"only means the scan was not aborted - it does not meanevery filter in the requested range was actually read.
Steps to reproduce
-blockfilterindex=1and let it mine/receive some blocks.<datadir>/<chain>/indexes/blockfilter, restart with-blockfilterindex=1.scanblocks start '["addr(<addr>)"]'.Before this change the call returns
"completed": truewith an empty/partialrelevant_blocks, silently skipping the range the index had not rebuilt yet.How it is fixed and why
Mirror
getblockfilter's error classification:BlockUntilSyncedToCurrentChain()only to obtain anindex_readyflag.Do not fail unconditionally when the index is still behind the tip - the
requested range may already be available.
LookupFilterRangefails and the index is not ready, throw the same"still in the process of being indexed"
RPC_MISC_ERRORthatgetblockfilteruses.LookupFilterRangefails after the index is ready, throwRPC_INTERNAL_ERROR(unexpected / corruption), instead of skipping the chunk.This closes the silent gap without rejecting scans whose range is already
covered while the index catches up to the tip.
The diff also reindents the match loop: inverting
if (LookupFilterRange(...))into an early
throwremoves one nesting level from the existing body. Thatreindentation is a consequence of the bug fix, not a standalone style change.
Tests
test/functional/rpc_scanblocks.py- newtest_scanblocks_unindexed_rangecovers the modified code: it pads the chain, wipes the filter index, restarts,
and while the index is behind the tip asserts that a scan to the tip returns
the indexing error, that a scan over an already-written prefix (genesis)
still succeeds, and that after sync the match is found.
run_testcases (whichwait_until(... synced ...)beforescanning) continue to exercise the normal synced path.
AI / tooling note
I found this regression while reviewing the RPC with an AI-assisted tool.
I reproduced the unsynced-index behavior myself, chose to mirror
getblockfilter's existing guard, and verified the fix with thefunctional test above. This PR text and any review replies are my own.