Skip to content

validation: merge PeekCoin into GetCoin#35078

Open
l0rinc wants to merge 3 commits intobitcoin:masterfrom
l0rinc:l0rinc/peekcoin-getcoin-merge
Open

validation: merge PeekCoin into GetCoin#35078
l0rinc wants to merge 3 commits intobitcoin:masterfrom
l0rinc:l0rinc/peekcoin-getcoin-merge

Conversation

@l0rinc
Copy link
Copy Markdown
Contributor

@l0rinc l0rinc commented Apr 14, 2026

Problem

Review on #34124 made it clear that the new PeekCoin method is hard to distinguish from the existing GetCoin at call sites where caching does not matter.

Fix

Merge PeekCoin into GetCoin by adding a peek_only flag to the CCoinsView interface and removing the separate virtual method.
This preserves the non-caching lookup behavior in CCoinsViewCache and CoinsViewOverlay, while allowing backends without caches to ignore the flag.

Details

Tests and fuzz scaffolding now call GetCoin(..., true) only where side-effect-free reads matter, and CCoinsViewCache now shares the common result handling between peeking and caching lookups.
The post-Flush() overlay assertions use plain GetCoin() because they only check the flushed spentness and do not need to preserve parent cache state.

Context

The unclear boundary between PeekCoin and GetCoin was flagged during review by:

@DrahtBot
Copy link
Copy Markdown
Contributor

DrahtBot commented Apr 14, 2026

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

Reviews

See the guideline for information on the review process.

Type Reviewers
Concept ACK ryanofsky

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:

  • #34864 (coins: make cache freshness imply dirtiness and remove invalid test states by l0rinc)
  • #34132 (coins: drop error catcher, centralize fatal read handling by l0rinc)
  • #31132 (validation: fetch block inputs on parallel threads by andrewtoth)

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.

The `coinsviewoverlay_tests` post-`Flush()` checks only verify that the spent state reached `main_cache`.
A plain `GetCoin()` miss does not populate the cache, so `PeekCoin()` does not provide extra coverage there.

Use `GetCoin()` now to keep the later `PeekCoin` removal focused on the call sites that still need side-effect-free reads.
@l0rinc l0rinc force-pushed the l0rinc/peekcoin-getcoin-merge branch 2 times, most recently from b755269 to c45d698 Compare April 15, 2026 21:23
@DrahtBot
Copy link
Copy Markdown
Contributor

🚧 At least one of the CI tasks failed.
Task test ancestor commits: https://github.com/bitcoin/bitcoin/actions/runs/24478465952/job/71536804214
LLM reason (✨ experimental): CI failed due to a C++ build/compilation error while compiling txdb.cpp for bitcoin_node (bitcoin_node.dir/txdb.cpp.o).

Hints

Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:

  • Possibly due to a silent merge conflict (the changes in this pull request being
    incompatible with the current code in the target branch). If so, make sure to rebase on the latest
    commit of the target branch.

  • A sanitizer issue, which can only be found by compiling with the sanitizer and running the
    affected test.

  • An intermittent issue.

Leave a comment here, if you need help tracking down a confusing failure.

l0rinc and others added 2 commits April 16, 2026 13:07
The `CCoinsView` hierarchy currently exposes peeking through a separate virtual method.
Add a `peek_only` parameter to `GetCoin()` declarations and overrides so a later commit can route peeking and fetching through one interface.

This commit does not change lookup behavior yet.

Co-authored-by: Anthony Towns <aj@erisian.com.au>
Review on bitcoin#34124 made it clear that `PeekCoin()` is hard to distinguish from `GetCoin()` at call sites where caching does not matter.
Now that `GetCoin()` already accepts a `peek_only` parameter, remove `PeekCoin()` and let callers request the non-caching path directly through `GetCoin(..., true)`.

`CCoinsViewCache` keeps the existing non-caching lookup behavior for `peek_only=true`, and `CoinsViewOverlay` continues to avoid populating parent caches by forwarding `true` to its base view.
Fuzz call sites where the caching behavior is irrelevant now randomize `peek_only` via `ConsumeBool()` for broader coverage.

This simplification was suggested by:
* Rus Yanofsky in bitcoin#34165 (review)
* Anthony Towns in bitcoin#34124 (comment)

Co-authored-by: ryanofsky <ryan@ofsky.org>
Co-authored-by: Anthony Towns <aj@erisian.com.au>
@purpleKarrot
Copy link
Copy Markdown
Contributor

This is known as a Boolean Trap. Many design guidelines forbid the use of boolean parameters and instead recommend having separate functions. This PR seems to go into the opposite direction.

@l0rinc
Copy link
Copy Markdown
Contributor Author

l0rinc commented Apr 16, 2026

@purpleKarrot, I agree, it's an anti-pattern, it's why we avoided it originally, but in this case the end-result is easier to work with and several reviewers mentioned they would prefer it.
Can you suggest a third version that's better than these two?

@purpleKarrot
Copy link
Copy Markdown
Contributor

Maybe a Non-Virtual Interface? You could make the virtual function private and call it from two public functions.

@ryanofsky
Copy link
Copy Markdown
Contributor

ryanofsky commented Apr 16, 2026

Maybe a Non-Virtual Interface? You could make the virtual function private and call it from two public functions.

Yeah, my original suggestion (642fa06 #34165 (comment)) was to make GetCoin PeekCoin and HaveCoin nonvirtual and implement them in terms of virtual hooks for subclasses. I also gave further suggestions there for enforcing GetCoin/PeekCoin distinction at compile time with const, but these would require separating the coin lookup interface from the coin writing interface.

In any case 8892881 seems like an improvement over status quo. IMO the booleans are not a significant problem, and could also be avoided with minimal changes by passing enum class values like CacheUpdate::Yes CacheUpdate::No.

So concept ACK for 8892881 or anything similar. (Would maybe just clarify the citation in the description since I did suggest a different approach.)

@l0rinc
Copy link
Copy Markdown
Contributor Author

l0rinc commented Apr 16, 2026

Would maybe just clairfy description since I'm mentioned that I did suggest a different approach

Thanks for the reminder, I have updated the PR description, please let me know if it reflects your opinion accurately.

these would require separating the coin lookup interface from the coin writing interface

I rechecked your approach; splitting the reader and writer is something I would like to move toward. One of the goals of these cleanups was segregating the interface. Do you think this current PR gets us closer there? It makes a few other coins refactors simpler (noticed while rebasing them).
I'm curious what others think - I don't mind pushing a full alternative PR based on @ryanofsky's prototype - I'd like to do it in small incremental steps.

@ryanofsky
Copy link
Copy Markdown
Contributor

ryanofsky commented Apr 16, 2026

Do you think this current PR gets us closer there?

Yes I think current PR is a basically a strict improvement. I just wanted to mention some other possible alternatives and goals. The only possible downside I see of current PR is that if you combine caching and noncaching lookups into one method it may require a few extra call sites to be updated later if separate const and non-const lookup methods are added. But this does not seem like a big deal since there is no const protection for the cache now. I guess I have a pretty clear idea of how I would design this API differently but many approaches seem reasonable here.

I do plan to review this too soon.

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.

4 participants