LCOV - code coverage report
Current view: top level - src/test/fuzz - partially_downloaded_block.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 100.0 % 64 64
Test Date: 2026-10-31 05:48:04 Functions: 100.0 % 4 4
Branches: 64.8 % 105 68

             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 https://opensource.org/license/mit.
       4                 :             : 
       5                 :             : #include <blockencodings.h>
       6                 :             : #include <consensus/merkle.h>
       7                 :             : #include <consensus/validation.h>
       8                 :             : #include <primitives/block.h>
       9                 :             : #include <primitives/transaction.h>
      10                 :             : #include <test/fuzz/FuzzedDataProvider.h>
      11                 :             : #include <test/fuzz/fuzz.h>
      12                 :             : #include <test/fuzz/util.h>
      13                 :             : #include <test/fuzz/util/mempool.h>
      14                 :             : #include <test/fuzz/util/reachability.h>
      15                 :             : #include <test/util/setup_common.h>
      16                 :             : #include <test/util/time.h>
      17                 :             : #include <test/util/txmempool.h>
      18                 :             : #include <txmempool.h>
      19                 :             : #include <util/check.h>
      20                 :             : #include <util/time.h>
      21                 :             : #include <util/translation.h>
      22                 :             : 
      23                 :             : #include <cstddef>
      24                 :             : #include <cstdint>
      25                 :             : #include <limits>
      26                 :             : #include <memory>
      27                 :             : #include <optional>
      28                 :             : #include <set>
      29                 :             : #include <vector>
      30                 :             : 
      31                 :             : namespace {
      32                 :             : const TestingSetup* g_setup;
      33                 :             : constexpr ReachabilityGoal COMPACT_BLOCK_INITIALIZATION_SUCCEEDS{"compact block initialization succeeds"};
      34                 :             : constexpr ReachabilityGoal COMPACT_BLOCK_RECONSTRUCTION_SUCCEEDS{"compact block reconstruction succeeds"};
      35                 :             : } // namespace
      36                 :             : 
      37                 :           1 : void initialize_pdb()
      38                 :             : {
      39                 :           1 :     RegisterReachabilityGoal(COMPACT_BLOCK_INITIALIZATION_SUCCEEDS);
      40                 :           1 :     RegisterReachabilityGoal(COMPACT_BLOCK_RECONSTRUCTION_SUCCEEDS);
      41   [ +  -  +  -  :           1 :     static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
                   +  - ]
      42                 :           1 :     g_setup = testing_setup.get();
      43                 :           1 : }
      44                 :             : 
      45                 :         912 : PartiallyDownloadedBlock::IsBlockMutatedFn FuzzedIsBlockMutated(bool result)
      46                 :             : {
      47                 :        1329 :     return [result](const CBlock& block, bool) {
      48                 :         417 :         return result;
      49                 :         912 :     };
      50                 :             : }
      51                 :             : 
      52         [ +  - ]:        1481 : FUZZ_TARGET(partially_downloaded_block, .init = initialize_pdb)
      53                 :             : {
      54                 :        1005 :     SeedRandomStateForTest(SeedRand::ZEROS);
      55                 :        1005 :     FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
      56                 :        1005 :     FakeNodeClock clock{ConsumeTime(fuzzed_data_provider)};
      57                 :             : 
      58                 :        1005 :     auto block{ConsumeDeserializable<CBlock>(fuzzed_data_provider, TX_WITH_WITNESS)};
      59   [ +  +  -  +  :        1005 :     if (!block || block->vtx.size() == 0 ||
                   +  + ]
      60         [ +  + ]:         913 :         block->vtx.size() >= std::numeric_limits<uint16_t>::max()) {
      61                 :          93 :         return;
      62                 :             :     }
      63                 :             : 
      64         [ +  - ]:         912 :     CBlockHeaderAndShortTxIDs cmpctblock{*block, fuzzed_data_provider.ConsumeIntegral<uint64_t>()};
      65                 :             : 
      66         [ +  - ]:         912 :     bilingual_str error;
      67   [ +  -  +  - ]:         912 :     CTxMemPool pool{MemPoolOptionsForTest(g_setup->m_node), error};
      68         [ +  - ]:         912 :     Assert(error.empty());
      69                 :         912 :     PartiallyDownloadedBlock pdb{&pool};
      70                 :             : 
      71                 :             :     // Set of available transactions (mempool or extra_txn)
      72         [ +  - ]:         912 :     std::set<uint16_t> available;
      73                 :             :     // The coinbase is always available
      74         [ +  - ]:         912 :     available.insert(0);
      75                 :             : 
      76                 :         912 :     std::vector<std::pair<Wtxid, CTransactionRef>> extra_txn;
      77   [ -  +  +  + ]:     2289596 :     for (size_t i = 1; i < block->vtx.size(); ++i) {
      78         [ +  - ]:     2288684 :         auto tx{block->vtx[i]};
      79                 :             : 
      80                 :     2288684 :         bool add_to_extra_txn{fuzzed_data_provider.ConsumeBool()};
      81                 :     2288684 :         bool add_to_mempool{fuzzed_data_provider.ConsumeBool()};
      82                 :             : 
      83         [ +  + ]:     2288684 :         if (add_to_extra_txn) {
      84         [ +  - ]:     1289708 :             extra_txn.emplace_back(tx->GetWitnessHash(), tx);
      85         [ +  - ]:     1289708 :             available.insert(i);
      86                 :             :         }
      87                 :             : 
      88   [ +  +  +  -  :     2288684 :         if (add_to_mempool && !pool.exists(tx->GetHash())) {
                   +  + ]
      89   [ +  -  +  - ]:      308252 :             LOCK2(cs_main, pool.cs);
      90         [ +  - ]:      308252 :             TryAddToMempool(pool, ConsumeTxMemPoolEntry(fuzzed_data_provider, *tx));
      91         [ +  - ]:      308252 :             available.insert(i);
      92         [ +  - ]:      616504 :         }
      93                 :     2288684 :     }
      94                 :             : 
      95         [ +  - ]:         912 :     auto init_status{pdb.InitData(cmpctblock, extra_txn)};
      96         [ +  - ]:         912 :     ObserveReachabilityGoal(init_status == READ_STATUS_OK, COMPACT_BLOCK_INITIALIZATION_SUCCEEDS);
      97                 :             : 
      98                 :         912 :     std::vector<CTransactionRef> missing;
      99                 :             :     // Whether we skipped a transaction that should be included in `missing`.
     100                 :             :     // FillBlock should never return READ_STATUS_OK if that is the case.
     101                 :         912 :     bool skipped_missing{false};
     102   [ -  +  +  + ]:     4581016 :     for (size_t i = 0; i < cmpctblock.BlockTxCount(); i++) {
     103                 :             :         // If init_status == READ_STATUS_OK then a available transaction in the
     104                 :             :         // compact block (i.e. IsTxAvailable(i) == true) implies that we marked
     105                 :             :         // that transaction as available above (i.e. available.contains(i)).
     106                 :             :         // The reverse is not true, due to possible compact block short id
     107                 :             :         // collisions (i.e. available.contains(i) does not imply
     108                 :             :         // IsTxAvailable(i) == true).
     109         [ +  + ]:     2289596 :         if (init_status == READ_STATUS_OK) {
     110   [ +  -  +  +  :        6393 :             assert(!pdb.IsTxAvailable(i) || available.contains(i));
                   -  + ]
     111                 :             :         }
     112                 :             : 
     113                 :     2289596 :         bool skip{fuzzed_data_provider.ConsumeBool()};
     114   [ +  -  +  +  :     2289596 :         if (!pdb.IsTxAvailable(i) && !skip) {
                   +  + ]
     115         [ +  - ]:     1649171 :             missing.push_back(block->vtx[i]);
     116                 :             :         }
     117                 :             : 
     118   [ +  -  +  +  :     3944495 :         skipped_missing |= (!pdb.IsTxAvailable(i) && skip);
                   +  + ]
     119                 :             :     }
     120                 :             : 
     121                 :         912 :     bool segwit_active{fuzzed_data_provider.ConsumeBool()};
     122                 :             : 
     123                 :             :     // Mock IsBlockMutated
     124                 :         912 :     bool fail_block_mutated{fuzzed_data_provider.ConsumeBool()};
     125                 :         912 :     pdb.m_check_block_mutated_mock = FuzzedIsBlockMutated(fail_block_mutated);
     126                 :             : 
     127                 :         912 :     CBlock reconstructed_block;
     128         [ +  - ]:         912 :     auto fill_status{pdb.FillBlock(reconstructed_block, missing, segwit_active)};
     129         [ +  - ]:         912 :     ObserveReachabilityGoal(fill_status == READ_STATUS_OK, COMPACT_BLOCK_RECONSTRUCTION_SUCCEEDS);
     130      [ +  +  + ]:         912 :     switch (fill_status) {
     131                 :         406 :     case READ_STATUS_OK:
     132         [ -  + ]:         406 :         assert(!skipped_missing);
     133         [ -  + ]:         406 :         assert(!fail_block_mutated);
     134   [ +  -  +  - ]:        1318 :         assert(block->GetHash() == reconstructed_block.GetHash());
     135                 :             :         break;
     136                 :          11 :     case READ_STATUS_FAILED:
     137         [ -  + ]:          11 :         assert(fail_block_mutated);
     138                 :             :         break;
     139                 :             :     case READ_STATUS_INVALID:
     140                 :             :         break;
     141                 :             :     }
     142                 :        3741 : }
        

Generated by: LCOV version 2.5.0-full