Skip to content

validation: merge PeekCoin into GetCoin - #35078

Closed
l0rinc wants to merge 3 commits into
bitcoin:masterfrom
l0rinc:l0rinc/peekcoin-getcoin-merge
Closed

l0rinc wants to merge 3 commits into
bitcoin:masterfrom
l0rinc:l0rinc/peekcoin-getcoin-merge

Conversation

@l0rinc

@l0rinc l0rinc commented Apr 14, 2026 •

Copy link
Copy Markdown
Contributor

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

DrahtBot commented Apr 14, 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/35078.

Reviews

See the guideline for information on the review process.

Type Reviewers
Concept NACK optout21
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:

  • #35295 (validation: fetch block inputs in parallel during ConnectBlock by andrewtoth)
  • #34864 (coins: make cache freshness imply dirtiness and remove invalid test states 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.

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 <[email protected]>
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 <[email protected]>
Co-authored-by: Anthony Towns <[email protected]>
@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

l0rinc commented Apr 16, 2026

Copy link
Copy Markdown
Contributor Author

@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

ryanofsky commented Apr 16, 2026 •

Copy link
Copy Markdown
Contributor

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

l0rinc commented Apr 16, 2026

Copy link
Copy Markdown
Contributor Author

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

ryanofsky commented Apr 16, 2026 •

Copy link
Copy Markdown
Contributor

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.

@l0rinc
l0rinc marked this pull request as draft April 30, 2026 08:50
@l0rinc

l0rinc commented Apr 30, 2026

Copy link
Copy Markdown
Contributor Author

Drafting to receive more conceptual review: does the existing split make sense (similarly to the existing flush/sync split) or should we unify similar methods to minimize needless overwrites?
If we're going towards splitting the reader and writer behavior (and the caching and other side-effects), merging these two (just to re-split them later) would be wasteful.
Ping: @ryanofsky, @andrewtoth, @ajtowns, @optout21

@ajtowns

ajtowns commented May 5, 2026

Copy link
Copy Markdown
Contributor

Ditto #35078 (comment)

Changing the argument to an enum class (PEEK_ONLY ?) might be better if we're really worried about boolean arguments.

I think having cache/peek behaviour be part of the type/interface (dbcache->GetROView() vs dbcache->GetWritableView() or something) might be better, and would likely obsolete this change, but a change like that is probably a lot more work and will take a while, so doing something like this PR in the meantime would be worthwhile. YMMV.

@optout21 optout21 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 NACK 8892881

Considering the pros and cons, I feel that the benefits are small. Moreover, this change would preclude the possibility of separating the immutable parts of the interface, hence I'd argue against it.

Background. The implementation of parallel input fetch (#31132) called for a non-caching version of GetCoin(), so PeekCoin() was introduced recently as a separate method (#34165).

My pro/con analysis:

Pro:

  • One less method in the interface(s), some less source code lines
  • Minimal performance: one less entry in coins cache instances' vtable.
  • Unified implementation of the two flavors

Con:

  • Less clear conceptual separation between the two flavors.
  • Minimal performance: some overhead due to an extra bool parameter.

However, my main issue is that with a common method it is not possible to place them in separate interfaces, should it be separated into 2 (or 3) levels based on mutability (immutable read, cache-mutable read & write). I've made a comment on #31132 that it would be nice if there was some extra code-level safeguard that the multithreaded accesses make only cache-immutable calls. One way to do this is separating off the cache-immutable read part of the CCoinsViewCache interface (e.g. see optout21@a15d2f9). The present PR would make it impossible to separate the non-caching and caching versions into different interfaces. (Note: another experiment at interface separation in: optout21#6 .)

@l0rinc

l0rinc commented May 8, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, I will investigate if we can split out the sub-interfaces without needing to merge these two - drafted in the meantime.

@l0rinc

l0rinc commented May 18, 2026

Copy link
Copy Markdown
Contributor Author

Closing for now, looks like the current behavior is favored by reviewers.

@l0rinc l0rinc closed this May 18, 2026
@l0rinc
l0rinc deleted the l0rinc/peekcoin-getcoin-merge branch May 18, 2026 18:15
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.

6 participants