fuzz: fix a couple incorrect assertions in the coins_view target #28215

pull darosior wants to merge 2 commits into bitcoin:master from darosior:fix_coins_view_fuzz_target changing 1 files +4 −2
  1. darosior commented at 11:59 am on August 4, 2023: member

    The coins_view fuzz target would assert in two places that the cache is consistent with the backend. But it’s never the case (that’s the whole point of using a cache).

    The only reason this didn’t result in a crash was that we would never actually hit these assertions. I ran into this while introducing a new target with an in-memory CCoinsViewDB as the backend view (see #28216) which made the code paths with those assertions actually reachable.

  2. fuzz: coins_view: correct an incorrect assertion
    It is incorrect to assert that `cache.HaveCoin()` will always be `true`
    if `backend.HaveCoin()` is. The coin could well have been marked as
    spent in the cache but not yet flushed, in which case `cache.HaveCoin()`
    would return `false`.
    
    Note this was never hit because `exists_using_have_coin_in_backend` is
    currently never `true` (it's the default implementation of `CCoinsView`.
    However this might change if we were to add a target where the backend
    is a `CCoinsViewDB`.
    c5f6b1db56
  3. DrahtBot commented at 11:59 am on August 4, 2023: contributor

    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
    ACK dergoegge
    Concept ACK brunoerg

    If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #28216 (fuzz: a new target for the coins database by darosior)

    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.

  4. DrahtBot added the label Tests on Aug 4, 2023
  5. fanquake requested review from dergoegge on Aug 7, 2023
  6. brunoerg commented at 11:17 pm on August 9, 2023: contributor
    Concept ACK
  7. in src/test/fuzz/coins_view.cpp:167 in 4f62dcaab5 outdated
    164         Coin coin_using_backend_get_coin;
    165         if (backend_coins_view.GetCoin(random_out_point, coin_using_backend_get_coin)) {
    166             assert(exists_using_have_coin_in_backend);
    167-            assert(coin_using_get_coin == coin_using_backend_get_coin);
    168+            // Note we can't assert that `coin_using_get_coin == coin_using_backend_get_coin` because the coin in
    169+            // the cache may have been modified above in the cache but not yet flushed.
    


    dergoegge commented at 10:16 am on August 10, 2023:
    0            // Note we can't assert that `coin_using_get_coin == coin_using_backend_get_coin` because the coin in
    1            // the cache may have been modified but not yet flushed.
    

    darosior commented at 4:15 pm on August 11, 2023:
    Thanks, done.
  8. dergoegge commented at 10:23 am on August 10, 2023: member

    lgtm (only one small suggestion inline)

    See commits messages for details.

    I’d prefer if you summarize what the PR does in the description. Mostly the fact that these assertions are only correct if we use the default CCoinsView but incorrect if we were using an actual backend impl.

  9. in src/test/fuzz/coins_view.cpp:160 in c5f6b1db56 outdated
    154@@ -155,8 +155,9 @@ FUZZ_TARGET(coins_view, .init = initialize_coins_view)
    155         }
    156         assert((exists_using_access_coin && exists_using_have_coin_in_cache && exists_using_have_coin && exists_using_get_coin) ||
    157                (!exists_using_access_coin && !exists_using_have_coin_in_cache && !exists_using_have_coin && !exists_using_get_coin));
    158+        // If HaveCoin on the backend is true, it must also be on the cache if the coin wasn't spent.
    159         const bool exists_using_have_coin_in_backend = backend_coins_view.HaveCoin(random_out_point);
    160-        if (exists_using_have_coin_in_backend) {
    161+        if (!coin_using_access_coin.IsSpent() && exists_using_have_coin_in_backend) {
    


    brunoerg commented at 12:58 pm on August 10, 2023:

    In https://github.com/bitcoin/bitcoin/pull/28215/commits/c5f6b1db56f67f529377bfb61f58c0a8c17b0127:

    0Note this was never hit because `exists_using_have_coin_in_backend` is
    1currently never `true` (it's the default implementation of `CCoinsView`.
    2However this might change if we were to add a target where the backend
    3is a `CCoinsViewDB`.
    

    What do you mean by “add a target”? Because if exists_using_have_coin_in_backend is always false, (!coin_using_access_coin.IsSpent() && exists_using_have_coin_in_backend) will never be reachable, so wouldn’t it be better to remove it? Or do you intend to modify this target to use CCoinsViewDB? Sorry if I’m missing anything.


    dergoegge commented at 1:00 pm on August 10, 2023:
    See #28216

    brunoerg commented at 1:16 pm on August 10, 2023:
    Thanks, @dergoegge
  10. fuzz: coins_view: remove an incorrect assertion
    Again, this was not hit because the default implementation of
    `CCoinsView` return `false` for `GetCoin`.
    e417c988f6
  11. darosior commented at 4:15 pm on August 11, 2023: member

    I’d prefer if you summarize what the PR does in the description.

    Fair enough, my PR description was sloppy. Done.

    Mostly the fact that these assertions are only correct if we use the default CCoinsView

    I would disagree here. While it’s true the code is “correct” as in the fuzz target doesn’t crash, the logic itself is not correct:

    • It’s asserting something that is always false, namely that the cache is always in sync with the backend.. Which is antithetical to using a cache in the first place.
    • The assertions are never actually executed.
  12. darosior force-pushed on Aug 11, 2023
  13. dergoegge approved
  14. dergoegge commented at 9:01 am on August 15, 2023: member
    Code review ACK e417c988f61bf9d3948d5c8e169626922fe6e24c
  15. fanquake merged this on Aug 15, 2023
  16. fanquake closed this on Aug 15, 2023

  17. darosior deleted the branch on Aug 15, 2023
  18. sidhujag referenced this in commit b3230931a8 on Aug 17, 2023
  19. bitcoin locked this on Aug 14, 2024

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2025-06-16 00:13 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me