LCOV - code coverage report
Current view: top level - src/test - coins_tests.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 98.1 % 583 572
Test Date: 2025-01-19 05:08:01 Functions: 94.9 % 39 37
Branches: 55.6 % 1808 1006

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2014-2022 The Bitcoin Core developers
       2                 :             : // Distributed under the MIT software license, see the accompanying
       3                 :             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       4                 :             : 
       5                 :             : #include <addresstype.h>
       6                 :             : #include <clientversion.h>
       7                 :             : #include <coins.h>
       8                 :             : #include <streams.h>
       9                 :             : #include <test/util/poolresourcetester.h>
      10                 :             : #include <test/util/random.h>
      11                 :             : #include <test/util/setup_common.h>
      12                 :             : #include <txdb.h>
      13                 :             : #include <uint256.h>
      14                 :             : #include <undo.h>
      15                 :             : #include <util/strencodings.h>
      16                 :             : 
      17                 :             : #include <map>
      18                 :             : #include <string>
      19                 :             : #include <variant>
      20                 :             : #include <vector>
      21                 :             : 
      22                 :             : #include <boost/test/unit_test.hpp>
      23                 :             : 
      24                 :             : using namespace util::hex_literals;
      25                 :             : 
      26                 :             : int ApplyTxInUndo(Coin&& undo, CCoinsViewCache& view, const COutPoint& out);
      27                 :             : void UpdateCoins(const CTransaction& tx, CCoinsViewCache& inputs, CTxUndo &txundo, int nHeight);
      28                 :             : 
      29                 :             : namespace
      30                 :             : {
      31                 :             : //! equality test
      32                 :     1058282 : bool operator==(const Coin &a, const Coin &b) {
      33                 :             :     // Empty Coin objects are always equal.
      34   [ +  +  -  + ]:     1058282 :     if (a.IsSpent() && b.IsSpent()) return true;
      35                 :      593094 :     return a.fCoinBase == b.fCoinBase &&
      36   [ +  -  +  -  :      593094 :            a.nHeight == b.nHeight &&
                   -  + ]
      37                 :      296547 :            a.out == b.out;
      38                 :             : }
      39                 :             : 
      40                 :           2 : class CCoinsViewTest : public CCoinsView
      41                 :             : {
      42                 :             :     FastRandomContext& m_rng;
      43                 :             :     uint256 hashBestBlock_;
      44                 :             :     std::map<COutPoint, Coin> map_;
      45                 :             : 
      46                 :             : public:
      47                 :           2 :     CCoinsViewTest(FastRandomContext& rng) : m_rng{rng} {}
      48                 :             : 
      49                 :     6163568 :     std::optional<Coin> GetCoin(const COutPoint& outpoint) const override
      50                 :             :     {
      51         [ +  + ]:     6163568 :         if (auto it{map_.find(outpoint)}; it != map_.end()) {
      52   [ +  +  +  + ]:      436536 :             if (!it->second.IsSpent() || m_rng.randbool()) {
      53                 :      272721 :                 return it->second; // TODO spent coins shouldn't be returned
      54                 :             :             }
      55                 :             :         }
      56                 :     5890847 :         return std::nullopt;
      57                 :             :     }
      58                 :             : 
      59                 :           0 :     uint256 GetBestBlock() const override { return hashBestBlock_; }
      60                 :             : 
      61                 :         254 :     bool BatchWrite(CoinsViewCacheCursor& cursor, const uint256& hashBlock) override
      62                 :             :     {
      63         [ +  + ]:      232406 :         for (auto it{cursor.Begin()}; it != cursor.End(); it = cursor.NextAndMaybeErase(*it)){
      64         [ +  + ]:      232152 :             if (it->second.IsDirty()) {
      65                 :             :                 // Same optimization used in CCoinsViewDB is to only write dirty entries.
      66                 :       84181 :                 map_[it->first] = it->second.coin;
      67   [ +  +  +  + ]:      121520 :                 if (it->second.coin.IsSpent() && m_rng.randrange(3) == 0) {
      68                 :             :                     // Randomly delete empty entries on write.
      69                 :       12555 :                     map_.erase(it->first);
      70                 :             :                 }
      71                 :             :             }
      72                 :             :         }
      73         [ -  + ]:         254 :         if (!hashBlock.IsNull())
      74                 :           0 :             hashBestBlock_ = hashBlock;
      75                 :         254 :         return true;
      76                 :             :     }
      77                 :             : };
      78                 :             : 
      79                 :           0 : class CCoinsViewCacheTest : public CCoinsViewCache
      80                 :             : {
      81                 :             : public:
      82   [ +  -  +  -  :         829 :     explicit CCoinsViewCacheTest(CCoinsView* _base) : CCoinsViewCache(_base) {}
          +  -  +  -  +  
                      - ]
      83                 :             : 
      84                 :         347 :     void SelfTest(bool sanity_check = true) const
      85                 :             :     {
      86                 :             :         // Manually recompute the dynamic usage of the whole data, and compare it.
      87                 :         347 :         size_t ret = memusage::DynamicUsage(cacheCoins);
      88                 :         347 :         size_t count = 0;
      89         [ +  + ]:      438662 :         for (const auto& entry : cacheCoins) {
      90         [ +  + ]:      438315 :             ret += entry.second.coin.DynamicMemoryUsage();
      91                 :      438315 :             ++count;
      92                 :             :         }
      93         [ +  - ]:         347 :         BOOST_CHECK_EQUAL(GetCacheSize(), count);
      94         [ +  - ]:         347 :         BOOST_CHECK_EQUAL(DynamicMemoryUsage(), ret);
      95         [ +  + ]:         347 :         if (sanity_check) {
      96                 :         238 :             SanityCheck();
      97                 :             :         }
      98                 :         347 :     }
      99                 :             : 
     100         [ +  - ]:          10 :     CCoinsMap& map() const { return cacheCoins; }
     101         [ +  - ]:         176 :     CoinsCachePair& sentinel() const { return m_sentinel; }
     102                 :         176 :     size_t& usage() const { return cachedCoinsUsage; }
     103                 :             : };
     104                 :             : 
     105                 :             : } // namespace
     106                 :             : 
     107                 :             : BOOST_FIXTURE_TEST_SUITE(coins_tests, BasicTestingSetup)
     108                 :             : 
     109                 :             : static const unsigned int NUM_SIMULATION_ITERATIONS = 40000;
     110                 :             : 
     111                 :           2 : struct CacheTest : BasicTestingSetup {
     112                 :             : // This is a large randomized insert/remove simulation test on a variable-size
     113                 :             : // stack of caches on top of CCoinsViewTest.
     114                 :             : //
     115                 :             : // It will randomly create/update/delete Coin entries to a tip of caches, with
     116                 :             : // txids picked from a limited list of random 256-bit hashes. Occasionally, a
     117                 :             : // new tip is added to the stack of caches, or the tip is flushed and removed.
     118                 :             : //
     119                 :             : // During the process, booleans are kept to make sure that the randomized
     120                 :             : // operation hits all branches.
     121                 :             : //
     122                 :             : // If fake_best_block is true, assign a random uint256 to mock the recording
     123                 :             : // of best block on flush. This is necessary when using CCoinsViewDB as the base,
     124                 :             : // otherwise we'll hit an assertion in BatchWrite.
     125                 :             : //
     126                 :           2 : void SimulationTest(CCoinsView* base, bool fake_best_block)
     127                 :             : {
     128                 :             :     // Various coverage trackers.
     129                 :           2 :     bool removed_all_caches = false;
     130                 :           2 :     bool reached_4_caches = false;
     131                 :           2 :     bool added_an_entry = false;
     132                 :           2 :     bool added_an_unspendable_entry = false;
     133                 :           2 :     bool removed_an_entry = false;
     134                 :           2 :     bool updated_an_entry = false;
     135                 :           2 :     bool found_an_entry = false;
     136                 :           2 :     bool missed_an_entry = false;
     137                 :           2 :     bool uncached_an_entry = false;
     138                 :           2 :     bool flushed_without_erase = false;
     139                 :             : 
     140                 :             :     // A simple map to track what we expect the cache stack to represent.
     141         [ +  - ]:           2 :     std::map<COutPoint, Coin> result;
     142                 :             : 
     143                 :             :     // The cache stack.
     144                 :           2 :     std::vector<std::unique_ptr<CCoinsViewCacheTest>> stack; // A stack of CCoinsViewCaches on top.
     145         [ +  - ]:           4 :     stack.push_back(std::make_unique<CCoinsViewCacheTest>(base)); // Start with one cache.
     146                 :             : 
     147                 :             :     // Use a limited set of random transaction ids, so we do test overwriting entries.
     148                 :           2 :     std::vector<Txid> txids;
     149         [ +  - ]:           2 :     txids.resize(NUM_SIMULATION_ITERATIONS / 8);
     150         [ +  + ]:       10002 :     for (unsigned int i = 0; i < txids.size(); i++) {
     151                 :       10000 :         txids[i] = Txid::FromUint256(m_rng.rand256());
     152                 :             :     }
     153                 :             : 
     154         [ +  + ]:       80002 :     for (unsigned int i = 0; i < NUM_SIMULATION_ITERATIONS; i++) {
     155                 :             :         // Do a random modification.
     156                 :       80000 :         {
     157         [ +  - ]:       80000 :             auto txid = txids[m_rng.randrange(txids.size())]; // txid we're going to modify in this iteration.
     158         [ +  - ]:       80000 :             Coin& coin = result[COutPoint(txid, 0)];
     159                 :             : 
     160                 :             :             // Determine whether to test HaveCoin before or after Access* (or both). As these functions
     161                 :             :             // can influence each other's behaviour by pulling things into the cache, all combinations
     162                 :             :             // are tested.
     163                 :       80000 :             bool test_havecoin_before = m_rng.randbits(2) == 0;
     164                 :       80000 :             bool test_havecoin_after = m_rng.randbits(2) == 0;
     165                 :             : 
     166   [ +  +  +  - ]:       80000 :             bool result_havecoin = test_havecoin_before ? stack.back()->HaveCoin(COutPoint(txid, 0)) : false;
     167                 :             : 
     168                 :             :             // Infrequently, test usage of AccessByTxid instead of AccessCoin - the
     169                 :             :             // former just delegates to the latter and returns the first unspent in a txn.
     170         [ +  + ]:      160000 :             const Coin& entry = (m_rng.randrange(500) == 0) ?
     171   [ +  -  +  - ]:       80000 :                 AccessByTxid(*stack.back(), txid) : stack.back()->AccessCoin(COutPoint(txid, 0));
     172   [ +  -  +  -  :      160000 :             BOOST_CHECK(coin == entry);
                   +  + ]
     173                 :             : 
     174         [ +  + ]:       80000 :             if (test_havecoin_before) {
     175   [ +  -  +  - ]:       40316 :                 BOOST_CHECK(result_havecoin == !entry.IsSpent());
     176                 :             :             }
     177                 :             : 
     178         [ +  + ]:       80000 :             if (test_havecoin_after) {
     179         [ +  - ]:       19807 :                 bool ret = stack.back()->HaveCoin(COutPoint(txid, 0));
     180   [ +  -  +  - ]:       39614 :                 BOOST_CHECK(ret == !entry.IsSpent());
     181                 :             :             }
     182                 :             : 
     183   [ +  +  +  + ]:       80000 :             if (m_rng.randrange(5) == 0 || coin.IsSpent()) {
     184                 :       47961 :                 Coin newcoin;
     185                 :       47961 :                 newcoin.out.nValue = RandMoney(m_rng);
     186                 :       47961 :                 newcoin.nHeight = 1;
     187                 :             : 
     188                 :             :                 // Infrequently test adding unspendable coins.
     189   [ +  +  +  + ]:       95922 :                 if (m_rng.randrange(16) == 0 && coin.IsSpent()) {
     190                 :        2521 :                     newcoin.out.scriptPubKey.assign(1 + m_rng.randbits(6), OP_RETURN);
     191   [ +  -  +  - ]:        5042 :                     BOOST_CHECK(newcoin.out.scriptPubKey.IsUnspendable());
     192                 :        2521 :                     added_an_unspendable_entry = true;
     193                 :             :                 } else {
     194                 :             :                     // Random sizes so we can test memory usage accounting
     195                 :       45440 :                     newcoin.out.scriptPubKey.assign(m_rng.randbits(6), 0);
     196         [ +  + ]:       45440 :                     (coin.IsSpent() ? added_an_entry : updated_an_entry) = true;
     197                 :       45440 :                     coin = newcoin;
     198                 :             :                 }
     199   [ +  +  +  + ]:       47961 :                 bool is_overwrite = !coin.IsSpent() || m_rng.rand32() & 1;
     200         [ +  - ]:       47961 :                 stack.back()->AddCoin(COutPoint(txid, 0), std::move(newcoin), is_overwrite);
     201                 :       47961 :             } else {
     202                 :             :                 // Spend the coin.
     203                 :       32039 :                 removed_an_entry = true;
     204                 :       32039 :                 coin.Clear();
     205   [ +  -  +  -  :       64078 :                 BOOST_CHECK(stack.back()->SpendCoin(COutPoint(txid, 0)));
                   +  - ]
     206                 :             :             }
     207                 :             :         }
     208                 :             : 
     209                 :             :         // Once every 10 iterations, remove a random entry from the cache
     210         [ +  + ]:      160000 :         if (m_rng.randrange(10) == 0) {
     211                 :        7989 :             COutPoint out(txids[m_rng.rand32() % txids.size()], 0);
     212         [ +  - ]:        7989 :             int cacheid = m_rng.rand32() % stack.size();
     213         [ +  - ]:        7989 :             stack[cacheid]->Uncache(out);
     214         [ +  - ]:        7989 :             uncached_an_entry |= !stack[cacheid]->HaveCoinInCache(out);
     215                 :             :         }
     216                 :             : 
     217                 :             :         // Once every 1000 iterations and at the end, verify the full cache.
     218   [ +  +  +  + ]:      160000 :         if (m_rng.randrange(1000) == 1 || i == NUM_SIMULATION_ITERATIONS - 1) {
     219         [ +  + ]:      315751 :             for (const auto& entry : result) {
     220         [ +  - ]:      315677 :                 bool have = stack.back()->HaveCoin(entry.first);
     221         [ +  - ]:      315677 :                 const Coin& coin = stack.back()->AccessCoin(entry.first);
     222   [ +  -  +  -  :      631354 :                 BOOST_CHECK(have == !coin.IsSpent());
                   +  - ]
     223   [ +  -  +  -  :      631354 :                 BOOST_CHECK(coin == entry.second);
                   +  + ]
     224         [ +  + ]:      315677 :                 if (coin.IsSpent()) {
     225                 :             :                     missed_an_entry = true;
     226                 :             :                 } else {
     227   [ +  -  +  -  :      365204 :                     BOOST_CHECK(stack.back()->HaveCoinInCache(entry.first));
                   +  - ]
     228                 :      182602 :                     found_an_entry = true;
     229                 :             :                 }
     230                 :             :             }
     231         [ +  + ]:         243 :             for (const auto& test : stack) {
     232         [ +  - ]:         169 :                 test->SelfTest();
     233                 :             :             }
     234                 :             :         }
     235                 :             : 
     236         [ +  + ]:      160000 :         if (m_rng.randrange(100) == 0) {
     237                 :             :             // Every 100 iterations, flush an intermediate cache
     238   [ +  +  +  + ]:         776 :             if (stack.size() > 1 && m_rng.randbool() == 0) {
     239                 :         235 :                 unsigned int flushIndex = m_rng.randrange(stack.size() - 1);
     240   [ +  +  +  - ]:         235 :                 if (fake_best_block) stack[flushIndex]->SetBestBlock(m_rng.rand256());
     241                 :         235 :                 bool should_erase = m_rng.randrange(4) < 3;
     242   [ +  -  +  +  :         470 :                 BOOST_CHECK(should_erase ? stack[flushIndex]->Flush() : stack[flushIndex]->Sync());
          +  -  +  -  +  
                      - ]
     243                 :         235 :                 flushed_without_erase |= !should_erase;
     244                 :             :             }
     245                 :             :         }
     246         [ +  + ]:      160000 :         if (m_rng.randrange(100) == 0) {
     247                 :             :             // Every 100 iterations, change the cache stack.
     248   [ +  -  +  + ]:         822 :             if (stack.size() > 0 && m_rng.randbool() == 0) {
     249                 :             :                 //Remove the top cache
     250   [ +  +  +  - ]:         427 :                 if (fake_best_block) stack.back()->SetBestBlock(m_rng.rand256());
     251                 :         427 :                 bool should_erase = m_rng.randrange(4) < 3;
     252   [ +  -  +  +  :         854 :                 BOOST_CHECK(should_erase ? stack.back()->Flush() : stack.back()->Sync());
          +  -  +  -  +  
                      - ]
     253                 :         427 :                 flushed_without_erase |= !should_erase;
     254                 :         427 :                 stack.pop_back();
     255                 :             :             }
     256   [ +  +  +  +  :         822 :             if (stack.size() == 0 || (stack.size() < 4 && m_rng.randbool())) {
                   +  + ]
     257                 :             :                 //Add a new cache
     258                 :         430 :                 CCoinsView* tip = base;
     259         [ +  + ]:         430 :                 if (stack.size() > 0) {
     260                 :         307 :                     tip = stack.back().get();
     261                 :             :                 } else {
     262                 :             :                     removed_all_caches = true;
     263                 :             :                 }
     264         [ +  - ]:         860 :                 stack.push_back(std::make_unique<CCoinsViewCacheTest>(tip));
     265         [ +  + ]:         430 :                 if (stack.size() == 4) {
     266                 :          89 :                     reached_4_caches = true;
     267                 :             :                 }
     268                 :             :             }
     269                 :             :         }
     270                 :             :     }
     271                 :             : 
     272                 :             :     // Verify coverage.
     273   [ +  -  +  -  :           4 :     BOOST_CHECK(removed_all_caches);
                   +  - ]
     274   [ +  -  +  -  :           4 :     BOOST_CHECK(reached_4_caches);
                   +  - ]
     275   [ +  -  +  -  :           4 :     BOOST_CHECK(added_an_entry);
                   +  - ]
     276   [ +  -  +  -  :           4 :     BOOST_CHECK(added_an_unspendable_entry);
                   +  - ]
     277   [ +  -  +  -  :           4 :     BOOST_CHECK(removed_an_entry);
                   +  - ]
     278   [ +  -  +  -  :           4 :     BOOST_CHECK(updated_an_entry);
                   +  - ]
     279   [ +  -  +  -  :           4 :     BOOST_CHECK(found_an_entry);
                   +  - ]
     280   [ +  -  +  -  :           4 :     BOOST_CHECK(missed_an_entry);
                   +  - ]
     281   [ +  -  +  -  :           4 :     BOOST_CHECK(uncached_an_entry);
                   +  - ]
     282   [ +  -  +  - ]:           4 :     BOOST_CHECK(flushed_without_erase);
     283                 :           2 : }
     284                 :             : }; // struct CacheTest
     285                 :             : 
     286                 :             : // Run the above simulation for multiple base types.
     287   [ +  -  +  -  :           7 : BOOST_FIXTURE_TEST_CASE(coins_cache_simulation_test, CacheTest)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     288                 :             : {
     289         [ +  - ]:           1 :     CCoinsViewTest base{m_rng};
     290         [ +  - ]:           1 :     SimulationTest(&base, false);
     291                 :             : 
     292                 :           0 :     CCoinsViewDB db_base{{.path = "test", .cache_bytes = 1 << 23, .memory_only = true}, {}};
     293         [ +  - ]:           1 :     SimulationTest(&db_base, true);
     294   [ +  -  +  - ]:           2 : }
     295                 :             : 
     296                 :             : struct UpdateTest : BasicTestingSetup {
     297                 :             : // Store of all necessary tx and undo data for next test
     298                 :             : typedef std::map<COutPoint, std::tuple<CTransaction,CTxUndo,Coin>> UtxoData;
     299                 :             : UtxoData utxoData;
     300                 :             : 
     301                 :       40257 : UtxoData::iterator FindRandomFrom(const std::set<COutPoint> &utxoSet) {
     302         [ -  + ]:       40257 :     assert(utxoSet.size());
     303                 :       40257 :     auto utxoSetIt = utxoSet.lower_bound(COutPoint(Txid::FromUint256(m_rng.rand256()), 0));
     304         [ +  + ]:       40257 :     if (utxoSetIt == utxoSet.end()) {
     305                 :         382 :         utxoSetIt = utxoSet.begin();
     306                 :             :     }
     307                 :       40257 :     auto utxoDataIt = utxoData.find(*utxoSetIt);
     308         [ -  + ]:       40257 :     assert(utxoDataIt != utxoData.end());
     309                 :       40257 :     return utxoDataIt;
     310                 :             : }
     311                 :             : }; // struct UpdateTest
     312                 :             : 
     313                 :             : 
     314                 :             : // This test is similar to the previous test
     315                 :             : // except the emphasis is on testing the functionality of UpdateCoins
     316                 :             : // random txs are created and UpdateCoins is used to update the cache stack
     317                 :             : // In particular it is tested that spending a duplicate coinbase tx
     318                 :             : // has the expected effect (the other duplicate is overwritten at all cache levels)
     319   [ +  -  +  -  :           7 : BOOST_FIXTURE_TEST_CASE(updatecoins_simulation_test, UpdateTest)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     320                 :             : {
     321                 :           1 :     SeedRandomForTest(SeedRand::ZEROS);
     322                 :             : 
     323                 :           1 :     bool spent_a_duplicate_coinbase = false;
     324                 :             :     // A simple map to track what we expect the cache stack to represent.
     325         [ +  - ]:           1 :     std::map<COutPoint, Coin> result;
     326                 :             : 
     327                 :             :     // The cache stack.
     328         [ +  - ]:           1 :     CCoinsViewTest base{m_rng}; // A CCoinsViewTest at the bottom.
     329                 :           1 :     std::vector<std::unique_ptr<CCoinsViewCacheTest>> stack; // A stack of CCoinsViewCaches on top.
     330         [ +  - ]:           2 :     stack.push_back(std::make_unique<CCoinsViewCacheTest>(&base)); // Start with one cache.
     331                 :             : 
     332                 :             :     // Track the txids we've used in various sets
     333                 :           1 :     std::set<COutPoint> coinbase_coins;
     334                 :           1 :     std::set<COutPoint> disconnected_coins;
     335                 :           1 :     std::set<COutPoint> duplicate_coins;
     336                 :           1 :     std::set<COutPoint> utxoset;
     337                 :             : 
     338         [ +  + ]:       40001 :     for (unsigned int i = 0; i < NUM_SIMULATION_ITERATIONS; i++) {
     339                 :       40000 :         uint32_t randiter = m_rng.rand32();
     340                 :             : 
     341                 :             :         // 19/20 txs add a new transaction
     342         [ +  + ]:       40000 :         if (randiter % 20 < 19) {
     343         [ +  - ]:       37952 :             CMutableTransaction tx;
     344         [ +  - ]:       37952 :             tx.vin.resize(1);
     345         [ +  - ]:       37952 :             tx.vout.resize(1);
     346                 :       37952 :             tx.vout[0].nValue = i; //Keep txs unique unless intended to duplicate
     347                 :       37952 :             tx.vout[0].scriptPubKey.assign(m_rng.rand32() & 0x3F, 0); // Random sizes so we can test memory usage accounting
     348                 :       37952 :             const int height{int(m_rng.rand32() >> 1)};
     349                 :       37952 :             Coin old_coin;
     350                 :             : 
     351                 :             :             // 2/20 times create a new coinbase
     352   [ +  +  +  + ]:       37952 :             if (randiter % 20 < 2 || coinbase_coins.size() < 10) {
     353                 :             :                 // 1/10 of those times create a duplicate coinbase
     354   [ +  +  -  + ]:        4049 :                 if (m_rng.randrange(10) == 0 && coinbase_coins.size()) {
     355                 :         380 :                     auto utxod = FindRandomFrom(coinbase_coins);
     356                 :             :                     // Reuse the exact same coinbase
     357         [ +  - ]:         380 :                     tx = CMutableTransaction{std::get<0>(utxod->second)};
     358                 :             :                     // shouldn't be available for reconnection if it's been duplicated
     359                 :         380 :                     disconnected_coins.erase(utxod->first);
     360                 :             : 
     361         [ +  - ]:         380 :                     duplicate_coins.insert(utxod->first);
     362                 :             :                 }
     363                 :             :                 else {
     364   [ +  -  +  - ]:        3669 :                     coinbase_coins.insert(COutPoint(tx.GetHash(), 0));
     365                 :             :                 }
     366   [ +  -  -  + ]:        8098 :                 assert(CTransaction(tx).IsCoinBase());
     367                 :             :             }
     368                 :             : 
     369                 :             :             // 17/20 times reconnect previous or add a regular tx
     370                 :             :             else {
     371                 :             : 
     372         [ +  + ]:       33903 :                 COutPoint prevout;
     373                 :             :                 // 1/20 times reconnect a previously disconnected tx
     374   [ +  +  +  + ]:       33903 :                 if (randiter % 20 == 2 && disconnected_coins.size()) {
     375                 :        2018 :                     auto utxod = FindRandomFrom(disconnected_coins);
     376         [ +  - ]:        2018 :                     tx = CMutableTransaction{std::get<0>(utxod->second)};
     377         [ +  - ]:        2018 :                     prevout = tx.vin[0].prevout;
     378   [ +  -  +  +  :        4036 :                     if (!CTransaction(tx).IsCoinBase() && !utxoset.count(prevout)) {
             +  +  +  + ]
     379                 :         581 :                         disconnected_coins.erase(utxod->first);
     380                 :         581 :                         continue;
     381                 :             :                     }
     382                 :             : 
     383                 :             :                     // If this tx is already IN the UTXO, then it must be a coinbase, and it must be a duplicate
     384         [ -  + ]:        1437 :                     if (utxoset.count(utxod->first)) {
     385   [ #  #  #  # ]:           0 :                         assert(CTransaction(tx).IsCoinBase());
     386         [ #  # ]:           0 :                         assert(duplicate_coins.count(utxod->first));
     387                 :             :                     }
     388                 :        1437 :                     disconnected_coins.erase(utxod->first);
     389                 :             :                 }
     390                 :             : 
     391                 :             :                 // 16/20 times create a regular tx
     392                 :             :                 else {
     393                 :       31885 :                     auto utxod = FindRandomFrom(utxoset);
     394         [ +  - ]:       31885 :                     prevout = utxod->first;
     395                 :             : 
     396                 :             :                     // Construct the tx to spend the coins of prevouthash
     397         [ +  - ]:       31885 :                     tx.vin[0].prevout = prevout;
     398   [ +  -  -  + ]:       63770 :                     assert(!CTransaction(tx).IsCoinBase());
     399                 :             :                 }
     400                 :             :                 // In this simple test coins only have two states, spent or unspent, save the unspent state to restore
     401         [ +  - ]:       33322 :                 old_coin = result[prevout];
     402                 :             :                 // Update the expected result of prevouthash to know these coins are spent
     403         [ +  - ]:       33322 :                 result[prevout].Clear();
     404                 :             : 
     405                 :       33322 :                 utxoset.erase(prevout);
     406                 :             : 
     407                 :             :                 // The test is designed to ensure spending a duplicate coinbase will work properly
     408                 :             :                 // if that ever happens and not resurrect the previously overwritten coinbase
     409         [ +  + ]:       33322 :                 if (duplicate_coins.count(prevout)) {
     410                 :         346 :                     spent_a_duplicate_coinbase = true;
     411                 :             :                 }
     412                 :             : 
     413                 :             :             }
     414                 :             :             // Update the expected result to know about the new output coins
     415         [ -  + ]:       37371 :             assert(tx.vout.size() == 1);
     416         [ +  - ]:       37371 :             const COutPoint outpoint(tx.GetHash(), 0);
     417   [ +  -  +  - ]:       37371 :             result[outpoint] = Coin{tx.vout[0], height, CTransaction{tx}.IsCoinBase()};
     418                 :             : 
     419                 :             :             // Call UpdateCoins on the top cache
     420                 :       37371 :             CTxUndo undo;
     421   [ +  -  +  - ]:       37371 :             UpdateCoins(CTransaction{tx}, *(stack.back()), undo, height);
     422                 :             : 
     423                 :             :             // Update the utxo set for future spends
     424         [ +  - ]:       37371 :             utxoset.insert(outpoint);
     425                 :             : 
     426                 :             :             // Track this tx and undo info to use later
     427   [ +  -  +  - ]:      112113 :             utxoData.emplace(outpoint, std::make_tuple(tx,undo,old_coin));
     428         [ +  - ]:       77952 :         } else if (utxoset.size()) {
     429                 :             :             //1/20 times undo a previous transaction
     430                 :        2048 :             auto utxod = FindRandomFrom(utxoset);
     431                 :             : 
     432         [ +  - ]:        2048 :             CTransaction &tx = std::get<0>(utxod->second);
     433                 :        2048 :             CTxUndo &undo = std::get<1>(utxod->second);
     434         [ +  - ]:        2048 :             Coin &orig_coin = std::get<2>(utxod->second);
     435                 :             : 
     436                 :             :             // Update the expected result
     437                 :             :             // Remove new outputs
     438         [ +  - ]:        2048 :             result[utxod->first].Clear();
     439                 :             :             // If not coinbase restore prevout
     440         [ +  + ]:        2048 :             if (!tx.IsCoinBase()) {
     441         [ +  - ]:        1810 :                 result[tx.vin[0].prevout] = orig_coin;
     442                 :             :             }
     443                 :             : 
     444                 :             :             // Disconnect the tx from the current UTXO
     445                 :             :             // See code in DisconnectBlock
     446                 :             :             // remove outputs
     447   [ +  -  +  -  :        4096 :             BOOST_CHECK(stack.back()->SpendCoin(utxod->first));
             +  -  +  + ]
     448                 :             :             // restore inputs
     449         [ +  + ]:        2048 :             if (!tx.IsCoinBase()) {
     450                 :        1810 :                 const COutPoint &out = tx.vin[0].prevout;
     451                 :        1810 :                 Coin coin = undo.vprevout[0];
     452         [ +  - ]:        1810 :                 ApplyTxInUndo(std::move(coin), *(stack.back()), out);
     453                 :        1810 :             }
     454                 :             :             // Store as a candidate for reconnection
     455         [ +  - ]:        2048 :             disconnected_coins.insert(utxod->first);
     456                 :             : 
     457                 :             :             // Update the utxoset
     458                 :        2048 :             utxoset.erase(utxod->first);
     459         [ +  + ]:        2048 :             if (!tx.IsCoinBase())
     460         [ +  - ]:        1810 :                 utxoset.insert(tx.vin[0].prevout);
     461                 :             :         }
     462                 :             : 
     463                 :             :         // Once every 1000 iterations and at the end, verify the full cache.
     464   [ +  +  +  + ]:       78838 :         if (m_rng.randrange(1000) == 1 || i == NUM_SIMULATION_ITERATIONS - 1) {
     465         [ +  + ]:      662643 :             for (const auto& entry : result) {
     466         [ +  - ]:      662605 :                 bool have = stack.back()->HaveCoin(entry.first);
     467         [ +  - ]:      662605 :                 const Coin& coin = stack.back()->AccessCoin(entry.first);
     468   [ +  -  +  -  :     1325210 :                 BOOST_CHECK(have == !coin.IsSpent());
                   +  - ]
     469   [ +  -  +  - ]:     1325210 :                 BOOST_CHECK(coin == entry.second);
     470                 :             :             }
     471                 :             :         }
     472                 :             : 
     473                 :             :         // One every 10 iterations, remove a random entry from the cache
     474   [ +  +  +  + ]:       78837 :         if (utxoset.size() > 1 && m_rng.randrange(30) == 0) {
     475         [ +  - ]:        1351 :             stack[m_rng.rand32() % stack.size()]->Uncache(FindRandomFrom(utxoset)->first);
     476                 :             :         }
     477   [ +  +  +  + ]:       77575 :         if (disconnected_coins.size() > 1 && m_rng.randrange(30) == 0) {
     478         [ +  - ]:        1231 :             stack[m_rng.rand32() % stack.size()]->Uncache(FindRandomFrom(disconnected_coins)->first);
     479                 :             :         }
     480   [ +  +  +  + ]:       78678 :         if (duplicate_coins.size() > 1 && m_rng.randrange(30) == 0) {
     481         [ +  - ]:        1344 :             stack[m_rng.rand32() % stack.size()]->Uncache(FindRandomFrom(duplicate_coins)->first);
     482                 :             :         }
     483                 :             : 
     484         [ +  + ]:       39419 :         if (m_rng.randrange(100) == 0) {
     485                 :             :             // Every 100 iterations, flush an intermediate cache
     486   [ +  +  +  + ]:         372 :             if (stack.size() > 1 && m_rng.randbool() == 0) {
     487                 :         173 :                 unsigned int flushIndex = m_rng.randrange(stack.size() - 1);
     488   [ +  -  +  -  :         346 :                 BOOST_CHECK(stack[flushIndex]->Flush());
                   +  - ]
     489                 :             :             }
     490                 :             :         }
     491         [ +  + ]:       39419 :         if (m_rng.randrange(100) == 0) {
     492                 :             :             // Every 100 iterations, change the cache stack.
     493   [ +  -  +  + ]:         398 :             if (stack.size() > 0 && m_rng.randbool() == 0) {
     494   [ +  -  +  -  :         390 :                 BOOST_CHECK(stack.back()->Flush());
                   +  - ]
     495                 :         195 :                 stack.pop_back();
     496                 :             :             }
     497   [ +  +  +  +  :         398 :             if (stack.size() == 0 || (stack.size() < 4 && m_rng.randbool())) {
                   +  + ]
     498                 :         196 :                 CCoinsView* tip = &base;
     499         [ +  + ]:         196 :                 if (stack.size() > 0) {
     500                 :         165 :                     tip = stack.back().get();
     501                 :             :                 }
     502         [ +  - ]:         392 :                 stack.push_back(std::make_unique<CCoinsViewCacheTest>(tip));
     503                 :             :             }
     504                 :             :         }
     505                 :             :     }
     506                 :             : 
     507                 :             :     // Verify coverage.
     508   [ +  -  +  - ]:           2 :     BOOST_CHECK(spent_a_duplicate_coinbase);
     509                 :           1 : }
     510                 :             : 
     511   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(ccoins_serialization)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     512                 :             : {
     513                 :             :     // Good example
     514                 :           1 :     DataStream ss1{"97f23c835800816115944e077fe7c803cfa57f29b36bf87c1d35"_hex};
     515                 :           1 :     Coin cc1;
     516         [ +  - ]:           1 :     ss1 >> cc1;
     517   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(cc1.fCoinBase, false);
     518   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(cc1.nHeight, 203998U);
     519   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(cc1.out.nValue, CAmount{60000000000});
     520   [ +  -  +  -  :           3 :     BOOST_CHECK_EQUAL(HexStr(cc1.out.scriptPubKey), HexStr(GetScriptForDestination(PKHash(uint160("816115944e077fe7c803cfa57f29b36bf87c1d35"_hex_u8)))));
          +  -  +  -  +  
                      - ]
     521                 :             : 
     522                 :             :     // Good example
     523         [ +  - ]:           1 :     DataStream ss2{"8ddf77bbd123008c988f1a4a4de2161e0f50aac7f17e7f9555caa4"_hex};
     524                 :           1 :     Coin cc2;
     525         [ +  - ]:           1 :     ss2 >> cc2;
     526   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(cc2.fCoinBase, true);
     527   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(cc2.nHeight, 120891U);
     528   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(cc2.out.nValue, 110397);
     529   [ +  -  +  -  :           3 :     BOOST_CHECK_EQUAL(HexStr(cc2.out.scriptPubKey), HexStr(GetScriptForDestination(PKHash(uint160("8c988f1a4a4de2161e0f50aac7f17e7f9555caa4"_hex_u8)))));
          +  -  +  -  +  
                      - ]
     530                 :             : 
     531                 :             :     // Smallest possible example
     532         [ +  - ]:           1 :     DataStream ss3{"000006"_hex};
     533                 :           1 :     Coin cc3;
     534         [ +  - ]:           1 :     ss3 >> cc3;
     535   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(cc3.fCoinBase, false);
     536   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(cc3.nHeight, 0U);
     537   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(cc3.out.nValue, 0);
     538   [ +  -  -  +  :           1 :     BOOST_CHECK_EQUAL(cc3.out.scriptPubKey.size(), 0U);
                   +  - ]
     539                 :             : 
     540                 :             :     // scriptPubKey that ends beyond the end of the stream
     541         [ +  - ]:           1 :     DataStream ss4{"000007"_hex};
     542                 :           1 :     try {
     543                 :           1 :         Coin cc4;
     544         [ -  + ]:           1 :         ss4 >> cc4;
     545   [ #  #  #  # ]:           0 :         BOOST_CHECK_MESSAGE(false, "We should have thrown");
     546         [ -  + ]:           1 :     } catch (const std::ios_base::failure&) {
     547                 :           1 :     }
     548                 :             : 
     549                 :             :     // Very large scriptPubKey (3*10^9 bytes) past the end of the stream
     550                 :           1 :     DataStream tmp{};
     551                 :           1 :     uint64_t x = 3000000000ULL;
     552         [ +  - ]:           1 :     tmp << VARINT(x);
     553   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(HexStr(tmp), "8a95c0bb00");
                   +  - ]
     554         [ +  - ]:           1 :     DataStream ss5{"00008a95c0bb00"_hex};
     555                 :           1 :     try {
     556                 :           1 :         Coin cc5;
     557         [ -  + ]:           1 :         ss5 >> cc5;
     558   [ #  #  #  # ]:           0 :         BOOST_CHECK_MESSAGE(false, "We should have thrown");
     559         [ -  + ]:           1 :     } catch (const std::ios_base::failure&) {
     560                 :           1 :     }
     561                 :           1 : }
     562                 :             : 
     563                 :             : const static COutPoint OUTPOINT;
     564                 :             : constexpr CAmount SPENT {-1};
     565                 :             : constexpr CAmount ABSENT{-2};
     566                 :             : constexpr CAmount VALUE1{100};
     567                 :             : constexpr CAmount VALUE2{200};
     568                 :             : constexpr CAmount VALUE3{300};
     569                 :             : 
     570                 :             : struct CoinEntry {
     571                 :             :     enum class State { CLEAN, DIRTY, FRESH, DIRTY_FRESH };
     572                 :             : 
     573                 :         167 :     const CAmount value;
     574                 :         167 :     const State state;
     575                 :             : 
     576                 :         253 :     constexpr CoinEntry(const CAmount v, const State s) : value{v}, state{s} {}
     577                 :             : 
     578   [ +  -  +  -  :         167 :     bool operator==(const CoinEntry& o) const = default;
             +  -  -  + ]
     579                 :           0 :     friend std::ostream& operator<<(std::ostream& os, const CoinEntry& e) { return os << e.value << ", " << e.state; }
     580                 :             : 
     581                 :             :     constexpr bool IsDirtyFresh() const { return state == State::DIRTY_FRESH; }
     582         [ +  + ]:         186 :     constexpr bool IsDirty() const { return state == State::DIRTY || IsDirtyFresh(); }
     583         [ +  + ]:         258 :     constexpr bool IsFresh() const { return state == State::FRESH || IsDirtyFresh(); }
     584                 :             : 
     585                 :         167 :     static constexpr State ToState(const bool is_dirty, const bool is_fresh) {
     586         [ +  + ]:         167 :         if (is_dirty && is_fresh) return State::DIRTY_FRESH;
     587         [ +  + ]:         110 :         if (is_dirty) return State::DIRTY;
     588         [ +  + ]:          43 :         if (is_fresh) return State::FRESH;
     589                 :             :         return State::CLEAN;
     590                 :             :     }
     591                 :             : };
     592                 :             : 
     593                 :             : using MaybeCoin   = std::optional<CoinEntry>;
     594                 :             : using CoinOrError = std::variant<MaybeCoin, std::string>;
     595                 :             : 
     596                 :             : constexpr MaybeCoin MISSING           {std::nullopt};
     597                 :             : constexpr MaybeCoin SPENT_DIRTY       {{SPENT,  CoinEntry::State::DIRTY}};
     598                 :             : constexpr MaybeCoin SPENT_DIRTY_FRESH {{SPENT,  CoinEntry::State::DIRTY_FRESH}};
     599                 :             : constexpr MaybeCoin SPENT_FRESH       {{SPENT,  CoinEntry::State::FRESH}};
     600                 :             : constexpr MaybeCoin SPENT_CLEAN       {{SPENT,  CoinEntry::State::CLEAN}};
     601                 :             : constexpr MaybeCoin VALUE1_DIRTY      {{VALUE1, CoinEntry::State::DIRTY}};
     602                 :             : constexpr MaybeCoin VALUE1_DIRTY_FRESH{{VALUE1, CoinEntry::State::DIRTY_FRESH}};
     603                 :             : constexpr MaybeCoin VALUE1_FRESH      {{VALUE1, CoinEntry::State::FRESH}};
     604                 :             : constexpr MaybeCoin VALUE1_CLEAN      {{VALUE1, CoinEntry::State::CLEAN}};
     605                 :             : constexpr MaybeCoin VALUE2_DIRTY      {{VALUE2, CoinEntry::State::DIRTY}};
     606                 :             : constexpr MaybeCoin VALUE2_DIRTY_FRESH{{VALUE2, CoinEntry::State::DIRTY_FRESH}};
     607                 :             : constexpr MaybeCoin VALUE2_FRESH      {{VALUE2, CoinEntry::State::FRESH}};
     608                 :             : constexpr MaybeCoin VALUE2_CLEAN      {{VALUE2, CoinEntry::State::CLEAN}};
     609                 :             : constexpr MaybeCoin VALUE3_DIRTY      {{VALUE3, CoinEntry::State::DIRTY}};
     610                 :             : constexpr MaybeCoin VALUE3_DIRTY_FRESH{{VALUE3, CoinEntry::State::DIRTY_FRESH}};
     611                 :             : 
     612                 :             : constexpr auto EX_OVERWRITE_UNSPENT{"Attempted to overwrite an unspent coin (when possible_overwrite is false)"};
     613                 :             : constexpr auto EX_FRESH_MISAPPLIED {"FRESH flag misapplied to coin that exists in parent cache"};
     614                 :             : 
     615                 :         320 : static void SetCoinsValue(const CAmount value, Coin& coin)
     616                 :             : {
     617         [ -  + ]:         320 :     assert(value != ABSENT);
     618                 :         320 :     coin.Clear();
     619         [ -  + ]:         320 :     assert(coin.IsSpent());
     620         [ +  + ]:         320 :     if (value != SPENT) {
     621                 :         160 :         coin.out.nValue = value;
     622                 :         160 :         coin.nHeight = 1;
     623                 :         160 :         assert(!coin.IsSpent());
     624                 :             :     }
     625                 :         320 : }
     626                 :             : 
     627                 :         320 : static size_t InsertCoinsMapEntry(CCoinsMap& map, CoinsCachePair& sentinel, const CoinEntry& cache_coin)
     628                 :             : {
     629                 :         320 :     CCoinsCacheEntry entry;
     630                 :         320 :     SetCoinsValue(cache_coin.value, entry.coin);
     631   [ +  -  -  + ]:         320 :     auto [iter, inserted] = map.emplace(OUTPOINT, std::move(entry));
     632         [ -  + ]:         320 :     assert(inserted);
     633   [ +  +  +  - ]:         578 :     if (cache_coin.IsDirty()) CCoinsCacheEntry::SetDirty(*iter, sentinel);
     634   [ +  +  +  + ]:         444 :     if (cache_coin.IsFresh()) CCoinsCacheEntry::SetFresh(*iter, sentinel);
     635         [ -  + ]:         640 :     return iter->second.coin.DynamicMemoryUsage();
     636                 :         320 : }
     637                 :             : 
     638                 :         202 : static MaybeCoin GetCoinsMapEntry(const CCoinsMap& map, const COutPoint& outp = OUTPOINT)
     639                 :             : {
     640         [ +  + ]:         202 :     if (auto it{map.find(outp)}; it != map.end()) {
     641         [ +  + ]:         167 :         return CoinEntry{
     642         [ +  + ]:         167 :             it->second.coin.IsSpent() ? SPENT : it->second.coin.out.nValue,
     643   [ +  +  +  + ]:         384 :             CoinEntry::ToState(it->second.IsDirty(), it->second.IsFresh())};
     644                 :             :     }
     645                 :          35 :     return MISSING;
     646                 :             : }
     647                 :             : 
     648                 :         288 : static void WriteCoinsViewEntry(CCoinsView& view, const MaybeCoin& cache_coin)
     649                 :             : {
     650                 :         288 :     CoinsCachePair sentinel{};
     651         [ +  - ]:         288 :     sentinel.second.SelfRef(sentinel);
     652         [ +  - ]:         288 :     CCoinsMapMemoryResource resource;
     653   [ +  -  +  - ]:         288 :     CCoinsMap map{0, CCoinsMap::hasher{}, CCoinsMap::key_equal{}, &resource};
     654   [ +  +  +  - ]:         288 :     auto usage{cache_coin ? InsertCoinsMapEntry(map, sentinel, *cache_coin) : 0};
     655         [ +  - ]:         288 :     auto cursor{CoinsViewCacheCursor(usage, sentinel, map, /*will_erase=*/true)};
     656   [ +  -  +  +  :         568 :     BOOST_CHECK(view.BatchWrite(cursor, {}));
                   +  - ]
     657                 :         288 : }
     658                 :             : 
     659                 :             : class SingleEntryCacheTest
     660                 :             : {
     661                 :             : public:
     662                 :         198 :     SingleEntryCacheTest(const CAmount base_value, const MaybeCoin& cache_coin)
     663   [ +  -  +  - ]:         198 :     {
     664         [ +  + ]:         198 :         auto base_cache_coin{base_value == ABSENT ? MISSING : CoinEntry{base_value, CoinEntry::State::DIRTY}};
     665         [ +  - ]:         198 :         WriteCoinsViewEntry(base, base_cache_coin);
     666   [ +  +  +  - ]:         198 :         if (cache_coin) cache.usage() += InsertCoinsMapEntry(cache.map(), cache.sentinel(), *cache_coin);
     667                 :         198 :     }
     668                 :             : 
     669                 :             :     CCoinsView root;
     670                 :             :     CCoinsViewCacheTest base{&root};
     671                 :             :     CCoinsViewCacheTest cache{&base};
     672                 :             : };
     673                 :             : 
     674                 :          27 : static void CheckAccessCoin(const CAmount base_value, const MaybeCoin& cache_coin, const MaybeCoin& expected)
     675                 :             : {
     676                 :          27 :     SingleEntryCacheTest test{base_value, cache_coin};
     677         [ +  - ]:          27 :     auto& coin = test.cache.AccessCoin(OUTPOINT);
     678   [ +  -  +  -  :          27 :     BOOST_CHECK_EQUAL(coin.IsSpent(), !test.cache.GetCoin(OUTPOINT));
                   +  - ]
     679         [ +  - ]:          27 :     test.cache.SelfTest(/*sanity_check=*/false);
     680   [ +  -  +  - ]:          27 :     BOOST_CHECK_EQUAL(GetCoinsMapEntry(test.cache.map()), expected);
     681                 :          27 : }
     682                 :             : 
     683   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(ccoins_access)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     684                 :             : {
     685                 :             :     /* Check AccessCoin behavior, requesting a coin from a cache view layered on
     686                 :             :      * top of a base view, and checking the resulting entry in the cache after
     687                 :             :      * the access.
     688                 :             :      *                  Base        Cache               Expected
     689                 :             :      */
     690         [ +  + ]:           4 :     for (auto base_value : {ABSENT, SPENT, VALUE1}) {
     691         [ +  + ]:           5 :         CheckAccessCoin(base_value, MISSING,            base_value == VALUE1 ? VALUE1_CLEAN : MISSING);
     692                 :             : 
     693                 :           3 :         CheckAccessCoin(base_value, SPENT_CLEAN,        SPENT_CLEAN       );
     694                 :           3 :         CheckAccessCoin(base_value, SPENT_FRESH,        SPENT_FRESH       );
     695                 :           3 :         CheckAccessCoin(base_value, SPENT_DIRTY,        SPENT_DIRTY       );
     696                 :           3 :         CheckAccessCoin(base_value, SPENT_DIRTY_FRESH,  SPENT_DIRTY_FRESH );
     697                 :             : 
     698                 :           3 :         CheckAccessCoin(base_value, VALUE2_CLEAN,       VALUE2_CLEAN      );
     699                 :           3 :         CheckAccessCoin(base_value, VALUE2_FRESH,       VALUE2_FRESH      );
     700                 :           3 :         CheckAccessCoin(base_value, VALUE2_DIRTY,       VALUE2_DIRTY      );
     701                 :           3 :         CheckAccessCoin(base_value, VALUE2_DIRTY_FRESH, VALUE2_DIRTY_FRESH);
     702                 :             :     }
     703                 :           1 : }
     704                 :             : 
     705                 :          27 : static void CheckSpendCoins(const CAmount base_value, const MaybeCoin& cache_coin, const MaybeCoin& expected)
     706                 :             : {
     707                 :          27 :     SingleEntryCacheTest test{base_value, cache_coin};
     708         [ +  - ]:          27 :     test.cache.SpendCoin(OUTPOINT);
     709         [ +  - ]:          27 :     test.cache.SelfTest();
     710   [ +  -  +  - ]:          27 :     BOOST_CHECK_EQUAL(GetCoinsMapEntry(test.cache.map()), expected);
     711                 :          27 : }
     712                 :             : 
     713   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(ccoins_spend)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     714                 :             : {
     715                 :             :     /* Check SpendCoin behavior, requesting a coin from a cache view layered on
     716                 :             :      * top of a base view, spending, and then checking
     717                 :             :      * the resulting entry in the cache after the modification.
     718                 :             :      *                  Base        Cache               Expected
     719                 :             :      */
     720         [ +  + ]:           4 :     for (auto base_value : {ABSENT, SPENT, VALUE1}) {
     721         [ +  + ]:           5 :         CheckSpendCoins(base_value, MISSING,            base_value == VALUE1 ? SPENT_DIRTY : MISSING);
     722                 :             : 
     723                 :           3 :         CheckSpendCoins(base_value, SPENT_CLEAN,        SPENT_DIRTY);
     724                 :           3 :         CheckSpendCoins(base_value, SPENT_FRESH,        MISSING    );
     725                 :           3 :         CheckSpendCoins(base_value, SPENT_DIRTY,        SPENT_DIRTY);
     726                 :           3 :         CheckSpendCoins(base_value, SPENT_DIRTY_FRESH,  MISSING    );
     727                 :             : 
     728                 :           3 :         CheckSpendCoins(base_value, VALUE2_CLEAN,       SPENT_DIRTY);
     729                 :           3 :         CheckSpendCoins(base_value, VALUE2_FRESH,       MISSING    );
     730                 :           3 :         CheckSpendCoins(base_value, VALUE2_DIRTY,       SPENT_DIRTY);
     731                 :           3 :         CheckSpendCoins(base_value, VALUE2_DIRTY_FRESH, MISSING    );
     732                 :             :     }
     733                 :           1 : }
     734                 :             : 
     735                 :          54 : static void CheckAddCoin(const CAmount base_value, const MaybeCoin& cache_coin, const CAmount modify_value, const CoinOrError& expected, const bool coinbase)
     736                 :             : {
     737                 :          54 :     SingleEntryCacheTest test{base_value, cache_coin};
     738                 :          54 :     bool possible_overwrite{coinbase};
     739   [ +  -  +  + ]:         120 :     auto add_coin{[&] { test.cache.AddCoin(OUTPOINT, Coin{CTxOut{modify_value, CScript{}}, 1, coinbase}, possible_overwrite); }};
     740         [ +  + ]:          54 :     if (auto* expected_coin{std::get_if<MaybeCoin>(&expected)}) {
     741         [ +  - ]:          42 :         add_coin();
     742         [ +  - ]:          42 :         test.cache.SelfTest();
     743   [ +  -  +  - ]:          42 :         BOOST_CHECK_EQUAL(GetCoinsMapEntry(test.cache.map()), *expected_coin);
     744                 :             :     } else {
     745   [ +  -  -  +  :          24 :         BOOST_CHECK_EXCEPTION(add_coin(), std::logic_error, HasReason(std::get<std::string>(expected)));
          -  -  -  -  -  
          +  +  -  -  +  
             +  -  +  - ]
     746                 :             :     }
     747                 :          54 : }
     748                 :             : 
     749   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(ccoins_add)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     750                 :             : {
     751                 :             :     /* Check AddCoin behavior, requesting a new coin from a cache view,
     752                 :             :      * writing a modification to the coin, and then checking the resulting
     753                 :             :      * entry in the cache after the modification. Verify behavior with the
     754                 :             :      * AddCoin coinbase argument set to false, and to true.
     755                 :             :      *               Base        Cache               Write   Expected              Coinbase
     756                 :             :      */
     757         [ +  + ]:           4 :     for (auto base_value : {ABSENT, SPENT, VALUE1}) {
     758         [ +  - ]:           3 :         CheckAddCoin(base_value, MISSING,            VALUE3, VALUE3_DIRTY_FRESH,   false);
     759         [ +  - ]:           3 :         CheckAddCoin(base_value, MISSING,            VALUE3, VALUE3_DIRTY,         true );
     760                 :             : 
     761         [ +  - ]:           3 :         CheckAddCoin(base_value, SPENT_CLEAN,        VALUE3, VALUE3_DIRTY_FRESH,   false);
     762         [ +  - ]:           3 :         CheckAddCoin(base_value, SPENT_CLEAN,        VALUE3, VALUE3_DIRTY,         true );
     763         [ +  - ]:           3 :         CheckAddCoin(base_value, SPENT_FRESH,        VALUE3, VALUE3_DIRTY_FRESH,   false);
     764         [ +  - ]:           3 :         CheckAddCoin(base_value, SPENT_FRESH,        VALUE3, VALUE3_DIRTY_FRESH,   true );
     765         [ +  - ]:           3 :         CheckAddCoin(base_value, SPENT_DIRTY,        VALUE3, VALUE3_DIRTY,         false);
     766         [ +  - ]:           3 :         CheckAddCoin(base_value, SPENT_DIRTY,        VALUE3, VALUE3_DIRTY,         true );
     767         [ +  - ]:           3 :         CheckAddCoin(base_value, SPENT_DIRTY_FRESH,  VALUE3, VALUE3_DIRTY_FRESH,   false);
     768         [ +  - ]:           3 :         CheckAddCoin(base_value, SPENT_DIRTY_FRESH,  VALUE3, VALUE3_DIRTY_FRESH,   true );
     769                 :             : 
     770         [ +  - ]:           3 :         CheckAddCoin(base_value, VALUE2_CLEAN,       VALUE3, EX_OVERWRITE_UNSPENT, false);
     771         [ +  - ]:           3 :         CheckAddCoin(base_value, VALUE2_CLEAN,       VALUE3, VALUE3_DIRTY,         true );
     772         [ +  - ]:           3 :         CheckAddCoin(base_value, VALUE2_FRESH,       VALUE3, EX_OVERWRITE_UNSPENT, false);
     773         [ +  - ]:           3 :         CheckAddCoin(base_value, VALUE2_FRESH,       VALUE3, VALUE3_DIRTY_FRESH,   true );
     774         [ +  - ]:           3 :         CheckAddCoin(base_value, VALUE2_DIRTY,       VALUE3, EX_OVERWRITE_UNSPENT, false);
     775         [ +  - ]:           3 :         CheckAddCoin(base_value, VALUE2_DIRTY,       VALUE3, VALUE3_DIRTY,         true );
     776         [ +  - ]:           3 :         CheckAddCoin(base_value, VALUE2_DIRTY_FRESH, VALUE3, EX_OVERWRITE_UNSPENT, false);
     777         [ +  - ]:           6 :         CheckAddCoin(base_value, VALUE2_DIRTY_FRESH, VALUE3, VALUE3_DIRTY_FRESH,   true );
     778                 :             :     }
     779                 :           1 : }
     780                 :             : 
     781                 :          90 : static void CheckWriteCoins(const MaybeCoin& parent, const MaybeCoin& child, const CoinOrError& expected)
     782                 :             : {
     783                 :          90 :     SingleEntryCacheTest test{ABSENT, parent};
     784                 :         180 :     auto write_coins{[&] { WriteCoinsViewEntry(test.cache, child); }};
     785         [ +  + ]:          90 :     if (auto* expected_coin{std::get_if<MaybeCoin>(&expected)}) {
     786         [ +  - ]:          82 :         write_coins();
     787         [ +  - ]:          82 :         test.cache.SelfTest(/*sanity_check=*/false);
     788   [ +  -  +  - ]:          82 :         BOOST_CHECK_EQUAL(GetCoinsMapEntry(test.cache.map()), *expected_coin);
     789                 :             :     } else {
     790   [ +  -  -  +  :          16 :         BOOST_CHECK_EXCEPTION(write_coins(), std::logic_error, HasReason(std::get<std::string>(expected)));
          -  -  -  -  -  
          +  +  -  -  +  
             +  -  +  - ]
     791                 :             :     }
     792                 :          90 : }
     793                 :             : 
     794   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(ccoins_write)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     795                 :             : {
     796                 :             :     /* Check BatchWrite behavior, flushing one entry from a child cache to a
     797                 :             :      * parent cache, and checking the resulting entry in the parent cache
     798                 :             :      * after the write.
     799                 :             :      *              Parent              Child               Expected
     800                 :             :      */
     801         [ +  - ]:           1 :     CheckWriteCoins(MISSING,            MISSING,            MISSING            );
     802         [ +  - ]:           1 :     CheckWriteCoins(MISSING,            SPENT_DIRTY,        SPENT_DIRTY        );
     803         [ +  - ]:           1 :     CheckWriteCoins(MISSING,            SPENT_DIRTY_FRESH,  MISSING            );
     804         [ +  - ]:           1 :     CheckWriteCoins(MISSING,            VALUE2_DIRTY,       VALUE2_DIRTY       );
     805         [ +  - ]:           1 :     CheckWriteCoins(MISSING,            VALUE2_DIRTY_FRESH, VALUE2_DIRTY_FRESH );
     806         [ +  - ]:           1 :     CheckWriteCoins(SPENT_CLEAN,        MISSING,            SPENT_CLEAN        );
     807         [ +  - ]:           1 :     CheckWriteCoins(SPENT_FRESH,        MISSING,            SPENT_FRESH        );
     808         [ +  - ]:           1 :     CheckWriteCoins(SPENT_DIRTY,        MISSING,            SPENT_DIRTY        );
     809         [ +  - ]:           1 :     CheckWriteCoins(SPENT_DIRTY_FRESH,  MISSING,            SPENT_DIRTY_FRESH  );
     810                 :             : 
     811         [ +  - ]:           1 :     CheckWriteCoins(SPENT_CLEAN,        SPENT_DIRTY,        SPENT_DIRTY        );
     812         [ +  - ]:           1 :     CheckWriteCoins(SPENT_CLEAN,        SPENT_DIRTY_FRESH,  SPENT_DIRTY        );
     813         [ +  - ]:           1 :     CheckWriteCoins(SPENT_FRESH,        SPENT_DIRTY,        MISSING            );
     814         [ +  - ]:           1 :     CheckWriteCoins(SPENT_FRESH,        SPENT_DIRTY_FRESH,  MISSING            );
     815         [ +  - ]:           1 :     CheckWriteCoins(SPENT_DIRTY,        SPENT_DIRTY,        SPENT_DIRTY        );
     816         [ +  - ]:           1 :     CheckWriteCoins(SPENT_DIRTY,        SPENT_DIRTY_FRESH,  SPENT_DIRTY        );
     817         [ +  - ]:           1 :     CheckWriteCoins(SPENT_DIRTY_FRESH,  SPENT_DIRTY,        MISSING            );
     818         [ +  - ]:           1 :     CheckWriteCoins(SPENT_DIRTY_FRESH,  SPENT_DIRTY_FRESH,  MISSING            );
     819                 :             : 
     820         [ +  - ]:           1 :     CheckWriteCoins(SPENT_CLEAN,        VALUE2_DIRTY,       VALUE2_DIRTY       );
     821         [ +  - ]:           1 :     CheckWriteCoins(SPENT_CLEAN,        VALUE2_DIRTY_FRESH, VALUE2_DIRTY       );
     822         [ +  - ]:           1 :     CheckWriteCoins(SPENT_FRESH,        VALUE2_DIRTY,       VALUE2_DIRTY_FRESH );
     823         [ +  - ]:           1 :     CheckWriteCoins(SPENT_FRESH,        VALUE2_DIRTY_FRESH, VALUE2_DIRTY_FRESH );
     824         [ +  - ]:           1 :     CheckWriteCoins(SPENT_DIRTY,        VALUE2_DIRTY,       VALUE2_DIRTY       );
     825         [ +  - ]:           1 :     CheckWriteCoins(SPENT_DIRTY,        VALUE2_DIRTY_FRESH, VALUE2_DIRTY       );
     826         [ +  - ]:           1 :     CheckWriteCoins(SPENT_DIRTY_FRESH,  VALUE2_DIRTY,       VALUE2_DIRTY_FRESH );
     827         [ +  - ]:           1 :     CheckWriteCoins(SPENT_DIRTY_FRESH,  VALUE2_DIRTY_FRESH, VALUE2_DIRTY_FRESH );
     828                 :             : 
     829         [ +  - ]:           1 :     CheckWriteCoins(VALUE1_CLEAN,       MISSING,            VALUE1_CLEAN       );
     830         [ +  - ]:           1 :     CheckWriteCoins(VALUE1_FRESH,       MISSING,            VALUE1_FRESH       );
     831         [ +  - ]:           1 :     CheckWriteCoins(VALUE1_DIRTY,       MISSING,            VALUE1_DIRTY       );
     832         [ +  - ]:           1 :     CheckWriteCoins(VALUE1_DIRTY_FRESH, MISSING,            VALUE1_DIRTY_FRESH );
     833         [ +  - ]:           1 :     CheckWriteCoins(VALUE1_CLEAN,       SPENT_DIRTY,        SPENT_DIRTY        );
     834         [ +  - ]:           1 :     CheckWriteCoins(VALUE1_CLEAN,       SPENT_DIRTY_FRESH,  EX_FRESH_MISAPPLIED);
     835         [ +  - ]:           1 :     CheckWriteCoins(VALUE1_FRESH,       SPENT_DIRTY,        MISSING            );
     836         [ +  - ]:           1 :     CheckWriteCoins(VALUE1_FRESH,       SPENT_DIRTY_FRESH,  EX_FRESH_MISAPPLIED);
     837         [ +  - ]:           1 :     CheckWriteCoins(VALUE1_DIRTY,       SPENT_DIRTY,        SPENT_DIRTY        );
     838         [ +  - ]:           1 :     CheckWriteCoins(VALUE1_DIRTY,       SPENT_DIRTY_FRESH,  EX_FRESH_MISAPPLIED);
     839         [ +  - ]:           1 :     CheckWriteCoins(VALUE1_DIRTY_FRESH, SPENT_DIRTY,        MISSING            );
     840         [ +  - ]:           1 :     CheckWriteCoins(VALUE1_DIRTY_FRESH, SPENT_DIRTY_FRESH,  EX_FRESH_MISAPPLIED);
     841                 :             : 
     842         [ +  - ]:           1 :     CheckWriteCoins(VALUE1_CLEAN,       VALUE2_DIRTY,       VALUE2_DIRTY       );
     843         [ +  - ]:           1 :     CheckWriteCoins(VALUE1_CLEAN,       VALUE2_DIRTY_FRESH, EX_FRESH_MISAPPLIED);
     844         [ +  - ]:           1 :     CheckWriteCoins(VALUE1_FRESH,       VALUE2_DIRTY,       VALUE2_DIRTY_FRESH );
     845         [ +  - ]:           1 :     CheckWriteCoins(VALUE1_FRESH,       VALUE2_DIRTY_FRESH, EX_FRESH_MISAPPLIED);
     846         [ +  - ]:           1 :     CheckWriteCoins(VALUE1_DIRTY,       VALUE2_DIRTY,       VALUE2_DIRTY       );
     847         [ +  - ]:           1 :     CheckWriteCoins(VALUE1_DIRTY,       VALUE2_DIRTY_FRESH, EX_FRESH_MISAPPLIED);
     848         [ +  - ]:           1 :     CheckWriteCoins(VALUE1_DIRTY_FRESH, VALUE2_DIRTY,       VALUE2_DIRTY_FRESH );
     849         [ +  - ]:           1 :     CheckWriteCoins(VALUE1_DIRTY_FRESH, VALUE2_DIRTY_FRESH, EX_FRESH_MISAPPLIED);
     850                 :             : 
     851                 :             :     // The checks above omit cases where the child state is not DIRTY, since
     852                 :             :     // they would be too repetitive (the parent cache is never updated in these
     853                 :             :     // cases). The loop below covers these cases and makes sure the parent cache
     854                 :             :     // is always left unchanged.
     855                 :           9 :     for (const MaybeCoin& parent : {MISSING,
     856                 :             :                                     SPENT_CLEAN, SPENT_DIRTY, SPENT_FRESH, SPENT_DIRTY_FRESH,
     857         [ +  + ]:          10 :                                     VALUE1_CLEAN, VALUE1_DIRTY, VALUE1_FRESH, VALUE1_DIRTY_FRESH}) {
     858                 :          99 :         for (const MaybeCoin& child : {MISSING,
     859                 :             :                                        SPENT_CLEAN, SPENT_FRESH,
     860         [ +  + ]:          54 :                                        VALUE2_CLEAN, VALUE2_FRESH}) {
     861         [ +  - ]:          45 :             auto expected{CoinOrError{parent}}; // TODO test failure cases as well
     862         [ +  - ]:          45 :             CheckWriteCoins(parent, child, expected);
     863                 :          45 :         }
     864                 :             :     }
     865                 :           1 : }
     866                 :             : 
     867                 :           2 : struct FlushTest : BasicTestingSetup {
     868                 :          12 : Coin MakeCoin()
     869                 :             : {
     870                 :          12 :     Coin coin;
     871                 :          12 :     coin.out.nValue = m_rng.rand32();
     872                 :          12 :     coin.nHeight = m_rng.randrange(4096);
     873                 :          12 :     coin.fCoinBase = 0;
     874                 :          12 :     return coin;
     875                 :             : }
     876                 :             : 
     877                 :             : 
     878                 :             : //! For CCoinsViewCache instances backed by either another cache instance or
     879                 :             : //! leveldb, test cache behavior and flag state (DIRTY/FRESH) by
     880                 :             : //!
     881                 :             : //! 1. Adding a random coin to the child-most cache,
     882                 :             : //! 2. Flushing all caches (without erasing),
     883                 :             : //! 3. Ensure the entry still exists in the cache and has been written to parent,
     884                 :             : //! 4. (if `do_erasing_flush`) Flushing the caches again (with erasing),
     885                 :             : //! 5. (if `do_erasing_flush`) Ensure the entry has been written to the parent and is no longer in the cache,
     886                 :             : //! 6. Spend the coin, ensure it no longer exists in the parent.
     887                 :             : //!
     888                 :           4 : void TestFlushBehavior(
     889                 :             :     CCoinsViewCacheTest* view,
     890                 :             :     CCoinsViewDB& base,
     891                 :             :     std::vector<std::unique_ptr<CCoinsViewCacheTest>>& all_caches,
     892                 :             :     bool do_erasing_flush)
     893                 :             : {
     894                 :           4 :     size_t cache_usage;
     895                 :           4 :     size_t cache_size;
     896                 :             : 
     897                 :          22 :     auto flush_all = [this, &all_caches](bool erase) {
     898                 :             :         // Flush in reverse order to ensure that flushes happen from children up.
     899         [ +  + ]:          54 :         for (auto i = all_caches.rbegin(); i != all_caches.rend(); ++i) {
     900                 :          36 :             auto& cache = *i;
     901                 :          36 :             cache->SanityCheck();
     902                 :             :             // hashBlock must be filled before flushing to disk; value is
     903                 :             :             // unimportant here. This is normally done during connect/disconnect block.
     904                 :          36 :             cache->SetBestBlock(m_rng.rand256());
     905         [ +  + ]:          36 :             erase ? cache->Flush() : cache->Sync();
     906                 :             :         }
     907                 :          22 :     };
     908                 :             : 
     909                 :           4 :     Txid txid = Txid::FromUint256(m_rng.rand256());
     910                 :           4 :     COutPoint outp = COutPoint(txid, 0);
     911                 :           4 :     Coin coin = MakeCoin();
     912                 :             :     // Ensure the coins views haven't seen this coin before.
     913   [ +  -  +  -  :           8 :     BOOST_CHECK(!base.HaveCoin(outp));
             +  -  +  - ]
     914   [ +  -  +  -  :           8 :     BOOST_CHECK(!view->HaveCoin(outp));
                   +  - ]
     915                 :             : 
     916                 :             :     // --- 1. Adding a random coin to the child cache
     917                 :             :     //
     918         [ +  - ]:           4 :     view->AddCoin(outp, Coin(coin), false);
     919                 :             : 
     920         [ +  - ]:           4 :     cache_usage = view->DynamicMemoryUsage();
     921         [ +  - ]:           4 :     cache_size = view->map().size();
     922                 :             : 
     923                 :             :     // `base` shouldn't have coin (no flush yet) but `view` should have cached it.
     924   [ +  -  +  -  :           8 :     BOOST_CHECK(!base.HaveCoin(outp));
             +  -  +  - ]
     925   [ +  -  +  -  :           8 :     BOOST_CHECK(view->HaveCoin(outp));
             +  -  +  - ]
     926                 :             : 
     927   [ +  -  +  - ]:           4 :     BOOST_CHECK_EQUAL(GetCoinsMapEntry(view->map(), outp), CoinEntry(coin.out.nValue, CoinEntry::State::DIRTY_FRESH));
     928                 :             : 
     929                 :             :     // --- 2. Flushing all caches (without erasing)
     930                 :             :     //
     931         [ +  - ]:           4 :     flush_all(/*erase=*/ false);
     932                 :             : 
     933                 :             :     // CoinsMap usage should be unchanged since we didn't erase anything.
     934   [ +  -  +  -  :           4 :     BOOST_CHECK_EQUAL(cache_usage, view->DynamicMemoryUsage());
                   +  - ]
     935   [ +  -  +  - ]:           4 :     BOOST_CHECK_EQUAL(cache_size, view->map().size());
     936                 :             : 
     937                 :             :     // --- 3. Ensuring the entry still exists in the cache and has been written to parent
     938                 :             :     //
     939   [ +  -  +  - ]:           4 :     BOOST_CHECK_EQUAL(GetCoinsMapEntry(view->map(), outp), CoinEntry(coin.out.nValue, CoinEntry::State::CLEAN)); // State should have been wiped.
     940                 :             : 
     941                 :             :     // Both views should now have the coin.
     942   [ +  -  +  -  :           8 :     BOOST_CHECK(base.HaveCoin(outp));
             +  -  +  - ]
     943   [ +  -  +  -  :           8 :     BOOST_CHECK(view->HaveCoin(outp));
             +  -  +  + ]
     944                 :             : 
     945         [ +  + ]:           4 :     if (do_erasing_flush) {
     946                 :             :         // --- 4. Flushing the caches again (with erasing)
     947                 :             :         //
     948         [ +  - ]:           2 :         flush_all(/*erase=*/ true);
     949                 :             : 
     950                 :             :         // Memory does not necessarily go down due to the map using a memory pool
     951   [ +  -  +  -  :           4 :         BOOST_TEST(view->DynamicMemoryUsage() <= cache_usage);
          +  -  +  -  +  
                      - ]
     952                 :             :         // Size of the cache must go down though
     953   [ +  -  +  -  :           4 :         BOOST_TEST(view->map().size() < cache_size);
             +  -  +  - ]
     954                 :             : 
     955                 :             :         // --- 5. Ensuring the entry is no longer in the cache
     956                 :             :         //
     957   [ +  -  +  -  :           4 :         BOOST_CHECK(!GetCoinsMapEntry(view->map(), outp));
                   +  - ]
     958         [ +  - ]:           2 :         view->AccessCoin(outp);
     959   [ +  -  +  - ]:           2 :         BOOST_CHECK_EQUAL(GetCoinsMapEntry(view->map(), outp), CoinEntry(coin.out.nValue, CoinEntry::State::CLEAN));
     960                 :             :     }
     961                 :             : 
     962                 :             :     // Can't overwrite an entry without specifying that an overwrite is
     963                 :             :     // expected.
     964   [ +  -  -  +  :          12 :     BOOST_CHECK_THROW(
          -  -  -  -  -  
             +  +  -  +  
                      - ]
     965                 :             :         view->AddCoin(outp, Coin(coin), /*possible_overwrite=*/ false),
     966                 :             :         std::logic_error);
     967                 :             : 
     968                 :             :     // --- 6. Spend the coin.
     969                 :             :     //
     970   [ +  -  +  -  :           8 :     BOOST_CHECK(view->SpendCoin(outp));
             +  -  +  - ]
     971                 :             : 
     972                 :             :     // The coin should be in the cache, but spent and marked dirty.
     973   [ +  -  +  - ]:           4 :     BOOST_CHECK_EQUAL(GetCoinsMapEntry(view->map(), outp), SPENT_DIRTY);
     974   [ +  -  +  -  :           8 :     BOOST_CHECK(!view->HaveCoin(outp)); // Coin should be considered spent in `view`.
             +  -  +  - ]
     975   [ +  -  +  -  :           8 :     BOOST_CHECK(base.HaveCoin(outp));  // But coin should still be unspent in `base`.
             +  -  +  - ]
     976                 :             : 
     977         [ +  - ]:           4 :     flush_all(/*erase=*/ false);
     978                 :             : 
     979                 :             :     // Coin should be considered spent in both views.
     980   [ +  -  +  -  :           8 :     BOOST_CHECK(!view->HaveCoin(outp));
             +  -  +  - ]
     981   [ +  -  +  -  :           8 :     BOOST_CHECK(!base.HaveCoin(outp));
             +  -  +  - ]
     982                 :             : 
     983                 :             :     // Spent coin should not be spendable.
     984   [ +  -  +  -  :           8 :     BOOST_CHECK(!view->SpendCoin(outp));
                   +  - ]
     985                 :             : 
     986                 :             :     // --- Bonus check: ensure that a coin added to the base view via one cache
     987                 :             :     //     can be spent by another cache which has never seen it.
     988                 :             :     //
     989                 :           4 :     txid = Txid::FromUint256(m_rng.rand256());
     990                 :           4 :     outp = COutPoint(txid, 0);
     991                 :           4 :     coin = MakeCoin();
     992   [ +  -  +  -  :           8 :     BOOST_CHECK(!base.HaveCoin(outp));
             +  -  +  - ]
     993   [ +  -  +  -  :           8 :     BOOST_CHECK(!all_caches[0]->HaveCoin(outp));
             +  -  +  - ]
     994   [ +  -  +  -  :           8 :     BOOST_CHECK(!all_caches[1]->HaveCoin(outp));
             +  -  +  - ]
     995                 :             : 
     996         [ +  - ]:           4 :     all_caches[0]->AddCoin(outp, std::move(coin), false);
     997         [ +  - ]:           4 :     all_caches[0]->Sync();
     998   [ +  -  +  -  :           8 :     BOOST_CHECK(base.HaveCoin(outp));
             +  -  +  - ]
     999   [ +  -  +  -  :           8 :     BOOST_CHECK(all_caches[0]->HaveCoin(outp));
             +  -  +  - ]
    1000   [ +  -  +  -  :           8 :     BOOST_CHECK(!all_caches[1]->HaveCoinInCache(outp));
             +  -  +  - ]
    1001                 :             : 
    1002   [ +  -  +  -  :           8 :     BOOST_CHECK(all_caches[1]->SpendCoin(outp));
             +  -  +  - ]
    1003         [ +  - ]:           4 :     flush_all(/*erase=*/ false);
    1004   [ +  -  +  -  :           8 :     BOOST_CHECK(!base.HaveCoin(outp));
             +  -  +  - ]
    1005   [ +  -  +  -  :           8 :     BOOST_CHECK(!all_caches[0]->HaveCoin(outp));
             +  -  +  - ]
    1006   [ +  -  +  -  :           8 :     BOOST_CHECK(!all_caches[1]->HaveCoin(outp));
             +  -  +  - ]
    1007                 :             : 
    1008         [ +  - ]:           4 :     flush_all(/*erase=*/ true); // Erase all cache content.
    1009                 :             : 
    1010                 :             :     // --- Bonus check 2: ensure that a FRESH, spent coin is deleted by Sync()
    1011                 :             :     //
    1012                 :           4 :     txid = Txid::FromUint256(m_rng.rand256());
    1013                 :           4 :     outp = COutPoint(txid, 0);
    1014                 :           4 :     coin = MakeCoin();
    1015                 :           4 :     CAmount coin_val = coin.out.nValue;
    1016   [ +  -  +  -  :           8 :     BOOST_CHECK(!base.HaveCoin(outp));
             +  -  +  - ]
    1017   [ +  -  +  -  :           8 :     BOOST_CHECK(!all_caches[0]->HaveCoin(outp));
             +  -  +  - ]
    1018   [ +  -  +  -  :           8 :     BOOST_CHECK(!all_caches[1]->HaveCoin(outp));
             +  -  +  - ]
    1019                 :             : 
    1020                 :             :     // Add and spend from same cache without flushing.
    1021         [ +  - ]:           4 :     all_caches[0]->AddCoin(outp, std::move(coin), false);
    1022                 :             : 
    1023                 :             :     // Coin should be FRESH in the cache.
    1024   [ +  -  +  - ]:           4 :     BOOST_CHECK_EQUAL(GetCoinsMapEntry(all_caches[0]->map(), outp), CoinEntry(coin_val, CoinEntry::State::DIRTY_FRESH));
    1025                 :             :     // Base shouldn't have seen coin.
    1026   [ +  -  +  -  :           8 :     BOOST_CHECK(!base.HaveCoin(outp));
             +  -  +  - ]
    1027                 :             : 
    1028   [ +  -  +  -  :           8 :     BOOST_CHECK(all_caches[0]->SpendCoin(outp));
             +  -  +  - ]
    1029         [ +  - ]:           4 :     all_caches[0]->Sync();
    1030                 :             : 
    1031                 :             :     // Ensure there is no sign of the coin after spend/flush.
    1032   [ +  -  +  -  :           8 :     BOOST_CHECK(!GetCoinsMapEntry(all_caches[0]->map(), outp));
                   +  - ]
    1033   [ +  -  +  -  :           8 :     BOOST_CHECK(!all_caches[0]->HaveCoinInCache(outp));
             +  -  +  - ]
    1034   [ +  -  +  -  :           8 :     BOOST_CHECK(!base.HaveCoin(outp));
                   +  - ]
    1035                 :           4 : }
    1036                 :             : }; // struct FlushTest
    1037                 :             : 
    1038   [ +  -  +  -  :           7 : BOOST_FIXTURE_TEST_CASE(ccoins_flush_behavior, FlushTest)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
    1039                 :             : {
    1040                 :             :     // Create two in-memory caches atop a leveldb view.
    1041                 :           0 :     CCoinsViewDB base{{.path = "test", .cache_bytes = 1 << 23, .memory_only = true}, {}};
    1042                 :           1 :     std::vector<std::unique_ptr<CCoinsViewCacheTest>> caches;
    1043         [ +  - ]:           2 :     caches.push_back(std::make_unique<CCoinsViewCacheTest>(&base));
    1044         [ +  - ]:           2 :     caches.push_back(std::make_unique<CCoinsViewCacheTest>(caches.back().get()));
    1045                 :             : 
    1046         [ +  + ]:           3 :     for (const auto& view : caches) {
    1047         [ +  - ]:           2 :         TestFlushBehavior(view.get(), base, caches, /*do_erasing_flush=*/false);
    1048         [ +  - ]:           2 :         TestFlushBehavior(view.get(), base, caches, /*do_erasing_flush=*/true);
    1049                 :             :     }
    1050         [ +  - ]:           2 : }
    1051                 :             : 
    1052   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(coins_resource_is_used)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
    1053                 :             : {
    1054                 :           1 :     CCoinsMapMemoryResource resource;
    1055         [ +  - ]:           1 :     PoolResourceTester::CheckAllDataAccountedFor(resource);
    1056                 :             : 
    1057                 :           1 :     {
    1058   [ +  -  +  - ]:           1 :         CCoinsMap map{0, CCoinsMap::hasher{}, CCoinsMap::key_equal{}, &resource};
    1059   [ +  -  +  -  :           2 :         BOOST_TEST(memusage::DynamicUsage(map) >= resource.ChunkSizeBytes());
             +  -  +  - ]
    1060                 :             : 
    1061         [ +  - ]:           1 :         map.reserve(1000);
    1062                 :             : 
    1063                 :             :         // The resource has preallocated a chunk, so we should have space for at several nodes without the need to allocate anything else.
    1064                 :           1 :         const auto usage_before = memusage::DynamicUsage(map);
    1065                 :             : 
    1066                 :           1 :         COutPoint out_point{};
    1067         [ +  + ]:        1001 :         for (size_t i = 0; i < 1000; ++i) {
    1068                 :        1000 :             out_point.n = i;
    1069         [ +  - ]:        1000 :             map[out_point];
    1070                 :             :         }
    1071   [ +  -  +  -  :           2 :         BOOST_TEST(usage_before == memusage::DynamicUsage(map));
                   +  - ]
    1072                 :           0 :     }
    1073                 :             : 
    1074         [ +  - ]:           1 :     PoolResourceTester::CheckAllDataAccountedFor(resource);
    1075                 :           1 : }
    1076                 :             : 
    1077                 :             : BOOST_AUTO_TEST_SUITE_END()
        

Generated by: LCOV version 2.0-1