LCOV - code coverage report
Current view: top level - src/test/fuzz - tx_pool.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 94.9 % 273 259
Test Date: 2024-09-01 05:20:30 Functions: 96.0 % 25 24
Branches: 56.5 % 425 240

             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                 :      365918 :     void RollingFeeUpdate() EXCLUSIVE_LOCKS_REQUIRED(!cs)
      36                 :             :     {
      37                 :      365918 :         LOCK(cs);
      38         [ +  - ]:      365918 :         lastRollingFeeUpdate = GetTime();
      39                 :      365918 :         blockSinceLastRollingFeeBump = true;
      40                 :      365918 :     }
      41                 :             : };
      42                 :             : 
      43                 :           0 : void initialize_tx_pool()
      44                 :             : {
      45   [ #  #  #  #  :           0 :     static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
                   #  # ]
      46                 :           0 :     g_setup = testing_setup.get();
      47                 :             : 
      48         [ #  # ]:           0 :     for (int i = 0; i < 2 * COINBASE_MATURITY; ++i) {
      49                 :           0 :         COutPoint prevout{MineBlock(g_setup->m_node, P2WSH_OP_TRUE)};
      50                 :             :         // Remember the txids to avoid expensive disk access later on
      51         [ #  # ]:           0 :         auto& outpoints = i < COINBASE_MATURITY ?
      52                 :             :                               g_outpoints_coinbase_init_mature :
      53                 :             :                               g_outpoints_coinbase_init_immature;
      54                 :           0 :         outpoints.push_back(prevout);
      55                 :           0 :     }
      56                 :           0 :     g_setup->m_node.validation_signals->SyncWithValidationInterfaceQueue();
      57                 :           0 : }
      58                 :             : 
      59                 :             : struct TransactionsDelta final : public CValidationInterface {
      60                 :             :     std::set<CTransactionRef>& m_removed;
      61                 :             :     std::set<CTransactionRef>& m_added;
      62                 :             : 
      63                 :      152294 :     explicit TransactionsDelta(std::set<CTransactionRef>& r, std::set<CTransactionRef>& a)
      64                 :      152294 :         : m_removed{r}, m_added{a} {}
      65                 :             : 
      66                 :       22572 :     void TransactionAddedToMempool(const NewMempoolTransactionInfo& tx, uint64_t /* mempool_sequence */) override
      67                 :             :     {
      68                 :       22572 :         Assert(m_added.insert(tx.info.m_tx).second);
      69                 :       22572 :     }
      70                 :             : 
      71                 :       11994 :     void TransactionRemovedFromMempool(const CTransactionRef& tx, MemPoolRemovalReason reason, uint64_t /* mempool_sequence */) override
      72                 :             :     {
      73                 :       11994 :         Assert(m_removed.insert(tx).second);
      74                 :       11994 :     }
      75                 :             : };
      76                 :             : 
      77                 :        7875 : void SetMempoolConstraints(ArgsManager& args, FuzzedDataProvider& fuzzed_data_provider)
      78                 :             : {
      79   [ +  -  +  - ]:       15750 :     args.ForceSetArg("-limitancestorcount",
      80   [ +  -  +  - ]:        7875 :                      ToString(fuzzed_data_provider.ConsumeIntegralInRange<unsigned>(0, 50)));
      81   [ +  -  +  - ]:       15750 :     args.ForceSetArg("-limitancestorsize",
      82   [ +  -  +  - ]:        7875 :                      ToString(fuzzed_data_provider.ConsumeIntegralInRange<unsigned>(0, 202)));
      83   [ +  -  +  - ]:       15750 :     args.ForceSetArg("-limitdescendantcount",
      84   [ +  -  +  - ]:        7875 :                      ToString(fuzzed_data_provider.ConsumeIntegralInRange<unsigned>(0, 50)));
      85   [ +  -  +  - ]:       15750 :     args.ForceSetArg("-limitdescendantsize",
      86   [ +  -  +  - ]:        7875 :                      ToString(fuzzed_data_provider.ConsumeIntegralInRange<unsigned>(0, 202)));
      87   [ +  -  +  - ]:       15750 :     args.ForceSetArg("-maxmempool",
      88   [ +  -  +  - ]:        7875 :                      ToString(fuzzed_data_provider.ConsumeIntegralInRange<unsigned>(0, 200)));
      89   [ +  -  +  - ]:       15750 :     args.ForceSetArg("-mempoolexpiry",
      90   [ +  -  +  - ]:        7875 :                      ToString(fuzzed_data_provider.ConsumeIntegralInRange<unsigned>(0, 999)));
      91                 :        7875 : }
      92                 :             : 
      93                 :        7875 : void Finish(FuzzedDataProvider& fuzzed_data_provider, MockedTxPool& tx_pool, Chainstate& chainstate)
      94                 :             : {
      95   [ +  -  +  -  :       15750 :     WITH_LOCK(::cs_main, tx_pool.check(chainstate.CoinsTip(), chainstate.m_chain.Height() + 1));
                   +  - ]
      96                 :             :     {
      97                 :        7875 :         BlockAssembler::Options options;
      98                 :        7875 :         options.nBlockMaxWeight = fuzzed_data_provider.ConsumeIntegralInRange(0U, MAX_BLOCK_WEIGHT);
      99                 :        7875 :         options.blockMinFeeRate = CFeeRate{ConsumeMoney(fuzzed_data_provider, /*max=*/COIN)};
     100                 :        7875 :         auto assembler = BlockAssembler{chainstate, &tx_pool, options};
     101   [ +  -  +  - ]:        7875 :         auto block_template = assembler.CreateNewBlock(CScript{} << OP_TRUE);
     102         [ +  - ]:        7875 :         Assert(block_template->block.vtx.size() >= 1);
     103                 :        7875 :     }
     104                 :        7875 :     const auto info_all = tx_pool.infoAll();
     105         [ +  + ]:        7875 :     if (!info_all.empty()) {
     106         [ +  - ]:        4482 :         const auto& tx_to_remove = *PickValue(fuzzed_data_provider, info_all).tx;
     107   [ +  -  +  - ]:        8964 :         WITH_LOCK(tx_pool.cs, tx_pool.removeRecursive(tx_to_remove, MemPoolRemovalReason::BLOCK /* dummy */));
     108   [ +  -  +  - ]:        4482 :         assert(tx_pool.size() < info_all.size());
     109   [ +  -  +  -  :        8964 :         WITH_LOCK(::cs_main, tx_pool.check(chainstate.CoinsTip(), chainstate.m_chain.Height() + 1));
                   +  - ]
     110                 :        4482 :     }
     111         [ +  - ]:        7875 :     g_setup->m_node.validation_signals->SyncWithValidationInterfaceQueue();
     112                 :        7875 : }
     113                 :             : 
     114                 :      324781 : void MockTime(FuzzedDataProvider& fuzzed_data_provider, const Chainstate& chainstate)
     115                 :             : {
     116                 :      649562 :     const auto time = ConsumeTime(fuzzed_data_provider,
     117                 :      324781 :                                   chainstate.m_chain.Tip()->GetMedianTimePast() + 1,
     118                 :      324781 :                                   std::numeric_limits<decltype(chainstate.m_chain.Tip()->nTime)>::max());
     119                 :      324781 :     SetMockTime(time);
     120                 :      324781 : }
     121                 :             : 
     122                 :        7875 : std::unique_ptr<CTxMemPool> MakeMempool(FuzzedDataProvider& fuzzed_data_provider, const NodeContext& node)
     123                 :             : {
     124                 :             :     // Take the default options for tests...
     125                 :        7875 :     CTxMemPool::Options mempool_opts{MemPoolOptionsForTest(node)};
     126                 :             : 
     127                 :             :     // ...override specific options for this specific fuzz suite
     128                 :        7875 :     mempool_opts.check_ratio = 1;
     129                 :        7875 :     mempool_opts.require_standard = fuzzed_data_provider.ConsumeBool();
     130                 :             : 
     131                 :             :     // ...and construct a CTxMemPool from it
     132                 :        7875 :     bilingual_str error;
     133         [ +  - ]:        7875 :     auto mempool{std::make_unique<CTxMemPool>(std::move(mempool_opts), error)};
     134                 :             :     // ... ignore the error since it might be beneficial to fuzz even when the
     135                 :             :     // mempool size is unreasonably small
     136   [ +  -  +  +  :        7875 :     Assert(error.empty() || error.original.starts_with("-maxmempool must be at least "));
                   +  - ]
     137                 :        7875 :     return mempool;
     138         [ +  - ]:        7875 : }
     139                 :             : 
     140                 :      152294 : void CheckATMPInvariants(const MempoolAcceptResult& res, bool txid_in_mempool, bool wtxid_in_mempool)
     141                 :             : {
     142                 :             : 
     143   [ -  +  +  -  :      152294 :     switch (res.m_result_type) {
                      - ]
     144                 :             :     case MempoolAcceptResult::ResultType::VALID:
     145                 :             :     {
     146                 :       22572 :         Assert(txid_in_mempool);
     147                 :       22572 :         Assert(wtxid_in_mempool);
     148                 :       22572 :         Assert(res.m_state.IsValid());
     149                 :       22572 :         Assert(!res.m_state.IsInvalid());
     150                 :       22572 :         Assert(res.m_vsize);
     151                 :       22572 :         Assert(res.m_base_fees);
     152                 :       22572 :         Assert(res.m_effective_feerate);
     153                 :       22572 :         Assert(res.m_wtxids_fee_calculations);
     154                 :       22572 :         Assert(!res.m_other_wtxid);
     155                 :       22572 :         break;
     156                 :             :     }
     157                 :             :     case MempoolAcceptResult::ResultType::INVALID:
     158                 :             :     {
     159                 :             :         // It may be already in the mempool since in ATMP cases we don't set MEMPOOL_ENTRY or DIFFERENT_WITNESS
     160                 :      129722 :         Assert(!res.m_state.IsValid());
     161                 :      129722 :         Assert(res.m_state.IsInvalid());
     162                 :             : 
     163                 :      129722 :         const bool is_reconsiderable{res.m_state.GetResult() == TxValidationResult::TX_RECONSIDERABLE};
     164                 :      129722 :         Assert(!res.m_vsize);
     165                 :      129722 :         Assert(!res.m_base_fees);
     166                 :             :         // Fee information is provided if the failure is TX_RECONSIDERABLE.
     167                 :             :         // In other cases, validation may be unable or unwilling to calculate the fees.
     168                 :      129722 :         Assert(res.m_effective_feerate.has_value() == is_reconsiderable);
     169                 :      129722 :         Assert(res.m_wtxids_fee_calculations.has_value() == is_reconsiderable);
     170                 :      129722 :         Assert(!res.m_other_wtxid);
     171                 :             :         break;
     172                 :      129722 :     }
     173                 :             :     case MempoolAcceptResult::ResultType::MEMPOOL_ENTRY:
     174                 :             :     {
     175                 :             :         // ATMP never sets this; only set in package settings
     176                 :           0 :         Assert(false);
     177                 :           0 :         break;
     178                 :             :     }
     179                 :             :     case MempoolAcceptResult::ResultType::DIFFERENT_WITNESS:
     180                 :             :     {
     181                 :             :         // ATMP never sets this; only set in package settings
     182                 :           0 :         Assert(false);
     183                 :           0 :         break;
     184                 :             :     }
     185                 :             :     }
     186                 :      152294 : }
     187                 :             : 
     188         [ +  - ]:        2427 : FUZZ_TARGET(tx_pool_standard, .init = initialize_tx_pool)
     189                 :             : {
     190                 :        2425 :     FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
     191                 :        2425 :     const auto& node = g_setup->m_node;
     192                 :        2425 :     auto& chainstate{static_cast<DummyChainState&>(node.chainman->ActiveChainstate())};
     193                 :             : 
     194                 :        2425 :     MockTime(fuzzed_data_provider, chainstate);
     195                 :             : 
     196                 :             :     // All RBF-spendable outpoints
     197                 :        2425 :     std::set<COutPoint> outpoints_rbf;
     198                 :             :     // All outpoints counting toward the total supply (subset of outpoints_rbf)
     199                 :        2425 :     std::set<COutPoint> outpoints_supply;
     200         [ +  + ]:      244925 :     for (const auto& outpoint : g_outpoints_coinbase_init_mature) {
     201   [ +  -  +  - ]:      242500 :         Assert(outpoints_supply.insert(outpoint).second);
     202                 :      242500 :     }
     203         [ +  - ]:        2425 :     outpoints_rbf = outpoints_supply;
     204                 :             : 
     205                 :             :     // The sum of the values of all spendable outpoints
     206                 :        2425 :     constexpr CAmount SUPPLY_TOTAL{COINBASE_MATURITY * 50 * COIN};
     207                 :             : 
     208         [ +  - ]:        2425 :     SetMempoolConstraints(*node.args, fuzzed_data_provider);
     209         [ +  - ]:        2425 :     auto tx_pool_{MakeMempool(fuzzed_data_provider, node)};
     210                 :        2425 :     MockedTxPool& tx_pool = *static_cast<MockedTxPool*>(tx_pool_.get());
     211                 :             : 
     212         [ +  - ]:        2425 :     chainstate.SetMempool(&tx_pool);
     213                 :             : 
     214                 :             :     // Helper to query an amount
     215   [ +  -  +  -  :        4850 :     const CCoinsViewMemPool amount_view{WITH_LOCK(::cs_main, return &chainstate.CoinsTip()), tx_pool};
             +  -  +  - ]
     216                 :    23666195 :     const auto GetAmount = [&](const COutPoint& outpoint) {
     217                 :    23663770 :         Coin c;
     218   [ +  -  +  - ]:    23663770 :         Assert(amount_view.GetCoin(outpoint, c));
     219                 :    23663770 :         return c.out.nValue;
     220                 :    23663770 :     };
     221                 :             : 
     222   [ -  +  +  +  :      154719 :     LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 300)
                   +  + ]
     223                 :             :     {
     224                 :             :         {
     225                 :             :             // Total supply is the mempool fee + all outpoints
     226   [ -  +  -  +  :      304588 :             CAmount supply_now{WITH_LOCK(tx_pool.cs, return tx_pool.GetTotalFee())};
                   +  - ]
     227         [ +  + ]:    20755455 :             for (const auto& op : outpoints_supply) {
     228         [ +  - ]:    20603161 :                 supply_now += GetAmount(op);
     229                 :    20603161 :             }
     230         [ -  + ]:      152294 :             Assert(supply_now == SUPPLY_TOTAL);
     231                 :      152294 :         }
     232         [ -  + ]:      152294 :         Assert(!outpoints_supply.empty());
     233                 :             : 
     234                 :             :         // Create transaction to add to the mempool
     235         [ -  + ]:      304588 :         const CTransactionRef tx = [&] {
     236                 :      152294 :             CMutableTransaction tx_mut;
     237         [ +  - ]:      152294 :             tx_mut.version = fuzzed_data_provider.ConsumeBool() ? TRUC_VERSION : CTransaction::CURRENT_VERSION;
     238   [ +  -  +  +  :      152294 :             tx_mut.nLockTime = fuzzed_data_provider.ConsumeBool() ? 0 : fuzzed_data_provider.ConsumeIntegral<uint32_t>();
                   +  - ]
     239         [ +  - ]:      152294 :             const auto num_in = fuzzed_data_provider.ConsumeIntegralInRange<int>(1, outpoints_rbf.size());
     240         [ +  - ]:      152294 :             const auto num_out = fuzzed_data_provider.ConsumeIntegralInRange<int>(1, outpoints_rbf.size() * 2);
     241                 :             : 
     242                 :      152294 :             CAmount amount_in{0};
     243         [ +  + ]:     3212903 :             for (int i = 0; i < num_in; ++i) {
     244                 :             :                 // Pop random outpoint
     245                 :     3060609 :                 auto pop = outpoints_rbf.begin();
     246   [ +  -  +  - ]:     3060609 :                 std::advance(pop, fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, outpoints_rbf.size() - 1));
     247                 :     3060609 :                 const auto outpoint = *pop;
     248         [ +  - ]:     3060609 :                 outpoints_rbf.erase(pop);
     249         [ +  - ]:     3060609 :                 amount_in += GetAmount(outpoint);
     250                 :             : 
     251                 :             :                 // Create input
     252                 :     3060609 :                 const auto sequence = ConsumeSequence(fuzzed_data_provider);
     253                 :     3060609 :                 const auto script_sig = CScript{};
     254   [ -  +  -  + ]:     3060609 :                 const auto script_wit_stack = std::vector<std::vector<uint8_t>>{WITNESS_STACK_ELEM_OP_TRUE};
     255         [ -  + ]:     3060609 :                 CTxIn in;
     256                 :     3060609 :                 in.prevout = outpoint;
     257                 :     3060609 :                 in.nSequence = sequence;
     258         [ +  - ]:     3060609 :                 in.scriptSig = script_sig;
     259         [ +  - ]:     3060609 :                 in.scriptWitness.stack = script_wit_stack;
     260                 :             : 
     261         [ +  - ]:     3060609 :                 tx_mut.vin.push_back(in);
     262                 :     3060609 :             }
     263         [ +  - ]:      152294 :             const auto amount_fee = fuzzed_data_provider.ConsumeIntegralInRange<CAmount>(-1000, amount_in);
     264                 :      152294 :             const auto amount_out = (amount_in - amount_fee) / num_out;
     265         [ +  + ]:     8710016 :             for (int i = 0; i < num_out; ++i) {
     266         [ +  - ]:     8557722 :                 tx_mut.vout.emplace_back(amount_out, P2WSH_OP_TRUE);
     267                 :     8557722 :             }
     268         [ +  - ]:      152294 :             auto tx = MakeTransactionRef(tx_mut);
     269                 :             :             // Restore previously removed outpoints
     270         [ +  + ]:     3212903 :             for (const auto& in : tx->vin) {
     271   [ +  -  +  - ]:     3060609 :                 Assert(outpoints_rbf.insert(in.prevout).second);
     272                 :     3060609 :             }
     273                 :      152294 :             return tx;
     274         [ +  - ]:      152294 :         }();
     275                 :             : 
     276   [ +  -  +  + ]:      152294 :         if (fuzzed_data_provider.ConsumeBool()) {
     277         [ +  - ]:      136270 :             MockTime(fuzzed_data_provider, chainstate);
     278                 :      136270 :         }
     279   [ +  -  +  + ]:      152294 :         if (fuzzed_data_provider.ConsumeBool()) {
     280         [ +  - ]:      119183 :             tx_pool.RollingFeeUpdate();
     281                 :      119183 :         }
     282   [ +  -  +  + ]:      152294 :         if (fuzzed_data_provider.ConsumeBool()) {
     283   [ +  -  +  + ]:      251460 :             const auto& txid = fuzzed_data_provider.ConsumeBool() ?
     284         [ +  - ]:      117450 :                                    tx->GetHash() :
     285         [ +  - ]:        8280 :                                    PickValue(fuzzed_data_provider, outpoints_rbf).hash;
     286         [ +  - ]:      125730 :             const auto delta = fuzzed_data_provider.ConsumeIntegralInRange<CAmount>(-50 * COIN, +50 * COIN);
     287   [ +  -  +  - ]:      125730 :             tx_pool.PrioritiseTransaction(txid.ToUint256(), delta);
     288                 :      125730 :         }
     289                 :             : 
     290                 :             :         // Remember all removed and added transactions
     291                 :      152294 :         std::set<CTransactionRef> removed;
     292                 :      152294 :         std::set<CTransactionRef> added;
     293         [ -  + ]:      152294 :         auto txr = std::make_shared<TransactionsDelta>(removed, added);
     294         [ -  + ]:      152294 :         node.validation_signals->RegisterSharedValidationInterface(txr);
     295         [ -  + ]:      152294 :         const bool bypass_limits = fuzzed_data_provider.ConsumeBool();
     296                 :             : 
     297                 :             :         // Make sure ProcessNewPackage on one transaction works.
     298                 :             :         // The result is not guaranteed to be the same as what is returned by ATMP.
     299   [ -  +  -  +  :      304588 :         const auto result_package = WITH_LOCK(::cs_main,
             +  -  +  - ]
     300                 :             :                                     return ProcessNewPackage(chainstate, tx_pool, {tx}, true, /*client_maxfeerate=*/{}));
     301                 :             :         // If something went wrong due to a package-specific policy, it might not return a
     302                 :             :         // validation result for the transaction.
     303   [ -  +  -  + ]:      152294 :         if (result_package.m_state.GetResult() != PackageValidationResult::PCKG_POLICY) {
     304   [ +  -  +  -  :      152294 :             auto it = result_package.m_tx_results.find(tx->GetWitnessHash());
                   +  - ]
     305         [ +  - ]:      152294 :             Assert(it != result_package.m_tx_results.end());
     306   [ +  +  +  - ]:      152294 :             Assert(it->second.m_result_type == MempoolAcceptResult::ResultType::VALID ||
     307                 :             :                    it->second.m_result_type == MempoolAcceptResult::ResultType::INVALID);
     308                 :      152294 :         }
     309                 :             : 
     310   [ +  -  +  -  :      304588 :         const auto res = WITH_LOCK(::cs_main, return AcceptToMemoryPool(chainstate, tx, GetTime(), bypass_limits, /*test_accept=*/false));
             +  -  +  - ]
     311                 :      152294 :         const bool accepted = res.m_result_type == MempoolAcceptResult::ResultType::VALID;
     312         [ +  - ]:      152294 :         node.validation_signals->SyncWithValidationInterfaceQueue();
     313         [ +  - ]:      152294 :         node.validation_signals->UnregisterSharedValidationInterface(txr);
     314                 :             : 
     315   [ +  -  +  -  :      152294 :         bool txid_in_mempool = tx_pool.exists(GenTxid::Txid(tx->GetHash()));
             +  -  +  - ]
     316   [ +  -  +  -  :      152294 :         bool wtxid_in_mempool = tx_pool.exists(GenTxid::Wtxid(tx->GetWitnessHash()));
             +  -  +  - ]
     317         [ +  - ]:      152294 :         CheckATMPInvariants(res, txid_in_mempool, wtxid_in_mempool);
     318                 :             : 
     319         [ +  - ]:      152294 :         Assert(accepted != added.empty());
     320         [ +  + ]:      152294 :         if (accepted) {
     321         [ -  + ]:       22572 :             Assert(added.size() == 1); // For now, no package acceptance
     322         [ +  - ]:       22572 :             Assert(tx == *added.begin());
     323         [ +  - ]:       22572 :             CheckMempoolTRUCInvariants(tx_pool);
     324                 :       22572 :         } else {
     325                 :             :             // Do not consider rejected transaction removed
     326         [ +  - ]:      129722 :             removed.erase(tx);
     327                 :             :         }
     328                 :             : 
     329                 :             :         // Helper to insert spent and created outpoints of a tx into collections
     330                 :             :         using Sets = std::vector<std::reference_wrapper<std::set<COutPoint>>>;
     331                 :      184914 :         const auto insert_tx = [](Sets created_by_tx, Sets consumed_by_tx, const auto& tx) {
     332         [ +  + ]:      494184 :             for (size_t i{0}; i < tx.vout.size(); ++i) {
     333         [ +  + ]:     1336676 :                 for (auto& set : created_by_tx) {
     334                 :      875112 :                     Assert(set.get().emplace(tx.GetHash(), i).second);
     335                 :      875112 :                 }
     336                 :      461564 :             }
     337         [ +  + ]:      113752 :             for (const auto& in : tx.vin) {
     338         [ +  + ]:      162264 :                 for (auto& set : consumed_by_tx) {
     339                 :       81132 :                     Assert(set.get().insert(in.prevout).second);
     340                 :       81132 :                 }
     341                 :       81132 :             }
     342                 :       32620 :         };
     343                 :             :         // Add created outpoints, remove spent outpoints
     344                 :             :         {
     345                 :             :             // Outpoints that no longer exist at all
     346                 :      152294 :             std::set<COutPoint> consumed_erased;
     347                 :             :             // Outpoints that no longer count toward the total supply
     348                 :      152294 :             std::set<COutPoint> consumed_supply;
     349         [ +  + ]:      162342 :             for (const auto& removed_tx : removed) {
     350   [ +  -  -  +  :       10048 :                 insert_tx(/*created_by_tx=*/{consumed_erased}, /*consumed_by_tx=*/{outpoints_supply}, /*tx=*/*removed_tx);
                   -  + ]
     351                 :       10048 :             }
     352         [ +  + ]:      174866 :             for (const auto& added_tx : added) {
     353   [ +  -  -  +  :       22572 :                 insert_tx(/*created_by_tx=*/{outpoints_supply, outpoints_rbf}, /*consumed_by_tx=*/{consumed_supply}, /*tx=*/*added_tx);
                   -  + ]
     354                 :       22572 :             }
     355         [ +  + ]:      200310 :             for (const auto& p : consumed_erased) {
     356   [ +  -  +  - ]:       48016 :                 Assert(outpoints_supply.erase(p) == 1);
     357   [ +  -  +  - ]:       48016 :                 Assert(outpoints_rbf.erase(p) == 1);
     358                 :       48016 :             }
     359         [ +  + ]:      209342 :             for (const auto& p : consumed_supply) {
     360   [ +  -  +  - ]:       57048 :                 Assert(outpoints_supply.erase(p) == 1);
     361                 :       57048 :             }
     362                 :      152294 :         }
     363                 :      152294 :     }
     364         [ +  - ]:        2425 :     Finish(fuzzed_data_provider, tx_pool, chainstate);
     365                 :        2425 : }
     366                 :             : 
     367         [ +  - ]:        5452 : FUZZ_TARGET(tx_pool, .init = initialize_tx_pool)
     368                 :             : {
     369                 :        5450 :     FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
     370                 :        5450 :     const auto& node = g_setup->m_node;
     371                 :        5450 :     auto& chainstate{static_cast<DummyChainState&>(node.chainman->ActiveChainstate())};
     372                 :             : 
     373                 :        5450 :     MockTime(fuzzed_data_provider, chainstate);
     374                 :             : 
     375                 :        5450 :     std::vector<Txid> txids;
     376         [ +  - ]:        5450 :     txids.reserve(g_outpoints_coinbase_init_mature.size());
     377         [ +  + ]:      550450 :     for (const auto& outpoint : g_outpoints_coinbase_init_mature) {
     378         [ +  - ]:      545000 :         txids.push_back(outpoint.hash);
     379                 :      545000 :     }
     380         [ +  + ]:       27250 :     for (int i{0}; i <= 3; ++i) {
     381                 :             :         // Add some immature and non-existent outpoints
     382   [ +  -  +  - ]:       21800 :         txids.push_back(g_outpoints_coinbase_init_immature.at(i).hash);
     383   [ +  -  +  - ]:       21800 :         txids.push_back(Txid::FromUint256(ConsumeUInt256(fuzzed_data_provider)));
     384                 :       21800 :     }
     385                 :             : 
     386         [ +  - ]:        5450 :     SetMempoolConstraints(*node.args, fuzzed_data_provider);
     387         [ +  - ]:        5450 :     auto tx_pool_{MakeMempool(fuzzed_data_provider, node)};
     388                 :        5450 :     MockedTxPool& tx_pool = *static_cast<MockedTxPool*>(tx_pool_.get());
     389                 :             : 
     390                 :        5450 :     chainstate.SetMempool(&tx_pool);
     391                 :             : 
     392   [ -  +  +  +  :      334652 :     LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 300)
                   +  + ]
     393                 :             :     {
     394         [ -  + ]:      329202 :         const auto mut_tx = ConsumeTransaction(fuzzed_data_provider, txids);
     395                 :             : 
     396   [ +  -  +  + ]:      329202 :         if (fuzzed_data_provider.ConsumeBool()) {
     397         [ +  - ]:      180636 :             MockTime(fuzzed_data_provider, chainstate);
     398                 :      180636 :         }
     399   [ +  -  +  + ]:      329202 :         if (fuzzed_data_provider.ConsumeBool()) {
     400         [ +  - ]:      246735 :             tx_pool.RollingFeeUpdate();
     401                 :      246735 :         }
     402   [ +  -  +  + ]:      329202 :         if (fuzzed_data_provider.ConsumeBool()) {
     403   [ +  -  +  + ]:      418600 :             const auto txid = fuzzed_data_provider.ConsumeBool() ?
     404         [ +  - ]:      164042 :                                    mut_tx.GetHash() :
     405         [ +  - ]:       45258 :                                    PickValue(fuzzed_data_provider, txids);
     406                 :      209300 :             const auto delta = fuzzed_data_provider.ConsumeIntegralInRange<CAmount>(-50 * COIN, +50 * COIN);
     407         [ -  + ]:      209300 :             tx_pool.PrioritiseTransaction(txid.ToUint256(), delta);
     408                 :      209300 :         }
     409                 :             : 
     410         [ -  + ]:      329202 :         const auto tx = MakeTransactionRef(mut_tx);
     411         [ -  + ]:      329202 :         const bool bypass_limits = fuzzed_data_provider.ConsumeBool();
     412   [ -  +  +  -  :      658404 :         const auto res = WITH_LOCK(::cs_main, return AcceptToMemoryPool(chainstate, tx, GetTime(), bypass_limits, /*test_accept=*/false));
                   +  - ]
     413                 :      329202 :         const bool accepted = res.m_result_type == MempoolAcceptResult::ResultType::VALID;
     414         [ +  + ]:      329202 :         if (accepted) {
     415         [ +  - ]:       55220 :             txids.push_back(tx->GetHash());
     416         [ +  - ]:       55220 :             CheckMempoolTRUCInvariants(tx_pool);
     417                 :       55220 :         }
     418                 :      329202 :     }
     419         [ +  - ]:        5450 :     Finish(fuzzed_data_provider, tx_pool, chainstate);
     420                 :        5450 : }
     421                 :             : } // namespace
        

Generated by: LCOV version 2.0-1