Skip to content

validation: don't treat snapshot block as linked on startup - #36702

Open
shuv-amp wants to merge 1 commit into
bitcoin:masterfrom
shuv-amp:fix-snapshot-block-unlinked-on-startup
Open

shuv-amp wants to merge 1 commit into
bitcoin:masterfrom
shuv-amp:fix-snapshot-block-unlinked-on-startup

Conversation

@shuv-amp

Copy link
Copy Markdown
Contributor

Background sync requests blocks up to and including the snapshot block from several peers at once, so the snapshot block can be stored before one of its ancestors. ReceivedBlockTransactions() then keeps it in m_blocks_unlinked, but that isn't persisted. On restart LoadBlockIndex() gives it the hardcoded m_chain_tx_count, PopulateBlockIndexCandidates() makes it a background candidate, and FindMostWorkChain() walks back to its parent, which has no chain tx count:

Assertion failed: (pindexTest->HaveNumChainTxs() || pindexTest->nHeight == 0), function FindMostWorkChain, file validation.cpp, line 3217.

It needs a restart in that window, so it should be rare, but after that the node aborts on every start. The only ways out I found are -reindex-chainstate or -reindex, and both delete the snapshot chainstate.

Fix this by adding the snapshot block to m_blocks_unlinked on startup if an ancestor is missing, and not making it a background candidate until then. Both are needed, without the first one background validation stalls. A node that is already stuck starts with the fix and finishes background validation without a reindex.

The new step in feature_assumeutxo.py restarts the node after the snapshot block is submitted out of order, and fails on master. I also reproduced it over p2p with a test peer that withholds the snapshot block's parent.

These are the two changes from #35307 (closed, the motivation there was submitblock before loadtxoutset) without its blockfile and VerifyDB changes. cyb3ralbert hit the same assertion while testing #36452 (#36452 (comment)), which is still needed when the snapshot tip is at the snapshot block. #36149 drops the BLOCK_HAVE_DATA check in the branch right below, so whichever is merged second should match it.

@DrahtBot

DrahtBot commented Oct 30, 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.

Type Reviewers
Concept NACK l0rinc

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #37094 (validation: validate assumeutxo snapshots with a single chainstate by fjahr)

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.

@ValeraFinebits

Copy link
Copy Markdown

Tested feature_assumeutxo.py at 80f6ddd on Windows x64: passes with the fix. Reverting only the two C++ changes while keeping the new test reproduces the startup assertion in FindMostWorkChain().

@Yudis-bit

Copy link
Copy Markdown

Can target_block->pprev be null under mock parameters in unit tests? pindex->pprev is dereferenced directly without a null guard before checking HaveNumChainTxs(), whereas the loop below explicitly guards against null parents.

Reverting the two C++ hunks against 80f6ddd reproduces the SIGABRT at validation.cpp:3217 in FindMostWorkChain. With the patch, feature_assumeutxo.py and validation unit tests pass locally.

PR #36149 drops BLOCK_HAVE_DATA in LoadBlockIndex so pruned blocks without data stay in m_blocks_unlinked while waiting for parents. Whichever lands second will need to align that branch.

@shuv-amp

shuv-amp commented Nov 6, 2026

Copy link
Copy Markdown
Contributor Author

Can target_block->pprev be null under mock parameters in unit tests?

The target block is only set on the historical chainstate to the snapshot base block (in AddChainstate), which has to match an assumeutxo entry from chainparams and is never genesis. The unit tests create snapshot chainstates on non-genesis blocks as well (or clear the target immediately). The pprev == nullptr guard further down in the loop is for genesis when iterating all block indices.

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.

@cyb3ralbert cyb3ralbert left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add wait_for_full_init=False to the restart for current master?

The test waits for index sync before establishing the peer connection needed to complete it. With this change, feature_assumeutxo.py passes.

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.
If the snapshot block is received during background sync before some
of its ancestors, ReceivedBlockTransactions() adds it to
m_blocks_unlinked and it isn't a background candidate until the
missing blocks arrive.

LoadBlockIndex() doesn't restore this on restart. The snapshot block
gets its m_chain_tx_count from the assumeutxo data and is never added
to m_blocks_unlinked, so PopulateBlockIndexCandidates() makes it a
background candidate. FindMostWorkChain() then walks back to its
parent, which has m_chain_tx_count 0, and hits the HaveNumChainTxs()
assert. This happens on every restart.

Add the block to m_blocks_unlinked on startup if an ancestor is
missing, and skip it as a background candidate until then. Without
the m_blocks_unlinked entry the block isn't picked up when the
missing blocks arrive, and background validation stalls.

Extend feature_assumeutxo.py to restart the node in this state.
@shuv-amp
shuv-amp force-pushed the fix-snapshot-block-unlinked-on-startup branch from 80f6ddd to aad458e Compare November 12, 2026 17:32
@shuv-amp

Copy link
Copy Markdown
Contributor Author

Could you add wait_for_full_init=False to the restart for current master?

Rebased and done.

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

l0rinc commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Concept NACK, instead of hacking it further I think we should just remove background sync (with AssumeUTXO) completely, see #36598

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.

6 participants