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-06-09 06:53:21 Functions: 100.0 % 27 27
Branches: 59.9 % 414 248

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

Generated by: LCOV version 2.0-1