AssumeUTXO: reject duplicate coins and empty snapshot groups #36500

Open
l0rinc wants to merge 4 commits from l0rinc/assumeutxo-import-count into master AGit
l0rinc commented 2026-10-11 11:10:56 +00:00 (Migrated from github.com)

Problem: Snapshot loading doesn't validate the announced coin count against the unique UTXOs loaded.
Repeated outpoints collapse into one coin, so a snapshot can repeat every record, announce the doubled count, and still match the committed UTXO hash.
loadtxoutset then reports the inflated count as coins_loaded.

Snapshot loading also accepts transaction groups with no coins.
They leave the UTXO hash unchanged and don't advance the coin count, so parsing them bypasses the interrupt check and can delay shutdown.

Fix: Compare the announced count with the unique coin count already produced by the existing UTXO statistics scan, and reject zero-count transaction groups while parsing the snapshot.
When both the content hash and coin count are wrong, loading reports the hash error first.

**Problem:** Snapshot loading doesn't validate the announced coin count against the unique UTXOs loaded. Repeated outpoints collapse into one coin, so a snapshot can repeat every record, announce the doubled count, and still match the committed UTXO hash. `loadtxoutset` then reports the inflated count as `coins_loaded`. Snapshot loading also accepts transaction groups with no coins. They leave the UTXO hash unchanged and don't advance the coin count, so parsing them bypasses the interrupt check and can delay shutdown. **Fix:** Compare the announced count with the unique coin count already produced by the existing UTXO statistics scan, and reject zero-count transaction groups while parsing the snapshot. When both the content hash and coin count are wrong, loading reports the hash error first.
DrahtBot commented 2026-10-11 11:11:05 +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
ACK fametrano, cyb3ralbert, danielabrozzoni

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:

  • #36778 (AssumeUTXO: replace loadtxoutset RPC with a startup arg by l0rinc)
  • #36603 (Improve loadtxoutset performance by ajtowns)
  • #36598 ([Demo] validation: remove AssumeUTXO 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/36500 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36500.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36500#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 | | ---- | --------- | | ACK | [fametrano](https://github.com/bitcoin/bitcoin/pull/36500#issuecomment-6397148633), [cyb3ralbert](https://github.com/bitcoin/bitcoin/pull/36500#issuecomment-6623775632), [danielabrozzoni](https://github.com/bitcoin/bitcoin/pull/36500#pullrequestreview-5863913131) | 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: * [#36778](https://github.com/bitcoin/bitcoin/pull/36778) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36778.svg"></sub> (AssumeUTXO: replace `loadtxoutset` RPC with a startup arg by l0rinc) * [#36603](https://github.com/bitcoin/bitcoin/pull/36603) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36603.svg"></sub> (Improve loadtxoutset performance by ajtowns) * [#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) 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: e8e8cbcaa25b · Full review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:8541d3fafd3ea45d3ad824914bf78a806527457ae4801671a2c4a580beddb288:1e657457416e20a18d8d2d136919c48e28e1bfed7a76c26939ab1ca10a232c6a --> <!-- ralph-base:ab7e668cc6af5d716ad40166d1f88235a2f6d25d --> <!-- ralph-head:e8e8cbcaa25ba75ad04cbacea2ca3d4254bf23d0 --> <!-- ralph-case:a8dfe58a68b75ab0372ed1996e3db29b8099166e89f7a88152cfb1398232bde1 --> No verified findings are available from this review. Review coverage was incomplete. <!-- Review tier: sensitive --> --- Reviewed: [`e8e8cbcaa25b`](<https://git.fish.foo/bitcoin/bitcoin/commit/e8e8cbcaa25ba75ad04cbacea2ca3d4254bf23d0>) · [Full review report](<https://review.fish.foo/traces/36500-e8e8cbcaa25ba75ad04cbacea2ca3d4254bf23d0-1880-8.html>) · [Previous reports](<https://review.fish.foo/traces/36500/>) [permalink](<https://review.fish.foo/traces/36500-e8e8cbcaa25ba75ad04cbacea2ca3d4254bf23d0-1880-8.html>)
cyb3ralbert commented 2026-10-15 07:04:11 +00:00 (Migrated from github.com)

Could the new cases use the existing node (nodes[1]) instead of adding a fifth node? At bf7ea08, feature_assumeutxo.py passes with snapshot_node = self.nodes[1], both with and without num_nodes = 4. The fifth node conflicts with #35307, which also sets num_nodes = 5 and uses nodes[4]. With both PRs merged (head d07900cf8), src/ merges cleanly, but feature_assumeutxo.py fails on the duplicate-records case with "Can't activate a snapshot-based chainstate more than once".

Tested bf7ea08 on regtest with a 299-coin snapshot. The new cases fail on the merge base (a8b69bd) and pass on the PR head. A file with duplicated records and a doubled count is accepted on the base (coins_loaded: 598) but rejected on the head with "Bad snapshot coins count: expected 598, got 299". A single empty group after the header is accepted on the base and rejected on the head.

With 30M empty groups (990 MB) before a valid body, the base accepts the file in 2.13 s and exits 1.00 s after stop is issued 1 s into the load. The head rejects it in 0.04 s. After either rejection, chainstate_snapshot is gone, getchainstates shows one chainstate, and a valid snapshot then loads and survives a restart.

The count check also catches a single repeated outpoint, both within a group and in a repeated group. In either case, the head rejects it with "Bad snapshot coins count: expected 300, got 299" and leaves one chainstate.

wallet_assumeutxo.py and validation_chainstatemanager_tests pass. Both utxo_snapshot and utxo_snapshot_invalid fuzz targets ran for 10 minutes each on the head using libFuzzer without sanitizers, starting from an empty corpus, with no crashes (127k and 1.45M runs, respectively).

Not tested: mainnet-size snapshots.

Could the new cases use the existing `node` (`nodes[1]`) instead of adding a fifth node? At bf7ea08, `feature_assumeutxo.py` passes with `snapshot_node = self.nodes[1]`, both with and without `num_nodes = 4`. The fifth node conflicts with #35307, which also sets `num_nodes = 5` and uses `nodes[4]`. With both PRs merged (head d07900cf8), `src/` merges cleanly, but `feature_assumeutxo.py` fails on the duplicate-records case with "Can't activate a snapshot-based chainstate more than once". Tested bf7ea08 on regtest with a 299-coin snapshot. The new cases fail on the merge base (a8b69bd) and pass on the PR head. A file with duplicated records and a doubled count is accepted on the base (`coins_loaded: 598`) but rejected on the head with "Bad snapshot coins count: expected 598, got 299". A single empty group after the header is accepted on the base and rejected on the head. With 30M empty groups (990 MB) before a valid body, the base accepts the file in 2.13 s and exits 1.00 s after `stop` is issued 1 s into the load. The head rejects it in 0.04 s. After either rejection, `chainstate_snapshot` is gone, `getchainstates` shows one chainstate, and a valid snapshot then loads and survives a restart. The count check also catches a single repeated outpoint, both within a group and in a repeated group. In either case, the head rejects it with "Bad snapshot coins count: expected 300, got 299" and leaves one chainstate. `wallet_assumeutxo.py` and `validation_chainstatemanager_tests` pass. Both `utxo_snapshot` and `utxo_snapshot_invalid` fuzz targets ran for 10 minutes each on the head using libFuzzer without sanitizers, starting from an empty corpus, with no crashes (127k and 1.45M runs, respectively). Not tested: mainnet-size snapshots.
fametrano commented 2026-10-25 11:37:57 +00:00 (Migrated from github.com)

tACK e8e8cbcaa2

A repeated outpoint need not repeat the same coin. I appended a second record for an existing outpoint to the regtest snapshot, with a 500-byte script, and raised the count by one. On the base (cce32b1687) the file loads with coins_loaded 300, and gettxout returns the original coin: the coins cache keeps the first record for an outpoint, so the content hash still matches. The head rejects the file with "Bad snapshot coins count: expected 300, got 299". With the extra record placed before the original, both reject it on the hash.

The loaded UTXO set is still the committed one. What the base leaves unchecked is the reported count and the extra bytes in the file. AssumeutxoData holds no coin count, so this holds for a file matching any hardcoded entry, mainnet included.

I built bitcoind at the head and at the base, and ran the head's feature_assumeutxo.py and a small loadtxoutset script on both. The test fails on the base at the duplicate case and passes on the head. I can share the script.

tACK e8e8cbcaa25ba75ad04cbacea2ca3d4254bf23d0 A repeated outpoint need not repeat the same coin. I appended a second record for an existing outpoint to the regtest snapshot, with a 500-byte script, and raised the count by one. On the base (cce32b1687) the file loads with `coins_loaded` 300, and `gettxout` returns the original coin: the coins cache keeps the first record for an outpoint, so the content hash still matches. The head rejects the file with "Bad snapshot coins count: expected 300, got 299". With the extra record placed before the original, both reject it on the hash. The loaded UTXO set is still the committed one. What the base leaves unchecked is the reported count and the extra bytes in the file. `AssumeutxoData` holds no coin count, so this holds for a file matching any hardcoded entry, mainnet included. I built bitcoind at the head and at the base, and ran the head's `feature_assumeutxo.py` and a small `loadtxoutset` script on both. The test fails on the base at the duplicate case and passes on the head. I can share the script.
cyb3ralbert commented 2026-11-04 05:56:38 +00:00 (Migrated from github.com)

tACK e8e8cbcaa2 (re-ACK)

The test uses four nodes and reuses nodes[1] for the invalid snapshot cases.

My saved 25 October run record (aarch64, Release) compares merge base cce32b1687 with this head:

  • feature_assumeutxo.py and wallet_assumeutxo.py pass on the head.
  • I reproduced @fametrano's case with a second record for an existing outpoint, a 500-byte script, and the header count raised from 299 to 300. Appending it after the original, the base accepts the file with coins_loaded=300, and gettxout returns the original coin. The head rejects it with "Bad snapshot coins count: expected 300, got 299".
  • Placing that record before the original makes both revisions reject the file on the content hash.
  • Appending a byte-for-byte copy of the original record instead also succeeds on the base and fails with the same coin-count error on the head.
  • After each rejection, getchainstates reports one chainstate.

These results cover this PR head alone; I have not tested it combined with #36603 or on mainnet-size snapshots.

tACK e8e8cbcaa25ba75ad04cbacea2ca3d4254bf23d0 (re-ACK) The test uses four nodes and reuses nodes[1] for the invalid snapshot cases. My saved 25 October run record (aarch64, Release) compares merge base cce32b16874e85e8a00cfa1a2190f0b6f4dcaf66 with this head: - feature_assumeutxo.py and wallet_assumeutxo.py pass on the head. - I reproduced @fametrano's case with a second record for an existing outpoint, a 500-byte script, and the header count raised from 299 to 300. Appending it after the original, the base accepts the file with coins_loaded=300, and gettxout returns the original coin. The head rejects it with "Bad snapshot coins count: expected 300, got 299". - Placing that record before the original makes both revisions reject the file on the content hash. - Appending a byte-for-byte copy of the original record instead also succeeds on the base and fails with the same coin-count error on the head. - After each rejection, getchainstates reports one chainstate. These results cover this PR head alone; I have not tested it combined with #36603 or on mainnet-size snapshots.
danielabrozzoni (Migrated from github.com) reviewed 2026-11-11 18:54:20 +00:00
danielabrozzoni (Migrated from github.com) left a comment

light code review ACK e8e8cbcaa2

Code looks good to me, only a light ACK as I'm new to this part of the code :)

light code review ACK e8e8cbcaa25ba75ad04cbacea2ca3d4254bf23d0 Code looks good to me, only a light ACK as I'm new to this part of the code :)
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!36500
No description provided.