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

Open
shuv-amp wants to merge 1 commit from fix-snapshot-block-unlinked-on-startup into master AGit
shuv-amp commented 2026-10-30 11:23:40 +00:00 (Migrated from github.com)

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 (https://github.com/bitcoin/bitcoin/pull/36452#issuecomment-6464524112), 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.

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 (https://github.com/bitcoin/bitcoin/pull/36452#issuecomment-6464524112), 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 commented 2026-10-30 11:23:49 +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.

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.

<!--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/36702 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36702.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36702#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. | Type | Reviewers | | ---- | --------- | | Concept NACK | [l0rinc](https://github.com/bitcoin/bitcoin/pull/36702#issuecomment-7357650531) | If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore. <!--174a7506f384e20aa4161008e828411d--> ### Conflicts Reviewers, this pull request conflicts with the following ones: * [#37094](https://github.com/bitcoin/bitcoin/pull/37094) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/37094.svg"></sub> (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. <!--5faf32d7da4f0f540f40219e4f7537a3-->
First-time contributor

No verified findings are available from this review.

Review coverage was incomplete.


Reviewed: aad458e78534 · Review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:dc5ab9c247f7a4208471adc14ad7616ed67ce50a9e353d4b351d0bc273ed8e05:ed9be5e94133131d95df3bd45346aff47c7772f27c22da7e230a47332f9fcbac --> <!-- ralph-base:ff90ca6cd56b59b803daa82d25145daaada5fb0b --> <!-- ralph-head:aad458e78534c6cc3deb0d42b57f1146c2ca4f69 --> <!-- ralph-case:6da34e3b30f0d0476f8f70c5b7ccd3a4db1fc93f48ab1df134595ea5accadbfc --> No verified findings are available from this review. Review coverage was incomplete. <!-- Review tier: sensitive --> --- Reviewed: [`aad458e78534`](<https://git.fish.foo/bitcoin/bitcoin/commit/aad458e78534c6cc3deb0d42b57f1146c2ca4f69>) · [Review report](<https://review.fish.foo/report/36702-aad458e78534c6cc3deb0d42b57f1146c2ca4f69-2599-3.html>) · [Previous reports](<https://review.fish.foo/traces/36702/>) [permalink](<https://review.fish.foo/report/36702-aad458e78534c6cc3deb0d42b57f1146c2ca4f69-2599-3.html>)
ValeraFinebits commented 2026-10-31 09:41:21 +00:00 (Migrated from github.com)

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

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 commented 2026-11-04 16:10:29 +00:00 (Migrated from github.com)

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 80f6ddd6c 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.

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 80f6ddd6c 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 commented 2026-11-06 16:17:39 +00:00 (Migrated from github.com)

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.

> 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`](https://github.com/bitcoin/bitcoin/blob/80f6ddd6cbba7c8104dc0b9dabb451af13cc4a7d/src/validation.cpp#L6272)), 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.
cyb3ralbert (Migrated from github.com) reviewed 2026-11-12 07:42:43 +00:00
cyb3ralbert (Migrated from github.com) left a comment

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.

Could you add `wait_for_full_init=False` to the [restart](https://github.com/bitcoin/bitcoin/blob/80f6ddd6cbba7c8104dc0b9dabb451af13cc4a7d/test/functional/feature_assumeutxo.py#L680-L685) 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.
shuv-amp commented 2026-11-12 17:35:28 +00:00 (Migrated from github.com)

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

Rebased and done.

> Could you add `wait_for_full_init=False` to the restart for current master? Rebased and done.
l0rinc commented 2026-12-02 20:23:04 +00:00 (Migrated from github.com)

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

Concept NACK, instead of hacking it further I think we should just remove background sync (with AssumeUTXO) completely, see #36598
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!36702
No description provided.