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 % 60 60
Test Date: 2025-07-01 04:09:48 Functions: 100.0 % 4 4
Branches: 66.7 % 99 66

             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::IsBlockMutatedFn FuzzedIsBlockMutated(bool result)
      40                 :             : {
      41                 :         661 :     return [result](const CBlock& block, bool) {
      42                 :         175 :         return result;
      43                 :         486 :     };
      44                 :             : }
      45                 :             : 
      46         [ +  - ]:        1017 : FUZZ_TARGET(partially_downloaded_block, .init = initialize_pdb)
      47                 :             : {
      48                 :         571 :     SeedRandomStateForTest(SeedRand::ZEROS);
      49                 :         571 :     FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
      50                 :         571 :     SetMockTime(ConsumeTime(fuzzed_data_provider));
      51                 :             : 
      52                 :         571 :     auto block{ConsumeDeserializable<CBlock>(fuzzed_data_provider, TX_WITH_WITNESS)};
      53   [ +  +  +  +  :         571 :     if (!block || block->vtx.size() == 0 ||
                   +  + ]
      54         [ +  + ]:         487 :         block->vtx.size() >= std::numeric_limits<uint16_t>::max()) {
      55                 :          85 :         return;
      56                 :             :     }
      57                 :             : 
      58         [ +  - ]:         486 :     CBlockHeaderAndShortTxIDs cmpctblock{*block, fuzzed_data_provider.ConsumeIntegral<uint64_t>()};
      59                 :             : 
      60         [ +  - ]:         486 :     bilingual_str error;
      61   [ +  -  +  - ]:         486 :     CTxMemPool pool{MemPoolOptionsForTest(g_setup->m_node), error};
      62         [ +  - ]:         486 :     Assert(error.empty());
      63                 :         486 :     PartiallyDownloadedBlock pdb{&pool};
      64                 :             : 
      65                 :             :     // Set of available transactions (mempool or extra_txn)
      66         [ +  - ]:         486 :     std::set<uint16_t> available;
      67                 :             :     // The coinbase is always available
      68         [ +  - ]:         486 :     available.insert(0);
      69                 :             : 
      70                 :         486 :     std::vector<CTransactionRef> extra_txn;
      71         [ +  + ]:      840378 :     for (size_t i = 1; i < block->vtx.size(); ++i) {
      72         [ +  - ]:      839892 :         auto tx{block->vtx[i]};
      73                 :             : 
      74                 :      839892 :         bool add_to_extra_txn{fuzzed_data_provider.ConsumeBool()};
      75                 :      839892 :         bool add_to_mempool{fuzzed_data_provider.ConsumeBool()};
      76                 :             : 
      77         [ +  + ]:      839892 :         if (add_to_extra_txn) {
      78         [ +  - ]:      644452 :             extra_txn.emplace_back(tx);
      79         [ +  - ]:      644452 :             available.insert(i);
      80                 :             :         }
      81                 :             : 
      82   [ +  +  +  -  :      839892 :         if (add_to_mempool && !pool.exists(GenTxid::Txid(tx->GetHash()))) {
                   +  + ]
      83   [ +  -  +  - ]:       60246 :             LOCK2(cs_main, pool.cs);
      84         [ +  - ]:       60246 :             AddToMempool(pool, ConsumeTxMemPoolEntry(fuzzed_data_provider, *tx));
      85         [ +  - ]:       60246 :             available.insert(i);
      86         [ +  - ]:      120492 :         }
      87                 :      839892 :     }
      88                 :             : 
      89         [ +  - ]:         486 :     auto init_status{pdb.InitData(cmpctblock, extra_txn)};
      90                 :             : 
      91                 :         486 :     std::vector<CTransactionRef> missing;
      92                 :             :     // Whether we skipped a transaction that should be included in `missing`.
      93                 :             :     // FillBlock should never return READ_STATUS_OK if that is the case.
      94                 :         486 :     bool skipped_missing{false};
      95         [ +  + ]:      840864 :     for (size_t i = 0; i < cmpctblock.BlockTxCount(); i++) {
      96                 :             :         // If init_status == READ_STATUS_OK then a available transaction in the
      97                 :             :         // compact block (i.e. IsTxAvailable(i) == true) implies that we marked
      98                 :             :         // that transaction as available above (i.e. available.count(i) > 0).
      99                 :             :         // The reverse is not true, due to possible compact block short id
     100                 :             :         // collisions (i.e. available.count(i) > 0 does not imply
     101                 :             :         // IsTxAvailable(i) == true).
     102         [ +  + ]:      840378 :         if (init_status == READ_STATUS_OK) {
     103   [ +  -  +  +  :        3517 :             assert(!pdb.IsTxAvailable(i) || available.count(i) > 0);
                   -  + ]
     104                 :             :         }
     105                 :             : 
     106                 :      840378 :         bool skip{fuzzed_data_provider.ConsumeBool()};
     107   [ +  -  +  +  :      840378 :         if (!pdb.IsTxAvailable(i) && !skip) {
                   +  + ]
     108         [ +  - ]:      555013 :             missing.push_back(block->vtx[i]);
     109                 :             :         }
     110                 :             : 
     111   [ +  -  +  +  :     1398382 :         skipped_missing |= (!pdb.IsTxAvailable(i) && skip);
                   +  + ]
     112                 :             :     }
     113                 :             : 
     114                 :         486 :     bool segwit_active{fuzzed_data_provider.ConsumeBool()};
     115                 :             : 
     116                 :             :     // Mock IsBlockMutated
     117                 :         486 :     bool fail_block_mutated{fuzzed_data_provider.ConsumeBool()};
     118                 :         486 :     pdb.m_check_block_mutated_mock = FuzzedIsBlockMutated(fail_block_mutated);
     119                 :             : 
     120                 :         486 :     CBlock reconstructed_block;
     121         [ +  - ]:         486 :     auto fill_status{pdb.FillBlock(reconstructed_block, missing, segwit_active)};
     122      [ +  +  + ]:         486 :     switch (fill_status) {
     123                 :         167 :     case READ_STATUS_OK:
     124         [ -  + ]:         167 :         assert(!skipped_missing);
     125         [ -  + ]:         167 :         assert(!fail_block_mutated);
     126   [ +  -  +  -  :         167 :         assert(block->GetHash() == reconstructed_block.GetHash());
                   -  + ]
     127                 :             :         break;
     128                 :           8 :     case READ_STATUS_FAILED:
     129         [ -  + ]:           8 :         assert(fail_block_mutated);
     130                 :             :         break;
     131                 :             :     case READ_STATUS_INVALID:
     132                 :             :         break;
     133                 :             :     }
     134                 :        2029 : }
        

Generated by: LCOV version 2.0-1