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 % 285 282
Test Date: 2026-07-19 07:06:27 Functions: 100.0 % 36 36
Branches: 69.2 % 370 256

             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/setup_common.h>
      20                 :             : #include <txdb.h>
      21                 :             : #include <util/hasher.h>
      22                 :             : #include <util/threadpool.h>
      23                 :             : 
      24                 :             : #include <cassert>
      25                 :             : #include <algorithm>
      26                 :             : #include <cstdint>
      27                 :             : #include <functional>
      28                 :             : #include <limits>
      29                 :             : #include <memory>
      30                 :             : #include <optional>
      31                 :             : #include <ranges>
      32                 :             : #include <stdexcept>
      33                 :             : #include <string>
      34                 :             : #include <utility>
      35                 :             : #include <vector>
      36                 :             : 
      37                 :             : namespace {
      38                 :             : const Coin EMPTY_COIN{};
      39                 :             : 
      40                 :       22910 : bool operator==(const Coin& a, const Coin& b)
      41                 :             : {
      42   [ +  +  -  + ]:       22910 :     if (a.IsSpent() && b.IsSpent()) return true;
      43   [ +  +  +  +  :        7272 :     return a.fCoinBase == b.fCoinBase && a.nHeight == b.nHeight && a.out == b.out;
                   +  + ]
      44                 :             : }
      45                 :             : 
      46                 :             : /**
      47                 :             :  * MutationGuardCoinsViewCache asserts that nothing mutates cacheCoins until
      48                 :             :  * BatchWrite is called. It keeps a snapshot of the cacheCoins state, which it
      49                 :             :  * uses for the assertion in BatchWrite. After the call to the superclass
      50                 :             :  * CCoinsViewCache::BatchWrite returns, it recomputes the snapshot at that
      51                 :             :  * moment.
      52                 :             :  */
      53                 :             : class MutationGuardCoinsViewCache final : public CCoinsViewCache
      54                 :             : {
      55                 :             : private:
      56         [ -  - ]:        6288 :     struct CacheCoinSnapshot {
      57                 :           4 :         COutPoint outpoint;
      58                 :           4 :         bool dirty{false};
      59                 :           4 :         bool fresh{false};
      60                 :           4 :         Coin coin;
      61   [ +  -  +  -  :           4 :         bool operator==(const CacheCoinSnapshot&) const = default;
             +  -  -  + ]
      62                 :             :     };
      63                 :             : 
      64                 :        9155 :     std::vector<CacheCoinSnapshot> ComputeCacheCoinsSnapshot() const
      65                 :             :     {
      66                 :        9155 :         std::vector<CacheCoinSnapshot> snapshot;
      67         [ +  - ]:        9155 :         snapshot.reserve(cacheCoins.size());
      68                 :             : 
      69   [ +  +  +  - ]:       13247 :         for (const auto& [outpoint, entry] : cacheCoins) {
      70         [ +  - ]:        4092 :             snapshot.emplace_back(outpoint, entry.IsDirty(), entry.IsFresh(), entry.coin);
      71                 :             :         }
      72                 :             : 
      73                 :        9155 :         std::ranges::sort(snapshot, std::less<>{}, &CacheCoinSnapshot::outpoint);
      74                 :        9155 :         return snapshot;
      75                 :           0 :     }
      76                 :             : 
      77                 :             :     mutable std::vector<CacheCoinSnapshot> m_expected_snapshot{ComputeCacheCoinsSnapshot()};
      78                 :             : 
      79                 :             : public:
      80                 :        3069 :     void BatchWrite(CoinsViewCacheCursor& cursor, const uint256& block_hash) override
      81                 :             :     {
      82                 :             :         // Nothing must modify cacheCoins other than BatchWrite.
      83         [ -  + ]:        3069 :         assert(ComputeCacheCoinsSnapshot() == m_expected_snapshot);
      84                 :        3069 :         CCoinsViewCache::BatchWrite(cursor, block_hash);
      85                 :        3069 :         m_expected_snapshot = ComputeCacheCoinsSnapshot();
      86                 :        3069 :     }
      87                 :             : 
      88                 :             :     using CCoinsViewCache::CCoinsViewCache;
      89                 :             : };
      90                 :             : 
      91                 :             : // Reuse a single global thread pool across fuzz iterations. Creating and destroying a pool every
      92                 :             : // iteration leaks memory, since iterations can run faster than the OS can tear down the threads.
      93                 :             : std::shared_ptr<ThreadPool> g_thread_pool{std::make_shared<ThreadPool>("view_fuzz")};
      94                 :             : 
      95                 :        5736 : void StartPoolIfNeeded()
      96                 :             : {
      97         [ +  + ]:        5736 :     if (!g_thread_pool->WorkersCount()) g_thread_pool->Start(DEFAULT_PREVOUTFETCH_THREADS);
      98                 :        5736 : }
      99                 :             : 
     100                 :             : //! Build a random block and seed a view with utxos for its inputs.
     101                 :        5736 : CBlock BuildRandomBlock(FuzzedDataProvider& fuzzed_data_provider, CCoinsView& view)
     102                 :             : {
     103                 :        5736 :     CBlock block;
     104         [ +  - ]:        5736 :     CMutableTransaction coinbase;
     105         [ +  - ]:        5736 :     coinbase.vin.emplace_back();
     106   [ +  -  +  -  :       11472 :     block.vtx.push_back(MakeTransactionRef(coinbase));
                   -  + ]
     107                 :             : 
     108         [ +  - ]:        5736 :     CCoinsViewCache seed_cache{&view, /*deterministic=*/true};
     109         [ +  - ]:        5736 :     seed_cache.SetBestBlock(uint256::ONE);
     110                 :             : 
     111                 :        5736 :     Txid prevhash{Txid::FromUint256(ConsumeUInt256(fuzzed_data_provider))};
     112   [ +  +  +  + ]:       17272 :     LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 100)
     113                 :             :     {
     114         [ +  - ]:       11536 :         CMutableTransaction tx;
     115   [ +  +  +  + ]:       27741 :         LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 100)
     116                 :             :         {
     117                 :       16205 :             const Txid txid{fuzzed_data_provider.ConsumeBool()
     118         [ +  + ]:       16205 :                                 ? Txid::FromUint256(ConsumeUInt256(fuzzed_data_provider))
     119                 :        5092 :                                 : prevhash};
     120                 :       16205 :             const COutPoint outpoint{txid, fuzzed_data_provider.ConsumeIntegral<uint32_t>()};
     121   [ +  +  +  - ]:       16205 :             if (auto coin{ConsumeDeserializable<Coin>(fuzzed_data_provider)}; coin && !coin->IsSpent()) {
     122         [ +  - ]:        6289 :                 seed_cache.AddCoin(outpoint, std::move(*coin), /*possible_overwrite=*/true);
     123                 :           0 :             }
     124         [ +  - ]:       16205 :             tx.vin.emplace_back(outpoint);
     125                 :             :         }
     126         [ +  - ]:       11536 :         prevhash = tx.GetHash();
     127   [ +  -  +  -  :       23072 :         block.vtx.push_back(MakeTransactionRef(tx));
                   -  + ]
     128                 :       11536 :     }
     129                 :             : 
     130         [ +  - ]:        5736 :     seed_cache.Flush();
     131                 :       11472 :     return block;
     132                 :       11472 : }
     133                 :             : 
     134                 :             : } // namespace
     135                 :             : 
     136                 :           4 : void initialize_coins_view()
     137                 :             : {
     138   [ +  -  +  -  :           4 :     static const auto testing_setup = MakeNoLogFileContext<>();
                   +  - ]
     139                 :           4 : }
     140                 :             : 
     141                 :       19272 : void TestCoinsView(FuzzedDataProvider& fuzzed_data_provider, CCoinsViewCache& coins_view_cache, CCoinsView* backend_coins_view)
     142                 :             : {
     143         [ +  - ]:       19272 :     auto* const db{dynamic_cast<CCoinsViewDB*>(backend_coins_view)};
     144                 :       19272 :     auto* const overlay{dynamic_cast<CoinsViewOverlay*>(&coins_view_cache)};
     145                 :       19272 :     const bool is_db{db != nullptr};
     146                 :       19272 :     bool good_data{true};
     147                 :       19272 :     auto* original_backend{backend_coins_view};
     148                 :             : 
     149         [ +  + ]:       19272 :     if (is_db) coins_view_cache.SetBestBlock(uint256::ONE);
     150                 :       19272 :     COutPoint random_out_point;
     151                 :       19272 :     Coin random_coin;
     152         [ +  - ]:       19272 :     CMutableTransaction random_mutable_transaction;
     153   [ +  +  +  +  :     1754138 :     LIMITED_WHILE (good_data && fuzzed_data_provider.ConsumeBool(), 10'000) {
                   +  + ]
     154         [ +  - ]:      858874 :         CallOneOf(
     155                 :             :             fuzzed_data_provider,
     156                 :       70121 :             [&] {
     157         [ +  + ]:       70121 :                 if (random_coin.IsSpent()) {
     158                 :             :                     return;
     159                 :             :                 }
     160                 :       43274 :                 COutPoint outpoint{random_out_point};
     161                 :       43274 :                 Coin coin{random_coin};
     162         [ +  + ]:       43274 :                 if (fuzzed_data_provider.ConsumeBool()) {
     163                 :             :                     // We can only skip the check if no unspent coin exists for this outpoint.
     164   [ +  -  +  +  :       43760 :                     const bool possible_overwrite{coins_view_cache.PeekCoin(outpoint) || fuzzed_data_provider.ConsumeBool()};
                   +  + ]
     165         [ +  - ]:       40111 :                     coins_view_cache.AddCoin(outpoint, std::move(coin), possible_overwrite);
     166                 :             :                 } else {
     167         [ +  - ]:        3163 :                     coins_view_cache.EmplaceCoinInternalDANGER(outpoint, std::move(coin));
     168                 :             :                 }
     169                 :       43274 :             },
     170                 :      366849 :             [&] {
     171   [ +  +  -  +  :      366849 :                 if (overlay && !overlay->AllInputsConsumed()) return; // CoinsViewOverlay::Flush() must have all inputs consumed before being called
                   +  + ]
     172                 :      366572 :                 coins_view_cache.Flush(/*reallocate_cache=*/fuzzed_data_provider.ConsumeBool());
     173                 :             :             },
     174                 :      241525 :             [&] {
     175         [ +  + ]:      241525 :                 if (overlay) return; // CoinsViewOverlay::Sync() is never called in production code
     176                 :      237644 :                 coins_view_cache.Sync();
     177                 :             :             },
     178                 :       30894 :             [&] {
     179   [ +  +  +  -  :      117516 :                 if (db) WITH_LOCK(::cs_main, (void)db->CompactFullAsync());
                   +  - ]
     180                 :       30894 :             },
     181                 :       18724 :             [&] {
     182                 :       18724 :                 uint256 best_block{ConsumeUInt256(fuzzed_data_provider)};
     183                 :             :                 // `CCoinsViewDB::BatchWrite()` requires a non-null best block.
     184   [ +  +  +  + ]:       34582 :                 if (is_db && best_block.IsNull()) best_block = uint256::ONE;
     185                 :       18724 :                 coins_view_cache.SetBestBlock(best_block);
     186                 :       18724 :             },
     187                 :        6121 :             [&] {
     188                 :        6121 :                 (void)coins_view_cache.CreateResetGuard();
     189                 :             :                 // Reset() clears the best block, so reseed db-backed caches.
     190         [ +  + ]:        6121 :                 if (is_db) {
     191                 :        4950 :                     const uint256 best_block{ConsumeUInt256(fuzzed_data_provider)};
     192         [ +  + ]:        9900 :                     if (best_block.IsNull()) {
     193                 :          65 :                         good_data = false;
     194                 :          65 :                         return;
     195                 :             :                     }
     196                 :        4885 :                     coins_view_cache.SetBestBlock(best_block);
     197                 :             :                 }
     198                 :             :             },
     199                 :       61332 :             [&] {
     200                 :       61332 :                 Coin move_to;
     201   [ +  +  +  - ]:       62656 :                 (void)coins_view_cache.SpendCoin(random_out_point, fuzzed_data_provider.ConsumeBool() ? &move_to : nullptr);
     202                 :       61332 :             },
     203                 :        7646 :             [&] {
     204                 :        7646 :                 coins_view_cache.Uncache(random_out_point);
     205                 :        7646 :             },
     206                 :       32105 :             [&] {
     207         [ +  + ]:       32105 :                 if (overlay) return; // // CoinsViewOverlay::SetBackend() is never called in production code
     208                 :       23225 :                 const bool use_original_backend{fuzzed_data_provider.ConsumeBool()};
     209   [ +  +  +  + ]:       23225 :                 if (use_original_backend && backend_coins_view != original_backend) {
     210                 :             :                     // FRESH flags valid against the empty backend may be invalid
     211                 :             :                     // against the original backend, so reset before restoring it.
     212                 :         811 :                     (void)coins_view_cache.CreateResetGuard();
     213                 :             :                     // Reset() clears the best block; db backends require a non-null hash.
     214         [ +  - ]:         811 :                     if (is_db) coins_view_cache.SetBestBlock(uint256::ONE);
     215                 :             :                 }
     216                 :       23225 :                 backend_coins_view = use_original_backend ? original_backend : &CoinsViewEmpty::Get();
     217                 :       23225 :                 coins_view_cache.SetBackend(*backend_coins_view);
     218                 :             :             },
     219                 :        5173 :             [&] {
     220                 :        5173 :                 const std::optional<COutPoint> opt_out_point = ConsumeDeserializable<COutPoint>(fuzzed_data_provider);
     221         [ +  + ]:        5173 :                 if (!opt_out_point) {
     222                 :         348 :                     good_data = false;
     223                 :         348 :                     return;
     224                 :             :                 }
     225                 :        4825 :                 random_out_point = *opt_out_point;
     226                 :             :             },
     227                 :        8133 :             [&] {
     228                 :        8133 :                 const std::optional<Coin> opt_coin = ConsumeDeserializable<Coin>(fuzzed_data_provider);
     229         [ +  + ]:        8133 :                 if (!opt_coin) {
     230                 :         595 :                     good_data = false;
     231                 :         595 :                     return;
     232                 :             :                 }
     233                 :        7538 :                 random_coin = *opt_coin;
     234                 :        8133 :             },
     235                 :        4841 :             [&] {
     236                 :        4841 :                 const std::optional<CMutableTransaction> opt_mutable_transaction = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS);
     237         [ +  + ]:        4841 :                 if (!opt_mutable_transaction) {
     238                 :        1005 :                     good_data = false;
     239         [ -  + ]:        1005 :                     return;
     240                 :             :                 }
     241         [ +  - ]:        3836 :                 random_mutable_transaction = *opt_mutable_transaction;
     242                 :        4841 :             },
     243                 :        5410 :             [&] {
     244                 :        5410 :                 CoinsCachePair sentinel{};
     245                 :        5410 :                 sentinel.second.SelfRef(sentinel);
     246                 :        5410 :                 size_t dirty_count{0};
     247         [ +  - ]:        5410 :                 CCoinsMapMemoryResource resource;
     248   [ +  -  +  - ]:        5410 :                 CCoinsMap coins_map{0, SaltedOutpointHasher{/*deterministic=*/true}, CCoinsMap::key_equal{}, &resource};
     249   [ +  -  +  +  :      466731 :                 LIMITED_WHILE (good_data && fuzzed_data_provider.ConsumeBool(), 10'000) {
                   +  + ]
     250                 :      461462 :                     CCoinsCacheEntry coins_cache_entry;
     251         [ +  + ]:      461462 :                     if (fuzzed_data_provider.ConsumeBool()) {
     252                 :      460581 :                         coins_cache_entry.coin = random_coin;
     253                 :             :                     } else {
     254                 :         881 :                         const std::optional<Coin> opt_coin = ConsumeDeserializable<Coin>(fuzzed_data_provider);
     255         [ +  + ]:         881 :                         if (!opt_coin) {
     256                 :         141 :                             good_data = false;
     257                 :         141 :                             return;
     258                 :             :                         }
     259                 :         740 :                         coins_cache_entry.coin = *opt_coin;
     260                 :         740 :                     }
     261                 :             :                     // Avoid setting FRESH for an outpoint that already exists unspent in the parent view.
     262   [ +  -  +  +  :      462792 :                     bool fresh{!coins_view_cache.PeekCoin(random_out_point) && fuzzed_data_provider.ConsumeBool()};
                   +  + ]
     263   [ +  +  +  + ]:      461321 :                     bool dirty{fresh || fuzzed_data_provider.ConsumeBool()};
     264         [ +  - ]:      461321 :                     auto it{coins_map.emplace(random_out_point, std::move(coins_cache_entry)).first};
     265         [ +  + ]:      461321 :                     if (dirty) CCoinsCacheEntry::SetDirty(*it, sentinel);
     266         [ +  + ]:      461321 :                     if (fresh) CCoinsCacheEntry::SetFresh(*it, sentinel);
     267                 :      461321 :                     dirty_count += dirty;
     268                 :      461462 :                 }
     269         [ +  - ]:        5269 :                 auto cursor{CoinsViewCacheCursor(dirty_count, sentinel, coins_map, /*will_erase=*/true)};
     270         [ +  - ]:        5269 :                 uint256 best_block{coins_view_cache.GetBestBlock()};
     271         [ +  + ]:        5269 :                 if (fuzzed_data_provider.ConsumeBool()) best_block = ConsumeUInt256(fuzzed_data_provider);
     272                 :             :                 // Set best block hash to non-null to satisfy the assertion in CCoinsViewDB::BatchWrite().
     273   [ +  +  +  + ]:        9646 :                 if (is_db && best_block.IsNull()) best_block = uint256::ONE;
     274         [ +  - ]:        5269 :                 coins_view_cache.BatchWrite(cursor, best_block);
     275                 :        5410 :             });
     276                 :             :     }
     277                 :             : 
     278                 :       19272 :     {
     279         [ +  - ]:       19272 :         (void)coins_view_cache.DynamicMemoryUsage();
     280         [ +  - ]:       19272 :         (void)coins_view_cache.EstimateSize();
     281         [ +  - ]:       19272 :         (void)coins_view_cache.GetBestBlock();
     282         [ +  - ]:       19272 :         (void)coins_view_cache.GetCacheSize();
     283         [ +  - ]:       19272 :         (void)coins_view_cache.GetHeadBlocks();
     284   [ +  -  +  - ]:       19272 :         (void)coins_view_cache.HaveInputs(CTransaction{random_mutable_transaction});
     285                 :             :     }
     286                 :             : 
     287                 :       19272 :     {
     288   [ +  +  +  + ]:       19272 :         if (is_db && backend_coins_view == original_backend) {
     289   [ +  -  -  + ]:        9417 :             assert(db->Cursor());
     290                 :             :         }
     291         [ +  - ]:       19272 :         (void)backend_coins_view->EstimateSize();
     292         [ +  - ]:       19272 :         (void)backend_coins_view->GetBestBlock();
     293         [ +  - ]:       19272 :         (void)backend_coins_view->GetHeadBlocks();
     294                 :             :     }
     295                 :             : 
     296         [ +  + ]:       19272 :     if (fuzzed_data_provider.ConsumeBool()) {
     297         [ +  - ]:        7524 :         CallOneOf(
     298                 :             :             fuzzed_data_provider,
     299                 :        1021 :             [&] {
     300                 :        1021 :                 const CTransaction transaction{random_mutable_transaction};
     301                 :        1021 :                 bool is_spent = false;
     302         [ +  + ]:        4844 :                 for (const CTxOut& tx_out : transaction.vout) {
     303         [ +  + ]:        3823 :                     if (Coin{tx_out, 0, transaction.IsCoinBase()}.IsSpent()) {
     304                 :          11 :                         is_spent = true;
     305                 :             :                     }
     306                 :             :                 }
     307         [ +  + ]:        1021 :                 if (is_spent) {
     308                 :             :                     // Avoid:
     309                 :             :                     // coins.cpp:69: void CCoinsViewCache::AddCoin(const COutPoint &, Coin &&, bool): Assertion `!coin.IsSpent()' failed.
     310                 :           4 :                     return;
     311                 :             :                 }
     312                 :        1017 :                 const int height{int(fuzzed_data_provider.ConsumeIntegral<uint32_t>() >> 1)};
     313   [ +  +  +  + ]:        2031 :                 const bool check_for_overwrite{transaction.IsCoinBase() || [&] {
     314   [ -  +  +  + ]:        4779 :                     for (uint32_t i{0}; i < transaction.vout.size(); ++i) {
     315         [ +  + ]:        3769 :                         if (coins_view_cache.PeekCoin(COutPoint{transaction.GetHash(), i})) return true;
     316                 :             :                     }
     317                 :        1010 :                     return fuzzed_data_provider.ConsumeBool();
     318         [ +  - ]:        1014 :                 }()}; // We can only skip the check if the current txid has no unspent outputs
     319         [ +  - ]:        1017 :                 AddCoins(coins_view_cache, transaction, height, check_for_overwrite);
     320                 :        1021 :             },
     321                 :        2483 :             [&] {
     322         [ +  - ]:        4966 :                 (void)ValidateInputsStandardness(CTransaction{random_mutable_transaction}, coins_view_cache);
     323                 :        2483 :             },
     324                 :         707 :             [&] {
     325         [ +  - ]:         707 :                 TxValidationState state;
     326                 :         707 :                 CAmount tx_fee_out;
     327         [ +  - ]:         707 :                 const CTransaction transaction{random_mutable_transaction};
     328         [ +  + ]:         707 :                 if (ContainsSpentInput(transaction, coins_view_cache)) {
     329                 :             :                     // Avoid:
     330                 :             :                     // consensus/tx_verify.cpp:171: bool Consensus::CheckTxInputs(const CTransaction &, TxValidationState &, const CCoinsViewCache &, int, CAmount &): Assertion `!coin.IsSpent()' failed.
     331                 :             :                     return;
     332                 :             :                 }
     333         [ +  - ]:         699 :                 TxValidationState dummy;
     334   [ +  -  +  + ]:         699 :                 if (!CheckTransaction(transaction, dummy)) {
     335                 :             :                     // It is not allowed to call CheckTxInputs if CheckTransaction failed
     336                 :         654 :                     return;
     337                 :             :                 }
     338   [ +  -  +  + ]:          45 :                 if (Consensus::CheckTxInputs(transaction, state, coins_view_cache, fuzzed_data_provider.ConsumeIntegralInRange<int>(0, std::numeric_limits<int>::max()), tx_fee_out)) {
     339         [ -  + ]:           3 :                     assert(MoneyRange(tx_fee_out));
     340                 :             :                 }
     341                 :        2113 :             },
     342                 :        1677 :             [&] {
     343                 :        1677 :                 const CTransaction transaction{random_mutable_transaction};
     344         [ +  + ]:        1677 :                 if (ContainsSpentInput(transaction, coins_view_cache)) {
     345                 :             :                     // Avoid:
     346                 :             :                     // consensus/tx_verify.cpp:130: unsigned int GetP2SHSigOpCount(const CTransaction &, const CCoinsViewCache &): Assertion `!coin.IsSpent()' failed.
     347                 :          31 :                     return;
     348                 :             :                 }
     349         [ +  - ]:        1646 :                 (void)GetP2SHSigOpCount(transaction, coins_view_cache);
     350                 :        1677 :             },
     351                 :         540 :             [&] {
     352                 :         540 :                 const CTransaction transaction{random_mutable_transaction};
     353         [ +  + ]:         540 :                 if (ContainsSpentInput(transaction, coins_view_cache)) {
     354                 :             :                     // Avoid:
     355                 :             :                     // consensus/tx_verify.cpp:130: unsigned int GetP2SHSigOpCount(const CTransaction &, const CCoinsViewCache &): Assertion `!coin.IsSpent()' failed.
     356                 :             :                     return;
     357                 :             :                 }
     358         [ +  + ]:         497 :                 const auto flags = script_verify_flags::from_int(fuzzed_data_provider.ConsumeIntegral<script_verify_flags::value_type>());
     359   [ +  +  +  +  :         497 :                 if (!transaction.vin.empty() && (flags & SCRIPT_VERIFY_WITNESS) != 0 && (flags & SCRIPT_VERIFY_P2SH) == 0) {
                   +  + ]
     360                 :             :                     // Avoid:
     361                 :             :                     // script/interpreter.cpp:1705: size_t CountWitnessSigOps(const CScript &, const CScript &, const CScriptWitness &, unsigned int): Assertion `(flags & SCRIPT_VERIFY_P2SH) != 0' failed.
     362                 :             :                     return;
     363                 :             :                 }
     364         [ +  - ]:         494 :                 (void)GetTransactionSigOpCost(transaction, coins_view_cache, flags);
     365                 :         540 :             },
     366                 :        1096 :             [&] {
     367         [ +  - ]:        1096 :                 (void)IsWitnessStandard(CTransaction{random_mutable_transaction}, coins_view_cache);
     368                 :        1096 :             });
     369                 :             :     }
     370                 :             : 
     371                 :       19272 :     {
     372         [ +  - ]:       19272 :         const Coin& coin_using_access_coin = coins_view_cache.AccessCoin(random_out_point);
     373                 :       19272 :         const bool exists_using_access_coin = !(coin_using_access_coin == EMPTY_COIN);
     374         [ +  - ]:       19272 :         const bool exists_using_have_coin = coins_view_cache.HaveCoin(random_out_point);
     375         [ +  - ]:       19272 :         const bool exists_using_have_coin_in_cache = coins_view_cache.HaveCoinInCache(random_out_point);
     376   [ +  -  +  + ]:       19272 :         if (auto coin{coins_view_cache.GetCoin(random_out_point)}) {
     377         [ -  + ]:        3634 :             assert(*coin == coin_using_access_coin);
     378   [ +  -  -  + ]:        3634 :             assert(exists_using_access_coin && exists_using_have_coin_in_cache && exists_using_have_coin);
     379                 :             :         } else {
     380   [ +  -  -  + ]:       15638 :             assert(!exists_using_access_coin && !exists_using_have_coin_in_cache && !exists_using_have_coin);
     381                 :       19272 :         }
     382                 :             :         // If HaveCoin on the backend is true, it must also be on the cache if the coin wasn't spent.
     383                 :       19272 :         std::optional<Coin> coin_in_backend;
     384                 :       19272 :         bool exists_using_have_coin_in_backend;
     385         [ +  + ]:       19272 :         if (dynamic_cast<CoinsViewOverlay*>(&coins_view_cache)) {
     386                 :             :             // PeekCoin does not mutate cacheCoins, so async workers can keep running.
     387         [ +  - ]:       11472 :             coin_in_backend = backend_coins_view->PeekCoin(random_out_point);
     388                 :        5736 :             exists_using_have_coin_in_backend = coin_in_backend.has_value();
     389                 :             :         } else {
     390         [ +  - ]:       13536 :             exists_using_have_coin_in_backend = backend_coins_view->HaveCoin(random_out_point);
     391         [ +  - ]:       27072 :             coin_in_backend = backend_coins_view->GetCoin(random_out_point);
     392                 :             :         }
     393   [ +  +  +  + ]:       19272 :         if (!coin_using_access_coin.IsSpent() && exists_using_have_coin_in_backend) {
     394         [ -  + ]:        1980 :             assert(exists_using_have_coin);
     395                 :             :         }
     396         [ +  + ]:       19272 :         if (coin_in_backend) {
     397         [ -  + ]:        2057 :             assert(exists_using_have_coin_in_backend);
     398                 :             :             // Note we can't assert that `coin_using_get_coin == *coin` because the coin in
     399                 :             :             // the cache may have been modified but not yet flushed.
     400                 :             :         } else {
     401         [ -  + ]:       17215 :             assert(!exists_using_have_coin_in_backend);
     402                 :             :         }
     403                 :       19272 :     }
     404                 :       19272 : }
     405                 :             : 
     406         [ +  - ]:        4019 : FUZZ_TARGET(coins_view, .init = initialize_coins_view)
     407                 :             : {
     408                 :        3547 :     FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
     409                 :        3547 :     CCoinsViewCache coins_view_cache{&CoinsViewEmpty::Get(), /*deterministic=*/true};
     410   [ +  -  +  - ]:        3547 :     TestCoinsView(fuzzed_data_provider, coins_view_cache, &CoinsViewEmpty::Get());
     411                 :        3547 : }
     412                 :             : 
     413         [ +  - ]:        5023 : FUZZ_TARGET(coins_view_db, .init = initialize_coins_view)
     414                 :             : {
     415                 :        4551 :     FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
     416                 :        4551 :     auto db_params = DBParams{
     417                 :             :         .path = "",
     418                 :             :         .cache_bytes = 1_MiB,
     419                 :             :         .memory_only = true,
     420                 :        4551 :     };
     421         [ +  - ]:        4551 :     CCoinsViewDB backend_coins_view{std::move(db_params), CoinsViewOptions{}};
     422         [ +  - ]:        4551 :     CCoinsViewCache coins_view_cache{&backend_coins_view, /*deterministic=*/true};
     423         [ +  - ]:        4551 :     TestCoinsView(fuzzed_data_provider, coins_view_cache, &backend_coins_view);
     424                 :        9102 : }
     425                 :             : 
     426                 :             : // Creates a CoinsViewOverlay and a MutationGuardCoinsViewCache as the base.
     427                 :             : // This allows us to exercise all methods on a CoinsViewOverlay, while also
     428                 :             : // ensuring that nothing can mutate the underlying cache until Flush or Sync is
     429                 :             : // called.
     430         [ +  - ]:        3489 : FUZZ_TARGET(coins_view_overlay, .init = initialize_coins_view)
     431                 :             : {
     432                 :        3017 :     SeedRandomStateForTest(SeedRand::ZEROS); // for SaltedTxidHasher
     433                 :        3017 :     StartPoolIfNeeded();
     434                 :        3017 :     FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
     435                 :        3017 :     MutationGuardCoinsViewCache backend_cache{&CoinsViewEmpty::Get(), /*deterministic=*/true};
     436   [ +  -  -  + ]:        6034 :     CoinsViewOverlay coins_view_cache{&backend_cache, g_thread_pool, /*deterministic=*/true};
     437         [ +  - ]:        3017 :     CBlock block{BuildRandomBlock(fuzzed_data_provider, backend_cache)};
     438                 :        3017 :     const auto reset_guard{coins_view_cache.StartFetching(block)};
     439         [ +  - ]:        3017 :     TestCoinsView(fuzzed_data_provider, coins_view_cache, &backend_cache);
     440                 :        3017 : }
     441                 :             : 
     442         [ +  - ]:        3191 : FUZZ_TARGET(coins_view_stacked, .init = initialize_coins_view)
     443                 :             : {
     444                 :        2719 :     SeedRandomStateForTest(SeedRand::ZEROS); // for SaltedTxidHasher
     445                 :        2719 :     StartPoolIfNeeded();
     446                 :        2719 :     FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
     447                 :        2719 :     auto db_params = DBParams{
     448                 :             :         .path = "",
     449                 :             :         .cache_bytes = 1_MiB,
     450                 :             :         .memory_only = true,
     451                 :        2719 :     };
     452         [ +  - ]:        2719 :     CCoinsViewDB backend_base_coins_view{std::move(db_params), CoinsViewOptions{}};
     453         [ +  - ]:        2719 :     CCoinsViewCache backend_cache{&backend_base_coins_view, /*deterministic=*/true};
     454         [ +  - ]:        2719 :     TestCoinsView(fuzzed_data_provider, backend_cache, &backend_base_coins_view);
     455   [ +  -  -  + ]:        5438 :     CoinsViewOverlay coins_view_cache{&backend_cache, g_thread_pool, /*deterministic=*/true};
     456         [ +  - ]:        2719 :     CBlock block{BuildRandomBlock(fuzzed_data_provider, backend_base_coins_view)};
     457                 :        2719 :     {
     458                 :        2719 :         const auto reset_guard{coins_view_cache.StartFetching(block)};
     459         [ +  - ]:        2719 :         TestCoinsView(fuzzed_data_provider, coins_view_cache, &backend_cache);
     460                 :           0 :     }
     461         [ +  - ]:        2719 :     TestCoinsView(fuzzed_data_provider, backend_cache, &backend_base_coins_view);
     462                 :        5438 : }
        

Generated by: LCOV version 2.0-1