LCOV - code coverage report
Current view: top level - src/test/fuzz - tx_pool.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 97.0 % 265 257
Test Date: 2025-10-10 04:04:55 Functions: 100.0 % 25 25
Branches: 60.1 % 351 211

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2021-2022 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 <consensus/validation.h>
       6                 :             : #include <node/context.h>
       7                 :             : #include <node/mempool_args.h>
       8                 :             : #include <node/miner.h>
       9                 :             : #include <policy/truc_policy.h>
      10                 :             : #include <test/fuzz/FuzzedDataProvider.h>
      11                 :             : #include <test/fuzz/fuzz.h>
      12                 :             : #include <test/fuzz/util.h>
      13                 :             : #include <test/fuzz/util/mempool.h>
      14                 :             : #include <test/util/mining.h>
      15                 :             : #include <test/util/script.h>
      16                 :             : #include <test/util/setup_common.h>
      17                 :             : #include <test/util/txmempool.h>
      18                 :             : #include <util/check.h>
      19                 :             : #include <util/rbf.h>
      20                 :             : #include <util/translation.h>
      21                 :             : #include <validation.h>
      22                 :             : #include <validationinterface.h>
      23                 :             : 
      24                 :             : using node::BlockAssembler;
      25                 :             : using node::NodeContext;
      26                 :             : using util::ToString;
      27                 :             : 
      28                 :             : namespace {
      29                 :             : 
      30                 :             : const TestingSetup* g_setup;
      31                 :             : std::vector<COutPoint> g_outpoints_coinbase_init_mature;
      32                 :             : std::vector<COutPoint> g_outpoints_coinbase_init_immature;
      33                 :             : 
      34                 :             : struct MockedTxPool : public CTxMemPool {
      35                 :      120739 :     void RollingFeeUpdate() EXCLUSIVE_LOCKS_REQUIRED(!cs)
      36                 :             :     {
      37                 :      120739 :         LOCK(cs);
      38         [ +  - ]:      120739 :         lastRollingFeeUpdate = GetTime();
      39         [ +  - ]:      120739 :         blockSinceLastRollingFeeBump = true;
      40                 :      120739 :     }
      41                 :             : };
      42                 :             : 
      43                 :           2 : void initialize_tx_pool()
      44                 :             : {
      45   [ +  -  +  -  :           2 :     static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
                   +  - ]
      46                 :           2 :     g_setup = testing_setup.get();
      47   [ +  -  +  - ]:           6 :     SetMockTime(WITH_LOCK(g_setup->m_node.chainman->GetMutex(), return g_setup->m_node.chainman->ActiveTip()->Time()));
      48                 :             : 
      49                 :           2 :     BlockAssembler::Options options;
      50                 :           2 :     options.coinbase_output_script = P2WSH_OP_TRUE;
      51                 :             : 
      52         [ +  + ]:         402 :     for (int i = 0; i < 2 * COINBASE_MATURITY; ++i) {
      53         [ +  - ]:         400 :         COutPoint prevout{MineBlock(g_setup->m_node, options)};
      54                 :             :         // Remember the txids to avoid expensive disk access later on
      55         [ +  + ]:         400 :         auto& outpoints = i < COINBASE_MATURITY ?
      56                 :             :                               g_outpoints_coinbase_init_mature :
      57                 :             :                               g_outpoints_coinbase_init_immature;
      58         [ +  - ]:         400 :         outpoints.push_back(prevout);
      59                 :             :     }
      60         [ +  - ]:           2 :     g_setup->m_node.validation_signals->SyncWithValidationInterfaceQueue();
      61                 :           2 : }
      62                 :             : 
      63                 :             : struct TransactionsDelta final : public CValidationInterface {
      64                 :             :     std::set<CTransactionRef>& m_removed;
      65                 :             :     std::set<CTransactionRef>& m_added;
      66                 :             : 
      67                 :       10972 :     explicit TransactionsDelta(std::set<CTransactionRef>& r, std::set<CTransactionRef>& a)
      68                 :       10972 :         : m_removed{r}, m_added{a} {}
      69                 :             : 
      70                 :         741 :     void TransactionAddedToMempool(const NewMempoolTransactionInfo& tx, uint64_t /* mempool_sequence */) override
      71                 :             :     {
      72                 :         741 :         Assert(m_added.insert(tx.info.m_tx).second);
      73                 :         741 :     }
      74                 :             : 
      75                 :         640 :     void TransactionRemovedFromMempool(const CTransactionRef& tx, MemPoolRemovalReason reason, uint64_t /* mempool_sequence */) override
      76                 :             :     {
      77                 :         640 :         Assert(m_removed.insert(tx).second);
      78                 :         640 :     }
      79                 :             : };
      80                 :             : 
      81                 :        3867 : void SetMempoolConstraints(ArgsManager& args, FuzzedDataProvider& fuzzed_data_provider)
      82                 :             : {
      83   [ +  -  +  - ]:        7734 :     args.ForceSetArg("-limitancestorcount",
      84                 :        3867 :                      ToString(fuzzed_data_provider.ConsumeIntegralInRange<unsigned>(0, 50)));
      85   [ +  -  +  - ]:        7734 :     args.ForceSetArg("-limitancestorsize",
      86                 :        3867 :                      ToString(fuzzed_data_provider.ConsumeIntegralInRange<unsigned>(0, 202)));
      87   [ +  -  +  - ]:        7734 :     args.ForceSetArg("-limitdescendantcount",
      88                 :        3867 :                      ToString(fuzzed_data_provider.ConsumeIntegralInRange<unsigned>(0, 50)));
      89   [ +  -  +  - ]:        7734 :     args.ForceSetArg("-limitdescendantsize",
      90                 :        3867 :                      ToString(fuzzed_data_provider.ConsumeIntegralInRange<unsigned>(0, 202)));
      91   [ +  -  +  - ]:        7734 :     args.ForceSetArg("-maxmempool",
      92                 :        3867 :                      ToString(fuzzed_data_provider.ConsumeIntegralInRange<unsigned>(0, 200)));
      93   [ +  -  +  - ]:        7734 :     args.ForceSetArg("-mempoolexpiry",
      94                 :        3867 :                      ToString(fuzzed_data_provider.ConsumeIntegralInRange<unsigned>(0, 999)));
      95                 :        3867 : }
      96                 :             : 
      97                 :        3867 : void Finish(FuzzedDataProvider& fuzzed_data_provider, MockedTxPool& tx_pool, Chainstate& chainstate)
      98                 :             : {
      99   [ -  +  +  -  :       11601 :     WITH_LOCK(::cs_main, tx_pool.check(chainstate.CoinsTip(), chainstate.m_chain.Height() + 1));
                   +  - ]
     100                 :        3867 :     {
     101                 :        3867 :         BlockAssembler::Options options;
     102                 :        3867 :         options.nBlockMaxWeight = fuzzed_data_provider.ConsumeIntegralInRange(0U, MAX_BLOCK_WEIGHT);
     103         [ +  - ]:        3867 :         options.blockMinFeeRate = CFeeRate{ConsumeMoney(fuzzed_data_provider, /*max=*/COIN)};
     104         [ +  - ]:        3867 :         auto assembler = BlockAssembler{chainstate, &tx_pool, options};
     105         [ +  - ]:        3867 :         auto block_template = assembler.CreateNewBlock();
     106   [ -  +  +  - ]:        3867 :         Assert(block_template->block.vtx.size() >= 1);
     107                 :        3867 :     }
     108                 :        3867 :     const auto info_all = tx_pool.infoAll();
     109         [ +  + ]:        3867 :     if (!info_all.empty()) {
     110         [ +  - ]:        1695 :         const auto& tx_to_remove = *PickValue(fuzzed_data_provider, info_all).tx;
     111   [ +  -  +  - ]:        5085 :         WITH_LOCK(tx_pool.cs, tx_pool.removeRecursive(tx_to_remove, MemPoolRemovalReason::BLOCK /* dummy */));
     112   [ +  -  -  +  :        1695 :         assert(tx_pool.size() < info_all.size());
                   -  + ]
     113   [ +  -  -  +  :        5085 :         WITH_LOCK(::cs_main, tx_pool.check(chainstate.CoinsTip(), chainstate.m_chain.Height() + 1));
             +  -  +  - ]
     114                 :             :     }
     115         [ +  - ]:        3867 :     g_setup->m_node.validation_signals->SyncWithValidationInterfaceQueue();
     116                 :        3867 : }
     117                 :             : 
     118                 :      105986 : void MockTime(FuzzedDataProvider& fuzzed_data_provider, const Chainstate& chainstate)
     119                 :             : {
     120                 :      105986 :     const auto time = ConsumeTime(fuzzed_data_provider,
     121                 :      105986 :                                   chainstate.m_chain.Tip()->GetMedianTimePast() + 1,
     122         [ -  + ]:      105986 :                                   std::numeric_limits<decltype(chainstate.m_chain.Tip()->nTime)>::max());
     123                 :      105986 :     SetMockTime(time);
     124                 :      105986 : }
     125                 :             : 
     126                 :        3867 : std::unique_ptr<CTxMemPool> MakeMempool(FuzzedDataProvider& fuzzed_data_provider, const NodeContext& node)
     127                 :             : {
     128                 :             :     // Take the default options for tests...
     129                 :        3867 :     CTxMemPool::Options mempool_opts{MemPoolOptionsForTest(node)};
     130                 :             : 
     131                 :             :     // ...override specific options for this specific fuzz suite
     132                 :        3867 :     mempool_opts.check_ratio = 1;
     133                 :        3867 :     mempool_opts.require_standard = fuzzed_data_provider.ConsumeBool();
     134                 :             : 
     135                 :             :     // ...and construct a CTxMemPool from it
     136         [ +  - ]:        3867 :     bilingual_str error;
     137         [ +  - ]:        3867 :     auto mempool{std::make_unique<CTxMemPool>(std::move(mempool_opts), error)};
     138                 :             :     // ... ignore the error since it might be beneficial to fuzz even when the
     139                 :             :     // mempool size is unreasonably small
     140   [ +  +  +  -  :        5005 :     Assert(error.empty() || error.original.starts_with("-maxmempool must be at least "));
                   +  - ]
     141                 :        3867 :     return mempool;
     142                 :        3867 : }
     143                 :             : 
     144                 :       10972 : void CheckATMPInvariants(const MempoolAcceptResult& res, bool txid_in_mempool, bool wtxid_in_mempool)
     145                 :             : {
     146                 :             : 
     147   [ +  +  -  -  :       10972 :     switch (res.m_result_type) {
                      - ]
     148                 :         741 :     case MempoolAcceptResult::ResultType::VALID:
     149                 :         741 :     {
     150                 :         741 :         Assert(txid_in_mempool);
     151                 :         741 :         Assert(wtxid_in_mempool);
     152                 :         741 :         Assert(res.m_state.IsValid());
     153                 :         741 :         Assert(!res.m_state.IsInvalid());
     154                 :         741 :         Assert(res.m_vsize);
     155                 :         741 :         Assert(res.m_base_fees);
     156                 :         741 :         Assert(res.m_effective_feerate);
     157                 :         741 :         Assert(res.m_wtxids_fee_calculations);
     158                 :         741 :         Assert(!res.m_other_wtxid);
     159                 :         741 :         break;
     160                 :             :     }
     161                 :       10231 :     case MempoolAcceptResult::ResultType::INVALID:
     162                 :       10231 :     {
     163                 :             :         // It may be already in the mempool since in ATMP cases we don't set MEMPOOL_ENTRY or DIFFERENT_WITNESS
     164                 :       10231 :         Assert(!res.m_state.IsValid());
     165                 :       10231 :         Assert(res.m_state.IsInvalid());
     166                 :             : 
     167                 :       10231 :         const bool is_reconsiderable{res.m_state.GetResult() == TxValidationResult::TX_RECONSIDERABLE};
     168                 :       10231 :         Assert(!res.m_vsize);
     169                 :       10231 :         Assert(!res.m_base_fees);
     170                 :             :         // Fee information is provided if the failure is TX_RECONSIDERABLE.
     171                 :             :         // In other cases, validation may be unable or unwilling to calculate the fees.
     172                 :       10231 :         Assert(res.m_effective_feerate.has_value() == is_reconsiderable);
     173                 :       10231 :         Assert(res.m_wtxids_fee_calculations.has_value() == is_reconsiderable);
     174                 :       10231 :         Assert(!res.m_other_wtxid);
     175                 :       10231 :         break;
     176                 :             :     }
     177                 :           0 :     case MempoolAcceptResult::ResultType::MEMPOOL_ENTRY:
     178                 :           0 :     {
     179                 :             :         // ATMP never sets this; only set in package settings
     180                 :           0 :         Assert(false);
     181                 :           0 :         break;
     182                 :             :     }
     183                 :           0 :     case MempoolAcceptResult::ResultType::DIFFERENT_WITNESS:
     184                 :           0 :     {
     185                 :             :         // ATMP never sets this; only set in package settings
     186                 :           0 :         Assert(false);
     187                 :           0 :         break;
     188                 :             :     }
     189                 :             :     }
     190                 :       10972 : }
     191                 :             : 
     192         [ +  - ]:        1375 : FUZZ_TARGET(tx_pool_standard, .init = initialize_tx_pool)
     193                 :             : {
     194                 :         919 :     SeedRandomStateForTest(SeedRand::ZEROS);
     195                 :         919 :     FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
     196                 :         919 :     const auto& node = g_setup->m_node;
     197                 :         919 :     auto& chainstate{static_cast<DummyChainState&>(node.chainman->ActiveChainstate())};
     198                 :             : 
     199                 :         919 :     MockTime(fuzzed_data_provider, chainstate);
     200                 :             : 
     201                 :             :     // All RBF-spendable outpoints
     202                 :         919 :     std::set<COutPoint> outpoints_rbf;
     203                 :             :     // All outpoints counting toward the total supply (subset of outpoints_rbf)
     204                 :         919 :     std::set<COutPoint> outpoints_supply;
     205         [ +  + ]:       92819 :     for (const auto& outpoint : g_outpoints_coinbase_init_mature) {
     206   [ +  -  +  - ]:       91900 :         Assert(outpoints_supply.insert(outpoint).second);
     207                 :             :     }
     208         [ +  - ]:         919 :     outpoints_rbf = outpoints_supply;
     209                 :             : 
     210                 :             :     // The sum of the values of all spendable outpoints
     211                 :         919 :     constexpr CAmount SUPPLY_TOTAL{COINBASE_MATURITY * 50 * COIN};
     212                 :             : 
     213         [ +  - ]:         919 :     SetMempoolConstraints(*node.args, fuzzed_data_provider);
     214         [ +  - ]:         919 :     auto tx_pool_{MakeMempool(fuzzed_data_provider, node)};
     215         [ +  - ]:         919 :     MockedTxPool& tx_pool = *static_cast<MockedTxPool*>(tx_pool_.get());
     216                 :             : 
     217         [ +  - ]:         919 :     chainstate.SetMempool(&tx_pool);
     218                 :             : 
     219                 :             :     // Helper to query an amount
     220   [ +  -  +  - ]:        2757 :     const CCoinsViewMemPool amount_view{WITH_LOCK(::cs_main, return &chainstate.CoinsTip()), tx_pool};
     221                 :     1836011 :     const auto GetAmount = [&](const COutPoint& outpoint) {
     222         [ +  - ]:     1835092 :         auto coin{amount_view.GetCoin(outpoint).value()};
     223                 :     1835092 :         return coin.out.nValue;
     224                 :     1836011 :     };
     225                 :             : 
     226   [ +  +  +  + ]:       11891 :     LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 100)
     227                 :             :     {
     228                 :       10972 :         {
     229                 :             :             // Total supply is the mempool fee + all outpoints
     230         [ +  - ]:       21944 :             CAmount supply_now{WITH_LOCK(tx_pool.cs, return tx_pool.GetTotalFee())};
     231         [ +  + ]:     1396899 :             for (const auto& op : outpoints_supply) {
     232         [ +  - ]:     1385927 :                 supply_now += GetAmount(op);
     233                 :             :             }
     234         [ +  - ]:       10972 :             Assert(supply_now == SUPPLY_TOTAL);
     235                 :             :         }
     236         [ +  - ]:       10972 :         Assert(!outpoints_supply.empty());
     237                 :             : 
     238                 :             :         // Create transaction to add to the mempool
     239                 :       21944 :         const CTransactionRef tx = [&] {
     240                 :       10972 :             CMutableTransaction tx_mut;
     241         [ +  + ]:       10972 :             tx_mut.version = fuzzed_data_provider.ConsumeBool() ? TRUC_VERSION : CTransaction::CURRENT_VERSION;
     242         [ +  + ]:       10972 :             tx_mut.nLockTime = fuzzed_data_provider.ConsumeBool() ? 0 : fuzzed_data_provider.ConsumeIntegral<uint32_t>();
     243                 :       10972 :             const auto num_in = fuzzed_data_provider.ConsumeIntegralInRange<int>(1, outpoints_rbf.size());
     244                 :       10972 :             const auto num_out = fuzzed_data_provider.ConsumeIntegralInRange<int>(1, outpoints_rbf.size() * 2);
     245                 :             : 
     246                 :       10972 :             CAmount amount_in{0};
     247         [ +  + ]:      460137 :             for (int i = 0; i < num_in; ++i) {
     248                 :             :                 // Pop random outpoint
     249                 :      449165 :                 auto pop = outpoints_rbf.begin();
     250                 :      449165 :                 std::advance(pop, fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, outpoints_rbf.size() - 1));
     251                 :      449165 :                 const auto outpoint = *pop;
     252                 :      449165 :                 outpoints_rbf.erase(pop);
     253         [ +  - ]:      449165 :                 amount_in += GetAmount(outpoint);
     254                 :             : 
     255                 :             :                 // Create input
     256                 :      449165 :                 const auto sequence = ConsumeSequence(fuzzed_data_provider);
     257                 :      449165 :                 const auto script_sig = CScript{};
     258   [ +  -  +  +  :     1347495 :                 const auto script_wit_stack = std::vector<std::vector<uint8_t>>{WITNESS_STACK_ELEM_OP_TRUE};
                   -  - ]
     259                 :      449165 :                 CTxIn in;
     260                 :      449165 :                 in.prevout = outpoint;
     261                 :      449165 :                 in.nSequence = sequence;
     262                 :      449165 :                 in.scriptSig = script_sig;
     263         [ +  - ]:      449165 :                 in.scriptWitness.stack = script_wit_stack;
     264                 :             : 
     265         [ +  - ]:      449165 :                 tx_mut.vin.push_back(in);
     266                 :      449165 :             }
     267                 :       10972 :             const auto amount_fee = fuzzed_data_provider.ConsumeIntegralInRange<CAmount>(-1000, amount_in);
     268                 :       10972 :             const auto amount_out = (amount_in - amount_fee) / num_out;
     269         [ +  + ]:     1054239 :             for (int i = 0; i < num_out; ++i) {
     270         [ +  - ]:     1043267 :                 tx_mut.vout.emplace_back(amount_out, P2WSH_OP_TRUE);
     271                 :             :             }
     272         [ +  - ]:       10972 :             auto tx = MakeTransactionRef(tx_mut);
     273                 :             :             // Restore previously removed outpoints
     274         [ +  + ]:      460137 :             for (const auto& in : tx->vin) {
     275   [ +  -  +  - ]:      449165 :                 Assert(outpoints_rbf.insert(in.prevout).second);
     276                 :             :             }
     277                 :       10972 :             return tx;
     278         [ +  - ]:      471109 :         }();
     279                 :             : 
     280         [ +  + ]:       10972 :         if (fuzzed_data_provider.ConsumeBool()) {
     281         [ +  - ]:        9858 :             MockTime(fuzzed_data_provider, chainstate);
     282                 :             :         }
     283         [ +  + ]:       10972 :         if (fuzzed_data_provider.ConsumeBool()) {
     284         [ +  - ]:        9345 :             tx_pool.RollingFeeUpdate();
     285                 :             :         }
     286         [ +  + ]:       10972 :         if (fuzzed_data_provider.ConsumeBool()) {
     287         [ +  + ]:        9692 :             const auto& txid = fuzzed_data_provider.ConsumeBool() ?
     288                 :        9245 :                                    tx->GetHash() :
     289                 :         447 :                                    PickValue(fuzzed_data_provider, outpoints_rbf).hash;
     290                 :        9692 :             const auto delta = fuzzed_data_provider.ConsumeIntegralInRange<CAmount>(-50 * COIN, +50 * COIN);
     291         [ +  - ]:        9692 :             tx_pool.PrioritiseTransaction(txid, delta);
     292                 :             :         }
     293                 :             : 
     294                 :             :         // Remember all removed and added transactions
     295         [ +  - ]:       10972 :         std::set<CTransactionRef> removed;
     296                 :       10972 :         std::set<CTransactionRef> added;
     297         [ +  - ]:       10972 :         auto txr = std::make_shared<TransactionsDelta>(removed, added);
     298   [ +  -  +  - ]:       21944 :         node.validation_signals->RegisterSharedValidationInterface(txr);
     299                 :             : 
     300                 :             :         // Make sure ProcessNewPackage on one transaction works.
     301                 :             :         // The result is not guaranteed to be the same as what is returned by ATMP.
     302   [ +  -  +  -  :       54860 :         const auto result_package = WITH_LOCK(::cs_main,
          +  -  +  +  +  
             -  -  -  -  
                      - ]
     303                 :             :                                     return ProcessNewPackage(chainstate, tx_pool, {tx}, true, /*client_maxfeerate=*/{}));
     304                 :             :         // If something went wrong due to a package-specific policy, it might not return a
     305                 :             :         // validation result for the transaction.
     306         [ +  - ]:       10972 :         if (result_package.m_state.GetResult() != PackageValidationResult::PCKG_POLICY) {
     307                 :       10972 :             auto it = result_package.m_tx_results.find(tx->GetWitnessHash());
     308         [ +  - ]:       10972 :             Assert(it != result_package.m_tx_results.end());
     309   [ +  +  +  -  :       20921 :             Assert(it->second.m_result_type == MempoolAcceptResult::ResultType::VALID ||
                   +  - ]
     310                 :             :                    it->second.m_result_type == MempoolAcceptResult::ResultType::INVALID);
     311                 :             :         }
     312                 :             : 
     313   [ +  -  +  - ]:       32916 :         const auto res = WITH_LOCK(::cs_main, return AcceptToMemoryPool(chainstate, tx, GetTime(), /*bypass_limits=*/false, /*test_accept=*/false));
     314                 :       10972 :         const bool accepted = res.m_result_type == MempoolAcceptResult::ResultType::VALID;
     315         [ +  - ]:       10972 :         node.validation_signals->SyncWithValidationInterfaceQueue();
     316   [ +  -  +  - ]:       21944 :         node.validation_signals->UnregisterSharedValidationInterface(txr);
     317                 :             : 
     318         [ +  - ]:       10972 :         bool txid_in_mempool = tx_pool.exists(tx->GetHash());
     319         [ +  - ]:       10972 :         bool wtxid_in_mempool = tx_pool.exists(tx->GetWitnessHash());
     320         [ +  - ]:       10972 :         CheckATMPInvariants(res, txid_in_mempool, wtxid_in_mempool);
     321                 :             : 
     322         [ +  - ]:       10972 :         Assert(accepted != added.empty());
     323         [ +  + ]:       10972 :         if (accepted) {
     324         [ +  - ]:         741 :             Assert(added.size() == 1); // For now, no package acceptance
     325         [ +  - ]:         741 :             Assert(tx == *added.begin());
     326         [ +  - ]:         741 :             CheckMempoolTRUCInvariants(tx_pool);
     327                 :             :         } else {
     328                 :             :             // Do not consider rejected transaction removed
     329                 :       10231 :             removed.erase(tx);
     330                 :             :         }
     331                 :             : 
     332                 :             :         // Helper to insert spent and created outpoints of a tx into collections
     333                 :       10972 :         using Sets = std::vector<std::reference_wrapper<std::set<COutPoint>>>;
     334                 :       11934 :         const auto insert_tx = [](Sets created_by_tx, Sets consumed_by_tx, const auto& tx) {
     335   [ -  +  +  + ]:       31644 :             for (size_t i{0}; i < tx.vout.size(); ++i) {
     336         [ +  + ]:       89772 :                 for (auto& set : created_by_tx) {
     337                 :       59090 :                     Assert(set.get().emplace(tx.GetHash(), i).second);
     338                 :             :                 }
     339                 :             :             }
     340         [ +  + ]:        7209 :             for (const auto& in : tx.vin) {
     341         [ +  + ]:       12494 :                 for (auto& set : consumed_by_tx) {
     342                 :        6247 :                     Assert(set.get().insert(in.prevout).second);
     343                 :             :                 }
     344                 :             :             }
     345                 :         962 :         };
     346                 :             :         // Add created outpoints, remove spent outpoints
     347                 :       10972 :         {
     348                 :             :             // Outpoints that no longer exist at all
     349                 :       10972 :             std::set<COutPoint> consumed_erased;
     350                 :             :             // Outpoints that no longer count toward the total supply
     351                 :       10972 :             std::set<COutPoint> consumed_supply;
     352         [ +  + ]:       11193 :             for (const auto& removed_tx : removed) {
     353   [ +  -  +  -  :         442 :                 insert_tx(/*created_by_tx=*/{consumed_erased}, /*consumed_by_tx=*/{outpoints_supply}, /*tx=*/*removed_tx);
                   +  - ]
     354                 :             :             }
     355         [ +  + ]:       11713 :             for (const auto& added_tx : added) {
     356   [ +  -  +  -  :        1482 :                 insert_tx(/*created_by_tx=*/{outpoints_supply, outpoints_rbf}, /*consumed_by_tx=*/{consumed_supply}, /*tx=*/*added_tx);
                   +  - ]
     357                 :             :             }
     358         [ +  + ]:       13246 :             for (const auto& p : consumed_erased) {
     359         [ +  - ]:        2274 :                 Assert(outpoints_supply.erase(p) == 1);
     360         [ +  - ]:        2274 :                 Assert(outpoints_rbf.erase(p) == 1);
     361                 :             :             }
     362         [ +  + ]:       15883 :             for (const auto& p : consumed_supply) {
     363         [ +  - ]:        4911 :                 Assert(outpoints_supply.erase(p) == 1);
     364                 :             :             }
     365                 :       10972 :         }
     366   [ +  -  +  - ]:       32916 :     }
     367         [ +  - ]:         919 :     Finish(fuzzed_data_provider, tx_pool, chainstate);
     368                 :         919 : }
     369                 :             : 
     370         [ +  - ]:        3404 : FUZZ_TARGET(tx_pool, .init = initialize_tx_pool)
     371                 :             : {
     372                 :        2948 :     SeedRandomStateForTest(SeedRand::ZEROS);
     373                 :        2948 :     FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
     374                 :        2948 :     const auto& node = g_setup->m_node;
     375                 :        2948 :     auto& chainstate{static_cast<DummyChainState&>(node.chainman->ActiveChainstate())};
     376                 :             : 
     377                 :        2948 :     MockTime(fuzzed_data_provider, chainstate);
     378                 :             : 
     379                 :        2948 :     std::vector<Txid> txids;
     380   [ -  +  +  - ]:        2948 :     txids.reserve(g_outpoints_coinbase_init_mature.size());
     381         [ +  + ]:      297748 :     for (const auto& outpoint : g_outpoints_coinbase_init_mature) {
     382         [ +  - ]:      294800 :         txids.push_back(outpoint.hash);
     383                 :             :     }
     384         [ +  + ]:       14740 :     for (int i{0}; i <= 3; ++i) {
     385                 :             :         // Add some immature and non-existent outpoints
     386   [ +  -  +  - ]:       11792 :         txids.push_back(g_outpoints_coinbase_init_immature.at(i).hash);
     387         [ +  - ]:       11792 :         txids.push_back(Txid::FromUint256(ConsumeUInt256(fuzzed_data_provider)));
     388                 :             :     }
     389                 :             : 
     390         [ +  - ]:        2948 :     SetMempoolConstraints(*node.args, fuzzed_data_provider);
     391         [ +  - ]:        2948 :     auto tx_pool_{MakeMempool(fuzzed_data_provider, node)};
     392                 :        2948 :     MockedTxPool& tx_pool = *static_cast<MockedTxPool*>(tx_pool_.get());
     393                 :             : 
     394                 :        2948 :     chainstate.SetMempool(&tx_pool);
     395                 :             : 
     396                 :             :     // If we ever bypass limits, do not do TRUC invariants checks
     397                 :        2948 :     bool ever_bypassed_limits{false};
     398                 :             : 
     399   [ +  +  +  + ]:      174632 :     LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 300)
     400                 :             :     {
     401         [ +  - ]:      171684 :         const auto mut_tx = ConsumeTransaction(fuzzed_data_provider, txids);
     402                 :             : 
     403         [ +  + ]:      171684 :         if (fuzzed_data_provider.ConsumeBool()) {
     404         [ +  - ]:       92261 :             MockTime(fuzzed_data_provider, chainstate);
     405                 :             :         }
     406         [ +  + ]:      171684 :         if (fuzzed_data_provider.ConsumeBool()) {
     407         [ +  - ]:      111394 :             tx_pool.RollingFeeUpdate();
     408                 :             :         }
     409         [ +  + ]:      171684 :         if (fuzzed_data_provider.ConsumeBool()) {
     410         [ +  + ]:       98632 :             const auto txid = fuzzed_data_provider.ConsumeBool() ?
     411         [ +  - ]:       76055 :                                    mut_tx.GetHash() :
     412                 :       22577 :                                    PickValue(fuzzed_data_provider, txids);
     413                 :       98632 :             const auto delta = fuzzed_data_provider.ConsumeIntegralInRange<CAmount>(-50 * COIN, +50 * COIN);
     414         [ +  - ]:       98632 :             tx_pool.PrioritiseTransaction(txid, delta);
     415                 :             :         }
     416                 :             : 
     417                 :      171684 :         const bool bypass_limits{fuzzed_data_provider.ConsumeBool()};
     418                 :      171684 :         ever_bypassed_limits |= bypass_limits;
     419                 :             : 
     420         [ +  - ]:      171684 :         const auto tx = MakeTransactionRef(mut_tx);
     421   [ +  -  +  - ]:      515052 :         const auto res = WITH_LOCK(::cs_main, return AcceptToMemoryPool(chainstate, tx, GetTime(), bypass_limits, /*test_accept=*/false));
     422                 :      171684 :         const bool accepted = res.m_result_type == MempoolAcceptResult::ResultType::VALID;
     423         [ +  + ]:      171684 :         if (accepted) {
     424         [ +  - ]:       30693 :             txids.push_back(tx->GetHash());
     425         [ +  + ]:       30693 :             if (!ever_bypassed_limits) {
     426         [ +  - ]:         666 :                 CheckMempoolTRUCInvariants(tx_pool);
     427                 :             :             }
     428                 :             :         }
     429         [ +  - ]:      515052 :     }
     430         [ +  - ]:        2948 :     Finish(fuzzed_data_provider, tx_pool, chainstate);
     431                 :        2948 : }
     432                 :             : } // namespace
        

Generated by: LCOV version 2.0-1