LCOV - code coverage report
Current view: top level - src/test/fuzz - cmpctblock.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 99.6 % 283 282
Test Date: 2026-11-21 05:38:30 Functions: 100.0 % 23 23
Branches: 60.1 % 406 244

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2026 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 <addrman.h>
       6                 :             : #include <blockencodings.h>
       7                 :             : #include <chain.h>
       8                 :             : #include <chainparams.h>
       9                 :             : #include <coins.h>
      10                 :             : #include <consensus/amount.h>
      11                 :             : #include <consensus/consensus.h>
      12                 :             : #include <consensus/merkle.h>
      13                 :             : #include <net.h>
      14                 :             : #include <net_processing.h>
      15                 :             : #include <netmessagemaker.h>
      16                 :             : #include <node/blockstorage.h>
      17                 :             : #include <policy/truc_policy.h>
      18                 :             : #include <primitives/block.h>
      19                 :             : #include <primitives/transaction.h>
      20                 :             : #include <protocol.h>
      21                 :             : #include <script/script.h>
      22                 :             : #include <serialize.h>
      23                 :             : #include <sync.h>
      24                 :             : #include <test/fuzz/FuzzedDataProvider.h>
      25                 :             : #include <test/fuzz/fuzz.h>
      26                 :             : #include <test/fuzz/util.h>
      27                 :             : #include <test/fuzz/util/net.h>
      28                 :             : #include <test/fuzz/util/reachability.h>
      29                 :             : #include <test/util/net.h>
      30                 :             : #include <test/util/random.h>
      31                 :             : #include <test/util/script.h>
      32                 :             : #include <test/util/setup_common.h>
      33                 :             : #include <test/util/time.h>
      34                 :             : #include <test/util/validation.h>
      35                 :             : #include <txmempool.h>
      36                 :             : #include <uint256.h>
      37                 :             : #include <util/check.h>
      38                 :             : #include <util/time.h>
      39                 :             : #include <validation.h>
      40                 :             : #include <validationinterface.h>
      41                 :             : 
      42                 :             : #include <boost/multi_index/detail/hash_index_iterator.hpp>
      43                 :             : 
      44                 :             : #include <cstddef>
      45                 :             : #include <cstdint>
      46                 :             : #include <functional>
      47                 :             : #include <iterator>
      48                 :             : #include <memory>
      49                 :             : #include <optional>
      50                 :             : #include <string>
      51                 :             : #include <utility>
      52                 :             : #include <vector>
      53                 :             : 
      54                 :             : namespace {
      55                 :             : 
      56                 :             : TestingSetup* g_setup;
      57                 :             : constexpr ReachabilityGoal CMPCTBLOCK_CHANGES_MEMPOOL{"cmpctblock changes the mempool"};
      58                 :             : constexpr ReachabilityGoal CMPCTBLOCK_CHANGES_BLOCK_INDEX{"cmpctblock changes the block index"};
      59                 :             : 
      60                 :             : //! Fee each created tx will pay.
      61                 :             : const CAmount AMOUNT_FEE{1000};
      62                 :             : //! Cached coinbases that each iteration can copy and use.
      63                 :             : std::vector<std::pair<COutPoint, CAmount>> g_mature_coinbase;
      64                 :             : //! Constant value used to create valid headers.
      65                 :             : uint32_t g_nBits;
      66                 :             : //! One for each block the fuzzer generates.
      67                 :      127834 : struct BlockInfo {
           [ -  +  +  -  
          -  -  +  -  -  
                -  -  - ]
      68                 :             :     std::shared_ptr<CBlock> block;
      69                 :             :     uint256 hash;
      70                 :             :     uint32_t height;
      71                 :             : };
      72                 :             : //! Used to access prefilledtxn and shorttxids.
      73                 :           0 : class FuzzedCBlockHeaderAndShortTxIDs : public CBlockHeaderAndShortTxIDs
      74                 :             : {
      75         [ +  - ]:       85664 :     using CBlockHeaderAndShortTxIDs::CBlockHeaderAndShortTxIDs;
      76                 :             : 
      77                 :             : public:
      78                 :        3260 :     void AddPrefilledTx(PrefilledTransaction&& prefilledtx)
      79                 :             :     {
      80                 :        6520 :         prefilledtxn.push_back(std::move(prefilledtx));
      81                 :        3260 :     }
      82                 :             : 
      83                 :        1012 :     void RemoveCoinbasePrefill()
      84                 :             :     {
      85                 :        1012 :         prefilledtxn.erase(prefilledtxn.begin());
      86                 :        1012 :     }
      87                 :             : 
      88                 :        1012 :     void InsertCoinbaseShortTxID(uint64_t shorttxid)
      89                 :             :     {
      90                 :        1012 :         shorttxids.insert(shorttxids.begin(), shorttxid);
      91                 :        1012 :     }
      92                 :             : 
      93                 :        3260 :     void EraseShortTxIDs(size_t index)
      94                 :             :     {
      95                 :        3260 :         shorttxids.erase(shorttxids.begin() + index);
      96                 :        3260 :     }
      97                 :             : 
      98                 :        1911 :     size_t PrefilledTxCount() {
      99                 :        3822 :         return prefilledtxn.size();
     100                 :             :     }
     101                 :             : 
     102                 :        1911 :     size_t ShortTxIDCount() {
     103                 :        3822 :         return shorttxids.size();
     104                 :             :     }
     105                 :             : };
     106                 :             : 
     107                 :             : 
     108                 :             : } // namespace
     109                 :             : 
     110                 :             : extern void MakeRandDeterministicDANGEROUS(const uint256& seed) noexcept;
     111                 :             : 
     112                 :           1 : void initialize_cmpctblock()
     113                 :             : {
     114                 :           1 :     RegisterReachabilityGoal(CMPCTBLOCK_CHANGES_MEMPOOL);
     115                 :           1 :     RegisterReachabilityGoal(CMPCTBLOCK_CHANGES_BLOCK_INDEX);
     116                 :           1 :     FakeNodeClock init_clock{}; // Uses the existing mock time
     117   [ +  -  +  -  :           1 :     static const auto testing_setup = MakeNoLogFileContext<TestingSetup>();
                   +  - ]
     118         [ +  - ]:           1 :     g_setup = testing_setup.get();
     119         [ +  - ]:           1 :     g_nBits = Params().GenesisBlock().nBits;
     120                 :             :     // Replace validation_signals before creating chainman and mempool so they use it.
     121   [ +  -  +  - ]:           1 :     testing_setup->m_node.validation_signals = std::make_unique<ValidationSignals>(std::make_unique<ImmediateBackgroundTaskRunner>());
     122         [ +  - ]:           2 :     g_mature_coinbase = ResetChainmanAndMempool(*g_setup, init_clock);
     123                 :           1 : }
     124                 :             : 
     125         [ +  - ]:        2599 : FUZZ_TARGET(cmpctblock, .init = initialize_cmpctblock)
     126                 :             : {
     127                 :        2123 :     SeedRandomStateForTest(SeedRand::ZEROS);
     128                 :        2123 :     FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
     129                 :             : 
     130                 :        2123 :     FakeNodeClock node_clock{1610000000s}; // 2021-01-07, arbitrary
     131         [ +  - ]:        2123 :     FakeSteadyClock steady_clock;
     132                 :             : 
     133                 :        2123 :     auto setup = g_setup;
     134         [ +  - ]:        2123 :     auto& connman = *static_cast<ConnmanTestMsg*>(setup->m_node.connman.get());
     135         [ +  - ]:        2123 :     connman.Reset();
     136         [ +  - ]:        2123 :     auto& mempool = *setup->m_node.mempool;
     137                 :        2123 :     auto& chainman = static_cast<TestChainstateManager&>(*setup->m_node.chainman);
     138         [ +  - ]:        2123 :     chainman.ResetIbd();
     139         [ +  - ]:        2123 :     chainman.DisableNextWrite();
     140 [ +  - ][ +  - ]:        4246 :     const size_t initial_index_size{WITH_LOCK(chainman.GetMutex(), return chainman.BlockIndex().size())};
     141                 :             : 
     142         [ +  - ]:        2123 :     AddrMan addrman{*setup->m_node.netgroupman, /*deterministic=*/true, /*consistency_check_ratio=*/0};
     143                 :        2123 :     auto peerman = PeerManager::make(connman, addrman,
     144                 :             :                                      /*banman=*/nullptr, chainman,
     145                 :        2123 :                                      mempool, *setup->m_node.warnings,
     146                 :             :                                      PeerManager::Options{
     147                 :             :                                          .deterministic_rng = true,
     148         [ +  - ]:        2123 :                                      });
     149         [ +  - ]:        2123 :     connman.SetMsgProc(peerman.get());
     150                 :             : 
     151         [ +  - ]:        2123 :     setup->m_node.validation_signals->RegisterValidationInterface(peerman.get());
     152         [ +  - ]:        2123 :     setup->m_node.validation_signals->SyncWithValidationInterfaceQueue();
     153                 :             : 
     154         [ +  - ]:        2123 :     LOCK(NetEventsInterface::g_msgproc_mutex);
     155                 :             : 
     156                 :        2123 :     std::vector<CNode*> peers;
     157         [ +  + ]:       10615 :     for (int i = 0; i < 4; ++i) {
     158         [ +  - ]:        8492 :         peers.push_back(ConsumeNodeAsUniquePtr(fuzzed_data_provider, steady_clock, i).release());
     159                 :        8492 :         CNode& p2p_node = *peers.back();
     160                 :        8492 :         FillNode(fuzzed_data_provider, connman, p2p_node);
     161         [ +  - ]:        8492 :         connman.AddTestNode(p2p_node);
     162                 :             :     }
     163                 :             : 
     164                 :             :     // Stores blocks generated this iteration.
     165                 :        2123 :     std::vector<BlockInfo> info;
     166                 :             : 
     167                 :             :     // Coinbase UTXOs for this iteration.
     168         [ +  - ]:        2123 :     std::vector<std::pair<COutPoint, CAmount>> mature_coinbase = g_mature_coinbase;
     169                 :             : 
     170 [ +  - ][ +  - ]:        4246 :     const uint64_t initial_sequence{WITH_LOCK(mempool.cs, return mempool.GetSequence())};
     171                 :             : 
     172                 :      329037 :     auto create_tx = [&]() -> CTransactionRef {
     173                 :      326914 :         CMutableTransaction tx_mut;
     174         [ +  + ]:      326914 :         tx_mut.version = fuzzed_data_provider.ConsumeBool() ? CTransaction::CURRENT_VERSION : TRUC_VERSION;
     175         [ +  + ]:      326914 :         tx_mut.nLockTime = fuzzed_data_provider.ConsumeBool() ? 0 : fuzzed_data_provider.ConsumeIntegral<uint32_t>();
     176                 :             : 
     177                 :             :         // Choose an outpoint from the mempool, created blocks, or coinbases.
     178                 :      326914 :         CAmount amount_in;
     179         [ +  - ]:      326914 :         COutPoint outpoint;
     180         [ +  - ]:      326914 :         unsigned long mempool_size = mempool.size();
     181   [ +  +  +  + ]:      326914 :         if (mempool_size != 0 && fuzzed_data_provider.ConsumeBool()) {
     182                 :      189997 :             size_t random_idx = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, mempool_size - 1);
     183   [ +  -  +  -  :      569991 :             CTransactionRef tx = WITH_LOCK(mempool.cs, return mempool.txns_randomized[random_idx].second->GetSharedTx(););
                   +  - ]
     184         [ +  - ]:      189997 :             outpoint = COutPoint(tx->GetHash(), 0);
     185         [ +  - ]:      189997 :             amount_in = tx->vout[0].nValue;
     186   [ -  +  +  +  :      326914 :         } else if (info.size() != 0 && fuzzed_data_provider.ConsumeBool()) {
                   +  + ]
     187                 :             :             // These blocks (and txs) may be invalid, use a spent output, or not be in the main chain.
     188         [ -  + ]:      124191 :             auto info_it = info.begin();
     189         [ -  + ]:      124191 :             std::advance(info_it, fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, info.size() - 1));
     190         [ -  + ]:      124191 :             auto tx_it = info_it->block->vtx.begin();
     191         [ -  + ]:      124191 :             std::advance(tx_it, fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, info_it->block->vtx.size() - 1));
     192                 :      124191 :             outpoint = COutPoint(tx_it->get()->GetHash(), 0);
     193                 :      124191 :             amount_in = tx_it->get()->vout[0].nValue;
     194                 :             :         } else {
     195         [ -  + ]:       12726 :             auto coinbase_it = mature_coinbase.begin();
     196         [ -  + ]:       12726 :             std::advance(coinbase_it, fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, mature_coinbase.size() - 1));
     197                 :       12726 :             outpoint = coinbase_it->first;
     198                 :       12726 :             amount_in = coinbase_it->second;
     199                 :             :         }
     200                 :             : 
     201                 :      326914 :         const auto sequence = ConsumeSequence(fuzzed_data_provider);
     202         [ +  - ]:      326914 :         const auto script_sig = CScript{};
     203   [ -  +  +  +  :      653828 :         const auto script_wit_stack = std::vector<std::vector<uint8_t>>{WITNESS_STACK_ELEM_OP_TRUE};
                   -  - ]
     204                 :             : 
     205                 :      326914 :         CTxIn in;
     206                 :      326914 :         in.prevout = outpoint;
     207                 :      326914 :         in.nSequence = sequence;
     208                 :      326914 :         in.scriptSig = script_sig;
     209         [ +  - ]:      326914 :         in.scriptWitness.stack = script_wit_stack;
     210         [ +  - ]:      326914 :         tx_mut.vin.push_back(in);
     211                 :             : 
     212                 :      326914 :         const CAmount amount_out = amount_in - AMOUNT_FEE;
     213         [ +  - ]:      326914 :         tx_mut.vout.emplace_back(amount_out, P2WSH_OP_TRUE);
     214                 :             : 
     215         [ +  - ]:      326914 :         auto tx = MakeTransactionRef(tx_mut);
     216                 :      326914 :         return tx;
     217         [ +  - ]:      982865 :     };
     218                 :             : 
     219                 :       38919 :     auto create_block = [&]() {
     220                 :       36796 :         uint256 prev;
     221                 :       36796 :         uint32_t height;
     222                 :             : 
     223   [ -  +  +  +  :       36796 :         if (info.size() == 0 || fuzzed_data_provider.ConsumeBool()) {
                   +  + ]
     224                 :       35052 :             LOCK(cs_main);
     225   [ +  -  -  + ]:       70104 :             prev = chainman.ActiveChain().Tip()->GetBlockHash();
     226   [ +  -  -  +  :       35052 :             height = chainman.ActiveChain().Height() + 1;
                   +  - ]
     227                 :       35052 :         } else {
     228         [ -  + ]:        1744 :             size_t index = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, info.size() - 1);
     229                 :        1744 :             prev = info[index].hash;
     230                 :        1744 :             height = info[index].height + 1;
     231                 :             :         }
     232                 :             : 
     233   [ +  -  -  +  :      147184 :         const auto new_time = WITH_LOCK(::cs_main, return chainman.ActiveChain().Tip()->GetMedianTimePast() + 1);
                   +  - ]
     234                 :             : 
     235                 :       36796 :         CBlockHeader header;
     236                 :       36796 :         header.nNonce = 0;
     237                 :       36796 :         header.hashPrevBlock = prev;
     238                 :       36796 :         header.nBits = g_nBits;
     239                 :       36796 :         header.nTime = new_time;
     240                 :       36796 :         header.nVersion = fuzzed_data_provider.ConsumeIntegral<int32_t>();
     241                 :             : 
     242                 :       36796 :         std::shared_ptr<CBlock> block = std::make_shared<CBlock>();
     243                 :       36796 :         *block = header;
     244                 :             : 
     245         [ +  - ]:       36796 :         CMutableTransaction coinbase_tx;
     246         [ +  - ]:       36796 :         coinbase_tx.vin.resize(1);
     247                 :       36796 :         coinbase_tx.vin[0].prevout.SetNull();
     248   [ +  -  +  - ]:       36796 :         coinbase_tx.vin[0].scriptSig = CScript() << height << OP_0;
     249         [ +  - ]:       36796 :         coinbase_tx.vout.resize(1);
     250         [ +  - ]:       36796 :         coinbase_tx.vout[0].scriptPubKey = CScript() << OP_TRUE;
     251         [ +  - ]:       36796 :         coinbase_tx.vout[0].nValue = COIN;
     252   [ +  -  +  -  :       73592 :         block->vtx.push_back(MakeTransactionRef(coinbase_tx));
                   -  + ]
     253                 :             : 
     254         [ +  - ]:       36796 :         const auto mempool_size = mempool.size();
     255   [ +  +  +  + ]:       36796 :         if (fuzzed_data_provider.ConsumeBool() && mempool_size != 0) {
     256                 :             :             // Add txns from the mempool. Since we do not include parents, it may be an invalid block.
     257                 :       14352 :             size_t num_txns = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(1, mempool_size);
     258                 :       14352 :             size_t random_idx = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, mempool_size - 1);
     259                 :             : 
     260         [ +  - ]:       14352 :             LOCK(mempool.cs);
     261         [ +  + ]:      105713 :             for (size_t i = random_idx; i < random_idx + num_txns; ++i) {
     262         [ +  - ]:       91361 :                 CTransactionRef mempool_tx = mempool.txns_randomized[i % mempool_size].second->GetSharedTx();
     263         [ +  - ]:       91361 :                 block->vtx.push_back(mempool_tx);
     264                 :       91361 :             }
     265                 :       14352 :         }
     266                 :             : 
     267                 :             :         // Create and add (possibly invalid) txns that are not in the mempool.
     268         [ +  + ]:       36796 :         if (fuzzed_data_provider.ConsumeBool()) {
     269                 :       34044 :             size_t new_txns = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(1, 10);
     270         [ +  + ]:      212654 :             for (size_t i = 0; i < new_txns; ++i) {
     271         [ +  - ]:      178610 :                 CTransactionRef non_mempool_tx = create_tx();
     272         [ +  - ]:      178610 :                 block->vtx.push_back(non_mempool_tx);
     273                 :      178610 :             }
     274                 :             :         }
     275                 :             : 
     276   [ +  -  +  -  :      110388 :         CBlockIndex* pindexPrev{WITH_LOCK(::cs_main, return chainman.m_blockman.LookupBlockIndex(prev))};
                   +  - ]
     277         [ +  - ]:       36796 :         chainman.GenerateCoinbaseCommitment(*block, pindexPrev);
     278                 :             : 
     279                 :       36796 :         bool mutated;
     280         [ +  - ]:       36796 :         block->hashMerkleRoot = BlockMerkleRoot(*block, &mutated);
     281         [ +  - ]:       36796 :         FinalizeHeader(*block, chainman);
     282                 :             : 
     283                 :       36796 :         BlockInfo block_info;
     284                 :       36796 :         block_info.block = block;
     285         [ +  - ]:       36796 :         block_info.hash = block->GetHash();
     286                 :       36796 :         block_info.height = height;
     287                 :             : 
     288                 :       36796 :         return block_info;
     289         [ +  - ]:       75715 :     };
     290                 :             : 
     291   [ +  +  +  + ]:      443006 :     LIMITED_WHILE (fuzzed_data_provider.ConsumeBool(), 1000) {
     292         [ +  - ]:      440883 :         CSerializedNetMsg net_msg;
     293                 :      440883 :         bool sent_net_msg = true;
     294                 :      440883 :         bool requested_hb = false;
     295                 :      440883 :         bool sent_sendcmpct = false;
     296                 :      440883 :         bool valid_sendcmpct = false;
     297                 :             : 
     298         [ +  - ]:      440883 :         CallOneOf(
     299                 :             :             fuzzed_data_provider,
     300                 :       85664 :             [&]() {
     301                 :             :                 // Send a compact block.
     302                 :       85664 :                 std::shared_ptr<CBlock> cblock;
     303                 :             : 
     304                 :             :                 // Pick an existing block or create a new block.
     305   [ +  +  -  +  :       85664 :                 if (fuzzed_data_provider.ConsumeBool() && info.size() != 0) {
                   +  + ]
     306                 :       82038 :                     size_t index = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, info.size() - 1);
     307                 :       82038 :                     cblock = info[index].block;
     308                 :             :                 } else {
     309         [ +  - ]:        3626 :                     BlockInfo block_info = create_block();
     310                 :        3626 :                     cblock = block_info.block;
     311         [ +  - ]:        3626 :                     info.push_back(block_info);
     312                 :        3626 :                 }
     313                 :             : 
     314                 :       85664 :                 uint64_t nonce = fuzzed_data_provider.ConsumeIntegral<uint64_t>();
     315         [ +  - ]:       85664 :                 FuzzedCBlockHeaderAndShortTxIDs cmpctblock(*cblock, nonce);
     316                 :             : 
     317         [ +  + ]:       85664 :                 if (fuzzed_data_provider.ConsumeBool()) {
     318         [ +  - ]:       83753 :                     CBlockHeaderAndShortTxIDs base_cmpctblock = cmpctblock;
     319   [ +  -  +  - ]:      167506 :                     net_msg = NetMsg::Make(NetMsgType::CMPCTBLOCK, base_cmpctblock);
     320                 :       83753 :                     return;
     321                 :       83753 :                 }
     322                 :             : 
     323                 :        1911 :                 int prev_idx = 0;
     324                 :        1911 :                 size_t num_erased = 1;
     325         [ -  + ]:        1911 :                 size_t num_txs = cblock->vtx.size();
     326                 :             : 
     327         [ +  + ]:       11273 :                 for (size_t i = 0; i < num_txs; ++i) {
     328         [ +  + ]:        9362 :                     if (i == 0) {
     329                 :             :                         // Handle the coinbase specially. We either keep it prefilled or remove it.
     330         [ +  + ]:        1911 :                         if (fuzzed_data_provider.ConsumeBool()) continue;
     331                 :             : 
     332                 :             :                         // Remove the prefilled coinbase.
     333                 :        1012 :                         num_erased = 0;
     334         [ +  - ]:        1012 :                         uint64_t coinbase_shortid = cmpctblock.GetShortID(cblock->vtx[0]->GetWitnessHash());
     335                 :        1012 :                         cmpctblock.RemoveCoinbasePrefill();
     336         [ +  - ]:        1012 :                         cmpctblock.InsertCoinbaseShortTxID(coinbase_shortid);
     337                 :        1012 :                         continue;
     338                 :        1012 :                     }
     339                 :             : 
     340         [ +  + ]:        7451 :                     if (fuzzed_data_provider.ConsumeBool()) continue;
     341                 :             : 
     342         [ +  + ]:        3260 :                     uint16_t prefill_idx = num_erased == 0 ? i : i - prev_idx - 1;
     343                 :        3260 :                     prev_idx = i;
     344         [ +  - ]:        3260 :                     CTransactionRef txref = cblock->vtx[i];
     345         [ +  - ]:        3260 :                     PrefilledTransaction prefilledtx = {/*index=*/prefill_idx, txref};
     346         [ +  - ]:        3260 :                     cmpctblock.AddPrefilledTx(std::move(prefilledtx));
     347                 :             : 
     348                 :             :                     // Remove from shorttxids since we've prefilled. Subtract however many txs have been prefilled.
     349                 :        3260 :                     cmpctblock.EraseShortTxIDs(i - num_erased);
     350         [ -  + ]:        3260 :                     ++num_erased;
     351         [ +  - ]:        6520 :                 }
     352                 :             : 
     353   [ -  +  -  +  :        1911 :                 assert(cmpctblock.PrefilledTxCount() + cmpctblock.ShortTxIDCount() == num_txs);
                   -  + ]
     354                 :             : 
     355         [ +  - ]:        1911 :                 CBlockHeaderAndShortTxIDs base_cmpctblock = cmpctblock;
     356   [ +  -  +  - ]:        3822 :                 net_msg = NetMsg::Make(NetMsgType::CMPCTBLOCK, base_cmpctblock);
     357   [ +  -  +  - ]:      173239 :             },
     358                 :        7992 :             [&]() {
     359                 :             :                 // Send a blocktxn message for an existing block (if one exists).
     360         [ -  + ]:        7992 :                 size_t num_blocks = info.size();
     361         [ +  + ]:        7992 :                 if (num_blocks == 0) {
     362                 :         878 :                     sent_net_msg = false;
     363                 :         878 :                     return;
     364                 :             :                 }
     365                 :             : 
     366                 :             :                 // Fetch an existing block and randomly choose transactions to send over.
     367                 :        7114 :                 size_t index = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, num_blocks - 1);
     368         [ +  - ]:        7114 :                 const BlockInfo& block_info = info[index];
     369                 :        7114 :                 BlockTransactions block_txn;
     370                 :        7114 :                 block_txn.blockhash = block_info.hash;
     371         [ +  - ]:        7114 :                 std::shared_ptr<CBlock> cblock = block_info.block;
     372                 :             : 
     373   [ -  +  +  + ]:       41331 :                 for (size_t i = 0; i < cblock->vtx.size(); i++) {
     374         [ +  + ]:       34217 :                     if (fuzzed_data_provider.ConsumeBool()) continue;
     375                 :             : 
     376         [ +  - ]:        2478 :                     block_txn.txn.push_back(cblock->vtx[i]);
     377                 :             :                 }
     378                 :             : 
     379   [ +  -  +  -  :       14228 :                 net_msg = NetMsg::Make(NetMsgType::BLOCKTXN, block_txn);
                   +  - ]
     380                 :        7114 :             },
     381                 :      118004 :             [&]() {
     382                 :             :                 // Send a headers message for an existing block (if one exists).
     383         [ -  + ]:      118004 :                 size_t num_blocks = info.size();
     384         [ +  + ]:      118004 :                 if (num_blocks == 0) {
     385                 :       11615 :                     sent_net_msg = false;
     386                 :       11615 :                     return;
     387                 :             :                 }
     388                 :             : 
     389                 :             :                 // Choose an existing block and send a HEADERS message for it.
     390                 :      106389 :                 size_t index = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, num_blocks - 1);
     391                 :      106389 :                 CBlock block = *info[index].block;
     392                 :      106389 :                 block.vtx.clear(); // No tx in HEADERS.
     393                 :      106389 :                 std::vector<CBlock> headers;
     394         [ +  - ]:      106389 :                 headers.emplace_back(block);
     395                 :             : 
     396   [ +  -  +  - ]:      212778 :                 net_msg = NetMsg::Make(NetMsgType::HEADERS, TX_WITH_WITNESS(headers));
     397                 :      106389 :             },
     398                 :       28766 :             [&]() {
     399                 :             :                 // Send a sendcmpct message, optionally setting hb mode.
     400                 :       28766 :                 bool hb = fuzzed_data_provider.ConsumeBool();
     401         [ +  + ]:       28766 :                 uint64_t version{fuzzed_data_provider.ConsumeBool() ? CMPCTBLOCKS_VERSION : fuzzed_data_provider.ConsumeIntegral<uint64_t>()};
     402         [ +  - ]:       57532 :                 net_msg = NetMsg::Make(NetMsgType::SENDCMPCT, /*high_bandwidth=*/hb, /*version=*/version);
     403                 :       28766 :                 requested_hb = hb;
     404                 :       28766 :                 sent_sendcmpct = true;
     405                 :       28766 :                 valid_sendcmpct = version == CMPCTBLOCKS_VERSION;
     406                 :       28766 :             },
     407                 :       33170 :             [&]() {
     408                 :             :                 // Mine a block, but don't send it.
     409                 :       33170 :                 BlockInfo block_info = create_block();
     410         [ +  - ]:       33170 :                 info.push_back(block_info);
     411         [ +  - ]:       33170 :                 sent_net_msg = false;
     412                 :       33170 :             },
     413                 :      148304 :             [&]() {
     414                 :             :                 // Send a transaction.
     415                 :      148304 :                 CTransactionRef tx = create_tx();
     416   [ +  -  +  -  :      296608 :                 net_msg = NetMsg::Make(NetMsgType::TX, TX_WITH_WITNESS(*tx));
                   +  - ]
     417                 :      148304 :             },
     418                 :       18983 :             [&]() {
     419                 :             :                 // Set mock time randomly or to tip's time.
     420         [ +  + ]:       18983 :                 if (fuzzed_data_provider.ConsumeBool()) {
     421                 :       16787 :                     node_clock.set(ConsumeTime(fuzzed_data_provider));
     422                 :             :                 } else {
     423   [ +  -  -  +  :        8784 :                     const NodeSeconds tip_time = WITH_LOCK(::cs_main, return chainman.ActiveChain().Tip()->Time());
                   +  - ]
     424                 :        2196 :                     node_clock.set(tip_time);
     425                 :             :                 }
     426                 :             : 
     427                 :       18983 :                 sent_net_msg = false;
     428                 :       18983 :             });
     429                 :             : 
     430         [ +  + ]:      440883 :         if (!sent_net_msg) {
     431                 :       64646 :             continue;
     432                 :             :         }
     433                 :             : 
     434                 :      376237 :         CNode& random_node = *PickValue(fuzzed_data_provider, peers);
     435         [ +  - ]:      376237 :         connman.FlushSendBuffer(random_node);
     436         [ +  - ]:      376237 :         (void)connman.ReceiveMsgFrom(random_node, std::move(net_msg));
     437                 :             : 
     438                 :             :         bool more_work{true};
     439         [ +  + ]:      752838 :         while (more_work) {
     440         [ +  - ]:      376601 :             random_node.fPauseSend = false;
     441                 :             : 
     442         [ +  - ]:      376601 :             more_work = connman.ProcessMessagesOnce(random_node);
     443         [ +  - ]:      376601 :             peerman->SendMessages(random_node);
     444                 :             :         }
     445                 :             : 
     446                 :      376237 :         std::vector<CNodeStats> stats;
     447         [ +  - ]:      376237 :         connman.GetNodeStats(stats);
     448                 :             : 
     449                 :             :         // We should have at maximum 3 HB peers.
     450                 :      376237 :         int num_hb = 0;
     451         [ +  + ]:     1881185 :         for (const CNodeStats& stat : stats) {
     452         [ +  + ]:     1504948 :             if (stat.m_bip152_highbandwidth_to) {
     453                 :             :                 // HB peers cannot be feelers or other "special" connections (besides addr-fetch).
     454         [ +  + ]:        3774 :                 CNode* hb_peer = peers[stat.nodeid];
     455         [ +  + ]:        3774 :                 if (!hb_peer->fDisconnect) num_hb += 1;
     456   [ +  +  +  -  :        3774 :                 assert(hb_peer->IsInboundConn() || hb_peer->IsOutboundOrBlockRelayConn() || hb_peer->IsManualConn() || hb_peer->IsAddrFetchConn());
          +  +  +  +  -  
                      + ]
     457                 :             :             }
     458                 :             :         }
     459         [ -  + ]:      376237 :         assert(num_hb <= 3);
     460                 :             : 
     461   [ +  +  +  + ]:      376237 :         if (sent_sendcmpct && !random_node.fDisconnect) {
     462                 :             :             // If the fuzzer sent SENDCMPCT with proper version, check the node's state matches what it sent.
     463         [ +  + ]:       23273 :             const CNodeStats& random_node_stats = stats[random_node.GetId()];
     464   [ +  +  -  + ]:       23273 :             if (valid_sendcmpct) assert(random_node_stats.m_bip152_highbandwidth_from == requested_hb);
     465                 :             :         }
     466                 :      817120 :     }
     467                 :             : 
     468         [ +  - ]:        2123 :     setup->m_node.validation_signals->SyncWithValidationInterfaceQueue();
     469         [ +  - ]:        2123 :     setup->m_node.validation_signals->UnregisterAllValidationInterfaces();
     470         [ +  - ]:        2123 :     connman.StopNodes();
     471                 :             : 
     472 [ +  - ][ +  - ]:        4246 :     const size_t end_index_size{WITH_LOCK(chainman.GetMutex(), return chainman.BlockIndex().size())};
     473 [ +  - ][ +  - ]:        4246 :     const uint64_t end_sequence{WITH_LOCK(mempool.cs, return mempool.GetSequence())};
     474                 :             : 
     475         [ +  - ]:        2123 :     ObserveReachabilityGoal(initial_sequence != end_sequence, CMPCTBLOCK_CHANGES_MEMPOOL);
     476         [ +  - ]:        2123 :     ObserveReachabilityGoal(initial_index_size != end_index_size, CMPCTBLOCK_CHANGES_BLOCK_INDEX);
     477         [ +  + ]:        2123 :     if (initial_index_size != end_index_size || initial_sequence != end_sequence) {
     478                 :        1123 :         MakeRandDeterministicDANGEROUS(uint256::ZERO);
     479         [ +  - ]:        2246 :         g_mature_coinbase = ResetChainmanAndMempool(*g_setup, node_clock);
     480                 :             :     }
     481         [ +  - ]:        4246 : }
        

Generated by: LCOV version 2.5.0-full