LCOV - code coverage report
Current view: top level - src/test/fuzz - tx_pool.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 0.0 % 259 0
Test Date: 2024-08-28 04:44:32 Functions: 0.0 % 24 0
Branches: 0.0 % 341 0

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

Generated by: LCOV version 2.0-1