validation: fix false corruption errors after pruning or AssumeUTXO

bitcoin/bitcoin: PR #36452

1 minor finding · 3 suggestions

Findings

Link

[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).

Details and suggested fix

During level-3/4 verification on a pruned or snapshot chainstate, cache exhaustion can set skipped_l3_checks before the loop reaches a restored block lacking undo. If older bodies are also available, for example after consecutive getblockfrompeer refetches, the new BLOCK_HAVE_UNDO check stops the scan even though no further DisconnectBlock calls would run. Previously the loop continued reading and checking those older bodies and any available undo, so corruption detected by those checks can now go unreported. Move the missing-undo check inside curr_coins_usage <= chainstate.m_coinstip_cache_size_bytes, immediately before the disconnect. This retains the required stop whenever disconnecting needs undo and leaves level-4 reconnection unchanged. This does not make incomplete verifychain checks return true; explicit startup verification already fails on SKIPPED_L3_CHECKS.


Optional improvements

Link

[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).

Details and suggested fix

Blocks above the snapshot base were connected normally, and ConnectBlock writes undo before advancing the coins view. With the body present but BLOCK_HAVE_UNDO cleared, level 2 skips the undo read and the new snapshot-wide guard permits SKIPPED_MISSING_DATA, which startup accepts. The base revision instead reached DisconnectBlock and reported corruption. Limit the snapshot part of the undo allowance to SnapshotBase() && pindex->nHeight <= SnapshotBase()->nHeight, while retaining IsPruneMode() for pruned refetches. This preserves the snapshot-base case and prior corruption detection above it. The existing missing-body allowance is equally broad, but matching it here extends the loss of detection to undo that should exist; a later reorg needing to disconnect the affected block still fails.


Link

[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).

Details and suggested fix

The fixture restores an interior block below a nonempty available suffix, but checks only the stopping message, false, restart success and persistent tip/body data. VerifyDB uses a temporary CCoinsViewCache, so the persistent-state assertions cannot establish reconnection. The RPC maps both CORRUPTED_BLOCK_DB and SKIPPED_MISSING_DATA to false, while startup accepts the latter. A reconnect error shared by both paths would fail the restart, but an RPC-only error would not: the RPC uses CoinsTip, whereas startup uses CoinsDB.

In both existing log contexts, also expect Verification: checked last {check_depth - 1} blocks at level 4 and pass unexpected_msgs=["Verification error"]. The completion message follows the reconnect phase and would catch the premature return and reconnect-error cases described here.


Link

[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).

Details and suggested fix

The level-2 assertion does not exercise a guard gated on nCheckLevel >= 3. At level 4, the refetched body is valid, so stopping before reading it produces the same stopping message, false result and successful restart. Add a focused prune-mode CVerifyDB test with BLOCK_HAVE_DATA set and BLOCK_HAVE_UNDO cleared on an interior block. Assert SKIPPED_MISSING_DATA for a valid body and CORRUPTED_BLOCK_DB for an unreadable body or one that fails CheckBlock. Checking the enum directly distinguishes skipped verification from corruption, which verifychain maps to the same boolean.


Coverage details

Background

Ralph's summary of historical context for the behaviour affected by this change. The historical record reviewed here is incomplete.

Documented decisions

Incomplete historical verification was deliberately made distinguishable from successful verification without making it a startup corruption failure.

The February 2023 change explicitly says verifychain should return false when pruning prevents the requested checks from finishing. Its patch introduces SKIPPED_MISSING_BLOCKS and accepts that result during chainstate loading. The present rename and extension therefore operate on an existing separation between permission to start and a claim that the requested verification completed.

Source 1