LCOV - code coverage report
Current view: top level - src/test/fuzz - coinscache_sim.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 97.3 % 255 248
Test Date: 2026-04-21 06:07:39 Functions: 100.0 % 24 24
Branches: 59.9 % 272 163

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2023-present 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 <coins.h>
       6                 :             : #include <crypto/sha256.h>
       7                 :             : #include <primitives/transaction.h>
       8                 :             : #include <test/fuzz/FuzzedDataProvider.h>
       9                 :             : #include <test/fuzz/fuzz.h>
      10                 :             : #include <test/fuzz/util.h>
      11                 :             : 
      12                 :             : #include <cassert>
      13                 :             : #include <cstdint>
      14                 :             : #include <memory>
      15                 :             : #include <optional>
      16                 :             : #include <vector>
      17                 :             : 
      18                 :             : namespace {
      19                 :             : 
      20                 :             : /** Number of distinct COutPoint values used in this test. */
      21                 :             : constexpr uint32_t NUM_OUTPOINTS = 256;
      22                 :             : /** Number of distinct Coin values used in this test (ignoring nHeight). */
      23                 :             : constexpr uint32_t NUM_COINS = 256;
      24                 :             : /** Maximum number CCoinsViewCache objects used in this test. */
      25                 :             : constexpr uint32_t MAX_CACHES = 4;
      26                 :             : /** Data type large enough to hold NUM_COINS-1. */
      27                 :             : using coinidx_type = uint8_t;
      28                 :             : 
      29                 :             : struct PrecomputedData
      30                 :             : {
      31                 :             :     //! Randomly generated COutPoint values.
      32                 :             :     COutPoint outpoints[NUM_OUTPOINTS];
      33                 :             : 
      34                 :             :     //! Randomly generated Coin values.
      35                 :             :     Coin coins[NUM_COINS];
      36                 :             : 
      37                 :           1 :     PrecomputedData()
      38   [ +  +  +  +  :         513 :     {
                   -  - ]
      39                 :             :         static const uint8_t PREFIX_O[1] = {'o'}; /** Hash prefix for outpoint hashes. */
      40                 :             :         static const uint8_t PREFIX_S[1] = {'s'}; /** Hash prefix for coins scriptPubKeys. */
      41                 :             :         static const uint8_t PREFIX_M[1] = {'m'}; /** Hash prefix for coins nValue/fCoinBase. */
      42                 :             : 
      43         [ +  + ]:         257 :         for (uint32_t i = 0; i < NUM_OUTPOINTS; ++i) {
      44                 :         256 :             uint32_t idx = (i * 1200U) >> 12; /* Map 3 or 4 entries to same txid. */
      45                 :         256 :             const uint8_t ser[4] = {uint8_t(idx), uint8_t(idx >> 8), uint8_t(idx >> 16), uint8_t(idx >> 24)};
      46                 :         256 :             uint256 txid;
      47   [ +  -  +  -  :         256 :             CSHA256().Write(PREFIX_O, 1).Write(ser, sizeof(ser)).Finalize(txid.begin());
             +  -  +  - ]
      48                 :         256 :             outpoints[i].hash = Txid::FromUint256(txid);
      49                 :         256 :             outpoints[i].n = i;
      50                 :             :         }
      51                 :             : 
      52         [ +  + ]:         257 :         for (uint32_t i = 0; i < NUM_COINS; ++i) {
      53                 :         256 :             const uint8_t ser[4] = {uint8_t(i), uint8_t(i >> 8), uint8_t(i >> 16), uint8_t(i >> 24)};
      54                 :         256 :             uint256 hash;
      55   [ +  -  +  -  :         256 :             CSHA256().Write(PREFIX_S, 1).Write(ser, sizeof(ser)).Finalize(hash.begin());
             +  -  +  - ]
      56                 :             :             /* Convert hash to scriptPubkeys (of different lengths, so SanityCheck's cached memory
      57                 :             :              * usage check has a chance to detect mismatches). */
      58   [ +  +  +  +  :         256 :             switch (i % 5U) {
                   +  - ]
      59                 :          52 :             case 0: /* P2PKH */
      60                 :          52 :                 coins[i].out.scriptPubKey.resize(25);
      61         [ +  - ]:          52 :                 coins[i].out.scriptPubKey[0] = OP_DUP;
      62         [ +  - ]:          52 :                 coins[i].out.scriptPubKey[1] = OP_HASH160;
      63         [ +  - ]:          52 :                 coins[i].out.scriptPubKey[2] = 20;
      64         [ +  - ]:         104 :                 std::copy(hash.begin(), hash.begin() + 20, coins[i].out.scriptPubKey.begin() + 3);
      65         [ +  - ]:          52 :                 coins[i].out.scriptPubKey[23] = OP_EQUALVERIFY;
      66         [ +  - ]:          52 :                 coins[i].out.scriptPubKey[24] = OP_CHECKSIG;
      67                 :          52 :                 break;
      68                 :          51 :             case 1: /* P2SH */
      69                 :          51 :                 coins[i].out.scriptPubKey.resize(23);
      70         [ +  - ]:          51 :                 coins[i].out.scriptPubKey[0] = OP_HASH160;
      71         [ +  - ]:          51 :                 coins[i].out.scriptPubKey[1] = 20;
      72         [ +  - ]:         102 :                 std::copy(hash.begin(), hash.begin() + 20, coins[i].out.scriptPubKey.begin() + 2);
      73         [ +  - ]:          51 :                 coins[i].out.scriptPubKey[12] = OP_EQUAL;
      74                 :          51 :                 break;
      75                 :          51 :             case 2: /* P2WPKH */
      76                 :          51 :                 coins[i].out.scriptPubKey.resize(22);
      77         [ +  - ]:          51 :                 coins[i].out.scriptPubKey[0] = OP_0;
      78         [ +  - ]:          51 :                 coins[i].out.scriptPubKey[1] = 20;
      79         [ +  - ]:         102 :                 std::copy(hash.begin(), hash.begin() + 20, coins[i].out.scriptPubKey.begin() + 2);
      80                 :             :                 break;
      81                 :          51 :             case 3: /* P2WSH */
      82                 :          51 :                 coins[i].out.scriptPubKey.resize(34);
      83         [ +  - ]:          51 :                 coins[i].out.scriptPubKey[0] = OP_0;
      84         [ +  - ]:          51 :                 coins[i].out.scriptPubKey[1] = 32;
      85         [ +  - ]:         102 :                 std::copy(hash.begin(), hash.begin() + 32, coins[i].out.scriptPubKey.begin() + 2);
      86                 :             :                 break;
      87                 :          51 :             case 4: /* P2TR */
      88                 :          51 :                 coins[i].out.scriptPubKey.resize(34);
      89         [ +  - ]:          51 :                 coins[i].out.scriptPubKey[0] = OP_1;
      90         [ +  - ]:          51 :                 coins[i].out.scriptPubKey[1] = 32;
      91         [ +  - ]:         102 :                 std::copy(hash.begin(), hash.begin() + 32, coins[i].out.scriptPubKey.begin() + 2);
      92                 :             :                 break;
      93                 :             :             }
      94                 :             :             /* Hash again to construct nValue and fCoinBase. */
      95   [ +  -  +  -  :         256 :             CSHA256().Write(PREFIX_M, 1).Write(ser, sizeof(ser)).Finalize(hash.begin());
             +  -  +  - ]
      96                 :         256 :             coins[i].out.nValue = CAmount(hash.GetUint64(0) % MAX_MONEY);
      97                 :         256 :             coins[i].fCoinBase = (hash.GetUint64(1) & 7) == 0;
      98                 :         256 :             coins[i].nHeight = 0; /* Real nHeight used in simulation is set dynamically. */
      99                 :             :         }
     100         [ -  - ]:           1 :     }
     101                 :             : };
     102                 :             : 
     103                 :             : enum class EntryType : uint8_t
     104                 :             : {
     105                 :             :     /* This entry in the cache does not exist (so we'd have to look in the parent cache). */
     106                 :             :     NONE,
     107                 :             : 
     108                 :             :     /* This entry in the cache corresponds to an unspent coin. */
     109                 :             :     UNSPENT,
     110                 :             : 
     111                 :             :     /* This entry in the cache corresponds to a spent coin. */
     112                 :             :     SPENT,
     113                 :             : };
     114                 :             : 
     115                 :             : struct CacheEntry
     116                 :             : {
     117                 :             :     /* Type of entry. */
     118                 :             :     EntryType entrytype;
     119                 :             : 
     120                 :             :     /* Index in the coins array this entry corresponds to (only if entrytype == UNSPENT). */
     121                 :             :     coinidx_type coinidx;
     122                 :             : 
     123                 :             :     /* nHeight value for this entry (so the coins[coinidx].nHeight value is ignored; only if entrytype == UNSPENT). */
     124                 :             :     uint32_t height;
     125                 :             : };
     126                 :             : 
     127                 :             : struct CacheLevel
     128                 :             : {
     129                 :             :     CacheEntry entry[NUM_OUTPOINTS];
     130                 :             : 
     131                 :       46699 :     void Wipe() {
     132   [ +  +  +  +  :    12001643 :         for (uint32_t i = 0; i < NUM_OUTPOINTS; ++i) {
             +  +  +  + ]
     133                 :    11954944 :             entry[i].entrytype = EntryType::NONE;
     134                 :             :         }
     135                 :             :     }
     136                 :             : };
     137                 :             : 
     138                 :             : /** Class for the base of the hierarchy (roughly simulating a memory-backed CCoinsViewDB).
     139                 :             :  *
     140                 :             :  * The initial state consists of the empty UTXO set.
     141                 :             :  */
     142                 :         305 : class CoinsViewBottom final : public CoinsViewEmpty
     143                 :             : {
     144                 :             :     std::map<COutPoint, Coin> m_data;
     145                 :             : 
     146                 :             : public:
     147                 :      291806 :     std::optional<Coin> GetCoin(const COutPoint& outpoint) const final
     148                 :             :     {
     149         [ +  + ]:      291806 :         if (auto it{m_data.find(outpoint)}; it != m_data.end()) {
     150         [ -  + ]:       39376 :             assert(!it->second.IsSpent());
     151                 :       39376 :             return it->second;
     152                 :             :         }
     153                 :      252430 :         return std::nullopt;
     154                 :             :     }
     155                 :             : 
     156                 :       32658 :     void BatchWrite(CoinsViewCacheCursor& cursor, const uint256&) final
     157                 :             :     {
     158         [ +  + ]:       92378 :         for (auto it{cursor.Begin()}; it != cursor.End(); it = cursor.NextAndMaybeErase(*it)) {
     159         [ +  - ]:       59720 :             if (it->second.IsDirty()) {
     160         [ +  + ]:       59720 :                 if (it->second.coin.IsSpent()) {
     161                 :       12969 :                     m_data.erase(it->first);
     162                 :             :                 } else {
     163         [ +  + ]:       46751 :                     if (cursor.WillErase(*it)) {
     164                 :       14619 :                         m_data[it->first] = std::move(it->second.coin);
     165                 :             :                     } else {
     166                 :       32132 :                         m_data[it->first] = it->second.coin;
     167                 :             :                     }
     168                 :             :                 }
     169                 :             :             } else {
     170                 :             :                 /* For non-dirty entries being written, compare them with what we have. */
     171                 :           0 :                 auto it2 = m_data.find(it->first);
     172         [ #  # ]:           0 :                 if (it->second.coin.IsSpent()) {
     173         [ #  # ]:           0 :                     assert(it2 == m_data.end());
     174                 :             :                 } else {
     175         [ #  # ]:           0 :                     assert(it2 != m_data.end());
     176         [ #  # ]:           0 :                     assert(it->second.coin.out == it2->second.out);
     177         [ #  # ]:           0 :                     assert(it->second.coin.fCoinBase == it2->second.fCoinBase);
     178         [ #  # ]:           0 :                     assert(it->second.coin.nHeight == it2->second.nHeight);
     179                 :             :                 }
     180                 :             :             }
     181                 :             :         }
     182                 :       32658 :     }
     183                 :             : };
     184                 :             : 
     185                 :             : } // namespace
     186                 :             : 
     187         [ +  - ]:         763 : FUZZ_TARGET(coinscache_sim)
     188                 :             : {
     189                 :             :     /** Precomputed COutPoint and CCoins values. */
     190   [ +  +  +  -  :         305 :     static const PrecomputedData data;
                   +  - ]
     191                 :             : 
     192                 :             :     /** Dummy coinsview instance (base of the hierarchy). */
     193                 :         305 :     CoinsViewBottom bottom;
     194                 :             :     /** Real CCoinsViewCache objects. */
     195                 :         305 :     std::vector<std::unique_ptr<CCoinsViewCache>> caches;
     196                 :             :     /** Simulated cache data (sim_caches[0] matches bottom, sim_caches[i+1] matches caches[i]). */
     197                 :         305 :     CacheLevel sim_caches[MAX_CACHES + 1];
     198                 :             :     /** Current height in the simulation. */
     199                 :         305 :     uint32_t current_height = 1U;
     200                 :             : 
     201                 :             :     // Initialize bottom simulated cache.
     202                 :         305 :     sim_caches[0].Wipe();
     203                 :             : 
     204                 :             :     /** Helper lookup function in the simulated cache stack. */
     205                 :      377347 :     auto lookup = [&](uint32_t outpointidx, int sim_idx = -1) -> std::optional<std::pair<coinidx_type, uint32_t>> {
     206   [ +  +  -  + ]:      377042 :         uint32_t cache_idx = sim_idx == -1 ? caches.size() : sim_idx;
     207                 :     1380250 :         while (true) {
     208                 :      878646 :             const auto& entry = sim_caches[cache_idx].entry[outpointidx];
     209         [ +  + ]:      878646 :             if (entry.entrytype == EntryType::UNSPENT) {
     210                 :      114039 :                 return {{entry.coinidx, entry.height}};
     211         [ +  + ]:      764607 :             } else if (entry.entrytype == EntryType::SPENT) {
     212                 :       56311 :                 return std::nullopt;
     213                 :      708296 :             };
     214         [ +  + ]:      708296 :             if (cache_idx == 0) break;
     215                 :      501604 :             --cache_idx;
     216                 :      501604 :         }
     217                 :      206692 :         return std::nullopt;
     218                 :         305 :     };
     219                 :             : 
     220                 :             :     /** Flush changes in top cache to the one below. */
     221                 :       72225 :     auto flush = [&]() {
     222   [ -  +  -  + ]:       71920 :         assert(caches.size() >= 1);
     223                 :       71920 :         auto& cache = sim_caches[caches.size()];
     224                 :       71920 :         auto& prev_cache = sim_caches[caches.size() - 1];
     225         [ +  + ]:    18483440 :         for (uint32_t outpointidx = 0; outpointidx < NUM_OUTPOINTS; ++outpointidx) {
     226         [ +  + ]:    18411520 :             if (cache.entry[outpointidx].entrytype != EntryType::NONE) {
     227                 :      192801 :                 prev_cache.entry[outpointidx] = cache.entry[outpointidx];
     228                 :      192801 :                 cache.entry[outpointidx].entrytype = EntryType::NONE;
     229                 :             :             }
     230                 :             :         }
     231                 :       72225 :     };
     232                 :             : 
     233                 :             :     // Main simulation loop: read commands from the fuzzer input, and apply them
     234                 :             :     // to both the real cache stack and the simulation.
     235                 :         305 :     FuzzedDataProvider provider(buffer.data(), buffer.size());
     236   [ +  +  +  + ]:      484390 :     LIMITED_WHILE(provider.remaining_bytes(), 10000) {
     237                 :             :         // Every operation (except "Change height") moves current height forward,
     238                 :             :         // so it functions as a kind of epoch, making ~all UTXOs unique.
     239                 :      484085 :         ++current_height;
     240                 :             :         // Make sure there is always at least one CCoinsViewCache.
     241         [ +  + ]:      484085 :         if (caches.empty()) {
     242   [ +  -  +  -  :       14334 :             caches.emplace_back(new CCoinsViewCache(&bottom, /*deterministic=*/true));
             +  -  -  - ]
     243         [ -  + ]:       14334 :             sim_caches[caches.size()].Wipe();
     244                 :             :         }
     245                 :             : 
     246                 :             :         // Execute command.
     247         [ +  - ]:      484085 :         CallOneOf(
     248                 :             :             provider,
     249                 :             : 
     250                 :       38950 :             [&]() { // PeekCoin/GetCoin
     251                 :       38950 :                 uint32_t outpointidx = provider.ConsumeIntegralInRange<uint32_t>(0, NUM_OUTPOINTS - 1);
     252                 :             :                 // Look up in simulation data.
     253                 :       38950 :                 auto sim = lookup(outpointidx);
     254                 :             :                 // Look up in real caches.
     255         [ +  + ]:       38950 :                 auto realcoin = provider.ConsumeBool() ?
     256                 :       22277 :                     caches.back()->PeekCoin(data.outpoints[outpointidx]) :
     257                 :       38950 :                     caches.back()->GetCoin(data.outpoints[outpointidx]);
     258                 :             :                 // Compare results.
     259         [ +  + ]:       38950 :                 if (!sim.has_value()) {
     260         [ -  + ]:       24804 :                     assert(!realcoin);
     261                 :             :                 } else {
     262   [ +  -  -  + ]:       14146 :                     assert(realcoin && !realcoin->IsSpent());
     263                 :       14146 :                     const auto& simcoin = data.coins[sim->first];
     264         [ -  + ]:       14146 :                     assert(realcoin->out == simcoin.out);
     265         [ -  + ]:       14146 :                     assert(realcoin->fCoinBase == simcoin.fCoinBase);
     266         [ -  + ]:       14146 :                     assert(realcoin->nHeight == sim->second);
     267                 :             :                 }
     268                 :       38950 :             },
     269                 :             : 
     270                 :       14684 :             [&]() { // HaveCoin
     271                 :       14684 :                 uint32_t outpointidx = provider.ConsumeIntegralInRange<uint32_t>(0, NUM_OUTPOINTS - 1);
     272                 :             :                 // Look up in simulation data.
     273                 :       14684 :                 auto sim = lookup(outpointidx);
     274                 :             :                 // Look up in real caches.
     275                 :       14684 :                 auto real = caches.back()->HaveCoin(data.outpoints[outpointidx]);
     276                 :             :                 // Compare results.
     277         [ -  + ]:       14684 :                 assert(sim.has_value() == real);
     278                 :       14684 :             },
     279                 :             : 
     280                 :       16876 :             [&]() { // HaveCoinInCache
     281                 :       16876 :                 uint32_t outpointidx = provider.ConsumeIntegralInRange<uint32_t>(0, NUM_OUTPOINTS - 1);
     282                 :             :                 // Invoke on real cache (there is no equivalent in simulation, so nothing to compare result with).
     283                 :       16876 :                 (void)caches.back()->HaveCoinInCache(data.outpoints[outpointidx]);
     284                 :       16876 :             },
     285                 :             : 
     286                 :       16801 :             [&]() { // AccessCoin
     287                 :       16801 :                 uint32_t outpointidx = provider.ConsumeIntegralInRange<uint32_t>(0, NUM_OUTPOINTS - 1);
     288                 :             :                 // Look up in simulation data.
     289                 :       16801 :                 auto sim = lookup(outpointidx);
     290                 :             :                 // Look up in real caches.
     291                 :       16801 :                 const auto& realcoin = caches.back()->AccessCoin(data.outpoints[outpointidx]);
     292                 :             :                 // Compare results.
     293         [ +  + ]:       16801 :                 if (!sim.has_value()) {
     294         [ -  + ]:       10962 :                     assert(realcoin.IsSpent());
     295                 :             :                 } else {
     296         [ -  + ]:        5839 :                     assert(!realcoin.IsSpent());
     297                 :        5839 :                     const auto& simcoin = data.coins[sim->first];
     298         [ -  + ]:        5839 :                     assert(simcoin.out == realcoin.out);
     299         [ -  + ]:        5839 :                     assert(simcoin.fCoinBase == realcoin.fCoinBase);
     300         [ -  + ]:        5839 :                     assert(realcoin.nHeight == sim->second);
     301                 :             :                 }
     302                 :       16801 :             },
     303                 :             : 
     304                 :       57166 :             [&]() { // AddCoin (only possible_overwrite if necessary)
     305                 :       57166 :                 uint32_t outpointidx = provider.ConsumeIntegralInRange<uint32_t>(0, NUM_OUTPOINTS - 1);
     306                 :       57166 :                 uint32_t coinidx = provider.ConsumeIntegralInRange<uint32_t>(0, NUM_COINS - 1);
     307                 :             :                 // Look up in simulation data (to know whether we must set possible_overwrite or not).
     308                 :       57166 :                 auto sim = lookup(outpointidx);
     309                 :             :                 // Invoke on real caches.
     310                 :       57166 :                 Coin coin = data.coins[coinidx];
     311                 :       57166 :                 coin.nHeight = current_height;
     312         [ +  - ]:       57166 :                 caches.back()->AddCoin(data.outpoints[outpointidx], std::move(coin), sim.has_value());
     313                 :             :                 // Apply to simulation data.
     314         [ -  + ]:       57166 :                 auto& entry = sim_caches[caches.size()].entry[outpointidx];
     315                 :       57166 :                 entry.entrytype = EntryType::UNSPENT;
     316                 :       57166 :                 entry.coinidx = coinidx;
     317                 :       57166 :                 entry.height = current_height;
     318                 :       57166 :             },
     319                 :             : 
     320                 :       76877 :             [&]() { // AddCoin (always possible_overwrite)
     321                 :       76877 :                 uint32_t outpointidx = provider.ConsumeIntegralInRange<uint32_t>(0, NUM_OUTPOINTS - 1);
     322                 :       76877 :                 uint32_t coinidx = provider.ConsumeIntegralInRange<uint32_t>(0, NUM_COINS - 1);
     323                 :             :                 // Invoke on real caches.
     324                 :       76877 :                 Coin coin = data.coins[coinidx];
     325                 :       76877 :                 coin.nHeight = current_height;
     326         [ +  - ]:       76877 :                 caches.back()->AddCoin(data.outpoints[outpointidx], std::move(coin), true);
     327                 :             :                 // Apply to simulation data.
     328         [ -  + ]:       76877 :                 auto& entry = sim_caches[caches.size()].entry[outpointidx];
     329                 :       76877 :                 entry.entrytype = EntryType::UNSPENT;
     330                 :       76877 :                 entry.coinidx = coinidx;
     331                 :       76877 :                 entry.height = current_height;
     332                 :       76877 :             },
     333                 :             : 
     334                 :       33536 :             [&]() { // SpendCoin (moveto = nullptr)
     335                 :       33536 :                 uint32_t outpointidx = provider.ConsumeIntegralInRange<uint32_t>(0, NUM_OUTPOINTS - 1);
     336                 :             :                 // Invoke on real caches.
     337                 :       33536 :                 caches.back()->SpendCoin(data.outpoints[outpointidx], nullptr);
     338                 :             :                 // Apply to simulation data.
     339         [ -  + ]:       33536 :                 sim_caches[caches.size()].entry[outpointidx].entrytype = EntryType::SPENT;
     340                 :       33536 :             },
     341                 :             : 
     342                 :       32609 :             [&]() { // SpendCoin (with moveto)
     343                 :       32609 :                 uint32_t outpointidx = provider.ConsumeIntegralInRange<uint32_t>(0, NUM_OUTPOINTS - 1);
     344                 :             :                 // Look up in simulation data (to compare the returned *moveto with).
     345                 :       32609 :                 auto sim = lookup(outpointidx);
     346                 :             :                 // Invoke on real caches.
     347                 :       32609 :                 Coin realcoin;
     348         [ +  - ]:       32609 :                 caches.back()->SpendCoin(data.outpoints[outpointidx], &realcoin);
     349                 :             :                 // Apply to simulation data.
     350         [ -  + ]:       32609 :                 sim_caches[caches.size()].entry[outpointidx].entrytype = EntryType::SPENT;
     351                 :             :                 // Compare *moveto with the value expected based on simulation data.
     352         [ +  + ]:       32609 :                 if (!sim.has_value()) {
     353         [ -  + ]:       16982 :                     assert(realcoin.IsSpent());
     354                 :             :                 } else {
     355         [ -  + ]:       15627 :                     assert(!realcoin.IsSpent());
     356                 :       15627 :                     const auto& simcoin = data.coins[sim->first];
     357         [ -  + ]:       15627 :                     assert(simcoin.out == realcoin.out);
     358         [ -  + ]:       15627 :                     assert(simcoin.fCoinBase == realcoin.fCoinBase);
     359         [ -  + ]:       15627 :                     assert(realcoin.nHeight == sim->second);
     360                 :             :                 }
     361                 :       32609 :             },
     362                 :             : 
     363                 :       14018 :             [&]() { // Uncache
     364                 :       14018 :                 uint32_t outpointidx = provider.ConsumeIntegralInRange<uint32_t>(0, NUM_OUTPOINTS - 1);
     365                 :             :                 // Apply to real caches (there is no equivalent in our simulation).
     366                 :       14018 :                 caches.back()->Uncache(data.outpoints[outpointidx]);
     367                 :       14018 :             },
     368                 :             : 
     369                 :       15821 :             [&]() { // Add a cache level (if not already at the max).
     370   [ -  +  +  + ]:       15821 :                 if (caches.size() != MAX_CACHES) {
     371                 :             :                     // Apply to real caches.
     372         [ +  + ]:        9700 :                     if (provider.ConsumeBool()) {
     373         [ +  - ]:        5030 :                         caches.emplace_back(new CCoinsViewCache(&*caches.back(), /*deterministic=*/true));
     374                 :             :                     } else {
     375         [ +  - ]:        4670 :                         caches.emplace_back(new CoinsViewOverlay(&*caches.back(), /*deterministic=*/true));
     376                 :             :                     }
     377                 :             :                     // Apply to simulation data.
     378         [ -  + ]:        9700 :                     sim_caches[caches.size()].Wipe();
     379                 :             :                 }
     380                 :       15821 :             },
     381                 :             : 
     382                 :       23492 :             [&]() { // Remove a cache level.
     383                 :             :                 // Apply to real caches (this reduces caches.size(), implicitly doing the same on the simulation data).
     384                 :       23492 :                 caches.back()->SanityCheck();
     385                 :       23492 :                 caches.pop_back();
     386                 :       23492 :             },
     387                 :             : 
     388                 :       28827 :             [&]() { // Flush.
     389                 :             :                 // Apply to simulation data.
     390                 :       28827 :                 flush();
     391                 :             :                 // Apply to real caches.
     392                 :       28827 :                 caches.back()->Flush(/*reallocate_cache=*/provider.ConsumeBool());
     393                 :       28827 :             },
     394                 :             : 
     395                 :       43093 :             [&]() { // Sync.
     396                 :             :                 // Apply to simulation data (note that in our simulation, syncing and flushing is the same thing).
     397                 :       43093 :                 flush();
     398                 :             :                 // Apply to real caches.
     399                 :       43093 :                 caches.back()->Sync();
     400                 :       43093 :             },
     401                 :             : 
     402                 :       22360 :             [&]() { // Reset.
     403         [ -  + ]:       22360 :                 sim_caches[caches.size()].Wipe();
     404                 :             :                 // Apply to real caches.
     405                 :       22360 :                 {
     406                 :       22360 :                     const auto reset_guard{caches.back()->CreateResetGuard()};
     407                 :       22360 :                 }
     408                 :       22360 :             },
     409                 :             : 
     410                 :       17472 :             [&]() { // GetCacheSize
     411                 :       17472 :                 (void)caches.back()->GetCacheSize();
     412                 :       17472 :             },
     413                 :             : 
     414                 :       11417 :             [&]() { // DynamicMemoryUsage
     415                 :       11417 :                 (void)caches.back()->DynamicMemoryUsage();
     416                 :       11417 :             },
     417                 :             : 
     418                 :       20086 :             [&]() { // Change height
     419                 :       20086 :                 current_height = provider.ConsumeIntegralInRange<uint32_t>(1, current_height - 1);
     420                 :       20086 :             }
     421                 :             :         );
     422                 :             :     }
     423                 :             : 
     424                 :             :     // Sanity check all the remaining caches
     425         [ +  + ]:         847 :     for (const auto& cache : caches) {
     426         [ +  - ]:         542 :         cache->SanityCheck();
     427                 :             :     }
     428                 :             : 
     429                 :             :     // Full comparison between caches and simulation data, from bottom to top,
     430                 :             :     // as AccessCoin on a higher cache may affect caches below it.
     431   [ -  +  +  + ]:         847 :     for (unsigned sim_idx = 1; sim_idx <= caches.size(); ++sim_idx) {
     432                 :         542 :         auto& cache = *caches[sim_idx - 1];
     433                 :         542 :         size_t cache_size = 0;
     434                 :             : 
     435         [ +  + ]:      139294 :         for (uint32_t outpointidx = 0; outpointidx < NUM_OUTPOINTS; ++outpointidx) {
     436         [ +  - ]:      138752 :             cache_size += cache.HaveCoinInCache(data.outpoints[outpointidx]);
     437         [ +  - ]:      138752 :             const auto& real = cache.AccessCoin(data.outpoints[outpointidx]);
     438                 :      138752 :             auto sim = lookup(outpointidx, sim_idx);
     439         [ +  + ]:      138752 :             if (!sim.has_value()) {
     440         [ -  + ]:      111475 :                 assert(real.IsSpent());
     441                 :             :             } else {
     442         [ -  + ]:       27277 :                 assert(!real.IsSpent());
     443         [ -  + ]:       27277 :                 assert(real.out == data.coins[sim->first].out);
     444         [ -  + ]:       27277 :                 assert(real.fCoinBase == data.coins[sim->first].fCoinBase);
     445         [ -  + ]:       27277 :                 assert(real.nHeight == sim->second);
     446                 :             :             }
     447                 :             :         }
     448                 :             : 
     449                 :             :         // HaveCoinInCache ignores spent coins, so GetCacheSize() may exceed it. */
     450   [ +  -  -  + ]:         542 :         assert(cache.GetCacheSize() >= cache_size);
     451                 :             :     }
     452                 :             : 
     453                 :             :     // Compare the bottom coinsview (not a CCoinsViewCache) with sim_cache[0].
     454         [ +  + ]:       78385 :     for (uint32_t outpointidx = 0; outpointidx < NUM_OUTPOINTS; ++outpointidx) {
     455                 :       78080 :         auto realcoin = bottom.GetCoin(data.outpoints[outpointidx]);
     456                 :       78080 :         auto sim = lookup(outpointidx, 0);
     457         [ +  + ]:       78080 :         if (!sim.has_value()) {
     458         [ -  + ]:       67786 :             assert(!realcoin);
     459                 :             :         } else {
     460   [ +  -  -  + ]:       10294 :             assert(realcoin && !realcoin->IsSpent());
     461         [ -  + ]:       10294 :             assert(realcoin->out == data.coins[sim->first].out);
     462         [ -  + ]:       10294 :             assert(realcoin->fCoinBase == data.coins[sim->first].fCoinBase);
     463         [ -  + ]:       10294 :             assert(realcoin->nHeight == sim->second);
     464                 :             :         }
     465                 :       78080 :     }
     466                 :         305 : }
        

Generated by: LCOV version 2.0-1