CCoinsViewCache code cleanup & deduplication #9384

pull ryanofsky wants to merge 1 commits into bitcoin:master from ryanofsky:pr/ccoins-cleanup changing 4 files +131 −105
  1. ryanofsky commented at 0:07 am on December 20, 2016: contributor

    CCoinsViewCache code cleanup & deduplication

    The change moves code responsible for updating the cache out of various CCoinsViewCache methods and into a Modifier class. This way the cache update code is just written once in a general way instead of being duplicated and split up to handle various special cases.

    This is a refactoring, with changes to cache behavior only in 2 corner cases (with corresponding tests in coins_test.cpp) which don’t affect the meaning of data stored in the cache:

    • In BatchWrite, overwriting a non-dirty pruned cache entry with a fresh pruned cache entry now deletes the cache entry instead of leaving behind a dirty pruned entry that will trigger an unnecessary database write later.

    • In BatchWrite, overwriting a dirty pruned fresh cache entry with a nonpruned entry updates the entry without dropping the fresh flag. There’s no reason to drop the fresh flag in this case because the flag accurately describes the state of the base view and could prevent unnecessary database writes in the future if the utxo is spent later.

  2. fanquake added the label Refactoring on Dec 20, 2016
  3. fanquake added the label UTXO Db and Indexes on Dec 20, 2016
  4. ryanofsky force-pushed on Dec 21, 2016
  5. ryanofsky renamed this:
    CCoinsViewCache code cleanup & deduplication (based on #9107, #9308, #9310)
    CCoinsViewCache code cleanup & deduplication (based on #9107 & #9310)
    on Dec 21, 2016
  6. ryanofsky force-pushed on Jan 4, 2017
  7. ryanofsky renamed this:
    CCoinsViewCache code cleanup & deduplication (based on #9107 & #9310)
    CCoinsViewCache code cleanup & deduplication (based on #9310)
    on Jan 4, 2017
  8. ryanofsky force-pushed on Jan 11, 2017
  9. ryanofsky renamed this:
    CCoinsViewCache code cleanup & deduplication (based on #9310)
    CCoinsViewCache code cleanup & deduplication
    on Jan 11, 2017
  10. in src/test/coins_tests.cpp: in c72de76bf8 outdated
    611@@ -612,7 +612,7 @@ BOOST_AUTO_TEST_CASE(ccoins_access)
    612     CheckAccessCoins(ABSENT, VALUE2, VALUE2, FRESH      , FRESH      );
    613     CheckAccessCoins(ABSENT, VALUE2, VALUE2, DIRTY      , DIRTY      );
    614     CheckAccessCoins(ABSENT, VALUE2, VALUE2, DIRTY|FRESH, DIRTY|FRESH);
    615-    CheckAccessCoins(PRUNED, ABSENT, PRUNED, NO_ENTRY   , FRESH      );
    616+    CheckAccessCoins(PRUNED, ABSENT, ABSENT, NO_ENTRY   , NO_ENTRY   );
    


    ryanofsky commented at 4:07 pm on January 11, 2017:

    Explanation of this change: Calling CCoinsViewCache::AccessCoins when no cache entry exists and the base entry is pruned no longer creates a cache entry.

    The changed behavior in this case is better than the current behavior because now this case is consistent with the case in line 606 above where no cache entry exists and no base entry exists. It doesn’t make any sense for the cache to behave differently just because a base entry is pruned instead of nonexistent.

  11. in src/test/coins_tests.cpp: in c72de76bf8 outdated
    825@@ -826,15 +826,15 @@ BOOST_AUTO_TEST_CASE(ccoins_write)
    826     CheckWriteCoins(PRUNED, ABSENT, PRUNED, DIRTY      , NO_ENTRY   , DIRTY      );
    827     CheckWriteCoins(PRUNED, ABSENT, PRUNED, DIRTY|FRESH, NO_ENTRY   , DIRTY|FRESH);
    828     CheckWriteCoins(PRUNED, PRUNED, PRUNED, 0          , DIRTY      , DIRTY      );
    829-    CheckWriteCoins(PRUNED, PRUNED, PRUNED, 0          , DIRTY|FRESH, DIRTY      );
    830+    CheckWriteCoins(PRUNED, PRUNED, ABSENT, 0          , DIRTY|FRESH, NO_ENTRY   );
    


    ryanofsky commented at 4:10 pm on January 11, 2017:
    Explanation of this change: Overwriting a non-dirty pruned cache entry with a fresh pruned cache entry now deletes the cache entry instead of leaving behind a dirty pruned entry that will trigger an unnecessary database write later.
  12. in src/test/coins_tests.cpp: in c72de76bf8 outdated
    834     CheckWriteCoins(PRUNED, PRUNED, PRUNED, DIRTY      , DIRTY|FRESH, DIRTY      );
    835     CheckWriteCoins(PRUNED, PRUNED, ABSENT, DIRTY|FRESH, DIRTY      , NO_ENTRY   );
    836     CheckWriteCoins(PRUNED, PRUNED, ABSENT, DIRTY|FRESH, DIRTY|FRESH, NO_ENTRY   );
    837     CheckWriteCoins(PRUNED, VALUE2, VALUE2, 0          , DIRTY      , DIRTY      );
    838-    CheckWriteCoins(PRUNED, VALUE2, VALUE2, 0          , DIRTY|FRESH, DIRTY      );
    839+    CheckWriteCoins(PRUNED, VALUE2, VALUE2, 0          , DIRTY|FRESH, DIRTY|FRESH);
    


    ryanofsky commented at 4:11 pm on January 11, 2017:
    Explanation of this change: Overwriting a dirty pruned fresh cache entry with a nonpruned entry updates the entry without dropping the fresh flag. There’s no reason to drop the fresh flag in this case because the flag accurately describes the state of the base view and could prevent unnecessary database writes in the future if the utxo is spent later.
  13. ryanofsky commented at 4:26 pm on January 11, 2017: contributor
    @sipa, this is the PR I mentioned other day. It does move code to the modifier object, but the code could be moved if the modifier object is going away.
  14. ryanofsky commented at 10:30 pm on March 1, 2017: contributor

    @sipa, I posted a version of your pertxoutcache branch rebased on top of this PR here: https://github.com/ryanofsky/bitcoin/commits/pr/pertxoutcache

    It might be useful to you even if you aren’t interested in this PR because it also updates coins_tests to be compatible with your change.

    Relevant commits:

    • 6b547c287e63a48d24547f48ac8108d83dcc7266 WIP per-txout chainstate – rebased version of your main commit on top of this PR.
    • 5853aed217e4fd7f4a02790491ef1ea01821a2d5 Re-enable and fix coins_test.cpp – coins_test fixes that you can incorporate (regardless of the code refactoring in this PR)
    • 1b8425fce6498be757e663d202be025730f86725 Fix wallet CCoinsViewCache::GetCoins call – fix for compile error in wallet caused by ccoins changes
  15. ryanofsky force-pushed on Jun 2, 2017
  16. ryanofsky commented at 8:03 pm on June 2, 2017: contributor
    Rebased c72de76bf803254454a5dc4618bde85d1d992b7d -> 2f2d3c856520da6fcdcb82b2330515136be3f028 (pr/ccoins-cleanup.6 -> pr/ccoins-cleanup.7) after #10195 merge.
  17. ryanofsky force-pushed on Jul 5, 2017
  18. ryanofsky force-pushed on Nov 29, 2017
  19. ryanofsky force-pushed on Nov 30, 2017
  20. ryanofsky force-pushed on Dec 12, 2017
  21. sipa commented at 5:15 pm on March 6, 2018: member
    Concept ACK
  22. DrahtBot closed this on Jul 20, 2018

  23. DrahtBot reopened this on Jul 20, 2018

  24. in src/coins.cpp:124 in 2279ff50b7 outdated
    109-        it->second.coin.Clear();
    110+        *moveout = std::move(coin);
    111     }
    112-    return true;
    113+    coin.Clear();
    114+    return !already_spent;
    


    ryanofsky commented at 2:15 pm on September 24, 2018:
    I think this change (after #10537) would have prevented the inflation bug caused by #9049 + #10195, discussed in the CVE-2018-17144 full disclosure.
  25. DrahtBot commented at 6:10 pm on March 15, 2019: 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
    Concept ACK sipa, jonatack

    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:

    • #26593 (tracing: Only prepare tracepoint arguments when actually tracing by 0xB10C)
    • #26331 (Implement CCoinsViewErrorCatcher::HaveCoin and check disk space periodically by aureleoules)
    • #17487 (coins: allow write to disk without cache drop by jamesob)

    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.

  26. DrahtBot closed this on Apr 28, 2019

  27. DrahtBot commented at 7:12 pm on April 28, 2019: contributor
  28. DrahtBot reopened this on Apr 28, 2019

  29. DrahtBot added the label Needs rebase on Jul 23, 2019
  30. ryanofsky force-pushed on Aug 1, 2019
  31. DrahtBot removed the label Needs rebase on Aug 1, 2019
  32. DrahtBot added the label Needs rebase on Apr 22, 2020
  33. ryanofsky force-pushed on Apr 23, 2020
  34. DrahtBot removed the label Needs rebase on Apr 23, 2020
  35. ryanofsky force-pushed on Apr 24, 2020
  36. ryanofsky commented at 8:33 pm on April 27, 2020: contributor
    Rebased 8c743919935f628e27fd24e8809c9cee273f3930 -> b837952bfac658aa868b9299f7cf6f8ffe248620 (pr/ccoins-cleanup.13 -> pr/ccoins-cleanup.14, compare) after #18752 to avoid mempool_reorg sync timeout https://travis-ci.org/github/bitcoin/bitcoin/jobs/678462266#L681 Rebased b837952bfac658aa868b9299f7cf6f8ffe248620 -> 0c33bd2efa48d276e3475d458f12cba863c83448 (pr/ccoins-cleanup.14 -> pr/ccoins-cleanup.15, compare) due to conflict with #19806 Updated 0c33bd2efa48d276e3475d458f12cba863c83448 -> a40f7e5ed7e55a22ecd604f5632c5c83f0eb0781 (pr/ccoins-cleanup.15 -> pr/ccoins-cleanup.16, compare) to fix fuzz and appveyor CI errors https://cirrus-ci.com/task/5569196852510720?command=ci#L2831 and https://ci.appveyor.com/project/DrahtBot/bitcoin/builds/38155594#L31 Rebased a40f7e5ed7e55a22ecd604f5632c5c83f0eb0781 -> ed037857dad102f0829ab16cce0bb9acf11892ff (pr/ccoins-cleanup.16 -> pr/ccoins-cleanup.17, compare) due to conflict with #22902
  37. DrahtBot added the label Needs rebase on Feb 16, 2021
  38. ryanofsky force-pushed on Mar 10, 2021
  39. DrahtBot removed the label Needs rebase on Mar 10, 2021
  40. ryanofsky force-pushed on Mar 29, 2021
  41. DrahtBot added the label Needs rebase on Nov 29, 2021
  42. CCoinsViewCache code cleanup & deduplication
    The change moves code responsible for updating the cache out of various
    CCoinsViewCache methods and into a Modifier class. This way the cache update
    code is just written once in a general way instead of being duplicated and
    split up to handle various special cases.
    
    This is a refactoring, with changes to cache behavior only in 2 corner cases
    (with corresponding tests in coins_test.cpp) which don't affect the meaning of
    data stored in the cache:
    
    * In BatchWrite, overwriting a non-dirty pruned cache entry with a fresh pruned
      cache entry now deletes the cache entry instead of leaving behind a dirty
      pruned entry that will trigger an unnecessary database write later.
    
    * In BatchWrite, overwriting a dirty pruned fresh cache entry with a nonpruned
      entry updates the entry without dropping the fresh flag. There's no reason to
      drop the fresh flag in this case because the flag accurately describes the
      state of the base view and could prevent unnecessary database writes in the
      future if the utxo is spent later.
    ed037857da
  43. ryanofsky force-pushed on Nov 29, 2021
  44. DrahtBot removed the label Needs rebase on Nov 29, 2021
  45. uvhw referenced this in commit 47d44ccc3e on Feb 14, 2022
  46. maflcko commented at 12:01 pm on February 22, 2022: member
    0fuzz: test/fuzz/coins_view.cpp:140: auto coins_view_fuzz_target(FuzzBufferType)::(anonymous class)::operator()() const: Assertion `expected_code_path' failed.
    
  47. DrahtBot added the label Needs rebase on Jan 30, 2023
  48. DrahtBot commented at 5:06 pm on January 30, 2023: contributor

    🐙 This pull request conflicts with the target branch and needs rebase.

  49. jonatack commented at 5:16 pm on January 30, 2023: contributor
    Concept ACK, please ping me to review when you rebase.
  50. achow101 closed this on Apr 25, 2023

  51. bitcoin locked this on Apr 24, 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: 2024-07-01 10:13 UTC

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