LCOV - code coverage report
Current view: top level - src/test/fuzz - tx_pool.cpp (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 18 275 6.5 %
Date: 2023-11-12 01:39:15 Functions: 2 37 5.4 %
Branches: 19 449 4.2 %

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

Generated by: LCOV version 1.14