LCOV - code coverage report
Current view: top level - src/test/fuzz - coins_view.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 98.9 % 282 279
Test Date: 2026-08-29 05:53:03 Functions: 100.0 % 35 35
Branches: 69.1 % 362 250

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2020-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 <consensus/amount.h>
       7                 :             : #include <consensus/tx_check.h>
       8                 :             : #include <consensus/tx_verify.h>
       9                 :             : #include <consensus/validation.h>
      10                 :             : #include <kernel/chainstatemanager_opts.h>
      11                 :             : #include <kernel/cs_main.h>
      12                 :             : #include <policy/policy.h>
      13                 :             : #include <primitives/block.h>
      14                 :             : #include <primitives/transaction.h>
      15                 :             : #include <script/interpreter.h>
      16                 :             : #include <test/fuzz/FuzzedDataProvider.h>
      17                 :             : #include <test/fuzz/fuzz.h>
      18                 :             : #include <test/fuzz/util.h>
      19                 :             : #include <test/util/coins.h>
      20                 :             : #include <test/util/setup_common.h>
      21                 :             : #include <txdb.h>
      22                 :             : #include <util/hasher.h>
      23                 :             : #include <util/threadpool.h>
      24                 :             : 
      25                 :             : #include <cassert>
      26                 :             : #include <algorithm>
      27                 :             : #include <cstdint>
      28                 :             : #include <functional>
      29                 :             : #include <limits>
      30                 :             : #include <memory>
      31                 :             : #include <optional>
      32                 :             : #include <ranges>
      33                 :             : #include <stdexcept>
      34                 :             : #include <string>
      35                 :             : #include <utility>
      36                 :             : #include <vector>
      37                 :             : 
      38                 :             : namespace {
      39                 :             : /**
      40                 :             :  * MutationGuardCoinsViewCache asserts that nothing mutates cacheCoins until
      41                 :             :  * BatchWrite is called. It keeps a snapshot of the cacheCoins state, which it
      42                 :             :  * uses for the assertion in BatchWrite. After the call to the superclass
      43                 :             :  * CCoinsViewCache::BatchWrite returns, it recomputes the snapshot at that
      44                 :             :  * moment.
      45                 :             :  */
      46                 :             : class MutationGuardCoinsViewCache final : public CCoinsViewCache
      47                 :             : {
      48                 :             : private:
      49         [ +  + ]:      572025 :     struct CacheCoinSnapshot {
      50                 :      110589 :         COutPoint outpoint;
      51                 :      110589 :         bool dirty{false};
      52                 :      110589 :         bool fresh{false};
      53                 :      110589 :         Coin coin;
      54   [ +  -  +  -  :      110589 :         bool operator==(const CacheCoinSnapshot&) const = default;
             +  -  -  + ]
      55                 :             :     };
      56                 :             : 
      57                 :       56926 :     std::vector<CacheCoinSnapshot> ComputeCacheCoinsSnapshot() const
      58                 :             :     {
      59                 :       56926 :         std::vector<CacheCoinSnapshot> snapshot;
      60         [ +  - ]:       56926 :         snapshot.reserve(cacheCoins.size());
      61                 :             : 
      62   [ +  +  +  - ]:      302306 :         for (const auto& [outpoint, entry] : cacheCoins) {
      63         [ +  - ]:      245380 :             snapshot.emplace_back(outpoint, entry.IsDirty(), entry.IsFresh(), entry.coin);
      64                 :             :         }
      65                 :             : 
      66                 :       56926 :         std::ranges::sort(snapshot, std::less<>{}, &CacheCoinSnapshot::outpoint);
      67                 :       56926 :         return snapshot;
      68                 :           0 :     }
      69                 :             : 
      70                 :             :     mutable std::vector<CacheCoinSnapshot> m_expected_snapshot{ComputeCacheCoinsSnapshot()};
      71                 :             : 
      72                 :             : public:
      73                 :       25681 :     void BatchWrite(CoinsViewCacheCursor& cursor, const uint256& block_hash) override
      74                 :             :     {
      75                 :             :         // Nothing must modify cacheCoins other than BatchWrite.
      76         [ -  + ]:       25681 :         assert(ComputeCacheCoinsSnapshot() == m_expected_snapshot);
      77                 :       25681 :         CCoinsViewCache::BatchWrite(cursor, block_hash);
      78                 :       25681 :         m_expected_snapshot = ComputeCacheCoinsSnapshot();
      79                 :       25681 :     }
      80                 :             : 
      81                 :             :     using CCoinsViewCache::CCoinsViewCache;
      82                 :             : };
      83                 :             : 
      84                 :             : // Reuse a single global thread pool across fuzz iterations. Creating and destroying a pool every
      85                 :             : // iteration leaks memory, since iterations can run faster than the OS can tear down the threads.
      86                 :             : std::shared_ptr<ThreadPool> g_thread_pool{std::make_shared<ThreadPool>("view_fuzz")};
      87                 :             : 
      88                 :        9813 : void StartPoolIfNeeded()
      89                 :             : {
      90         [ +  + ]:        9813 :     if (!g_thread_pool->WorkersCount()) g_thread_pool->Start(DEFAULT_PREVOUTFETCH_THREADS);
      91                 :        9813 : }
      92                 :             : 
      93                 :             : //! Build a random block and seed a view with utxos for its inputs.
      94                 :        9813 : CBlock BuildRandomBlock(FuzzedDataProvider& fuzzed_data_provider, CCoinsView& view)
      95                 :             : {
      96                 :        9813 :     CBlock block;
      97         [ +  - ]:        9813 :     CMutableTransaction coinbase;
      98         [ +  - ]:        9813 :     coinbase.vin.emplace_back();
      99   [ +  -  +  -  :       19626 :     block.vtx.push_back(MakeTransactionRef(coinbase));
                   -  + ]
     100                 :             : 
     101         [ +  - ]:        9813 :     CCoinsViewCache seed_cache{&view, /*deterministic=*/true};
     102         [ +  - ]:        9813 :     seed_cache.SetBestBlock(uint256::ONE);
     103                 :             : 
     104                 :        9813 :     Txid prevhash{Txid::FromUint256(ConsumeUInt256(fuzzed_data_provider))};
     105   [ +  +  +  + ]:       39283 :     LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 100)
     106                 :             :     {
     107         [ +  - ]:       29470 :         CMutableTransaction tx;
     108   [ +  +  +  + ]:      133218 :         LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 100)
     109                 :             :         {
     110                 :      103748 :             const Txid txid{fuzzed_data_provider.ConsumeBool()
     111         [ +  + ]:      103748 :                                 ? Txid::FromUint256(ConsumeUInt256(fuzzed_data_provider))
     112                 :       15310 :                                 : prevhash};
     113                 :      103748 :             const COutPoint outpoint{txid, fuzzed_data_provider.ConsumeIntegral<uint32_t>()};
     114   [ +  +  +  - ]:      103748 :             if (auto coin{ConsumeDeserializable<Coin>(fuzzed_data_provider)}; coin && !coin->IsSpent()) {
     115         [ +  - ]:       39684 :                 seed_cache.AddCoin(outpoint, std::move(*coin), /*possible_overwrite=*/true);
     116                 :           0 :             }
     117         [ +  - ]:      103748 :             tx.vin.emplace_back(outpoint);
     118                 :             :         }
     119         [ +  - ]:       29470 :         prevhash = tx.GetHash();
     120   [ +  -  +  -  :       58940 :         block.vtx.push_back(MakeTransactionRef(tx));
                   -  + ]
     121                 :       29470 :     }
     122                 :             : 
     123         [ +  - ]:        9813 :     seed_cache.Flush();
     124                 :       19626 :     return block;
     125                 :       19626 : }
     126                 :             : 
     127                 :             : } // namespace
     128                 :             : 
     129                 :           4 : void initialize_coins_view()
     130                 :             : {
     131   [ +  -  +  -  :           4 :     static const auto testing_setup = MakeNoLogFileContext<>();
                   +  - ]
     132                 :           4 : }
     133                 :             : 
     134                 :       31354 : void TestCoinsView(FuzzedDataProvider& fuzzed_data_provider, CCoinsViewCache& coins_view_cache, CCoinsView* backend_coins_view)
     135                 :             : {
     136         [ +  - ]:       31354 :     auto* const db{dynamic_cast<CCoinsViewDB*>(backend_coins_view)};
     137                 :       31354 :     auto* const overlay{dynamic_cast<CoinsViewOverlay*>(&coins_view_cache)};
     138                 :       31354 :     const bool is_db{db != nullptr};
     139                 :       31354 :     bool good_data{true};
     140                 :       31354 :     auto* original_backend{backend_coins_view};
     141                 :             : 
     142         [ +  + ]:       31354 :     if (is_db) coins_view_cache.SetBestBlock(uint256::ONE);
     143                 :       31354 :     COutPoint random_out_point;
     144                 :       31354 :     Coin random_coin;
     145         [ +  - ]:       31354 :     CMutableTransaction random_mutable_transaction;
     146   [ +  +  +  +  :    12145880 :     LIMITED_WHILE (good_data && fuzzed_data_provider.ConsumeBool(), 10'000) {
                   +  + ]
     147         [ +  - ]:     6043544 :         CallOneOf(
     148                 :             :             fuzzed_data_provider,
     149                 :      482509 :             [&] {
     150         [ +  + ]:      482509 :                 if (random_coin.IsSpent()) {
     151                 :             :                     return;
     152                 :             :                 }
     153                 :      421563 :                 COutPoint outpoint{random_out_point};
     154                 :      421563 :                 Coin coin{random_coin};
     155         [ +  + ]:      421563 :                 if (fuzzed_data_provider.ConsumeBool()) {
     156                 :             :                     // We can only skip the check if no unspent coin exists for this outpoint.
     157   [ +  -  +  +  :      471325 :                     const bool possible_overwrite{coins_view_cache.PeekCoin(outpoint) || fuzzed_data_provider.ConsumeBool()};
                   +  + ]
     158         [ +  - ]:      396444 :                     coins_view_cache.AddCoin(outpoint, std::move(coin), possible_overwrite);
     159                 :             :                 } else {
     160         [ +  - ]:       25119 :                     coins_view_cache.EmplaceCoinInternalDANGER(outpoint, std::move(coin));
     161                 :             :                 }
     162                 :      421563 :             },
     163                 :     1665278 :             [&] {
     164   [ +  +  -  +  :     1665278 :                 if (overlay && !overlay->AllInputsConsumed()) return; // CoinsViewOverlay::Flush() must have all inputs consumed before being called
                   +  + ]
     165                 :     1657729 :                 coins_view_cache.Flush(/*reallocate_cache=*/fuzzed_data_provider.ConsumeBool());
     166                 :             :             },
     167                 :     2722669 :             [&] {
     168         [ +  + ]:     2722669 :                 if (overlay) return; // CoinsViewOverlay::Sync() is never called in production code
     169                 :     2658520 :                 coins_view_cache.Sync();
     170                 :             :             },
     171                 :      581645 :             [&] {
     172   [ +  +  +  -  :     2292410 :                 if (db) WITH_LOCK(::cs_main, (void)db->CompactFullAsync());
                   +  - ]
     173                 :      581645 :             },
     174                 :       51512 :             [&] {
     175                 :       51512 :                 uint256 best_block{ConsumeUInt256(fuzzed_data_provider)};
     176                 :             :                 // `CCoinsViewDB::BatchWrite()` requires a non-null best block.
     177   [ +  +  +  + ]:       95255 :                 if (is_db && best_block.IsNull()) best_block = uint256::ONE;
     178                 :       51512 :                 coins_view_cache.SetBestBlock(best_block);
     179                 :       51512 :             },
     180                 :       33104 :             [&] {
     181                 :       33104 :                 (void)coins_view_cache.CreateResetGuard();
     182                 :             :                 // Reset() clears the best block, so reseed db-backed caches.
     183         [ +  + ]:       33104 :                 if (is_db) {
     184                 :       19746 :                     const uint256 best_block{ConsumeUInt256(fuzzed_data_provider)};
     185         [ +  + ]:       39492 :                     if (best_block.IsNull()) {
     186                 :         105 :                         good_data = false;
     187                 :         105 :                         return;
     188                 :             :                     }
     189                 :       19641 :                     coins_view_cache.SetBestBlock(best_block);
     190                 :             :                 }
     191                 :             :             },
     192                 :      187385 :             [&] {
     193                 :      187385 :                 Coin move_to;
     194   [ +  +  +  - ]:      208280 :                 (void)coins_view_cache.SpendCoin(random_out_point, fuzzed_data_provider.ConsumeBool() ? &move_to : nullptr);
     195                 :      187385 :             },
     196                 :       48479 :             [&] {
     197                 :       48479 :                 coins_view_cache.Uncache(random_out_point);
     198                 :       48479 :             },
     199                 :      161095 :             [&] {
     200         [ +  + ]:      161095 :                 if (overlay) return; // // CoinsViewOverlay::SetBackend() is never called in production code
     201                 :      128465 :                 const bool use_original_backend{fuzzed_data_provider.ConsumeBool()};
     202   [ +  +  +  + ]:      128465 :                 if (use_original_backend && backend_coins_view != original_backend) {
     203                 :             :                     // FRESH flags valid against the empty backend may be invalid
     204                 :             :                     // against the original backend, so reset before restoring it.
     205                 :       19783 :                     (void)coins_view_cache.CreateResetGuard();
     206                 :             :                     // Reset() clears the best block; db backends require a non-null hash.
     207         [ +  - ]:       19783 :                     if (is_db) coins_view_cache.SetBestBlock(uint256::ONE);
     208                 :             :                 }
     209                 :      128465 :                 backend_coins_view = use_original_backend ? original_backend : &CoinsViewEmpty::Get();
     210                 :      128465 :                 coins_view_cache.SetBackend(*backend_coins_view);
     211                 :             :             },
     212                 :       17578 :             [&] {
     213                 :       17578 :                 const std::optional<COutPoint> opt_out_point = ConsumeDeserializable<COutPoint>(fuzzed_data_provider);
     214         [ +  + ]:       17578 :                 if (!opt_out_point) {
     215                 :         525 :                     good_data = false;
     216                 :         525 :                     return;
     217                 :             :                 }
     218                 :       17053 :                 random_out_point = *opt_out_point;
     219                 :             :             },
     220                 :       29041 :             [&] {
     221                 :       29041 :                 const std::optional<Coin> opt_coin = ConsumeDeserializable<Coin>(fuzzed_data_provider);
     222         [ +  + ]:       29041 :                 if (!opt_coin) {
     223                 :         852 :                     good_data = false;
     224                 :         852 :                     return;
     225                 :             :                 }
     226                 :       28189 :                 random_coin = *opt_coin;
     227                 :       29041 :             },
     228                 :       24928 :             [&] {
     229                 :       24928 :                 const std::optional<CMutableTransaction> opt_mutable_transaction = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS);
     230         [ +  + ]:       24928 :                 if (!opt_mutable_transaction) {
     231                 :        2121 :                     good_data = false;
     232         [ -  + ]:        2121 :                     return;
     233                 :             :                 }
     234         [ +  - ]:       22807 :                 random_mutable_transaction = *opt_mutable_transaction;
     235                 :       24928 :             },
     236                 :       38321 :             [&] {
     237                 :       38321 :                 CoinsCachePair sentinel{};
     238                 :       38321 :                 sentinel.second.SelfRef(sentinel);
     239                 :       38321 :                 size_t dirty_count{0};
     240         [ +  - ]:       38321 :                 CCoinsMapMemoryResource resource;
     241   [ +  -  +  - ]:       38321 :                 CCoinsMap coins_map{0, SaltedCoinsCacheHasher{/*deterministic=*/true}, CCoinsMap::key_equal{}, &resource};
     242   [ +  -  +  +  :     1842087 :                 LIMITED_WHILE (good_data && fuzzed_data_provider.ConsumeBool(), 10'000) {
                   +  + ]
     243                 :     1804079 :                     CCoinsCacheEntry coins_cache_entry;
     244         [ +  + ]:     1804079 :                     if (fuzzed_data_provider.ConsumeBool()) {
     245                 :     1799575 :                         coins_cache_entry.coin = random_coin;
     246                 :             :                     } else {
     247                 :        4504 :                         const std::optional<Coin> opt_coin = ConsumeDeserializable<Coin>(fuzzed_data_provider);
     248         [ +  + ]:        4504 :                         if (!opt_coin) {
     249                 :         313 :                             good_data = false;
     250                 :         313 :                             return;
     251                 :             :                         }
     252                 :        4191 :                         coins_cache_entry.coin = *opt_coin;
     253                 :        4191 :                     }
     254                 :             :                     // Avoid setting FRESH for an outpoint that already exists unspent in the parent view.
     255   [ +  -  +  +  :     1814636 :                     bool fresh{!coins_view_cache.PeekCoin(random_out_point) && fuzzed_data_provider.ConsumeBool()};
                   +  + ]
     256   [ +  +  +  + ]:     1803766 :                     bool dirty{fresh || fuzzed_data_provider.ConsumeBool()};
     257         [ +  - ]:     1803766 :                     auto it{coins_map.emplace(random_out_point, std::move(coins_cache_entry)).first};
     258         [ +  + ]:     1803766 :                     if (dirty) CCoinsCacheEntry::SetDirty(*it, sentinel);
     259         [ +  + ]:     1803766 :                     if (fresh) CCoinsCacheEntry::SetFresh(*it, sentinel);
     260                 :     1803766 :                     dirty_count += dirty;
     261                 :     1804079 :                 }
     262         [ +  - ]:       38008 :                 auto cursor{CoinsViewCacheCursor(dirty_count, sentinel, coins_map, /*will_erase=*/true)};
     263         [ +  - ]:       38008 :                 uint256 best_block{coins_view_cache.GetBestBlock()};
     264         [ +  + ]:       38008 :                 if (fuzzed_data_provider.ConsumeBool()) best_block = ConsumeUInt256(fuzzed_data_provider);
     265                 :             :                 // Set best block hash to non-null to satisfy the assertion in CCoinsViewDB::BatchWrite().
     266   [ +  +  +  + ]:       53533 :                 if (is_db && best_block.IsNull()) best_block = uint256::ONE;
     267         [ +  - ]:       38008 :                 coins_view_cache.BatchWrite(cursor, best_block);
     268                 :       38321 :             });
     269                 :             :     }
     270                 :             : 
     271                 :       31354 :     {
     272         [ +  - ]:       31354 :         (void)coins_view_cache.DynamicMemoryUsage();
     273         [ +  - ]:       31354 :         (void)coins_view_cache.EstimateSize();
     274         [ +  - ]:       31354 :         (void)coins_view_cache.GetBestBlock();
     275         [ +  - ]:       31354 :         (void)coins_view_cache.GetCacheSize();
     276         [ +  - ]:       31354 :         (void)coins_view_cache.GetHeadBlocks();
     277   [ +  -  +  - ]:       31354 :         (void)coins_view_cache.HaveInputs(CTransaction{random_mutable_transaction});
     278                 :             :     }
     279                 :             : 
     280                 :       31354 :     {
     281   [ +  +  +  + ]:       31354 :         if (is_db && backend_coins_view == original_backend) {
     282   [ +  -  -  + ]:       14751 :             assert(db->Cursor());
     283                 :             :         }
     284         [ +  - ]:       31354 :         (void)backend_coins_view->EstimateSize();
     285         [ +  - ]:       31354 :         (void)backend_coins_view->GetBestBlock();
     286         [ +  - ]:       31354 :         (void)backend_coins_view->GetHeadBlocks();
     287                 :             :     }
     288                 :             : 
     289         [ +  + ]:       31354 :     if (fuzzed_data_provider.ConsumeBool()) {
     290         [ +  - ]:       15004 :         CallOneOf(
     291                 :             :             fuzzed_data_provider,
     292                 :        1570 :             [&] {
     293                 :        1570 :                 const CTransaction transaction{random_mutable_transaction};
     294                 :        1570 :                 bool is_spent = false;
     295         [ +  + ]:      135110 :                 for (const CTxOut& tx_out : transaction.vout) {
     296         [ +  + ]:      133540 :                     if (Coin{tx_out, 0, transaction.IsCoinBase()}.IsSpent()) {
     297                 :         200 :                         is_spent = true;
     298                 :             :                     }
     299                 :             :                 }
     300         [ +  + ]:        1570 :                 if (is_spent) {
     301                 :             :                     // Avoid:
     302                 :             :                     // coins.cpp:69: void CCoinsViewCache::AddCoin(const COutPoint &, Coin &&, bool): Assertion `!coin.IsSpent()' failed.
     303                 :          27 :                     return;
     304                 :             :                 }
     305                 :        1543 :                 const int height{int(fuzzed_data_provider.ConsumeIntegral<uint32_t>() >> 1)};
     306   [ +  +  +  + ]:        3074 :                 const bool check_for_overwrite{transaction.IsCoinBase() || [&] {
     307   [ -  +  +  + ]:      133463 :                     for (uint32_t i{0}; i < transaction.vout.size(); ++i) {
     308         [ +  + ]:      131937 :                         if (coins_view_cache.PeekCoin(COutPoint{transaction.GetHash(), i})) return true;
     309                 :             :                     }
     310                 :        1526 :                     return fuzzed_data_provider.ConsumeBool();
     311         [ +  - ]:        1531 :                 }()}; // We can only skip the check if the current txid has no unspent outputs
     312         [ +  - ]:        1543 :                 AddCoins(coins_view_cache, transaction, height, check_for_overwrite);
     313                 :        1570 :             },
     314                 :        6923 :             [&] {
     315         [ +  - ]:       13846 :                 (void)ValidateInputsStandardness(CTransaction{random_mutable_transaction}, coins_view_cache);
     316                 :        6923 :             },
     317                 :        1060 :             [&] {
     318         [ +  - ]:        1060 :                 TxValidationState state;
     319                 :        1060 :                 CAmount tx_fee_out;
     320         [ +  - ]:        1060 :                 const CTransaction transaction{random_mutable_transaction};
     321         [ +  + ]:        1060 :                 if (ContainsSpentInput(transaction, coins_view_cache)) {
     322                 :             :                     // Avoid:
     323                 :             :                     // consensus/tx_verify.cpp:171: bool Consensus::CheckTxInputs(const CTransaction &, TxValidationState &, const CCoinsViewCache &, int, CAmount &): Assertion `!coin.IsSpent()' failed.
     324                 :             :                     return;
     325                 :             :                 }
     326         [ +  - ]:        1028 :                 TxValidationState dummy;
     327   [ +  -  +  + ]:        1028 :                 if (!CheckTransaction(transaction, dummy)) {
     328                 :             :                     // It is not allowed to call CheckTxInputs if CheckTransaction failed
     329                 :         866 :                     return;
     330                 :             :                 }
     331   [ +  -  +  + ]:         162 :                 if (Consensus::CheckTxInputs(transaction, state, coins_view_cache, fuzzed_data_provider.ConsumeIntegralInRange<int>(0, std::numeric_limits<int>::max()), tx_fee_out)) {
     332         [ -  + ]:          11 :                     assert(MoneyRange(tx_fee_out));
     333                 :             :                 }
     334                 :        3148 :             },
     335                 :        2031 :             [&] {
     336                 :        2031 :                 const CTransaction transaction{random_mutable_transaction};
     337         [ +  + ]:        2031 :                 if (ContainsSpentInput(transaction, coins_view_cache)) {
     338                 :             :                     // Avoid:
     339                 :             :                     // consensus/tx_verify.cpp:130: unsigned int GetP2SHSigOpCount(const CTransaction &, const CCoinsViewCache &): Assertion `!coin.IsSpent()' failed.
     340                 :         141 :                     return;
     341                 :             :                 }
     342         [ +  - ]:        1890 :                 (void)GetP2SHSigOpCount(transaction, coins_view_cache);
     343                 :        2031 :             },
     344                 :        1193 :             [&] {
     345                 :        1193 :                 const CTransaction transaction{random_mutable_transaction};
     346         [ +  + ]:        1193 :                 if (ContainsSpentInput(transaction, coins_view_cache)) {
     347                 :             :                     // Avoid:
     348                 :             :                     // consensus/tx_verify.cpp:130: unsigned int GetP2SHSigOpCount(const CTransaction &, const CCoinsViewCache &): Assertion `!coin.IsSpent()' failed.
     349                 :             :                     return;
     350                 :             :                 }
     351         [ +  + ]:        1107 :                 const auto flags = script_verify_flags::from_int(fuzzed_data_provider.ConsumeIntegral<script_verify_flags::value_type>());
     352   [ +  +  +  +  :        1107 :                 if (!transaction.vin.empty() && (flags & SCRIPT_VERIFY_WITNESS) != 0 && (flags & SCRIPT_VERIFY_P2SH) == 0) {
                   +  + ]
     353                 :             :                     // Avoid:
     354                 :             :                     // script/interpreter.cpp:1705: size_t CountWitnessSigOps(const CScript &, const CScript &, const CScriptWitness &, unsigned int): Assertion `(flags & SCRIPT_VERIFY_P2SH) != 0' failed.
     355                 :             :                     return;
     356                 :             :                 }
     357         [ +  - ]:        1097 :                 (void)GetTransactionSigOpCost(transaction, coins_view_cache, flags);
     358                 :        1193 :             },
     359                 :        2227 :             [&] {
     360         [ +  - ]:        2227 :                 (void)IsWitnessStandard(CTransaction{random_mutable_transaction}, coins_view_cache);
     361                 :        2227 :             });
     362                 :             :     }
     363                 :             : 
     364                 :       31354 :     {
     365         [ +  - ]:       31354 :         const Coin& coin_using_access_coin = coins_view_cache.AccessCoin(random_out_point);
     366         [ +  - ]:       31354 :         const bool exists_using_access_coin = !coin_using_access_coin.IsSpent();
     367         [ +  - ]:       31354 :         const bool exists_using_have_coin = coins_view_cache.HaveCoin(random_out_point);
     368         [ +  - ]:       31354 :         const bool exists_using_have_coin_in_cache = coins_view_cache.HaveCoinInCache(random_out_point);
     369   [ +  -  +  + ]:       31354 :         if (auto coin{coins_view_cache.GetCoin(random_out_point)}) {
     370         [ -  + ]:        9354 :             assert(*coin == coin_using_access_coin);
     371   [ +  -  -  + ]:        9354 :             assert(exists_using_access_coin && exists_using_have_coin_in_cache && exists_using_have_coin);
     372                 :             :         } else {
     373   [ +  -  -  + ]:       22000 :             assert(!exists_using_access_coin && !exists_using_have_coin_in_cache && !exists_using_have_coin);
     374                 :       31354 :         }
     375                 :             :         // If HaveCoin on the backend is true, it must also be on the cache if the coin wasn't spent.
     376                 :       31354 :         std::optional<Coin> coin_in_backend;
     377                 :       31354 :         bool exists_using_have_coin_in_backend;
     378         [ +  + ]:       31354 :         if (dynamic_cast<CoinsViewOverlay*>(&coins_view_cache)) {
     379                 :             :             // PeekCoin does not mutate cacheCoins, so async workers can keep running.
     380         [ +  - ]:       19626 :             coin_in_backend = backend_coins_view->PeekCoin(random_out_point);
     381                 :        9813 :             exists_using_have_coin_in_backend = coin_in_backend.has_value();
     382                 :             :         } else {
     383         [ +  - ]:       21541 :             exists_using_have_coin_in_backend = backend_coins_view->HaveCoin(random_out_point);
     384         [ +  - ]:       43082 :             coin_in_backend = backend_coins_view->GetCoin(random_out_point);
     385                 :             :         }
     386   [ +  +  +  + ]:       31354 :         if (!coin_using_access_coin.IsSpent() && exists_using_have_coin_in_backend) {
     387         [ -  + ]:        3939 :             assert(exists_using_have_coin);
     388                 :             :         }
     389         [ +  + ]:       31354 :         if (coin_in_backend) {
     390         [ -  + ]:        4119 :             assert(exists_using_have_coin_in_backend);
     391                 :             :             // Note we can't assert that `coin_using_get_coin == *coin` because the coin in
     392                 :             :             // the cache may have been modified but not yet flushed.
     393                 :             :         } else {
     394         [ -  + ]:       27235 :             assert(!exists_using_have_coin_in_backend);
     395                 :             :         }
     396                 :       31354 :     }
     397                 :       31354 : }
     398                 :             : 
     399         [ +  - ]:        6210 : FUZZ_TARGET(coins_view, .init = initialize_coins_view)
     400                 :             : {
     401                 :        5734 :     FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
     402                 :        5734 :     CCoinsViewCache coins_view_cache{&CoinsViewEmpty::Get(), /*deterministic=*/true};
     403   [ +  -  +  - ]:        5734 :     TestCoinsView(fuzzed_data_provider, coins_view_cache, &CoinsViewEmpty::Get());
     404                 :        5734 : }
     405                 :             : 
     406         [ +  - ]:        7785 : FUZZ_TARGET(coins_view_db, .init = initialize_coins_view)
     407                 :             : {
     408                 :        7309 :     FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
     409                 :        7309 :     auto db_params = DBParams{
     410                 :             :         .path = "",
     411                 :             :         .cache_bytes = 1_MiB,
     412                 :             :         .memory_only = true,
     413                 :        7309 :     };
     414         [ +  - ]:        7309 :     CCoinsViewDB backend_coins_view{std::move(db_params), CoinsViewOptions{}};
     415         [ +  - ]:        7309 :     CCoinsViewCache coins_view_cache{&backend_coins_view, /*deterministic=*/true};
     416         [ +  - ]:        7309 :     TestCoinsView(fuzzed_data_provider, coins_view_cache, &backend_coins_view);
     417                 :       14618 : }
     418                 :             : 
     419                 :             : // Creates a CoinsViewOverlay and a MutationGuardCoinsViewCache as the base.
     420                 :             : // This allows us to exercise all methods on a CoinsViewOverlay, while also
     421                 :             : // ensuring that nothing can mutate the underlying cache until Flush or Sync is
     422                 :             : // called.
     423         [ +  - ]:        6040 : FUZZ_TARGET(coins_view_overlay, .init = initialize_coins_view)
     424                 :             : {
     425                 :        5564 :     SeedRandomStateForTest(SeedRand::ZEROS); // for SaltedCoinsCacheHasher
     426                 :        5564 :     StartPoolIfNeeded();
     427                 :        5564 :     FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
     428                 :        5564 :     MutationGuardCoinsViewCache backend_cache{&CoinsViewEmpty::Get(), /*deterministic=*/true};
     429   [ +  -  -  + ]:       11128 :     CoinsViewOverlay coins_view_cache{&backend_cache, g_thread_pool, /*deterministic=*/true};
     430         [ +  - ]:        5564 :     CBlock block{BuildRandomBlock(fuzzed_data_provider, backend_cache)};
     431                 :        5564 :     const auto reset_guard{coins_view_cache.StartFetching(block)};
     432         [ +  - ]:        5564 :     TestCoinsView(fuzzed_data_provider, coins_view_cache, &backend_cache);
     433                 :        5564 : }
     434                 :             : 
     435         [ +  - ]:        4725 : FUZZ_TARGET(coins_view_stacked, .init = initialize_coins_view)
     436                 :             : {
     437                 :        4249 :     SeedRandomStateForTest(SeedRand::ZEROS); // for SaltedCoinsCacheHasher
     438                 :        4249 :     StartPoolIfNeeded();
     439                 :        4249 :     FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
     440                 :        4249 :     auto db_params = DBParams{
     441                 :             :         .path = "",
     442                 :             :         .cache_bytes = 1_MiB,
     443                 :             :         .memory_only = true,
     444                 :        4249 :     };
     445         [ +  - ]:        4249 :     CCoinsViewDB backend_base_coins_view{std::move(db_params), CoinsViewOptions{}};
     446         [ +  - ]:        4249 :     CCoinsViewCache backend_cache{&backend_base_coins_view, /*deterministic=*/true};
     447         [ +  - ]:        4249 :     TestCoinsView(fuzzed_data_provider, backend_cache, &backend_base_coins_view);
     448   [ +  -  -  + ]:        8498 :     CoinsViewOverlay coins_view_cache{&backend_cache, g_thread_pool, /*deterministic=*/true};
     449         [ +  - ]:        4249 :     CBlock block{BuildRandomBlock(fuzzed_data_provider, backend_base_coins_view)};
     450                 :        4249 :     {
     451                 :        4249 :         const auto reset_guard{coins_view_cache.StartFetching(block)};
     452         [ +  - ]:        4249 :         TestCoinsView(fuzzed_data_provider, coins_view_cache, &backend_cache);
     453                 :           0 :     }
     454         [ +  - ]:        4249 :     TestCoinsView(fuzzed_data_provider, backend_cache, &backend_base_coins_view);
     455                 :        8498 : }
        

Generated by: LCOV version 2.5.0-full