LCOV - code coverage report
Current view: top level - src/test/fuzz - partially_downloaded_block.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 0.0 % 63 0
Test Date: 2024-08-28 04:44:32 Functions: 0.0 % 5 0
Branches: 0.0 % 107 0

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

Generated by: LCOV version 2.0-1