blockstorage: keep snapshot base in normal blockfile range #35307

Closed
shuv-amp wants to merge 1 commit from blockstorage-assumeutxo-base-range into master AGit
shuv-amp commented 2026-05-17 15:57:52 +00:00 (Migrated from github.com)

A node that has the assumeutxo snapshot base block on disk before loadtxoutset() runs ends up
in a state it cannot recover from. Depending on whether it is restarted, it either aborts or
refuses to start.

Aborting, when background validation reaches the base block:

node/blockstorage.cpp:985 bool node::BlockManager::WriteBlockUndo(const CBlockUndo &, BlockValidationState &, CBlockIndex &): Assertion `m_blockfile_cursors[type]' failed.

Refusing to start, if the node is restarted after the snapshot is loaded:

[ReadBlockUndo] OpenUndoFile failed for FlatFilePos(nFile=-1, nPos=0) while reading block undo
[DisconnectBlock] failure reading undo data
[VerifyDB] Verification error: irrecoverable inconsistency in block data at 299
Corrupted block database detected.
Please restart with -reindex or -reindex-chainstate to recover.

The block database is not corrupt, so the reindex that message asks for is wasted work.

Reaching this needs the base block stored before the snapshot is loaded, which submitblock
does: it is accepted and written while BlockManager::m_snapshot_height is still unset, so it
goes to the normal blockfile cursor. loadtxoutset() then sets m_snapshot_height to that same
height, and BlockfileTypeForHeight() starts reporting ASSUMED for it.

From there the two failures follow:

  • WriteBlockUndo() looks up the cursor for the block's height and does
    *Assert(m_blockfile_cursors[type]). The block is now ASSUMED, but nothing above the
    snapshot height has been written, so that cursor does not exist and the assertion fires when
    the background chainstate connects the base. FindNextBlockPos() creates the cursor lazily in
    the same situation; WriteBlockUndo() asserts instead.
  • VerifyDB() skips blocks without BLOCK_HAVE_DATA on a snapshot chainstate
    (validation.cpp:4674), which is why the base is normally left alone. Here the base does have
    data, and no undo data because the snapshot chainstate never connected it, so the skip does not
    apply and DisconnectBlock() is attempted on it.

The base block is connected by the background chainstate, not the snapshot one, so it belongs to
the normal blockfile range. BlockfileTypeForHeight() now classifies only blocks above the
snapshot height as ASSUMED. A snapshot chainstate sitting at the base height must not flush the
normal cursor, so FlushChainstateBlockFile() takes the chainstate into account.

VerifyDB() stops before disconnecting the snapshot base, where the snapshot database does not
have the ancestor UTXO data the disconnect needs.

When a snapshot chainstate is loaded from disk, the base's hardcoded m_chain_tx_count does not
mean the background chainstate has processed its parents, so the base is kept in
m_blocks_unlinked until they arrive, and a historical chainstate does not add its target to the
block index candidates before then.

The functional test submits the base block, loads the snapshot, restarts, then feeds the missing
historical blocks and checks that background validation completes. Both failures above are what
it hits without this change.

Tested:

build/test/functional/test_runner.py feature_assumeutxo.py wallet_assumeutxo.py feature_pruning.py feature_reindex.py --timeout-factor=4
build/bin/test_bitcoin
A node that has the assumeutxo snapshot base block on disk before `loadtxoutset()` runs ends up in a state it cannot recover from. Depending on whether it is restarted, it either aborts or refuses to start. Aborting, when background validation reaches the base block: ``` node/blockstorage.cpp:985 bool node::BlockManager::WriteBlockUndo(const CBlockUndo &, BlockValidationState &, CBlockIndex &): Assertion `m_blockfile_cursors[type]' failed. ``` Refusing to start, if the node is restarted after the snapshot is loaded: ``` [ReadBlockUndo] OpenUndoFile failed for FlatFilePos(nFile=-1, nPos=0) while reading block undo [DisconnectBlock] failure reading undo data [VerifyDB] Verification error: irrecoverable inconsistency in block data at 299 Corrupted block database detected. Please restart with -reindex or -reindex-chainstate to recover. ``` The block database is not corrupt, so the reindex that message asks for is wasted work. Reaching this needs the base block stored before the snapshot is loaded, which `submitblock` does: it is accepted and written while `BlockManager::m_snapshot_height` is still unset, so it goes to the normal blockfile cursor. `loadtxoutset()` then sets `m_snapshot_height` to that same height, and `BlockfileTypeForHeight()` starts reporting `ASSUMED` for it. From there the two failures follow: - `WriteBlockUndo()` looks up the cursor for the block's height and does `*Assert(m_blockfile_cursors[type])`. The block is now `ASSUMED`, but nothing above the snapshot height has been written, so that cursor does not exist and the assertion fires when the background chainstate connects the base. `FindNextBlockPos()` creates the cursor lazily in the same situation; `WriteBlockUndo()` asserts instead. - `VerifyDB()` skips blocks without `BLOCK_HAVE_DATA` on a snapshot chainstate (`validation.cpp:4674`), which is why the base is normally left alone. Here the base does have data, and no undo data because the snapshot chainstate never connected it, so the skip does not apply and `DisconnectBlock()` is attempted on it. The base block is connected by the background chainstate, not the snapshot one, so it belongs to the normal blockfile range. `BlockfileTypeForHeight()` now classifies only blocks *above* the snapshot height as `ASSUMED`. A snapshot chainstate sitting at the base height must not flush the normal cursor, so `FlushChainstateBlockFile()` takes the chainstate into account. `VerifyDB()` stops before disconnecting the snapshot base, where the snapshot database does not have the ancestor UTXO data the disconnect needs. When a snapshot chainstate is loaded from disk, the base's hardcoded `m_chain_tx_count` does not mean the background chainstate has processed its parents, so the base is kept in `m_blocks_unlinked` until they arrive, and a historical chainstate does not add its target to the block index candidates before then. The functional test submits the base block, loads the snapshot, restarts, then feeds the missing historical blocks and checks that background validation completes. Both failures above are what it hits without this change. Tested: ```bash build/test/functional/test_runner.py feature_assumeutxo.py wallet_assumeutxo.py feature_pruning.py feature_reindex.py --timeout-factor=4 build/bin/test_bitcoin ```
DrahtBot commented 2026-05-17 15:57:59 +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 fjahr

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:

  • #36598 ([Demo] validation: remove AssumeUTXO by l0rinc)
  • #36500 (AssumeUTXO: reject duplicate coins and empty snapshot groups by l0rinc)
  • #36452 (validation: fix false corruption errors after pruning or AssumeUTXO by l0rinc)
  • #35731 (Indexes: Harden the flush-error notification invariant by arejula27)
  • #35714 (validation: return block-file flush errors by l0rinc)

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/35307 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/35307.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/35307#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 | [fjahr](https://github.com/bitcoin/bitcoin/pull/35307#issuecomment-6510211870) | 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: * [#36598](https://github.com/bitcoin/bitcoin/pull/36598) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36598.svg"></sub> ([Demo] validation: remove AssumeUTXO by l0rinc) * [#36500](https://github.com/bitcoin/bitcoin/pull/36500) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36500.svg"></sub> (AssumeUTXO: reject duplicate coins and empty snapshot groups by l0rinc) * [#36452](https://github.com/bitcoin/bitcoin/pull/36452) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36452.svg"></sub> (validation: fix false corruption errors after pruning or AssumeUTXO by l0rinc) * [#35731](https://github.com/bitcoin/bitcoin/pull/35731) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/35731.svg"></sub> (Indexes: Harden the flush-error notification invariant by arejula27) * [#35714](https://github.com/bitcoin/bitcoin/pull/35714) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/35714.svg"></sub> (validation: return block-file flush errors by l0rinc) 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-->
mzumsande (Migrated from github.com) reviewed 2026-05-18 13:45:59 +00:00
mzumsande (Migrated from github.com) left a comment

why would someone load a snapshot when they already downloaded the block, which means that their tip must be no more than 1024 blocks behind that block.

why would someone load a snapshot when they already downloaded the block, which means that their tip must be no more than 1024 blocks behind that block.
shuv-amp commented 2026-05-18 15:47:29 +00:00 (Migrated from github.com)

I don't think the normal P2P download path is a strong motivation here. In that case, as you say, the node would already be close to the snapshot height.

The case covered here is that this blockstorage state is accepted through local block submission/import before snapshot activation. For example, submitblock can store the snapshot base block while m_snapshot_height is still unset, so it is accounted under the normal blockfile cursor. After loadtxoutset(), the same height maps to ASSUMED; if no assumed blockfile cursor exists yet, background validation can later connect that block and hit the cursor assertion in WriteBlockUndo().

So I’d frame this as making an accepted blockstorage state not abort, rather than as an important/common assumeutxo path. If maintainers prefer not to support that state, rejecting it during snapshot
activation would be another possible direction.

I don't think the normal P2P download path is a strong motivation here. In that case, as you say, the node would already be close to the snapshot height. The case covered here is that this blockstorage state is accepted through local block submission/import before snapshot activation. For example, `submitblock` can store the snapshot base block while `m_snapshot_height` is still unset, so it is accounted under the normal blockfile cursor. After `loadtxoutset()`, the same height maps to `ASSUMED`; if no assumed blockfile cursor exists yet, background validation can later connect that block and hit the cursor assertion in `WriteBlockUndo()`. So I’d frame this as making an accepted blockstorage state not abort, rather than as an important/common assumeutxo path. If maintainers prefer not to support that state, rejecting it during snapshot activation would be another possible direction.
shuv-amp commented 2026-05-19 19:28:12 +00:00 (Migrated from github.com)

Taking this back to draft for now.

Taking this back to draft for now.
shuv-amp commented 2026-08-19 22:11:42 +00:00 (Migrated from github.com)

Following up with what this actually does on master, in May I only described the assertion.

If the node is restarted after the snapshot is loaded, it doesn't come back up:

[ReadBlockUndo] OpenUndoFile failed for FlatFilePos(nFile=-1, nPos=0) while reading block undo
[DisconnectBlock] failure reading undo data
[VerifyDB] Verification error: irrecoverable inconsistency in block data at 299
Corrupted block database detected.
Please restart with -reindex or -reindex-chainstate to recover.

The database isn't corrupt, with this change the same sequence restarts and background
validation completes. On a snapshot chainstate VerifyDB() stops its walk at the first block
with no data, which is normally the base, so it never gets that far. Here the base does have
data, and no undo data because the snapshot chainstate never connected it, so the walk goes into
it and the disconnect fails. The WriteBlockUndo() assertion I mentioned in May is the other
path, when the node keeps running rather than restarting.

I've kept this as fixing the state rather than rejecting it at activation, since rejecting
wouldn't help a node that has already reached it.

Rebased.

Following up with what this actually does on master, in May I only described the assertion. If the node is restarted after the snapshot is loaded, it doesn't come back up: [ReadBlockUndo] OpenUndoFile failed for FlatFilePos(nFile=-1, nPos=0) while reading block undo [DisconnectBlock] failure reading undo data [VerifyDB] Verification error: irrecoverable inconsistency in block data at 299 Corrupted block database detected. Please restart with -reindex or -reindex-chainstate to recover. The database isn't corrupt, with this change the same sequence restarts and background validation completes. On a snapshot chainstate `VerifyDB()` stops its walk at the first block with no data, which is normally the base, so it never gets that far. Here the base does have data, and no undo data because the snapshot chainstate never connected it, so the walk goes into it and the disconnect fails. The `WriteBlockUndo()` assertion I mentioned in May is the other path, when the node keeps running rather than restarting. I've kept this as fixing the state rather than rejecting it at activation, since rejecting wouldn't help a node that has already reached it. Rebased.
First-time contributor

Review failed because required verification did not complete.

Earlier concerns remain unresolved; see the full report for their current assessments.

Review coverage was incomplete.


Reviewed: d07900cf8cc7 · Full review report · Previous reports

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:7ba49ca38b129bc35f71e566efbd136b17ae87b582376d9eebd04f18959e8274:9b5e0237a3fe9e1d05225291551198f07780ddb92e41e55ade9e98c2687fc127 --> <!-- ralph-base:6ed5aad06111177c39f9b9f07a52634476380ae1 --> <!-- ralph-head:d07900cf8cc724d87a6acfd6111def0bcff15a36 --> <!-- ralph-case:70dfcfcc25d726cc681bf00129e210933e026d535db7c0160e9cb62df70ca9d6 --> Review failed because required verification did not complete. Earlier concerns remain unresolved; see the full report for their current assessments. Review coverage was incomplete. <!-- Review tier: sensitive --> --- Reviewed: [`d07900cf8cc7`](<https://git.fish.foo/bitcoin/bitcoin/commit/d07900cf8cc724d87a6acfd6111def0bcff15a36>) · [Full review report](<https://review.fish.foo/traces/35307-d07900cf8cc724d87a6acfd6111def0bcff15a36-1749-3.html>) · [Previous reports](<https://review.fish.foo/traces/35307/>)
DrahtBot commented 2026-10-29 16:53:21 +00:00 (Migrated from github.com)

🐙 This pull request conflicts with the target branch and needs rebase.

<!--cf906140f33d8803c4a75a2196329ecb--> 🐙 This pull request conflicts with the target branch and [needs rebase](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#rebasing-changes).
fjahr commented 2026-10-29 22:42:25 +00:00 (Migrated from github.com)

why would someone load a snapshot when they already downloaded the block, which means that their tip must be no more than 1024 blocks behind that block.

I agree that this is an issue that can only be hit in theory and I don't think it's important enough to fix as the motivation is stated.

Concept NACK

> why would someone load a snapshot when they already downloaded the block, which means that their tip must be no more than 1024 blocks behind that block. I agree that this is an issue that can only be hit in theory and I don't think it's important enough to fix as the motivation is stated. Concept NACK
maflcko commented 2026-10-30 04:59:50 +00:00 (Migrated from github.com)

Closing for now due to controversy. If there is need in the future, this can be re-opened, or a new pull can be opened.

Closing for now due to controversy. If there is need in the future, this can be re-opened, or a new pull can be opened.
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!35307
No description provided.