Skip to content

rpc: reject AssumeUTXO rollback without tip data - #36503

Closed
l0rinc wants to merge 3 commits into
bitcoin:masterfrom
l0rinc:l0rinc/rpc-dumptxoutset-missing-undo
Closed

l0rinc wants to merge 3 commits into
bitcoin:masterfrom
l0rinc:l0rinc/rpc-dumptxoutset-missing-undo

Conversation

@l0rinc

@l0rinc l0rinc commented Oct 11, 2026

Copy link
Copy Markdown
Contributor

Problem: Loading a UTXO snapshot on a pruned node can leave the active tip without block or undo data. Attempting a rollback with dumptxoutset then triggers an assertion and terminates the node.

Fix: Reuse GetPruneHeight to reject rollback with the existing pruning error when the required data is unavailable.

@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
Concept ACK musaHaruna, danielabrozzoni
Stale ACK cyb3ralbert

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)
  • #36702 (validation: don't treat snapshot block as linked on startup by shuv-amp)
  • #36598 ([Demo] validation: remove AssumeUTXO by l0rinc)
  • #36266 (rpc: Let dumptxoutset fail early on stale targets and concurrent runs 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.

@musaHaruna musaHaruna left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Concept ACK. The fix looks straightforward and contained: GetPruneHeight handles missing data at the tip before calling GetFirstBlock, allowing the RPC to return an error instead of hitting the assertion.

I have a few questions about the surrounding behavior.

  • Should the early availability check also cover non-pruned nodes? They can load a snapshot without having the historical block or undo data. I understand that the code limits the early check to pruning mode, and non-pruned nodes can fail later during rollback. Could checking availability earlier avoid copying the UTXO set before discovering that rollback is impossible? Registering TemporaryPruneLock could still remain conditional on IsPruneMode().

  • Would a more general error message describe these cases better? Specifically, changing:
    Could not roll back to requested height since necessary block data is already pruned.
    to:
    Could not roll back to requested height because required block or undo data is unavailable.
    After loading a UTXO snapshot, the node may lack historical block or undo data because it has not obtained or generated it yet, not because pruning deleted it. Keeping the existing wording for this focused fix seems reasonable, though.

  • Could the boundary eventually compare the highest missing-data height against the target using > instead of >=? The loop in CreateRolledBackUTXOSnapshot uses: while (block_index->nHeight > target->nHeight)
    For example, exporting state 299 from tip 301 disconnects blocks 301 and 300, so it does not need undo for block 299. If GetPruneHeight returns 299, the current 299 >= 299 check rejects that request, whereas 299 > 299 would allow it. Am I missing another requirement for the target’s data?
    In a locally modified version using > and applying the early availability check to both pruned and non-pruned nodes, the added boundary tests passed. Exporting state 299 succeeded and matched the original snapshot’s UTXO hash, while exporting state 298 still failed.

@l0rinc l0rinc changed the title rpc: reject snapshot rollback without tip data rpc: reject AssumeUTXO rollback without tip data Oct 15, 2026

@cyb3ralbert cyb3ralbert left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Tested ACK 3cb70c2

Tested on Linux aarch64, GCC 14.2.0, Release build:

  • feature_assumeutxo.py and rpc_dumptxoutset.py passed.
  • 874 unit tests passed (one with warnings).

Cases checked:

  • Missing snapshot tip and missing undo data: both abort in GetFirstBlock() on the base commit and return the expected RPC errors with the fix.
  • Pruned-node export at height 398 (tip 399) matches the full node's UTXO hash.
  • Historical-gap and target-equality checks pass on the base.

To reproduce the functional tests:

python3 build/test/functional/test_runner.py feature_assumeutxo.py rpc_dumptxoutset.py

@DrahtBot
DrahtBot requested a review from musaHaruna November 12, 2026 07:37
l0rinc added a commit to l0rinc/bitcoin that referenced this pull request Nov 18, 2026
Rollback disconnects only blocks above the requested height. Check those blocks for missing body or undo data before copying the UTXO set on pruned nodes and snapshot chainstates.
Report unavailable data because snapshot history may not yet have been downloaded.

Suggested in bitcoin#36503 (review)
@l0rinc
l0rinc force-pushed the l0rinc/rpc-dumptxoutset-missing-undo branch from 3cb70c2 to 4107215 Compare November 18, 2026 18:12
Record the assertion when the snapshot tip lacks block data or undo, then restart the node to continue the existing checks.
Reuse GetPruneHeight to return the existing pruning error when the snapshot tip lacks block or undo data.
Rollback disconnects only blocks above the requested height. Check those blocks for missing body or undo data before copying the UTXO set on pruned nodes and snapshot chainstates.
Report unavailable data because snapshot history may not yet have been downloaded.

Suggested in bitcoin#36503 (review)

@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.

Reviewed 4b3849d

Left a couple of nits, and some thoughts around rollback=0. If we decide it's not worth fixing, or it's better to fix in a separate PR, happy to ACK.

I took some notes while reviewing, sharing in case it's helpful to someone else: https://github.com/danielabrozzoni/bitcoin-notes/blob/master/reviews/36503.md :)

Comment thread src/rpc/blockchain.cpp
} else {
// Check pruning constraints before attempting rollback and prevent
// pruning of the necessary blocks with a temporary prune lock
// Check that the blocks to disconnect are available and protect them from pruning

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.

nit: I would expand the comment a little bit, it makes sense to explain why we are guarding with m_from_snapshot_blockhash, and also why GetPruneHeight makes sense even on non-pruned nodes:

// Check that the blocks to disconnect are available and protect them from pruning.
// They can be missing either because they were pruned, or because this is a snapshot
// chainstate whose historical blocks have not been downloaded or connected yet.
// We use GetPruneHeight in either case, as it scans down from the tip and returns the
// highest block missing body or undo data

Comment thread test/functional/feature_assumeutxo.py Outdated
first_snapshot_child = n1.getblockhash(SNAPSHOT_BASE_HEIGHT + 1)
n1.invalidateblock(first_snapshot_child)
assert_equal(n1.getbestblockhash(), snapshot_hash)
assert_equal(n1.getblock(snapshot_hash, 0), snapshot_block)

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.

I think this is fine to leave, it proves that n1 has the block data for snapshot_block, it's only missing undo data

Comment thread src/rpc/blockchain.cpp
LOCK(node.chainman->GetMutex());
if (GetPruneHeight(node.chainman->m_blockman, chainstate.m_chain).value_or(0) >= target_index->nHeight) {
throw JSONRPCError(RPC_MISC_ERROR, "Could not roll back to requested height since necessary block data is already pruned.");
if (GetPruneHeight(blockman, chainstate.m_chain).value_or(0) > target_index->nHeight) {

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.

Came up during review with Opus 5:

In master we currently allow to call dumptxoutset with rollback=0, and the call will succeed as long as we are non pruned, and will produce an empty snapshot.

With this PR, we introduce another case where rollback=0 is allowed, although much more exotic: pruned nodes but with all the block+undo data available, which can happen if one sets prune=1 and then doesn't prune anything, or if a pruned node is in early IBD.
(In master this doesn't work, because GetFirstBlock excludes the genesis block as it has no undo data, and the rollback would fail with "Could not roll back to requested height since necessary block data is already pruned.", despite not having anything pruned. In this sense, this PR removes a confusing error that the user could get)

In both cases, the snapshot produced is not wrong, it's simply expensive to create and useless. Not sure if it's worth to fix, but wanted to mention :)

See: master...danielabrozzoni:bitcoin:assumeutxo_rollback_0 - I pushed one commit to show the current behavior, the tree commits from this PR, and one test change to show the new behavior.

@l0rinc

l0rinc commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for the reviews, based on the latest comments I'm proposing complete removal instead, see #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