Skip to content

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

Open
l0rinc wants to merge 4 commits into
bitcoin:masterfrom
l0rinc:l0rinc/verify-pruned-undo
Open

l0rinc wants to merge 4 commits into
bitcoin:masterfrom
l0rinc:l0rinc/verify-pruned-undo

Conversation

@l0rinc

@l0rinc l0rinc commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

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.

@DrahtBot

DrahtBot commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

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.

@andrewtoth

Copy link
Copy Markdown
Contributor

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
l0rinc force-pushed the l0rinc/verify-pruned-undo branch from 86904e6 to 1639200 Compare October 8, 2026 06:38
@l0rinc

l0rinc commented Oct 8, 2026

Copy link
Copy Markdown
Contributor Author

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

@l0rinc l0rinc changed the title validation: fix false corruption errors after refetching pruned blocks validation: fix false corruption errors after pruning or AssumeUTXO Oct 15, 2026
@l0rinc
l0rinc force-pushed the l0rinc/verify-pruned-undo branch from 1639200 to e262a1f Compare October 15, 2026 11:42
@cyb3ralbert

Copy link
Copy Markdown
Contributor

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 e262a1f 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, e262a1f (merges cleanly with master de96f3a). 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.

sedited pushed a commit to sedited/bitcoin that referenced this pull request Nov 9, 2026
The block index was already driven by a small set of functions that
write nStatus and the derived sets (AddToBlockIndex,
ReceivedBlockTransactions, InvalidBlockFound, InvalidateBlock,
SetBlockFailureFlags, ResetBlockFailureFlags, PruneOneBlockFile,
TryAddBlockIndexCandidate, PruneBlockIndexCandidates, OnUndoWritten),
with CheckBlockIndex as the invariant checker and the block_index_tree
fuzz target driving event sequences against it. Two writers were still
inline and move into named transitions:

- the validity raise after a block is connected becomes
  ChainstateManager::MarkBlockScriptsValid;
- the removal of an unusable candidate chain inside FindMostWorkChain
  becomes Chainstate::DropUnusableCandidateChain.

CBlockIndex gains a typed state, BlockIndexState, derived from nStatus
(which stays the stored representation, so nothing on disk changes),
and HaveUndo() for the orthogonal undo bit. CheckBlockIndex checks that
the typed state agrees with the flags, that HaveUndo implies a valid
undo position and a connected block, in addition to its existing
invariants.

Unit tests cover the state mapping and the sequence behind bitcoin#36149 (a
block in m_blocks_unlinked whose file is pruned must leave the set),
which the checker's "no data implies not unlinked" invariant also
guards. The other known bug classes of the plan are covered by
invariants the checker already has: bitcoin#36702 by the snapshot-base
m_chain_tx_count checks, bitcoin#36452 by CheckBlockDataAvailability and the
HAVE_DATA/nTx relations, bitcoin#34254 by the failed-flag propagation checks.
sedited pushed a commit to sedited/bitcoin that referenced this pull request Nov 9, 2026
The block index was already driven by a small set of functions that
write nStatus and the derived sets (AddToBlockIndex,
ReceivedBlockTransactions, InvalidBlockFound, InvalidateBlock,
SetBlockFailureFlags, ResetBlockFailureFlags, PruneOneBlockFile,
TryAddBlockIndexCandidate, PruneBlockIndexCandidates, OnUndoWritten),
with CheckBlockIndex as the invariant checker and the block_index_tree
fuzz target driving event sequences against it. Two writers were still
inline and move into named transitions:

- the validity raise after a block is connected becomes
  ChainstateManager::MarkBlockScriptsValid;
- the removal of an unusable candidate chain inside FindMostWorkChain
  becomes Chainstate::DropUnusableCandidateChain.

CBlockIndex gains a typed state, BlockIndexState, derived from nStatus
(which stays the stored representation, so nothing on disk changes),
and HaveUndo() for the orthogonal undo bit. CheckBlockIndex checks that
the typed state agrees with the flags, that HaveUndo implies a valid
undo position and a connected block, in addition to its existing
invariants.

Unit tests cover the state mapping and the sequence behind bitcoin#36149 (a
block in m_blocks_unlinked whose file is pruned must leave the set),
which the checker's "no data implies not unlinked" invariant also
guards. The other known bug classes of the plan are covered by
invariants the checker already has: bitcoin#36702 by the snapshot-base
m_chain_tx_count checks, bitcoin#36452 by CheckBlockDataAvailability and the
HAVE_DATA/nTx relations, bitcoin#34254 by the failed-flag propagation checks.
sedited pushed a commit to sedited/bitcoin that referenced this pull request Nov 9, 2026
The block index was already driven by a small set of functions that
write nStatus and the derived sets (AddToBlockIndex,
ReceivedBlockTransactions, InvalidBlockFound, InvalidateBlock,
SetBlockFailureFlags, ResetBlockFailureFlags, PruneOneBlockFile,
TryAddBlockIndexCandidate, PruneBlockIndexCandidates, OnUndoWritten),
with CheckBlockIndex as the invariant checker and the block_index_tree
fuzz target driving event sequences against it. Two writers were still
inline and move into named transitions:

- the validity raise after a block is connected becomes
  ChainstateManager::MarkBlockScriptsValid;
- the removal of an unusable candidate chain inside FindMostWorkChain
  becomes Chainstate::DropUnusableCandidateChain.

CBlockIndex gains a typed state, BlockIndexState, derived from nStatus
(which stays the stored representation, so nothing on disk changes),
and HaveUndo() for the orthogonal undo bit. CheckBlockIndex checks that
the typed state agrees with the flags, that HaveUndo implies a valid
undo position and a connected block, in addition to its existing
invariants.

Unit tests cover the state mapping and the sequence behind bitcoin#36149 (a
block in m_blocks_unlinked whose file is pruned must leave the set),
which the checker's "no data implies not unlinked" invariant also
guards. The other known bug classes of the plan are covered by
invariants the checker already has: bitcoin#36702 by the snapshot-base
m_chain_tx_count checks, bitcoin#36452 by CheckBlockDataAvailability and the
HAVE_DATA/nTx relations, bitcoin#34254 by the failed-flag propagation checks.
sedited pushed a commit to sedited/bitcoin that referenced this pull request Nov 10, 2026
The block index was already driven by a small set of functions that
write nStatus and the derived sets (AddToBlockIndex,
ReceivedBlockTransactions, InvalidBlockFound, InvalidateBlock,
SetBlockFailureFlags, ResetBlockFailureFlags, PruneOneBlockFile,
TryAddBlockIndexCandidate, PruneBlockIndexCandidates, OnUndoWritten),
with CheckBlockIndex as the invariant checker and the block_index_tree
fuzz target driving event sequences against it. Two writers were still
inline and move into named transitions:

- the validity raise after a block is connected becomes
  ChainstateManager::MarkBlockScriptsValid;
- the removal of an unusable candidate chain inside FindMostWorkChain
  becomes Chainstate::DropUnusableCandidateChain.

CBlockIndex gains a typed state, BlockIndexState, derived from nStatus
(which stays the stored representation, so nothing on disk changes),
and HaveUndo() for the orthogonal undo bit. CheckBlockIndex checks that
the typed state agrees with the flags, that HaveUndo implies a valid
undo position and a connected block, in addition to its existing
invariants.

Unit tests cover the state mapping and the sequence behind bitcoin#36149 (a
block in m_blocks_unlinked whose file is pruned must leave the set),
which the checker's "no data implies not unlinked" invariant also
guards. The other known bug classes of the plan are covered by
invariants the checker already has: bitcoin#36702 by the snapshot-base
m_chain_tx_count checks, bitcoin#36452 by CheckBlockDataAvailability and the
HAVE_DATA/nTx relations, bitcoin#34254 by the failed-flag propagation checks.
sedited pushed a commit to sedited/bitcoin that referenced this pull request Nov 10, 2026
The block index was already driven by a small set of functions that
write nStatus and the derived sets (AddToBlockIndex,
ReceivedBlockTransactions, InvalidBlockFound, InvalidateBlock,
SetBlockFailureFlags, ResetBlockFailureFlags, PruneOneBlockFile,
TryAddBlockIndexCandidate, PruneBlockIndexCandidates, OnUndoWritten),
with CheckBlockIndex as the invariant checker and the block_index_tree
fuzz target driving event sequences against it. Two writers were still
inline and move into named transitions:

- the validity raise after a block is connected becomes
  ChainstateManager::MarkBlockScriptsValid;
- the removal of an unusable candidate chain inside FindMostWorkChain
  becomes Chainstate::DropUnusableCandidateChain.

CBlockIndex gains a typed state, BlockIndexState, derived from nStatus
(which stays the stored representation, so nothing on disk changes),
and HaveUndo() for the orthogonal undo bit. CheckBlockIndex checks that
the typed state agrees with the flags, that HaveUndo implies a valid
undo position and a connected block, in addition to its existing
invariants.

Unit tests cover the state mapping and the sequence behind bitcoin#36149 (a
block in m_blocks_unlinked whose file is pruned must leave the set),
which the checker's "no data implies not unlinked" invariant also
guards. The other known bug classes of the plan are covered by
invariants the checker already has: bitcoin#36702 by the snapshot-base
m_chain_tx_count checks, bitcoin#36452 by CheckBlockDataAvailability and the
HAVE_DATA/nTx relations, bitcoin#34254 by the failed-flag propagation checks.
sedited pushed a commit to sedited/bitcoin that referenced this pull request Nov 11, 2026
The block index was already driven by a small set of functions that
write nStatus and the derived sets (AddToBlockIndex,
ReceivedBlockTransactions, InvalidBlockFound, InvalidateBlock,
SetBlockFailureFlags, ResetBlockFailureFlags, PruneOneBlockFile,
TryAddBlockIndexCandidate, PruneBlockIndexCandidates, OnUndoWritten),
with CheckBlockIndex as the invariant checker and the block_index_tree
fuzz target driving event sequences against it. Two writers were still
inline and move into named transitions:

- the validity raise after a block is connected becomes
  ChainstateManager::MarkBlockScriptsValid;
- the removal of an unusable candidate chain inside FindMostWorkChain
  becomes Chainstate::DropUnusableCandidateChain.

CBlockIndex gains a typed state, BlockIndexState, derived from nStatus
(which stays the stored representation, so nothing on disk changes),
and HaveUndo() for the orthogonal undo bit. CheckBlockIndex checks that
the typed state agrees with the flags, that HaveUndo implies a valid
undo position and a connected block, in addition to its existing
invariants.

Unit tests cover the state mapping and the sequence behind bitcoin#36149 (a
block in m_blocks_unlinked whose file is pruned must leave the set),
which the checker's "no data implies not unlinked" invariant also
guards. The other known bug classes of the plan are covered by
invariants the checker already has: bitcoin#36702 by the snapshot-base
m_chain_tx_count checks, bitcoin#36452 by CheckBlockDataAvailability and the
HAVE_DATA/nTx relations, bitcoin#34254 by the failed-flag propagation checks.
sedited pushed a commit to sedited/bitcoin that referenced this pull request Nov 11, 2026
The block index was already driven by a small set of functions that
write nStatus and the derived sets (AddToBlockIndex,
ReceivedBlockTransactions, InvalidBlockFound, InvalidateBlock,
SetBlockFailureFlags, ResetBlockFailureFlags, PruneOneBlockFile,
TryAddBlockIndexCandidate, PruneBlockIndexCandidates, OnUndoWritten),
with CheckBlockIndex as the invariant checker and the block_index_tree
fuzz target driving event sequences against it. Two writers were still
inline and move into named transitions:

- the validity raise after a block is connected becomes
  ChainstateManager::MarkBlockScriptsValid;
- the removal of an unusable candidate chain inside FindMostWorkChain
  becomes Chainstate::DropUnusableCandidateChain.

CBlockIndex gains a typed state, BlockIndexState, derived from nStatus
(which stays the stored representation, so nothing on disk changes),
and HaveUndo() for the orthogonal undo bit. CheckBlockIndex checks that
the typed state agrees with the flags, that HaveUndo implies a valid
undo position and a connected block, in addition to its existing
invariants.

Unit tests cover the state mapping and the sequence behind bitcoin#36149 (a
block in m_blocks_unlinked whose file is pruned must leave the set),
which the checker's "no data implies not unlinked" invariant also
guards. The other known bug classes of the plan are covered by
invariants the checker already has: bitcoin#36702 by the snapshot-base
m_chain_tx_count checks, bitcoin#36452 by CheckBlockDataAvailability and the
HAVE_DATA/nTx relations, bitcoin#34254 by the failed-flag propagation checks.
sedited pushed a commit to sedited/bitcoin that referenced this pull request Nov 11, 2026
The block index was already driven by a small set of functions that
write nStatus and the derived sets (AddToBlockIndex,
ReceivedBlockTransactions, InvalidBlockFound, InvalidateBlock,
SetBlockFailureFlags, ResetBlockFailureFlags, PruneOneBlockFile,
TryAddBlockIndexCandidate, PruneBlockIndexCandidates, OnUndoWritten),
with CheckBlockIndex as the invariant checker and the block_index_tree
fuzz target driving event sequences against it. Two writers were still
inline and move into named transitions:

- the validity raise after a block is connected becomes
  ChainstateManager::MarkBlockScriptsValid;
- the removal of an unusable candidate chain inside FindMostWorkChain
  becomes Chainstate::DropUnusableCandidateChain.

CBlockIndex gains a typed state, BlockIndexState, derived from nStatus
(which stays the stored representation, so nothing on disk changes),
and HaveUndo() for the orthogonal undo bit. CheckBlockIndex checks that
the typed state agrees with the flags, that HaveUndo implies a valid
undo position and a connected block, in addition to its existing
invariants.

Unit tests cover the state mapping and the sequence behind bitcoin#36149 (a
block in m_blocks_unlinked whose file is pruned must leave the set),
which the checker's "no data implies not unlinked" invariant also
guards. The other known bug classes of the plan are covered by
invariants the checker already has: bitcoin#36702 by the snapshot-base
m_chain_tx_count checks, bitcoin#36452 by CheckBlockDataAvailability and the
HAVE_DATA/nTx relations, bitcoin#34254 by the failed-flag propagation checks.
sedited pushed a commit to sedited/bitcoin that referenced this pull request Nov 11, 2026
The block index was already driven by a small set of functions that
write nStatus and the derived sets (AddToBlockIndex,
ReceivedBlockTransactions, InvalidBlockFound, InvalidateBlock,
SetBlockFailureFlags, ResetBlockFailureFlags, PruneOneBlockFile,
TryAddBlockIndexCandidate, PruneBlockIndexCandidates, OnUndoWritten),
with CheckBlockIndex as the invariant checker and the block_index_tree
fuzz target driving event sequences against it. Two writers were still
inline and move into named transitions:

- the validity raise after a block is connected becomes
  ChainstateManager::MarkBlockScriptsValid;
- the removal of an unusable candidate chain inside FindMostWorkChain
  becomes Chainstate::DropUnusableCandidateChain.

CBlockIndex gains a typed state, BlockIndexState, derived from nStatus
(which stays the stored representation, so nothing on disk changes),
and HaveUndo() for the orthogonal undo bit. CheckBlockIndex checks that
the typed state agrees with the flags, that HaveUndo implies a valid
undo position and a connected block, in addition to its existing
invariants.

Unit tests cover the state mapping and the sequence behind bitcoin#36149 (a
block in m_blocks_unlinked whose file is pruned must leave the set),
which the checker's "no data implies not unlinked" invariant also
guards. The other known bug classes of the plan are covered by
invariants the checker already has: bitcoin#36702 by the snapshot-base
m_chain_tx_count checks, bitcoin#36452 by CheckBlockDataAvailability and the
HAVE_DATA/nTx relations, bitcoin#34254 by the failed-flag propagation checks.
sedited pushed a commit to sedited/bitcoin that referenced this pull request Nov 12, 2026
The block index was already driven by a small set of functions that
write nStatus and the derived sets (AddToBlockIndex,
ReceivedBlockTransactions, InvalidBlockFound, InvalidateBlock,
SetBlockFailureFlags, ResetBlockFailureFlags, PruneOneBlockFile,
TryAddBlockIndexCandidate, PruneBlockIndexCandidates, OnUndoWritten),
with CheckBlockIndex as the invariant checker and the block_index_tree
fuzz target driving event sequences against it. Two writers were still
inline and move into named transitions:

- the validity raise after a block is connected becomes
  ChainstateManager::MarkBlockScriptsValid;
- the removal of an unusable candidate chain inside FindMostWorkChain
  becomes Chainstate::DropUnusableCandidateChain.

CBlockIndex gains a typed state, BlockIndexState, derived from nStatus
(which stays the stored representation, so nothing on disk changes),
and HaveUndo() for the orthogonal undo bit. CheckBlockIndex checks that
the typed state agrees with the flags, that HaveUndo implies a valid
undo position and a connected block, in addition to its existing
invariants.

Unit tests cover the state mapping and the sequence behind bitcoin#36149 (a
block in m_blocks_unlinked whose file is pruned must leave the set),
which the checker's "no data implies not unlinked" invariant also
guards. The other known bug classes of the plan are covered by
invariants the checker already has: bitcoin#36702 by the snapshot-base
m_chain_tx_count checks, bitcoin#36452 by CheckBlockDataAvailability and the
HAVE_DATA/nTx relations, bitcoin#34254 by the failed-flag propagation checks.
sedited pushed a commit to sedited/bitcoin that referenced this pull request Nov 12, 2026
The block index was already driven by a small set of functions that
write nStatus and the derived sets (AddToBlockIndex,
ReceivedBlockTransactions, InvalidBlockFound, InvalidateBlock,
SetBlockFailureFlags, ResetBlockFailureFlags, PruneOneBlockFile,
TryAddBlockIndexCandidate, PruneBlockIndexCandidates, OnUndoWritten),
with CheckBlockIndex as the invariant checker and the block_index_tree
fuzz target driving event sequences against it. Two writers were still
inline and move into named transitions:

- the validity raise after a block is connected becomes
  ChainstateManager::MarkBlockScriptsValid;
- the removal of an unusable candidate chain inside FindMostWorkChain
  becomes Chainstate::DropUnusableCandidateChain.

CBlockIndex gains a typed state, BlockIndexState, derived from nStatus
(which stays the stored representation, so nothing on disk changes),
and HaveUndo() for the orthogonal undo bit. CheckBlockIndex checks that
the typed state agrees with the flags, that HaveUndo implies a valid
undo position and a connected block, in addition to its existing
invariants.

Unit tests cover the state mapping and the sequence behind bitcoin#36149 (a
block in m_blocks_unlinked whose file is pruned must leave the set),
which the checker's "no data implies not unlinked" invariant also
guards. The other known bug classes of the plan are covered by
invariants the checker already has: bitcoin#36702 by the snapshot-base
m_chain_tx_count checks, bitcoin#36452 by CheckBlockDataAvailability and the
HAVE_DATA/nTx relations, bitcoin#34254 by the failed-flag propagation checks.
sedited pushed a commit to sedited/bitcoin that referenced this pull request Nov 12, 2026
The block index was already driven by a small set of functions that
write nStatus and the derived sets (AddToBlockIndex,
ReceivedBlockTransactions, InvalidBlockFound, InvalidateBlock,
SetBlockFailureFlags, ResetBlockFailureFlags, PruneOneBlockFile,
TryAddBlockIndexCandidate, PruneBlockIndexCandidates, OnUndoWritten),
with CheckBlockIndex as the invariant checker and the block_index_tree
fuzz target driving event sequences against it. Two writers were still
inline and move into named transitions:

- the validity raise after a block is connected becomes
  ChainstateManager::MarkBlockScriptsValid;
- the removal of an unusable candidate chain inside FindMostWorkChain
  becomes Chainstate::DropUnusableCandidateChain.

CBlockIndex gains a typed state, BlockIndexState, derived from nStatus
(which stays the stored representation, so nothing on disk changes),
and HaveUndo() for the orthogonal undo bit. CheckBlockIndex checks that
the typed state agrees with the flags, that HaveUndo implies a valid
undo position and a connected block, in addition to its existing
invariants.

Unit tests cover the state mapping and the sequence behind bitcoin#36149 (a
block in m_blocks_unlinked whose file is pruned must leave the set),
which the checker's "no data implies not unlinked" invariant also
guards. The other known bug classes of the plan are covered by
invariants the checker already has: bitcoin#36702 by the snapshot-base
m_chain_tx_count checks, bitcoin#36452 by CheckBlockDataAvailability and the
HAVE_DATA/nTx relations, bitcoin#34254 by the failed-flag propagation checks.
sedited pushed a commit to sedited/bitcoin that referenced this pull request Nov 12, 2026
The block index was already driven by a small set of functions that
write nStatus and the derived sets (AddToBlockIndex,
ReceivedBlockTransactions, InvalidBlockFound, InvalidateBlock,
SetBlockFailureFlags, ResetBlockFailureFlags, PruneOneBlockFile,
TryAddBlockIndexCandidate, PruneBlockIndexCandidates, OnUndoWritten),
with CheckBlockIndex as the invariant checker and the block_index_tree
fuzz target driving event sequences against it. Two writers were still
inline and move into named transitions:

- the validity raise after a block is connected becomes
  ChainstateManager::MarkBlockScriptsValid;
- the removal of an unusable candidate chain inside FindMostWorkChain
  becomes Chainstate::DropUnusableCandidateChain.

CBlockIndex gains a typed state, BlockIndexState, derived from nStatus
(which stays the stored representation, so nothing on disk changes),
and HaveUndo() for the orthogonal undo bit. CheckBlockIndex checks that
the typed state agrees with the flags, that HaveUndo implies a valid
undo position and a connected block, in addition to its existing
invariants.

Unit tests cover the state mapping and the sequence behind bitcoin#36149 (a
block in m_blocks_unlinked whose file is pruned must leave the set),
which the checker's "no data implies not unlinked" invariant also
guards. The other known bug classes of the plan are covered by
invariants the checker already has: bitcoin#36702 by the snapshot-base
m_chain_tx_count checks, bitcoin#36452 by CheckBlockDataAvailability and the
HAVE_DATA/nTx relations, bitcoin#34254 by the failed-flag propagation checks.
sedited pushed a commit to sedited/bitcoin that referenced this pull request Nov 13, 2026
The block index was already driven by a small set of functions that
write nStatus and the derived sets (AddToBlockIndex,
ReceivedBlockTransactions, InvalidBlockFound, InvalidateBlock,
SetBlockFailureFlags, ResetBlockFailureFlags, PruneOneBlockFile,
TryAddBlockIndexCandidate, PruneBlockIndexCandidates, OnUndoWritten),
with CheckBlockIndex as the invariant checker and the block_index_tree
fuzz target driving event sequences against it. Two writers were still
inline and move into named transitions:

- the validity raise after a block is connected becomes
  ChainstateManager::MarkBlockScriptsValid;
- the removal of an unusable candidate chain inside FindMostWorkChain
  becomes Chainstate::DropUnusableCandidateChain.

CBlockIndex gains a typed state, BlockIndexState, derived from nStatus
(which stays the stored representation, so nothing on disk changes),
and HaveUndo() for the orthogonal undo bit. CheckBlockIndex checks that
the typed state agrees with the flags, that HaveUndo implies a valid
undo position and a connected block, in addition to its existing
invariants.

Unit tests cover the state mapping and the sequence behind bitcoin#36149 (a
block in m_blocks_unlinked whose file is pruned must leave the set),
which the checker's "no data implies not unlinked" invariant also
guards. The other known bug classes of the plan are covered by
invariants the checker already has: bitcoin#36702 by the snapshot-base
m_chain_tx_count checks, bitcoin#36452 by CheckBlockDataAvailability and the
HAVE_DATA/nTx relations, bitcoin#34254 by the failed-flag propagation checks.
sedited pushed a commit to sedited/bitcoin that referenced this pull request Nov 13, 2026
The block index was already driven by a small set of functions that
write nStatus and the derived sets (AddToBlockIndex,
ReceivedBlockTransactions, InvalidBlockFound, InvalidateBlock,
SetBlockFailureFlags, ResetBlockFailureFlags, PruneOneBlockFile,
TryAddBlockIndexCandidate, PruneBlockIndexCandidates, OnUndoWritten),
with CheckBlockIndex as the invariant checker and the block_index_tree
fuzz target driving event sequences against it. Two writers were still
inline and move into named transitions:

- the validity raise after a block is connected becomes
  ChainstateManager::MarkBlockScriptsValid;
- the removal of an unusable candidate chain inside FindMostWorkChain
  becomes Chainstate::DropUnusableCandidateChain.

CBlockIndex gains a typed state, BlockIndexState, derived from nStatus
(which stays the stored representation, so nothing on disk changes),
and HaveUndo() for the orthogonal undo bit. CheckBlockIndex checks that
the typed state agrees with the flags, that HaveUndo implies a valid
undo position and a connected block, in addition to its existing
invariants.

Unit tests cover the state mapping and the sequence behind bitcoin#36149 (a
block in m_blocks_unlinked whose file is pruned must leave the set),
which the checker's "no data implies not unlinked" invariant also
guards. The other known bug classes of the plan are covered by
invariants the checker already has: bitcoin#36702 by the snapshot-base
m_chain_tx_count checks, bitcoin#36452 by CheckBlockDataAvailability and the
HAVE_DATA/nTx relations, bitcoin#34254 by the failed-flag propagation checks.
sedited pushed a commit to sedited/bitcoin that referenced this pull request Nov 14, 2026
The block index was already driven by a small set of functions that
write nStatus and the derived sets (AddToBlockIndex,
ReceivedBlockTransactions, InvalidBlockFound, InvalidateBlock,
SetBlockFailureFlags, ResetBlockFailureFlags, PruneOneBlockFile,
TryAddBlockIndexCandidate, PruneBlockIndexCandidates, OnUndoWritten),
with CheckBlockIndex as the invariant checker and the block_index_tree
fuzz target driving event sequences against it. Two writers were still
inline and move into named transitions:

- the validity raise after a block is connected becomes
  ChainstateManager::MarkBlockScriptsValid;
- the removal of an unusable candidate chain inside FindMostWorkChain
  becomes Chainstate::DropUnusableCandidateChain.

CBlockIndex gains a typed state, BlockIndexState, derived from nStatus
(which stays the stored representation, so nothing on disk changes),
and HaveUndo() for the orthogonal undo bit. CheckBlockIndex checks that
the typed state agrees with the flags, that HaveUndo implies a valid
undo position and a connected block, in addition to its existing
invariants.

Unit tests cover the state mapping and the sequence behind bitcoin#36149 (a
block in m_blocks_unlinked whose file is pruned must leave the set),
which the checker's "no data implies not unlinked" invariant also
guards. The other known bug classes of the plan are covered by
invariants the checker already has: bitcoin#36702 by the snapshot-base
m_chain_tx_count checks, bitcoin#36452 by CheckBlockDataAvailability and the
HAVE_DATA/nTx relations, bitcoin#34254 by the failed-flag propagation checks.
sedited pushed a commit to sedited/bitcoin that referenced this pull request Nov 15, 2026
The block index was already driven by a small set of functions that
write nStatus and the derived sets (AddToBlockIndex,
ReceivedBlockTransactions, InvalidBlockFound, InvalidateBlock,
SetBlockFailureFlags, ResetBlockFailureFlags, PruneOneBlockFile,
TryAddBlockIndexCandidate, PruneBlockIndexCandidates, OnUndoWritten),
with CheckBlockIndex as the invariant checker and the block_index_tree
fuzz target driving event sequences against it. Two writers were still
inline and move into named transitions:

- the validity raise after a block is connected becomes
  ChainstateManager::MarkBlockScriptsValid;
- the removal of an unusable candidate chain inside FindMostWorkChain
  becomes Chainstate::DropUnusableCandidateChain.

CBlockIndex gains a typed state, BlockIndexState, derived from nStatus
(which stays the stored representation, so nothing on disk changes),
and HaveUndo() for the orthogonal undo bit. CheckBlockIndex checks that
the typed state agrees with the flags, that HaveUndo implies a valid
undo position and a connected block, in addition to its existing
invariants.

Unit tests cover the state mapping and the sequence behind bitcoin#36149 (a
block in m_blocks_unlinked whose file is pruned must leave the set),
which the checker's "no data implies not unlinked" invariant also
guards. Other known bug classes are covered by
invariants the checker already has: bitcoin#36702 by the snapshot-base
m_chain_tx_count checks, bitcoin#36452 by CheckBlockDataAvailability and the
HAVE_DATA/nTx relations, bitcoin#34254 by the failed-flag propagation checks.
Restore the highest pruned block so verification reaches its missing undo before an older missing block body.
Record the corruption error from deep verifychain checks and confirm that level 2 can still verify the available bodies.
Record the failed deep startup check and confirm that restarting with shallow checks preserves the chain tip and restored block.

Record the same corruption error from default `verifychain` checks on an AssumeUTXO snapshot tip before background validation reaches it.
Name the existing pruning-or-snapshot allowance so the following undo-data check can reuse it.
Rename the skipped-data flag to cover unavailable undo as well as block bodies.
Refetching a pruned block with `getblockfrompeer` restores its body but not its undo record.
Stop startup verification and `verifychain` when their disconnect checks need missing pruned undo, rather than reporting database corruption.
The existing allowance for unavailable snapshot history also covers missing undo on AssumeUTXO snapshot chainstates.
Check available block bodies before stopping for missing undo, preserving the checks at lower verification levels.
This result covers unavailable block bodies and undo records.
@l0rinc
l0rinc force-pushed the l0rinc/verify-pruned-undo branch from e262a1f to f685225 Compare November 18, 2026 14:38
@l0rinc

l0rinc commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

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 up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants