Skip to content

txdb: reject undecodable first coin cursor key - #35654

Closed
l0rinc wants to merge 1 commit into
bitcoin:masterfrom
l0rinc:l0rinc/txdb-malformed-first-coin-cursor-key
Closed

l0rinc wants to merge 1 commit into
bitcoin:masterfrom
l0rinc:l0rinc/txdb-malformed-first-coin-cursor-key

Conversation

@l0rinc

@l0rinc l0rinc commented Jul 4, 2026 •

Copy link
Copy Markdown
Contributor

Problem: CCoinsViewDB::Cursor() already handles the case where Seek(DB_COIN) finds no coin record, but could still accept an undecodable first coin key as a valid cursor position.
CCoinsViewDBCursor::Next() already invalidates the cursor for the same decode failure later in iteration, so initial cursor setup has a small correctness mismatch.

Fix: Make initial cursor setup match Next(): keep the cached key invalid unless the first key decodes successfully.

Fixes #35172, with credit to the issue author and previous attempts #35191 and #35248.

@DrahtBot

DrahtBot commented Jul 4, 2026 •

Copy link
Copy Markdown
Contributor

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Code Coverage & Benchmarks

For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/35654.

Reviews

See the guideline and AI policy for information on the review process.

Type Reviewers
ACK sedited

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:

  • #34320 (coins: delegate CCoinsViewDB::HaveCoin to GetCoin 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.

@sedited

sedited commented Jul 4, 2026

Copy link
Copy Markdown
Contributor

Quoting myself from the issue: If the chainstate is corrupted to the point that the condition described here is reached, I'm not sure if the check introduced here actually improves the situation for the running node. Commit 822755a also explicitly works around the "no valid first entry" scenario.

The reasons stated here (crc collisions, and fork tools) also don't seem very compelling to me. If this is really something worthwhile, maybe just frame it as a correctness improvement and getting rid of a weird albeit harmless edge case?

`CCoinsViewDB::Cursor()` already handles an empty coin keyspace after `Seek(DB_COIN)`, but could still accept an undecodable first coin key as a valid cursor position.
`CCoinsViewDBCursor::Next()` invalidates the cursor for the same decode failure later in iteration, so the warmup path had a small malformed-chainstate consistency gap.

Make initial cursor setup match `Next()`: keep the cached key invalid unless the first key decodes successfully.
Add a unit test that writes a valid `Coin` under a malformed `DB_COIN` key and checks the cursor is invalid.

Co-authored-by: Shuvam Pandey <[email protected]>
Co-authored-by: Arthur Ariel Sabintsev <[email protected]>
Co-authored-by: CruzMolina <[email protected]>
@l0rinc l0rinc changed the title txdb: reject malformed first coin cursor key txdb: reject undecodable first coin cursor key Jul 6, 2026
@l0rinc
l0rinc force-pushed the l0rinc/txdb-malformed-first-coin-cursor-key branch from e835635 to 3837d91 Compare July 6, 2026 04:45
@l0rinc

l0rinc commented Jul 6, 2026

Copy link
Copy Markdown
Contributor Author

maybe just frame it as a correctness improvement and getting rid of a weird albeit harmless edge case

Adjusted the code comment, commit message, PR description and title - let me know if this is what you meant

@sedited

sedited commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

let me know if this is what you meant

Yes, that is what I meant. I'm still not sure if the change is worthwhile. This was repeatedly flagged and picked up by AI agents. I get the impression that this is more busywork than anything, but at this point it will just be picked up again and again, which seems like even more of a time sink, so I'd rather this change get in.

ACK 3837d91

@650DataNerd

Copy link
Copy Markdown

Following up on the "is this worthwhile" question above traced through the
typical consumer pattern (while (cursor->Valid()) { ...; cursor->Next(); }).

Before this fix: a corrupted first record means one garbage entry gets
processed, but Next() still runs afterward, so the rest of a valid UTXO
set is still visited correctly.

After this fix: Valid() is false immediately, so Next() is never called
and the loop exits before visiting any of the (potentially many) valid coins
after the bad one trading "one garbage entry mixed into an otherwise-
complete result" for "reports entirely empty despite valid data existing."

Given the project's general preference for failing loudly on corrupted state
(e.g. the -reindex guidance elsewhere in this file) rather than silently
continuing, an obviously-empty result may still be preferable to a subtly-
wrong one. But it might be worth an explicit LogError here so operators
aren't left guessing why a tool suddenly reports nothing happy to be
corrected if I am missing something about how these call sites actually
handle an empty cursor.

@l0rinc l0rinc mentioned this pull request Aug 5, 2026
14 tasks
@l0rinc

l0rinc commented Aug 12, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the hint @650DataNerd - a malformed first coin can short circuit iteration, we need to make deserialization error fatal like we do in #34132.
I'm closing this for now rather than adding cursor-specific error state, once #34132 is merged, I'll revive this as a small follow-up that makes iterator and index deserialization failures also fatal.

@l0rinc l0rinc closed this Aug 12, 2026
@l0rinc
l0rinc deleted the l0rinc/txdb-malformed-first-coin-cursor-key branch August 12, 2026 02:10
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.

txdb: Cursor warmup accepts malformed first coin key

4 participants