Skip to content

AssumeUTXO: reject duplicate coins and empty snapshot groups - #36500

Closed
l0rinc wants to merge 4 commits into
bitcoin:masterfrom
l0rinc:l0rinc/assumeutxo-import-count
Closed

l0rinc wants to merge 4 commits into
bitcoin:masterfrom
l0rinc:l0rinc/assumeutxo-import-count

Conversation

@l0rinc

@l0rinc l0rinc commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

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

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

@l0rinc
l0rinc force-pushed the l0rinc/assumeutxo-import-count branch from 3d5a6af to 963611c Compare October 11, 2026 12:11
@l0rinc l0rinc changed the title validation: reject duplicate UTXO snapshot records validation: reject duplicate coins and empty snapshot groups Oct 11, 2026
@l0rinc
l0rinc force-pushed the l0rinc/assumeutxo-import-count branch from 963611c to bf7ea08 Compare October 11, 2026 12:59
@l0rinc l0rinc changed the title validation: reject duplicate coins and empty snapshot groups assumeUTXO: reject duplicate coins and empty snapshot groups Oct 14, 2026
@cyb3ralbert

Copy link
Copy Markdown
Contributor

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 d07900c), 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.

@l0rinc l0rinc changed the title assumeUTXO: reject duplicate coins and empty snapshot groups AssumeUTXO: reject duplicate coins and empty snapshot groups Oct 15, 2026
A repeated snapshot payload preserves the unique UTXO set and content hash while doubling the announced record count.
Record that current loading accepts this mismatch before enforcing the metadata contract.
Also preserve the content-hash error when the announced count differs from the unique UTXO count.
Repeated records can produce the committed set while overstating the announced coin count.
Check the unique count from the existing statistics scan after the content hash, preserving hash-error precedence.
An empty transaction group adds no coin and leaves the committed UTXO hash unchanged.
Record its current acceptance before requiring every group to contain a coin.
@l0rinc
l0rinc force-pushed the l0rinc/assumeutxo-import-count branch from bf7ea08 to e7a3816 Compare October 15, 2026 11:38
Reject transaction groups with no coins instead of accepting bytes that contribute nothing to the checked UTXO set.
Snapshots generated by dumptxoutset already contain only nonempty groups.
@l0rinc
l0rinc force-pushed the l0rinc/assumeutxo-import-count branch from e7a3816 to e8e8cbc Compare October 15, 2026 13:41
@fametrano

Copy link
Copy Markdown

tACK e8e8cbc

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

cyb3ralbert commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

tACK e8e8cbc (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 cce32b1 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 danielabrozzoni left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

light code review ACK e8e8cbc

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

@l0rinc

l0rinc commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

Closing in favor of #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.

5 participants