LCOV - code coverage report
Current view: top level - src/test/fuzz - cmpctblock.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 99.7 % 300 299
Test Date: 2026-05-21 06:55:05 Functions: 100.0 % 27 27
Branches: 59.9 % 416 249

             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 <node/miner.h>
      18                 :             : #include <policy/truc_policy.h>
      19                 :             : #include <primitives/block.h>
      20                 :             : #include <primitives/transaction.h>
      21                 :             : #include <protocol.h>
      22                 :             : #include <script/script.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/util/mining.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/txmempool.h>
      35                 :             : #include <test/util/validation.h>
      36                 :             : #include <txmempool.h>
      37                 :             : #include <uint256.h>
      38                 :             : #include <util/check.h>
      39                 :             : #include <util/task_runner.h>
      40                 :             : #include <util/time.h>
      41                 :             : #include <util/translation.h>
      42                 :             : #include <validation.h>
      43                 :             : #include <validationinterface.h>
      44                 :             : 
      45                 :             : #include <boost/multi_index/detail/hash_index_iterator.hpp>
      46                 :             : #include <boost/operators.hpp>
      47                 :             : 
      48                 :             : #include <cstddef>
      49                 :             : #include <cstdint>
      50                 :             : #include <functional>
      51                 :             : #include <iterator>
      52                 :             : #include <memory>
      53                 :             : #include <optional>
      54                 :             : #include <string>
      55                 :             : #include <string_view>
      56                 :             : #include <thread>
      57                 :             : #include <utility>
      58                 :             : #include <vector>
      59                 :             : 
      60                 :             : namespace {
      61                 :             : 
      62                 :             : TestingSetup* g_setup;
      63                 :             : 
      64                 :             : //! Fee each created tx will pay.
      65                 :             : const CAmount AMOUNT_FEE{1000};
      66                 :             : //! Cached coinbases that each iteration can copy and use.
      67                 :             : std::vector<std::pair<COutPoint, CAmount>> g_mature_coinbase;
      68                 :             : //! Constant value used to create valid headers.
      69                 :             : uint32_t g_nBits;
      70                 :             : //! One for each block the fuzzer generates.
      71   [ -  +  +  -  :       30118 : struct BlockInfo {
          -  -  +  -  -  
                -  -  - ]
      72                 :             :     std::shared_ptr<CBlock> block;
      73                 :             :     uint256 hash;
      74                 :             :     uint32_t height;
      75                 :             : };
      76                 :             : //! Used to access prefilledtxn and shorttxids.
      77                 :           0 : class FuzzedCBlockHeaderAndShortTxIDs : public CBlockHeaderAndShortTxIDs
      78                 :             : {
      79         [ +  - ]:       19075 :     using CBlockHeaderAndShortTxIDs::CBlockHeaderAndShortTxIDs;
      80                 :             : 
      81                 :             : public:
      82                 :         869 :     void AddPrefilledTx(PrefilledTransaction&& prefilledtx)
      83                 :             :     {
      84                 :        1738 :         prefilledtxn.push_back(std::move(prefilledtx));
      85                 :         869 :     }
      86                 :             : 
      87                 :         372 :     void RemoveCoinbasePrefill()
      88                 :             :     {
      89                 :         372 :         prefilledtxn.erase(prefilledtxn.begin());
      90                 :         372 :     }
      91                 :             : 
      92                 :         372 :     void InsertCoinbaseShortTxID(uint64_t shorttxid)
      93                 :             :     {
      94                 :         372 :         shorttxids.insert(shorttxids.begin(), shorttxid);
      95                 :         372 :     }
      96                 :             : 
      97                 :         869 :     void EraseShortTxIDs(size_t index)
      98                 :             :     {
      99                 :         869 :         shorttxids.erase(shorttxids.begin() + index);
     100                 :         869 :     }
     101                 :             : 
     102                 :         897 :     size_t PrefilledTxCount() {
     103                 :        1794 :         return prefilledtxn.size();
     104                 :             :     }
     105                 :             : 
     106                 :         897 :     size_t ShortTxIDCount() {
     107                 :        1794 :         return shorttxids.size();
     108                 :             :     }
     109                 :             : };
     110                 :             : 
     111                 :         926 : void ResetChainmanAndMempool(TestingSetup& setup)
     112                 :             : {
     113                 :         926 :     SetMockTime(Params().GenesisBlock().Time());
     114                 :             : 
     115         [ +  - ]:         926 :     bilingual_str error{};
     116         [ +  - ]:         926 :     setup.m_node.mempool.reset();
     117   [ +  -  +  - ]:         926 :     setup.m_node.mempool = std::make_unique<CTxMemPool>(MemPoolOptionsForTest(setup.m_node), error);
     118         [ -  + ]:         926 :     Assert(error.empty());
     119                 :             : 
     120         [ +  - ]:         926 :     setup.m_node.chainman.reset();
     121         [ +  - ]:         926 :     setup.m_make_chainman();
     122         [ +  - ]:         926 :     setup.LoadVerifyActivateChainstate();
     123                 :             : 
     124         [ +  - ]:         926 :     node::BlockAssembler::Options options;
     125                 :         926 :     options.coinbase_output_script = P2WSH_OP_TRUE;
     126                 :             : 
     127         [ +  + ]:         926 :     g_mature_coinbase.clear();
     128                 :             : 
     129         [ +  + ]:      186126 :     for (int i = 0; i < 2 * COINBASE_MATURITY; ++i) {
     130         [ +  - ]:      185200 :         COutPoint prevout{MineBlock(setup.m_node, options)};
     131         [ +  + ]:      185200 :         if (i < COINBASE_MATURITY) {
     132         [ +  - ]:       92600 :             LOCK(cs_main);
     133   [ +  -  +  -  :       92600 :             CAmount subsidy{setup.m_node.chainman->ActiveChainstate().CoinsTip().GetCoin(prevout)->out.nValue};
                   +  - ]
     134         [ +  - ]:       92600 :             g_mature_coinbase.emplace_back(prevout, subsidy);
     135                 :       92600 :         }
     136                 :             :     }
     137                 :        1852 : }
     138                 :             : 
     139                 :             : //! Used to run tasks in a std::thread to avoid DEBUG_LOCKORDER false positives.
     140                 :           1 : class ImmediateBackgroundTaskRunner : public util::TaskRunnerInterface
     141                 :             : {
     142                 :             : public:
     143         [ +  - ]:      760969 :     void insert(std::function<void()> func) override { std::thread(std::move(func)).join(); }
     144                 :           1 :     void flush() override {}
     145                 :      187334 :     size_t size() override { return 0; }
     146                 :             : };
     147                 :             : 
     148                 :             : } // namespace
     149                 :             : 
     150                 :             : extern void MakeRandDeterministicDANGEROUS(const uint256& seed) noexcept;
     151                 :             : 
     152                 :           1 : void initialize_cmpctblock()
     153                 :             : {
     154   [ +  -  +  -  :           1 :     static const auto testing_setup = MakeNoLogFileContext<TestingSetup>();
                   +  - ]
     155                 :           1 :     g_setup = testing_setup.get();
     156                 :           1 :     g_nBits = Params().GenesisBlock().nBits;
     157                 :             :     // Replace validation_signals before creating chainman and mempool so they use it.
     158         [ +  - ]:           1 :     testing_setup->m_node.validation_signals = std::make_unique<ValidationSignals>(std::make_unique<ImmediateBackgroundTaskRunner>());
     159                 :           1 :     ResetChainmanAndMempool(*g_setup);
     160                 :           1 : }
     161                 :             : 
     162         [ +  - ]:        1895 : FUZZ_TARGET(cmpctblock, .init = initialize_cmpctblock)
     163                 :             : {
     164                 :        1435 :     SeedRandomStateForTest(SeedRand::ZEROS);
     165                 :        1435 :     FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
     166                 :             : 
     167                 :        1435 :     NodeClockContext clock_ctx{1610000000s};
     168                 :             : 
     169                 :        1435 :     auto setup = g_setup;
     170         [ +  - ]:        1435 :     auto& mempool = *setup->m_node.mempool;
     171                 :        1435 :     auto& chainman = static_cast<TestChainstateManager&>(*setup->m_node.chainman);
     172         [ +  - ]:        1435 :     chainman.ResetIbd();
     173         [ +  - ]:        1435 :     chainman.DisableNextWrite();
     174         [ +  - ]:        2870 :     const size_t initial_index_size{WITH_LOCK(chainman.GetMutex(), return chainman.BlockIndex().size())};
     175                 :             : 
     176         [ +  - ]:        1435 :     AddrMan addrman{*setup->m_node.netgroupman, /*deterministic=*/true, /*consistency_check_ratio=*/0};
     177         [ +  - ]:        1435 :     auto& connman = *static_cast<ConnmanTestMsg*>(setup->m_node.connman.get());
     178                 :        1435 :     auto peerman = PeerManager::make(connman, addrman,
     179                 :             :                                      /*banman=*/nullptr, chainman,
     180                 :        1435 :                                      mempool, *setup->m_node.warnings,
     181                 :             :                                      PeerManager::Options{
     182                 :             :                                          .deterministic_rng = true,
     183         [ +  - ]:        1435 :                                      });
     184         [ +  - ]:        1435 :     connman.SetMsgProc(peerman.get());
     185                 :             : 
     186         [ +  - ]:        1435 :     setup->m_node.validation_signals->RegisterValidationInterface(peerman.get());
     187         [ +  - ]:        1435 :     setup->m_node.validation_signals->SyncWithValidationInterfaceQueue();
     188                 :             : 
     189         [ +  - ]:        1435 :     LOCK(NetEventsInterface::g_msgproc_mutex);
     190                 :             : 
     191                 :        1435 :     std::vector<CNode*> peers;
     192         [ +  + ]:        7175 :     for (int i = 0; i < 4; ++i) {
     193         [ +  - ]:        5740 :         peers.push_back(ConsumeNodeAsUniquePtr(fuzzed_data_provider, i).release());
     194                 :        5740 :         CNode& p2p_node = *peers.back();
     195                 :        5740 :         FillNode(fuzzed_data_provider, connman, p2p_node);
     196         [ +  - ]:        5740 :         connman.AddTestNode(p2p_node);
     197                 :             :     }
     198                 :             : 
     199                 :             :     // Stores blocks generated this iteration.
     200                 :        1435 :     std::vector<BlockInfo> info;
     201                 :             : 
     202                 :             :     // Coinbase UTXOs for this iteration.
     203         [ +  - ]:        1435 :     std::vector<std::pair<COutPoint, CAmount>> mature_coinbase = g_mature_coinbase;
     204                 :             : 
     205         [ +  - ]:        2870 :     const uint64_t initial_sequence{WITH_LOCK(mempool.cs, return mempool.GetSequence())};
     206                 :             : 
     207                 :      126606 :     auto create_tx = [&]() -> CTransactionRef {
     208                 :      125171 :         CMutableTransaction tx_mut;
     209         [ +  + ]:      125171 :         tx_mut.version = fuzzed_data_provider.ConsumeBool() ? CTransaction::CURRENT_VERSION : TRUC_VERSION;
     210         [ +  + ]:      125171 :         tx_mut.nLockTime = fuzzed_data_provider.ConsumeBool() ? 0 : fuzzed_data_provider.ConsumeIntegral<uint32_t>();
     211                 :             : 
     212                 :             :         // Choose an outpoint from the mempool, created blocks, or coinbases.
     213                 :      125171 :         CAmount amount_in;
     214         [ +  - ]:      125171 :         COutPoint outpoint;
     215         [ +  - ]:      125171 :         unsigned long mempool_size = mempool.size();
     216   [ +  +  +  + ]:      125171 :         if (mempool_size != 0 && fuzzed_data_provider.ConsumeBool()) {
     217                 :       65350 :             size_t random_idx = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, mempool_size - 1);
     218   [ +  -  +  -  :      196050 :             CTransactionRef tx = WITH_LOCK(mempool.cs, return mempool.txns_randomized[random_idx].second->GetSharedTx(););
                   +  - ]
     219         [ +  - ]:       65350 :             outpoint = COutPoint(tx->GetHash(), 0);
     220         [ +  - ]:       65350 :             amount_in = tx->vout[0].nValue;
     221   [ -  +  +  +  :      125171 :         } else if (info.size() != 0 && fuzzed_data_provider.ConsumeBool()) {
                   +  + ]
     222                 :             :             // These blocks (and txs) may be invalid, use a spent output, or not be in the main chain.
     223         [ -  + ]:       51466 :             auto info_it = info.begin();
     224         [ -  + ]:       51466 :             std::advance(info_it, fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, info.size() - 1));
     225         [ -  + ]:       51466 :             auto tx_it = info_it->block->vtx.begin();
     226         [ -  + ]:       51466 :             std::advance(tx_it, fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, info_it->block->vtx.size() - 1));
     227                 :       51466 :             outpoint = COutPoint(tx_it->get()->GetHash(), 0);
     228                 :       51466 :             amount_in = tx_it->get()->vout[0].nValue;
     229                 :             :         } else {
     230         [ -  + ]:        8355 :             auto coinbase_it = mature_coinbase.begin();
     231         [ -  + ]:        8355 :             std::advance(coinbase_it, fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, mature_coinbase.size() - 1));
     232                 :        8355 :             outpoint = coinbase_it->first;
     233                 :        8355 :             amount_in = coinbase_it->second;
     234                 :             :         }
     235                 :             : 
     236                 :      125171 :         const auto sequence = ConsumeSequence(fuzzed_data_provider);
     237                 :      125171 :         const auto script_sig = CScript{};
     238   [ -  +  +  +  :      250342 :         const auto script_wit_stack = std::vector<std::vector<uint8_t>>{WITNESS_STACK_ELEM_OP_TRUE};
                   -  - ]
     239                 :             : 
     240                 :      125171 :         CTxIn in;
     241                 :      125171 :         in.prevout = outpoint;
     242                 :      125171 :         in.nSequence = sequence;
     243                 :      125171 :         in.scriptSig = script_sig;
     244         [ +  - ]:      125171 :         in.scriptWitness.stack = script_wit_stack;
     245         [ +  - ]:      125171 :         tx_mut.vin.push_back(in);
     246                 :             : 
     247                 :      125171 :         const CAmount amount_out = amount_in - AMOUNT_FEE;
     248         [ +  - ]:      125171 :         tx_mut.vout.emplace_back(amount_out, P2WSH_OP_TRUE);
     249                 :             : 
     250         [ +  - ]:      125171 :         auto tx = MakeTransactionRef(tx_mut);
     251                 :      125171 :         return tx;
     252         [ +  - ]:      376948 :     };
     253                 :             : 
     254                 :       10157 :     auto create_block = [&]() {
     255                 :        8722 :         uint256 prev;
     256                 :        8722 :         uint32_t height;
     257                 :             : 
     258   [ -  +  +  +  :        8722 :         if (info.size() == 0 || fuzzed_data_provider.ConsumeBool()) {
                   +  + ]
     259                 :        7493 :             LOCK(cs_main);
     260   [ +  -  -  + ]:       14986 :             prev = chainman.ActiveChain().Tip()->GetBlockHash();
     261   [ +  -  -  +  :        7493 :             height = chainman.ActiveChain().Height() + 1;
                   +  - ]
     262                 :        7493 :         } else {
     263         [ -  + ]:        1229 :             size_t index = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, info.size() - 1);
     264                 :        1229 :             prev = info[index].hash;
     265                 :        1229 :             height = info[index].height + 1;
     266                 :             :         }
     267                 :             : 
     268   [ +  -  -  +  :       34888 :         const auto new_time = WITH_LOCK(::cs_main, return chainman.ActiveChain().Tip()->GetMedianTimePast() + 1);
                   +  - ]
     269                 :             : 
     270                 :        8722 :         CBlockHeader header;
     271                 :        8722 :         header.nNonce = 0;
     272                 :        8722 :         header.hashPrevBlock = prev;
     273                 :        8722 :         header.nBits = g_nBits;
     274                 :        8722 :         header.nTime = new_time;
     275                 :        8722 :         header.nVersion = fuzzed_data_provider.ConsumeIntegral<int32_t>();
     276                 :             : 
     277                 :        8722 :         std::shared_ptr<CBlock> block = std::make_shared<CBlock>();
     278                 :        8722 :         *block = header;
     279                 :             : 
     280         [ +  - ]:        8722 :         CMutableTransaction coinbase_tx;
     281         [ +  - ]:        8722 :         coinbase_tx.vin.resize(1);
     282                 :        8722 :         coinbase_tx.vin[0].prevout.SetNull();
     283   [ +  -  +  - ]:        8722 :         coinbase_tx.vin[0].scriptSig = CScript() << height << OP_0;
     284         [ +  - ]:        8722 :         coinbase_tx.vout.resize(1);
     285         [ +  - ]:        8722 :         coinbase_tx.vout[0].scriptPubKey = CScript() << OP_TRUE;
     286         [ +  - ]:        8722 :         coinbase_tx.vout[0].nValue = COIN;
     287   [ +  -  +  -  :       17444 :         block->vtx.push_back(MakeTransactionRef(coinbase_tx));
                   -  + ]
     288                 :             : 
     289         [ +  - ]:        8722 :         const auto mempool_size = mempool.size();
     290   [ +  +  +  + ]:        8722 :         if (fuzzed_data_provider.ConsumeBool() && mempool_size != 0) {
     291                 :             :             // Add txns from the mempool. Since we do not include parents, it may be an invalid block.
     292                 :        1611 :             size_t num_txns = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(1, mempool_size);
     293                 :        1611 :             size_t random_idx = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, mempool_size - 1);
     294                 :             : 
     295         [ +  - ]:        1611 :             LOCK(mempool.cs);
     296         [ +  + ]:        9695 :             for (size_t i = random_idx; i < random_idx + num_txns; ++i) {
     297         [ +  - ]:        8084 :                 CTransactionRef mempool_tx = mempool.txns_randomized[i % mempool_size].second->GetSharedTx();
     298         [ +  - ]:        8084 :                 block->vtx.push_back(mempool_tx);
     299                 :        8084 :             }
     300                 :        1611 :         }
     301                 :             : 
     302                 :             :         // Create and add (possibly invalid) txns that are not in the mempool.
     303         [ +  + ]:        8722 :         if (fuzzed_data_provider.ConsumeBool()) {
     304                 :        6758 :             size_t new_txns = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(1, 10);
     305         [ +  + ]:       41494 :             for (size_t i = 0; i < new_txns; ++i) {
     306         [ +  - ]:       34736 :                 CTransactionRef non_mempool_tx = create_tx();
     307         [ +  - ]:       34736 :                 block->vtx.push_back(non_mempool_tx);
     308                 :       34736 :             }
     309                 :             :         }
     310                 :             : 
     311   [ +  -  +  -  :       26166 :         CBlockIndex* pindexPrev{WITH_LOCK(::cs_main, return chainman.m_blockman.LookupBlockIndex(prev))};
                   +  - ]
     312         [ +  - ]:        8722 :         chainman.GenerateCoinbaseCommitment(*block, pindexPrev);
     313                 :             : 
     314                 :        8722 :         bool mutated;
     315         [ +  - ]:        8722 :         block->hashMerkleRoot = BlockMerkleRoot(*block, &mutated);
     316         [ +  - ]:        8722 :         FinalizeHeader(*block, chainman);
     317                 :             : 
     318                 :        8722 :         BlockInfo block_info;
     319                 :        8722 :         block_info.block = block;
     320         [ +  - ]:        8722 :         block_info.hash = block->GetHash();
     321                 :        8722 :         block_info.height = height;
     322                 :             : 
     323                 :        8722 :         return block_info;
     324         [ +  - ]:       18879 :     };
     325                 :             : 
     326   [ +  +  +  + ]:      178226 :     LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 1000)
     327                 :             :     {
     328         [ +  - ]:      176791 :         CSerializedNetMsg net_msg;
     329                 :      176791 :         bool sent_net_msg = true;
     330                 :      176791 :         bool requested_hb = false;
     331                 :      176791 :         bool sent_sendcmpct = false;
     332                 :      176791 :         bool valid_sendcmpct = false;
     333                 :             : 
     334         [ +  - ]:      176791 :         CallOneOf(
     335                 :             :             fuzzed_data_provider,
     336                 :       19075 :             [&]() {
     337                 :             :                 // Send a compact block.
     338                 :       19075 :                 std::shared_ptr<CBlock> cblock;
     339                 :             : 
     340                 :             :                 // Pick an existing block or create a new block.
     341   [ +  +  -  +  :       19075 :                 if (fuzzed_data_provider.ConsumeBool() && info.size() != 0) {
                   +  + ]
     342                 :       16634 :                     size_t index = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, info.size() - 1);
     343                 :       16634 :                     cblock = info[index].block;
     344                 :             :                 } else {
     345         [ +  - ]:        2441 :                     BlockInfo block_info = create_block();
     346                 :        2441 :                     cblock = block_info.block;
     347         [ +  - ]:        2441 :                     info.push_back(block_info);
     348                 :        2441 :                 }
     349                 :             : 
     350                 :       19075 :                 uint64_t nonce = fuzzed_data_provider.ConsumeIntegral<uint64_t>();
     351         [ +  - ]:       19075 :                 FuzzedCBlockHeaderAndShortTxIDs cmpctblock(*cblock, nonce);
     352                 :             : 
     353         [ +  + ]:       19075 :                 if (fuzzed_data_provider.ConsumeBool()) {
     354         [ +  - ]:       18178 :                     CBlockHeaderAndShortTxIDs base_cmpctblock = cmpctblock;
     355   [ +  -  +  - ]:       36356 :                     net_msg = NetMsg::Make(NetMsgType::CMPCTBLOCK, base_cmpctblock);
     356                 :       18178 :                     return;
     357                 :       18178 :                 }
     358                 :             : 
     359                 :         897 :                 int prev_idx = 0;
     360                 :         897 :                 size_t num_erased = 1;
     361         [ -  + ]:         897 :                 size_t num_txs = cblock->vtx.size();
     362                 :             : 
     363         [ +  + ]:        3935 :                 for (size_t i = 0; i < num_txs; ++i) {
     364         [ +  + ]:        3038 :                     if (i == 0) {
     365                 :             :                         // Handle the coinbase specially. We either keep it prefilled or remove it.
     366         [ +  + ]:         897 :                         if (fuzzed_data_provider.ConsumeBool()) continue;
     367                 :             : 
     368                 :             :                         // Remove the prefilled coinbase.
     369                 :         372 :                         num_erased = 0;
     370         [ +  - ]:         372 :                         uint64_t coinbase_shortid = cmpctblock.GetShortID(cblock->vtx[0]->GetWitnessHash());
     371                 :         372 :                         cmpctblock.RemoveCoinbasePrefill();
     372         [ +  - ]:         372 :                         cmpctblock.InsertCoinbaseShortTxID(coinbase_shortid);
     373                 :         372 :                         continue;
     374                 :         372 :                     }
     375                 :             : 
     376         [ +  + ]:        2141 :                     if (fuzzed_data_provider.ConsumeBool()) continue;
     377                 :             : 
     378         [ +  + ]:         869 :                     uint16_t prefill_idx = num_erased == 0 ? i : i - prev_idx - 1;
     379                 :         869 :                     prev_idx = i;
     380         [ +  - ]:         869 :                     CTransactionRef txref = cblock->vtx[i];
     381         [ +  - ]:         869 :                     PrefilledTransaction prefilledtx = {/*index=*/prefill_idx, txref};
     382         [ +  - ]:         869 :                     cmpctblock.AddPrefilledTx(std::move(prefilledtx));
     383                 :             : 
     384                 :             :                     // Remove from shorttxids since we've prefilled. Subtract however many txs have been prefilled.
     385                 :         869 :                     cmpctblock.EraseShortTxIDs(i - num_erased);
     386         [ -  + ]:         869 :                     ++num_erased;
     387         [ +  - ]:        1738 :                 }
     388                 :             : 
     389   [ -  +  -  +  :         897 :                 assert(cmpctblock.PrefilledTxCount() + cmpctblock.ShortTxIDCount() == num_txs);
                   -  + ]
     390                 :             : 
     391         [ +  - ]:         897 :                 CBlockHeaderAndShortTxIDs base_cmpctblock = cmpctblock;
     392   [ +  -  +  - ]:        1794 :                 net_msg = NetMsg::Make(NetMsgType::CMPCTBLOCK, base_cmpctblock);
     393   [ +  -  +  - ]:       39047 :             },
     394                 :        5551 :             [&]() {
     395                 :             :                 // Send a blocktxn message for an existing block (if one exists).
     396         [ -  + ]:        5551 :                 size_t num_blocks = info.size();
     397         [ +  + ]:        5551 :                 if (num_blocks == 0) {
     398                 :         588 :                     sent_net_msg = false;
     399                 :         588 :                     return;
     400                 :             :                 }
     401                 :             : 
     402                 :             :                 // Fetch an existing block and randomly choose transactions to send over.
     403                 :        4963 :                 size_t index = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, num_blocks - 1);
     404         [ +  - ]:        4963 :                 const BlockInfo& block_info = info[index];
     405                 :        4963 :                 BlockTransactions block_txn;
     406                 :        4963 :                 block_txn.blockhash = block_info.hash;
     407         [ +  - ]:        4963 :                 std::shared_ptr<CBlock> cblock = block_info.block;
     408                 :             : 
     409   [ -  +  +  + ]:       31997 :                 for (size_t i = 0; i < cblock->vtx.size(); i++) {
     410         [ +  + ]:       27034 :                     if (fuzzed_data_provider.ConsumeBool()) continue;
     411                 :             : 
     412         [ +  - ]:         923 :                     block_txn.txn.push_back(cblock->vtx[i]);
     413                 :             :                 }
     414                 :             : 
     415   [ +  -  +  -  :        9926 :                 net_msg = NetMsg::Make(NetMsgType::BLOCKTXN, block_txn);
                   +  - ]
     416                 :        4963 :             },
     417                 :       42249 :             [&]() {
     418                 :             :                 // Send a headers message for an existing block (if one exists).
     419         [ -  + ]:       42249 :                 size_t num_blocks = info.size();
     420         [ +  + ]:       42249 :                 if (num_blocks == 0) {
     421                 :        7988 :                     sent_net_msg = false;
     422                 :        7988 :                     return;
     423                 :             :                 }
     424                 :             : 
     425                 :             :                 // Choose an existing block and send a HEADERS message for it.
     426                 :       34261 :                 size_t index = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, num_blocks - 1);
     427                 :       34261 :                 CBlock block = *info[index].block;
     428                 :       34261 :                 block.vtx.clear(); // No tx in HEADERS.
     429                 :       34261 :                 std::vector<CBlock> headers;
     430         [ +  - ]:       34261 :                 headers.emplace_back(block);
     431                 :             : 
     432   [ +  -  +  - ]:       68522 :                 net_msg = NetMsg::Make(NetMsgType::HEADERS, TX_WITH_WITNESS(headers));
     433                 :       34261 :             },
     434                 :       10070 :             [&]() {
     435                 :             :                 // Send a sendcmpct message, optionally setting hb mode.
     436                 :       10070 :                 bool hb = fuzzed_data_provider.ConsumeBool();
     437         [ +  + ]:       10070 :                 uint64_t version{fuzzed_data_provider.ConsumeBool() ? CMPCTBLOCKS_VERSION : fuzzed_data_provider.ConsumeIntegral<uint64_t>()};
     438         [ +  - ]:       20140 :                 net_msg = NetMsg::Make(NetMsgType::SENDCMPCT, /*high_bandwidth=*/hb, /*version=*/version);
     439                 :       10070 :                 requested_hb = hb;
     440                 :       10070 :                 sent_sendcmpct = true;
     441                 :       10070 :                 valid_sendcmpct = version == CMPCTBLOCKS_VERSION;
     442                 :       10070 :             },
     443                 :        6281 :             [&]() {
     444                 :             :                 // Mine a block, but don't send it.
     445                 :        6281 :                 BlockInfo block_info = create_block();
     446         [ +  - ]:        6281 :                 info.push_back(block_info);
     447         [ +  - ]:        6281 :                 sent_net_msg = false;
     448                 :        6281 :             },
     449                 :       90435 :             [&]() {
     450                 :             :                 // Send a transaction.
     451                 :       90435 :                 CTransactionRef tx = create_tx();
     452   [ +  -  +  -  :      180870 :                 net_msg = NetMsg::Make(NetMsgType::TX, TX_WITH_WITNESS(*tx));
                   +  - ]
     453                 :       90435 :             },
     454                 :        3130 :             [&]() {
     455                 :             :                 // Set mock time randomly or to tip's time.
     456         [ +  + ]:        3130 :                 if (fuzzed_data_provider.ConsumeBool()) {
     457                 :        2036 :                     clock_ctx.set(ConsumeTime(fuzzed_data_provider));
     458                 :             :                 } else {
     459   [ +  -  -  +  :        4376 :                     const NodeSeconds tip_time = WITH_LOCK(::cs_main, return chainman.ActiveChain().Tip()->Time());
                   +  - ]
     460                 :        1094 :                     clock_ctx.set(tip_time);
     461                 :             :                 }
     462                 :             : 
     463                 :        3130 :                 sent_net_msg = false;
     464                 :        3130 :             });
     465                 :             : 
     466         [ +  + ]:      176791 :         if (!sent_net_msg) {
     467                 :       17987 :             continue;
     468                 :             :         }
     469                 :             : 
     470                 :      158804 :         CNode& random_node = *PickValue(fuzzed_data_provider, peers);
     471         [ +  - ]:      158804 :         connman.FlushSendBuffer(random_node);
     472         [ +  - ]:      158804 :         (void)connman.ReceiveMsgFrom(random_node, std::move(net_msg));
     473                 :             : 
     474                 :             :         bool more_work{true};
     475         [ +  + ]:      317963 :         while (more_work) {
     476         [ +  - ]:      159159 :             random_node.fPauseSend = false;
     477                 :             : 
     478         [ +  - ]:      159159 :             more_work = connman.ProcessMessagesOnce(random_node);
     479         [ +  - ]:      159159 :             peerman->SendMessages(random_node);
     480                 :             :         }
     481                 :             : 
     482                 :      158804 :         std::vector<CNodeStats> stats;
     483         [ +  - ]:      158804 :         connman.GetNodeStats(stats);
     484                 :             : 
     485                 :             :         // We should have at maximum 3 HB peers.
     486                 :      158804 :         int num_hb = 0;
     487         [ +  + ]:      794020 :         for (const CNodeStats& stat : stats) {
     488         [ +  + ]:      635216 :             if (stat.m_bip152_highbandwidth_to) {
     489                 :             :                 // HB peers cannot be feelers or other "special" connections (besides addr-fetch).
     490         [ +  + ]:        3340 :                 CNode* hb_peer = peers[stat.nodeid];
     491         [ +  + ]:        3340 :                 if (!hb_peer->fDisconnect) num_hb += 1;
     492   [ +  +  +  -  :        3340 :                 assert(hb_peer->IsInboundConn() || hb_peer->IsOutboundOrBlockRelayConn() || hb_peer->IsManualConn() || hb_peer->IsAddrFetchConn());
             -  +  -  - ]
     493                 :             :             }
     494                 :             :         }
     495         [ -  + ]:      158804 :         assert(num_hb <= 3);
     496                 :             : 
     497   [ +  +  +  + ]:      158804 :         if (sent_sendcmpct && !random_node.fDisconnect) {
     498                 :             :             // If the fuzzer sent SENDCMPCT with proper version, check the node's state matches what it sent.
     499         [ +  + ]:        6532 :             const CNodeStats& random_node_stats = stats[random_node.GetId()];
     500   [ +  +  -  + ]:        6532 :             if (valid_sendcmpct) assert(random_node_stats.m_bip152_highbandwidth_from == requested_hb);
     501                 :             :         }
     502                 :      335595 :     }
     503                 :             : 
     504         [ +  - ]:        1435 :     setup->m_node.validation_signals->SyncWithValidationInterfaceQueue();
     505         [ +  - ]:        1435 :     setup->m_node.validation_signals->UnregisterAllValidationInterfaces();
     506         [ +  - ]:        1435 :     connman.StopNodes();
     507                 :             : 
     508         [ +  - ]:        2870 :     const size_t end_index_size{WITH_LOCK(chainman.GetMutex(), return chainman.BlockIndex().size())};
     509         [ +  - ]:        2870 :     const uint64_t end_sequence{WITH_LOCK(mempool.cs, return mempool.GetSequence())};
     510                 :             : 
     511         [ +  + ]:        1435 :     if (initial_index_size != end_index_size || initial_sequence != end_sequence) {
     512                 :         925 :         MakeRandDeterministicDANGEROUS(uint256::ZERO);
     513         [ +  - ]:         925 :         ResetChainmanAndMempool(*g_setup);
     514                 :             :     }
     515         [ +  - ]:        2870 : }
        

Generated by: LCOV version 2.0-1