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 % 65 65
Test Date: 2025-06-01 05:27:21 Functions: 100.0 % 5 5
Branches: 67.3 % 107 72

             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/util/setup_common.h>
      15                 :             : #include <test/util/txmempool.h>
      16                 :             : #include <txmempool.h>
      17                 :             : #include <util/check.h>
      18                 :             : #include <util/time.h>
      19                 :             : #include <util/translation.h>
      20                 :             : 
      21                 :             : #include <cstddef>
      22                 :             : #include <cstdint>
      23                 :             : #include <limits>
      24                 :             : #include <memory>
      25                 :             : #include <optional>
      26                 :             : #include <set>
      27                 :             : #include <vector>
      28                 :             : 
      29                 :             : namespace {
      30                 :             : const TestingSetup* g_setup;
      31                 :             : } // namespace
      32                 :             : 
      33                 :           1 : void initialize_pdb()
      34                 :             : {
      35   [ +  -  +  - ]:           2 :     static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
      36                 :           1 :     g_setup = testing_setup.get();
      37         [ +  - ]:           2 : }
      38                 :             : 
      39                 :         486 : PartiallyDownloadedBlock::CheckBlockFn FuzzedCheckBlock(std::optional<BlockValidationResult> result)
      40                 :             : {
      41                 :         661 :     return [result](const CBlock&, BlockValidationState& state, const Consensus::Params&, bool, bool) {
      42         [ +  + ]:         175 :         if (result) {
      43   [ +  -  +  - ]:          12 :             return state.Invalid(*result);
      44                 :             :         }
      45                 :             : 
      46                 :             :         return true;
      47                 :         486 :     };
      48                 :             : }
      49                 :             : 
      50         [ +  - ]:        1013 : FUZZ_TARGET(partially_downloaded_block, .init = initialize_pdb)
      51                 :             : {
      52                 :         571 :     SeedRandomStateForTest(SeedRand::ZEROS);
      53                 :         571 :     FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
      54                 :         571 :     SetMockTime(ConsumeTime(fuzzed_data_provider));
      55                 :             : 
      56                 :         571 :     auto block{ConsumeDeserializable<CBlock>(fuzzed_data_provider, TX_WITH_WITNESS)};
      57   [ +  +  +  +  :         571 :     if (!block || block->vtx.size() == 0 ||
                   +  + ]
      58         [ +  + ]:         487 :         block->vtx.size() >= std::numeric_limits<uint16_t>::max()) {
      59                 :          85 :         return;
      60                 :             :     }
      61                 :             : 
      62         [ +  - ]:         486 :     CBlockHeaderAndShortTxIDs cmpctblock{*block, fuzzed_data_provider.ConsumeIntegral<uint64_t>()};
      63                 :             : 
      64         [ +  - ]:         486 :     bilingual_str error;
      65   [ +  -  +  - ]:         486 :     CTxMemPool pool{MemPoolOptionsForTest(g_setup->m_node), error};
      66         [ +  - ]:         486 :     Assert(error.empty());
      67                 :         486 :     PartiallyDownloadedBlock pdb{&pool};
      68                 :             : 
      69                 :             :     // Set of available transactions (mempool or extra_txn)
      70         [ +  - ]:         486 :     std::set<uint16_t> available;
      71                 :             :     // The coinbase is always available
      72         [ +  - ]:         486 :     available.insert(0);
      73                 :             : 
      74                 :         486 :     std::vector<CTransactionRef> extra_txn;
      75         [ +  + ]:      840378 :     for (size_t i = 1; i < block->vtx.size(); ++i) {
      76         [ +  - ]:      839892 :         auto tx{block->vtx[i]};
      77                 :             : 
      78                 :      839892 :         bool add_to_extra_txn{fuzzed_data_provider.ConsumeBool()};
      79                 :      839892 :         bool add_to_mempool{fuzzed_data_provider.ConsumeBool()};
      80                 :             : 
      81         [ +  + ]:      839892 :         if (add_to_extra_txn) {
      82         [ +  - ]:      644452 :             extra_txn.emplace_back(tx);
      83         [ +  - ]:      644452 :             available.insert(i);
      84                 :             :         }
      85                 :             : 
      86   [ +  +  +  -  :      839892 :         if (add_to_mempool && !pool.exists(GenTxid::Txid(tx->GetHash()))) {
                   +  + ]
      87   [ +  -  +  - ]:       60246 :             LOCK2(cs_main, pool.cs);
      88         [ +  - ]:       60246 :             AddToMempool(pool, ConsumeTxMemPoolEntry(fuzzed_data_provider, *tx));
      89         [ +  - ]:       60246 :             available.insert(i);
      90         [ +  - ]:      120492 :         }
      91                 :      839892 :     }
      92                 :             : 
      93         [ +  - ]:         486 :     auto init_status{pdb.InitData(cmpctblock, extra_txn)};
      94                 :             : 
      95                 :         486 :     std::vector<CTransactionRef> missing;
      96                 :             :     // Whether we skipped a transaction that should be included in `missing`.
      97                 :             :     // FillBlock should never return READ_STATUS_OK if that is the case.
      98                 :         486 :     bool skipped_missing{false};
      99         [ +  + ]:      840864 :     for (size_t i = 0; i < cmpctblock.BlockTxCount(); i++) {
     100                 :             :         // If init_status == READ_STATUS_OK then a available transaction in the
     101                 :             :         // compact block (i.e. IsTxAvailable(i) == true) implies that we marked
     102                 :             :         // that transaction as available above (i.e. available.count(i) > 0).
     103                 :             :         // The reverse is not true, due to possible compact block short id
     104                 :             :         // collisions (i.e. available.count(i) > 0 does not imply
     105                 :             :         // IsTxAvailable(i) == true).
     106         [ +  + ]:      840378 :         if (init_status == READ_STATUS_OK) {
     107   [ +  -  +  +  :        3517 :             assert(!pdb.IsTxAvailable(i) || available.count(i) > 0);
                   -  + ]
     108                 :             :         }
     109                 :             : 
     110                 :      840378 :         bool skip{fuzzed_data_provider.ConsumeBool()};
     111   [ +  -  +  +  :      840378 :         if (!pdb.IsTxAvailable(i) && !skip) {
                   +  + ]
     112         [ +  - ]:      555013 :             missing.push_back(block->vtx[i]);
     113                 :             :         }
     114                 :             : 
     115   [ +  -  +  +  :     1398382 :         skipped_missing |= (!pdb.IsTxAvailable(i) && skip);
                   +  + ]
     116                 :             :     }
     117                 :             : 
     118                 :             :     // Mock CheckBlock
     119                 :         486 :     bool fail_check_block{fuzzed_data_provider.ConsumeBool()};
     120                 :         486 :     auto validation_result =
     121                 :         486 :         fuzzed_data_provider.PickValueInArray(
     122                 :             :             {BlockValidationResult::BLOCK_RESULT_UNSET,
     123                 :             :              BlockValidationResult::BLOCK_CONSENSUS,
     124                 :             :              BlockValidationResult::BLOCK_CACHED_INVALID,
     125                 :             :              BlockValidationResult::BLOCK_INVALID_HEADER,
     126                 :             :              BlockValidationResult::BLOCK_MUTATED,
     127                 :             :              BlockValidationResult::BLOCK_MISSING_PREV,
     128                 :             :              BlockValidationResult::BLOCK_INVALID_PREV,
     129                 :             :              BlockValidationResult::BLOCK_TIME_FUTURE,
     130                 :         486 :              BlockValidationResult::BLOCK_HEADER_LOW_WORK});
     131         [ +  + ]:         486 :     pdb.m_check_block_mock = FuzzedCheckBlock(
     132                 :             :         fail_check_block ?
     133                 :             :             std::optional<BlockValidationResult>{validation_result} :
     134                 :         486 :             std::nullopt);
     135                 :             : 
     136                 :         486 :     CBlock reconstructed_block;
     137         [ +  - ]:         486 :     auto fill_status{pdb.FillBlock(reconstructed_block, missing)};
     138      [ +  +  + ]:         486 :     switch (fill_status) {
     139                 :         163 :     case READ_STATUS_OK:
     140         [ -  + ]:         163 :         assert(!skipped_missing);
     141         [ -  + ]:         163 :         assert(!fail_check_block);
     142   [ +  -  +  -  :         163 :         assert(block->GetHash() == reconstructed_block.GetHash());
                   -  + ]
     143                 :             :         break;
     144                 :          12 :     case READ_STATUS_CHECKBLOCK_FAILED: [[fallthrough]];
     145                 :          12 :     case READ_STATUS_FAILED:
     146         [ -  + ]:          12 :         assert(fail_check_block);
     147                 :             :         break;
     148                 :             :     case READ_STATUS_INVALID:
     149                 :             :         break;
     150                 :             :     }
     151                 :        2029 : }
        

Generated by: LCOV version 2.0-1