Skip to content

validation: validate assumeutxo snapshots with a single chainstate - #37094

Open
fjahr wants to merge 10 commits into
bitcoin:masterfrom
fjahr:2026-12-au-verifier
Open

fjahr wants to merge 10 commits into
bitcoin:masterfrom
fjahr:2026-12-au-verifier

Conversation

@fjahr

@fjahr fjahr commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

Assumeutxo currently needs two Chainstate objects, and validation code has to pay attention to that everywhere with chainstate roles, target blocks, a second set of block index candidates and role-aware notifications. This PR removes all of it and keeps the feature complete, including the background validation of the snapshot.

The idea is simple. Validating a snapshot never really needed chain selection. It connects a fixed list of blocks and compares one hash at the end, so it needs neither a Chainstate nor a place in validation.

What this change does:

  • Validation only works on a single Chainstate again. loadtxoutset moves the coins of the snapshot into it in place.
  • A new class in the node, node::HistoricalReplayVerifier, validates the snapshot on its own thread. It connects the blocks before the snapshot to the coins the chainstate had before and compares the UTXO set hash at the end.
  • All the code that only existed for handling the second chainstate is deleted.

validation.cpp shrinks by more than 500 lines and the kernel sources by about 50. Outside of tests the code gets smaller overall, and that includes the new verifier. With this, assumeutxo stops being something most validation changes have to take into account.

The concerns people raised about the implementation, and how this answers them:

  • Validation has to reason about several chainstates. There is only one now, and the accessors for a validated and a historical chainstate are gone. The object is created once and never replaced, so code can keep a reference to it. A new block goes through chain selection once and not once per chainstate. Validating the snapshot is one small class in the node that validation never calls. It can't select a chain, reorganize or touch the mempool. The only validation code it runs is ConnectBlock() with its own coins, and ConnectBlock() no longer knows anything about snapshots.
  • Unrelated validation work kept running into assumeutxo. The cases that came up over time share a cause, and that cause is gone. There is one set of block index candidates, so no second set can hold a block index entry while it is changed. Connecting a block, the tip log, the fork warnings and the tip notifications no longer check which chainstate they run for. One accessor says whether the chain is based on a snapshot that is not validated yet, and it changes the moment the validation completes, without a restart. The UTXO set is hashed once, and a restart afterwards only cleans up the directories.
  • The kernel library has to carry the complexity. The verifier is not part of the kernel library, and notifications no longer carry a chainstate role. An application built on the kernel sees one chainstate.
  • cs_main is held for minutes while the UTXO set is hashed. The verifier hashes without holding it.
  • Code outside of validation has to know about chainstate roles. Wallets, indexes and ZMQ get BlockConnected without a role, and indexes follow a single accessor.

A few things to note:

  • "Single chainstate" refers to the Chainstate object. While a snapshot is validated there are still two coins databases on disk, in the same directories as on master, and getchainstates still reports both. A datadir with a loaded snapshot needs no migration.
  • The snapshot loader is left as it is. loadtxoutset still fills a temporary Chainstate with the coins of the snapshot and then moves them into the real one. The temporary object never leaves the loader.

The following behavior changes compared to master are intentional:

  • loadtxoutset no longer accepts a snapshot while the node's tip is on a stale branch. The snapshot's block has to be a descendant of the tip. Background validation then only ever connects blocks, so the cases where it first had to disconnect a stale branch are gone.
  • The node starts the background validation, validation itself does not. An application built on the kernel library that opens a datadir with a snapshot keeps working on it and leaves it unvalidated. On master ProcessNewBlock() advances the background validation.
  • Indexes no longer get notifications for the blocks before the snapshot. Their sync thread stays active while a snapshot is validated and indexes each block once the verifier has written it to disk, so an index is never ahead of the verifier after an unclean shutdown. getindexinfo reports them as not synced until they have reached the tip. The sync thread reads the verifier's progress from ChainstateManager, the same way the loop already reads the chain. If this sounds like a architectural regression, indexes: Stop using node internal types and locking cs_main, improve sync logic #24230 moves that loop out of index code into the node, and this wait would move with it.
  • A pruned node no longer splits its -prune target in half while a snapshot is validated. A prune lock protects the blocks the verifier still needs, and the pruning code has no snapshot rule left.

Also, this is my fourth big iteration of this concept and the one that I think strikes the best balance but a lot of tweaks to the approach are possible depending on what people prefer. And there are lots of good follow-ups that I left out here like for example shrinking kernel awareness to almost none is possible at the cost of expanding the interface of the kernel. Or connecting the historical blocks with minimized holding of cs_main and keeping requests for those blocks from delaying blocks for the tip.

I couldn't check everything yet, but for now fixes #37056, fixes #37064, fixes #37070, fixes #37071, fixes #37079, fixes #37050, fixes #37053, fixes #37072, fixes #37076, fixes #37078, fixes #36495, fixes #36562, fixes #36939, fixes #32029.

@DrahtBot

DrahtBot commented Dec 3, 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
Approach ACK ismaelsadeeq

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:

  • #37038 (coins: store spent dbcache entries compactly by LarryRuane)
  • #37023 (contrib: Convert check-deps.sh to Python by jeanpablojp)
  • #36946 (scripted-diff: [test] Add util/check.h includes for assertions by maflcko)
  • #36904 (log: Remove trailing and embedded newlines from log messages by maflcko)
  • #36858 (refactor: break the blockstorage/validation circular dependency by pzafonte)
  • #36856 (kernel: add manual pruning by pzafonte)
  • #36854 (kernel: let callers create the block manager by pzafonte)
  • #36827 (test: Kill validation mutants by Zeegaths)
  • #36778 (AssumeUTXO: replace loadtxoutset RPC with a startup arg by l0rinc)
  • #36739 (refactor: pass BlockManager to ChainstateManager by pzafonte)
  • #36738 (refactor: move block writes and pruning into BlockManager by pzafonte)
  • #36702 (validation: don't treat snapshot block as linked on startup by shuv-amp)
  • #36621 (node: batch block file pruning in a single pass over block index by mutatrum)
  • #36603 (Improve loadtxoutset performance by ajtowns)
  • #36520 (interfaces: Avoid node crashes from invalid Chain IPC calls by ryanofsky)
  • #36326 (kernel: Use typed errors for fatal and flush error notifications by arejula27)
  • #36220 (rpc: report background validation progress in getchainstates by arejula27)
  • #36170 (net: stop sharing P2P listeners with automatic Tor services by l0rinc)
  • #36167 (build: Enable -Wunused by fanquake)
  • #36149 (validation: Leave some pruned blocks in m_blocks_unlinked, fix another nSequenceId issue by mzumsande)
  • #36000 (validation: prefetch blocks while connecting by l0rinc)
  • #35731 (Indexes: Harden the flush-error notification invariant by arejula27)
  • #35522 (refactor: Extract per-message helpers from SendMessages() (move-only) by pablomartin4btc)
  • #35229 (refactor: Use CBlockIndex parameters as reference by optout21)
  • #35003 (validation: improve block data I/O error handling in P2P paths by furszy)
  • #34778 (logging: rewrite macros to enforce restrictions at compile-time, improve efficiency and usability by ryanofsky)
  • #34534 (rpc: Manual prune lock management (Take 2) by fjahr)
  • #34360 (bench: add WalletBalanceManySpent for high-history wallet scenario by w0xlt)
  • #34132 (coins,dbwrapper: make database point-read failures fatal by l0rinc)
  • #29256 (log, refactor: Allow log macros to accept context arguments by ryanofsky)

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.

LLM Linter (✨ experimental)

Possible typos and grammar issues:

  • src/node/historical_verify.cpp: As below a chain tip → As with the chain tip [the original phrasing is unclear]

Possible places where named args for integral literals may be used (e.g. func(x, /*named_arg=*/0) in C++, and func(x, named_arg=0) in Python):

  • src/rpc/blockchain.cpp: chainman.ActivateSnapshot(afile, metadata, false)

2026-12-03 15:59:34

@ismaelsadeeq

Copy link
Copy Markdown
Member

Nice Work,
I read through the description and skimmed the code, this addresses my concern in bitcoin#36598 about components not being able to hold a stable Chainstate handle.

@fjahr I think this is essentially a rewrite of AssumeUTXO's background validation, we first need to align on whether we want to keep the feature at all, I think it's otherwise, given the sentiment in (#36598).

If we do decide to keep AssumeUTXO, I'm Approach ACK on doing it this way rather than maintaining two Chainstate objects.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment