Skip to content

txdb: handle malformed first coin cursor key - #35191

Closed
ArtSabintsev wants to merge 1 commit into
bitcoin:masterfrom
ArtSabintsev:codex/fix-txdb-cursor-malformed-key
Closed

ArtSabintsev wants to merge 1 commit into
bitcoin:masterfrom
ArtSabintsev:codex/fix-txdb-cursor-malformed-key

Conversation

@ArtSabintsev

@ArtSabintsev ArtSabintsev commented May 1, 2026 •

Copy link
Copy Markdown

Fixes #35172.

I am still getting familiar with the codebase, so doing easy tickets for now. Thanks for bearing with me.

What?

CCoinsViewDB::Cursor() caches the first coin key after seeking into the chainstate DB. If the first DB_COIN entry is malformed, CDBIterator::GetKey() returns false, but the cursor warmup path ignored that return value.

Because CoinEntry defaults its key marker to DB_COIN, a malformed key like just C could leave the cursor reporting Valid() == true and GetKey() == true even though the key was not decoded.

This makes the warmup path match CCoinsViewDBCursor::Next(): if the iterator is invalid or the key cannot be decoded, invalidate the cached key.

Scenario

This would require an already-corrupted or manually modified local chainstate LevelDB where the first coin key is readable by LevelDB but not decodable as a CoinEntry.

Normal node operation should not create this key shape, so this is a correctness/hardening fix rather than a common user-facing issue.

Testing

The regression test writes a single-byte C key, which enters the DB_COIN keyspace but is too short to decode as a CoinEntry. It then reopens the DB through CCoinsViewDB and checks that the returned cursor is invalid.

cmake --build build --target test_bitcoin -j8
build/bin/test_bitcoin --run_test=coins_tests_dbbase/malformed_first_coin_key_cursor_invalid
build/bin/test_bitcoin --run_test=coins_tests_dbbase

@DrahtBot

DrahtBot commented May 1, 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/35191.

Reviews

See the guideline for information on the review process.

Type Reviewers
ACK junbyjun1238

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

@ArtSabintsev
ArtSabintsev marked this pull request as ready for review May 1, 2026 01:55
@junbyjun1238

Copy link
Copy Markdown
Contributor

utACK c02c4ae

Reviewed the diff. This makes the initial CCoinsViewDB::Cursor() warmup consistent with CCoinsViewDBCursor::Next() by invalidating the cached key when the first DB_COIN key cannot be decoded, instead of exposing the default DB_COIN marker as a valid cursor entry. The regression test covers the malformed first-key case. I did not run the tests.

Comment thread src/txdb.cpp
i->pcursor->GetKey(entry);
i->keyTmp.first = entry.key;
} else {
CoinEntry entry(&i->keyTmp.second);

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.

A slight change in behavior is that the entry is created even if the cursor is invalid. Other versions where this is not the case:

    if (i->pcursor->Valid() && (CoinEntry entry(&i->keyTmp.second); i->pcursor->GetKey(entry))) {
        i->keyTmp.first = entry.key;
    } else {
        i->keyTmp.first = 0; // Make sure Valid() and GetKey() return false
    }
    if (i->pcursor->Valid()) {
        CoinEntry entry(&i->keyTmp.second);
        if (i->pcursor->GetKey(entry)) {
            i->keyTmp.first = entry.key;
        } else {
            i->keyTmp.first = 0; // Make sure Valid() and GetKey() return false
        }
    } else {
        i->keyTmp.first = 0; // Make sure Valid() and GetKey() return false
    }

The latter is more verbose, but more readable and less error prone for future changes, does not relay on early exit conditional evaluation, and is closest to the original.
Please comment, consider. (Note: untested code.)

@achow101

achow101 commented May 7, 2026

Copy link
Copy Markdown
Member

Thanks, but LLM output is not accepted if the author can not properly explain, test or could have written the change themselves.

The bottleneck in this project has always been review and testing, not writing code. Development here is intentionally conservative and slow, and reviewer attention is the scarcest resource we have. LLMs have made this worse, anyone can now prompt them and post their output as PRs. There is an infinite amount plausible looking "improvements" for LLMs to suggest and work on.

Unless we fully trust LLMs to both write and review code, humans still have to spend time understanding the proposed changes, which incurs a non-zero cost for every opened PR.

I understand that contributing to this project can be intimidating, and using LLMs may seem tempting, but it really creates more issues for this project than it solves. The best way to help this project, is to review and test changes. You can use LLMs for this, but you shouldn't solely rely on them, or just post their output.

Please reconsider whether it's something you genuinely think the project should pursue, independent of what your LLM suggested.

I'll close this for now.

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

5 participants