validation: fix false corruption errors after pruning or AssumeUTXO #36452

Open
l0rinc wants to merge 4 commits from l0rinc/verify-pruned-undo into master AGit
l0rinc commented 2026-10-07 12:37:28 +00:00 (Migrated from github.com)

Problem: getblockfrompeer restores a pruned block's body without recreating its undo record. If startup verification reaches that block at level >2, it attempts to disconnect it and reports database corruption, preventing the node from restarting.

With ordinary pruning, reaching the restored block requires a deeper check than the default six blocks. The AssumeUTXO case can occur with the default checks.

Fix: Stop deep verification when a pruned node reaches missing undo data, using the existing result for unavailable history. The same allowance covers missing undo on AssumeUTXO snapshot chainstates, matching the existing treatment of unavailable snapshot history. Level 4 still reconnects the newer blocks that were disconnected. verifychain returns false to indicate incomplete verification, including when undo is missing at the snapshot base. Lower verification levels and corruption checks for undo records marked as present retain their existing behavior.

An AssumeUTXO restart can still hit a separate block-index candidate assertion after verification.

This came up during pruned txindex review. The pending pruneassumevalid change also needs verification to handle missing undo correctly, so this fix is being split out to land first.

**Problem:** `getblockfrompeer` restores a pruned block's body without recreating its undo record. If startup verification reaches that block at level >2, it attempts to disconnect it and reports database corruption, preventing the node from restarting. With ordinary pruning, reaching the restored block requires a deeper check than the default six blocks. The AssumeUTXO case can occur with the default checks. **Fix:** Stop deep verification when a pruned node reaches missing undo data, using the existing result for unavailable history. The same allowance covers missing undo on AssumeUTXO snapshot chainstates, matching the existing treatment of unavailable snapshot history. Level 4 still reconnects the newer blocks that were disconnected. `verifychain` returns `false` to indicate incomplete verification, including when undo is missing at the snapshot base. Lower verification levels and corruption checks for undo records marked as present retain their existing behavior. An AssumeUTXO restart can still hit a [separate block-index candidate assertion](https://github.com/bitcoin/bitcoin/pull/36452#issuecomment-6464524112) after verification. This came up during [pruned `txindex` review](https://github.com/bitcoin/bitcoin/pull/36002). The pending `pruneassumevalid` change also needs verification to handle missing undo correctly, so this fix is being split out to land first.
DrahtBot commented 2026-10-07 12:37:37 +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

No conflicts as of last run.

<!--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/36452 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36452.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36452#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 No conflicts as of last run. <!--5faf32d7da4f0f540f40219e4f7537a3-->
First-time contributor

🟠 Minor · Missing undo stops lower-level checks after cache exhaustion

When cache pressure has already disabled level-3 disconnects, the new missing-undo stop also omits lower-level checks of older available bodies, although no concrete deployment exhibiting this combination was identified.

Location: src/validation.cpp:4775 (head).

🔵 Suggestion · Snapshot undo allowance extends above the snapshot base

On an unpruned snapshot chainstate, local index damage clearing BLOCK_HAVE_UNDO above the base can now bypass startup corruption detection when deep verification reaches that block with sufficient cache.

Location: src/validation.cpp:4728 (head).

🔵 Suggestion · Level-4 assertions miss an early missing-data return

The new level-4 test would accept a premature SKIPPED_MISSING_DATA return that skips reconnection, and its RPC assertion cannot distinguish a reconnect error from expected incompleteness.

Location: test/functional/rpc_getblockfrompeer.py:167 (head).

🔵 Suggestion · Deep-verification tests do not pin boundary-body validation

The new valid-body fixture would still pass if the level-3/4 missing-undo stop moved ahead of ReadBlock and CheckBlock, bypassing validation of the restored boundary body.

Location: test/functional/rpc_getblockfrompeer.py:165 (head).

Review coverage was incomplete.


Reviewed: f685225d6857 · Review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:2b20819f882b885f79f242c01722f54f2a55e27dcfa7d5e13701b4a0b5214248:c39fdacfa3b84799844a5e841c04997e3cd1142653dfad8e36dbb07c4b1e684b --> <!-- ralph-base:95dcec43b864e80eeeaf4f7e0b623a5c7a866bb5 --> <!-- ralph-head:f685225d6857e55c34ac9e8a139210e3b06f38a1 --> <!-- ralph-case:082637c0104d56888685a39bc99870aa6aae7187ca0f75d1cc3c9dbe7eb49199 --> **🟠 Minor · Missing undo stops lower-level checks after cache exhaustion** When cache pressure has already disabled level-3 disconnects, the new missing-undo stop also omits lower-level checks of older available bodies, although no concrete deployment exhibiting this combination was identified. Location: [`src/validation.cpp:4775`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/f685225d6857e55c34ac9e8a139210e3b06f38a1/src/validation.cpp#L4775>) (head). **🔵 Suggestion · Snapshot undo allowance extends above the snapshot base** On an unpruned snapshot chainstate, local index damage clearing `BLOCK_HAVE_UNDO` above the base can now bypass startup corruption detection when deep verification reaches that block with sufficient cache. Location: [`src/validation.cpp:4728`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/f685225d6857e55c34ac9e8a139210e3b06f38a1/src/validation.cpp#L4728>) (head). **🔵 Suggestion · Level-4 assertions miss an early missing-data return** The new level-4 test would accept a premature `SKIPPED_MISSING_DATA` return that skips reconnection, and its RPC assertion cannot distinguish a reconnect error from expected incompleteness. Location: [`test/functional/rpc_getblockfrompeer.py:167`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/f685225d6857e55c34ac9e8a139210e3b06f38a1/test/functional/rpc_getblockfrompeer.py#L167>) (head). **🔵 Suggestion · Deep-verification tests do not pin boundary-body validation** The new valid-body fixture would still pass if the level-3/4 missing-undo stop moved ahead of `ReadBlock` and `CheckBlock`, bypassing validation of the restored boundary body. Location: [`test/functional/rpc_getblockfrompeer.py:165`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/f685225d6857e55c34ac9e8a139210e3b06f38a1/test/functional/rpc_getblockfrompeer.py#L165>) (head). Review coverage was incomplete. <!-- Review tier: sensitive --> --- Reviewed: [`f685225d6857`](<https://git.fish.foo/bitcoin/bitcoin/commit/f685225d6857e55c34ac9e8a139210e3b06f38a1>) · [Review report](<https://review.fish.foo/report/36452-f685225d6857e55c34ac9e8a139210e3b06f38a1-2798-7.html>) · [Previous reports](<https://review.fish.foo/traces/36452/>) [permalink](<https://review.fish.foo/report/36452-f685225d6857e55c34ac9e8a139210e3b06f38a1-2798-7.html>)
andrewtoth commented 2026-10-07 18:12:07 +00:00 (Migrated from github.com)

IIUC this bug can't realistically be triggered with default checkblocks=6 config.
This would require a checkblocks value of > 288 (or 0 to go all the way back) and the user would have to call getblockfrompeer for the block at the exact prune boundary. If a single fully-pruned block sits between the last non-pruned block and the fetched block with no undo, then this bug will not trigger.

IIUC this bug can't realistically be triggered with default `checkblocks=6` config. This would require a `checkblocks` value of > 288 (or 0 to go all the way back) and the user would have to call `getblockfrompeer` for the block at the exact prune boundary. If a single fully-pruned block sits between the last non-pruned block and the fetched block with no undo, then this bug will not trigger.
l0rinc commented 2026-10-08 06:41:45 +00:00 (Migrated from github.com)

@andrewtoth, that’s right for ordinary pruning, but this can also happen with default checks under AssumeUTXO before background validation reaches the requested block, so I’ve added a small test for that and expanded the verifychain test coverage following Ralph AI’s suggestions - hope it's clearer now.

This change is also a prerequisite for the pruneassumevalid change I’m currently working on, which won’t retain the 288-block reorg buffer in the assumevalid region during IBD since we don’t expect reorgs there after headers-first sync.

@andrewtoth, that’s right for ordinary pruning, but this can also happen with default checks under AssumeUTXO before background validation reaches the requested block, so I’ve added a small test for that and expanded the `verifychain` test coverage following Ralph AI’s suggestions - hope it's clearer now. This change is also a prerequisite for the `pruneassumevalid` change I’m currently working on, which won’t retain the 288-block reorg buffer in the assumevalid region during IBD since we don’t expect reorgs there after headers-first sync.
cyb3ralbert commented 2026-10-28 08:41:23 +00:00 (Migrated from github.com)

Question first: when the snapshot base block's body is present but has no undo data (the state the new feature_assumeutxo.py case creates; it arrives the same way via submitblock or as an unsolicited block message from a peer), a restart of that node with default -checkblocks/-checklevel on e262a1fbf7 now passes VerifyDB (Block verification stopping at height 299 (no undo data).) and then aborts at startup: validation.cpp:3144: CBlockIndex* Chainstate::FindMostWorkChain(): Assertion `pindexTest->HaveNumChainTxs() || pindexTest->nHeight == 0' failed. Is the AssumeUTXO part of this PR meant to make startup verification stop erroring on such a node, with the restart itself left to #35307? A line in the description would make that explicit.

The abort is not introduced here: the merge-base with -checklevel=1 (so VerifyDB does not fail first) hits the same assertion on the same line, and so does a snapshot tip above the base (base + 6 blocks, base body delivered first) on both trees; without the base body the restart works on both. With #35307 merged the same restarts succeed, and applying only its PopulateBlockIndexCandidates hunk on top of this head is enough.

Related: #35307 adds if (is_snapshot_cs && pindex == chainstate.SnapshotBase()) break; in the same nCheckLevel >= 3 block, and this PR removes is_snapshot_cs. The two conflict in validation.cpp, and keeping #35307's hunk as is does not compile ('is_snapshot_cs' was not declared in this scope). Its break does not set skipped_missing_data, so the order of the two checks decides what verifychain() returns at the snapshot base: with this PR's check first the new feature_assumeutxo.py assertion passes, with #35307's first it fails (not(True == False)). Which value is intended once both land?

Tested on Linux aarch64 (Debian 13), GCC 14.2.0, Release, e262a1fbf7 (merges cleanly with master de96f3ae6d). feature_assumeutxo.py and rpc_getblockfrompeer.py from this PR fail on the merge-base cce32b1687 (Block verification stopping at height 299 / 752 (no undo data). not found) and pass on the head. On a pruned regtest node with the highest pruned block (249) refetched via getblockfrompeer, a restart with -checkblocks=0 fails at -checklevel=3 and 4 on the merge-base (irrecoverable inconsistency in block data at 249) and starts on the head (checked last 551 blocks); level 2 and default checks start on both. verifychain at levels 3 and 4 returns false both before and after. A flipped last byte of the newest rev*.dat is still rejected at levels 2 and 3 on both trees (found bad undo data at 800). The neighbouring prune, reindex and assumeutxo functional tests (10, incl. feature_pruning.py and wallet_assumeutxo.py) pass on the head. With #35307 merged (conflict resolved by keeping both checks), feature_assumeutxo.py, rpc_getblockfrompeer.py and wallet_assumeutxo.py pass.

Reading the code: the new check sits after the level 0-2 reads; it tests the BLOCK_HAVE_UNDO bit directly, while the level-2 check reads the undo position via GetUndoPos(). PruneOneBlockFile clears the bit and zeroes the position together. The code comment notes that a block refetched after pruning has its body but not its undo data. Nit: SKIPPED_MISSING_BLOCKS now also covers a missing undo record; the local flag was renamed, the enum value was not.

Question first: when the snapshot base block's body is present but has no undo data (the state the new `feature_assumeutxo.py` case creates; it arrives the same way via `submitblock` or as an unsolicited `block` message from a peer), a restart of that node with default `-checkblocks`/`-checklevel` on e262a1fbf7 now passes `VerifyDB` (`Block verification stopping at height 299 (no undo data).`) and then aborts at startup: ``validation.cpp:3144: CBlockIndex* Chainstate::FindMostWorkChain(): Assertion `pindexTest->HaveNumChainTxs() || pindexTest->nHeight == 0' failed.`` Is the AssumeUTXO part of this PR meant to make startup verification stop erroring on such a node, with the restart itself left to #35307? A line in the description would make that explicit. The abort is not introduced here: the merge-base with `-checklevel=1` (so `VerifyDB` does not fail first) hits the same assertion on the same line, and so does a snapshot tip above the base (base + 6 blocks, base body delivered first) on both trees; without the base body the restart works on both. With #35307 merged the same restarts succeed, and applying only its `PopulateBlockIndexCandidates` hunk on top of this head is enough. Related: #35307 adds `if (is_snapshot_cs && pindex == chainstate.SnapshotBase()) break;` in the same `nCheckLevel >= 3` block, and this PR removes `is_snapshot_cs`. The two conflict in `validation.cpp`, and keeping #35307's hunk as is does not compile (`'is_snapshot_cs' was not declared in this scope`). Its `break` does not set `skipped_missing_data`, so the order of the two checks decides what `verifychain()` returns at the snapshot base: with this PR's check first the new `feature_assumeutxo.py` assertion passes, with #35307's first it fails (`not(True == False)`). Which value is intended once both land? Tested on Linux aarch64 (Debian 13), GCC 14.2.0, Release, e262a1fbf7 (merges cleanly with master de96f3ae6d). `feature_assumeutxo.py` and `rpc_getblockfrompeer.py` from this PR fail on the merge-base `cce32b1687` (`Block verification stopping at height 299 / 752 (no undo data).` not found) and pass on the head. On a pruned regtest node with the highest pruned block (249) refetched via `getblockfrompeer`, a restart with `-checkblocks=0` fails at `-checklevel=3` and `4` on the merge-base (`irrecoverable inconsistency in block data at 249`) and starts on the head (`checked last 551 blocks`); level 2 and default checks start on both. `verifychain` at levels 3 and 4 returns false both before and after. A flipped last byte of the newest `rev*.dat` is still rejected at levels 2 and 3 on both trees (`found bad undo data at 800`). The neighbouring prune, reindex and assumeutxo functional tests (10, incl. `feature_pruning.py` and `wallet_assumeutxo.py`) pass on the head. With #35307 merged (conflict resolved by keeping both checks), `feature_assumeutxo.py`, `rpc_getblockfrompeer.py` and `wallet_assumeutxo.py` pass. Reading the code: the new check sits after the level 0-2 reads; it tests the `BLOCK_HAVE_UNDO` bit directly, while the level-2 check reads the undo position via `GetUndoPos()`. `PruneOneBlockFile` clears the bit and zeroes the position together. The code comment notes that a block refetched after pruning has its body but not its undo data. Nit: `SKIPPED_MISSING_BLOCKS` now also covers a missing undo record; the local flag was renamed, the enum value was not.
l0rinc commented 2026-11-18 14:55:14 +00:00 (Migrated from github.com)

Rebased, renamed SKIPPED_MISSING_BLOCKS to SKIPPED_MISSING_DATA to cover missing undo, and clarified that default checks can reach this case under AssumeUTXO and that a separate startup assertion can still occur.

Rebased, renamed `SKIPPED_MISSING_BLOCKS` to `SKIPPED_MISSING_DATA` to cover missing undo, and clarified that default checks can reach this case under AssumeUTXO and that a separate startup assertion can still occur.
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!36452
No description provided.