LCOV - code coverage report
Current view: top level - src/test/fuzz - tx_pool.cpp (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 13 270 4.8 %
Date: 2024-01-03 14:57:27 Functions: 2 32 6.2 %
Branches: 17 447 3.8 %

           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                 :            : #include <util/rbf.h>
      18                 :            : #include <validation.h>
      19                 :            : #include <validationinterface.h>
      20                 :            : 
      21                 :            : using node::BlockAssembler;
      22         [ +  - ]:          2 : using node::NodeContext;
      23                 :          2 : 
      24         [ +  - ]:          2 : namespace {
      25 [ +  - ][ +  - ]:          2 : 
                 [ +  - ]
      26                 :            : const TestingSetup* g_setup;
      27                 :            : std::vector<COutPoint> g_outpoints_coinbase_init_mature;
      28                 :            : 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 NewMempoolTransactionInfo& tx, uint64_t /* mempool_sequence */) override
      63                 :            :     {
      64                 :          0 :         Assert(m_added.insert(tx.info.m_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.check_ratio = 1;
     127                 :          0 :     mempool_opts.require_standard = fuzzed_data_provider.ConsumeBool();
     128                 :            : 
     129                 :            :     // ...and construct a CTxMemPool from it
     130                 :          0 :     return CTxMemPool{mempool_opts};
     131                 :            : }
     132                 :            : 
     133                 :          0 : void CheckATMPInvariants(const MempoolAcceptResult& res, bool txid_in_mempool, bool wtxid_in_mempool)
     134                 :            : {
     135                 :            : 
     136   [ #  #  #  #  :          0 :     switch (res.m_result_type) {
                      # ]
     137                 :            :     case MempoolAcceptResult::ResultType::VALID:
     138                 :            :     {
     139                 :          0 :         Assert(txid_in_mempool);
     140                 :          0 :         Assert(wtxid_in_mempool);
     141                 :          0 :         Assert(res.m_state.IsValid());
     142                 :          0 :         Assert(!res.m_state.IsInvalid());
     143                 :          0 :         Assert(res.m_replaced_transactions);
     144                 :          0 :         Assert(res.m_vsize);
     145                 :          0 :         Assert(res.m_base_fees);
     146                 :          0 :         Assert(res.m_effective_feerate);
     147                 :          0 :         Assert(res.m_wtxids_fee_calculations);
     148                 :          0 :         Assert(!res.m_other_wtxid);
     149                 :          0 :         break;
     150                 :            :     }
     151                 :            :     case MempoolAcceptResult::ResultType::INVALID:
     152                 :            :     {
     153                 :            :         // It may be already in the mempool since in ATMP cases we don't set MEMPOOL_ENTRY or DIFFERENT_WITNESS
     154                 :          0 :         Assert(!res.m_state.IsValid());
     155                 :          0 :         Assert(res.m_state.IsInvalid());
     156                 :            : 
     157                 :          0 :         const bool is_reconsiderable{res.m_state.GetResult() == TxValidationResult::TX_RECONSIDERABLE};
     158                 :          0 :         Assert(!res.m_replaced_transactions);
     159                 :          0 :         Assert(!res.m_vsize);
     160                 :          0 :         Assert(!res.m_base_fees);
     161                 :            :         // Fee information is provided if the failure is TX_RECONSIDERABLE.
     162                 :            :         // In other cases, validation may be unable or unwilling to calculate the fees.
     163                 :          0 :         Assert(res.m_effective_feerate.has_value() == is_reconsiderable);
     164                 :          0 :         Assert(res.m_wtxids_fee_calculations.has_value() == is_reconsiderable);
     165                 :          0 :         Assert(!res.m_other_wtxid);
     166                 :          0 :         break;
     167                 :            :     }
     168                 :            :     case MempoolAcceptResult::ResultType::MEMPOOL_ENTRY:
     169                 :            :     {
     170                 :            :         // ATMP never sets this; only set in package settings
     171                 :          0 :         Assert(false);
     172                 :          0 :         break;
     173                 :            :     }
     174                 :            :     case MempoolAcceptResult::ResultType::DIFFERENT_WITNESS:
     175                 :            :     {
     176                 :            :         // ATMP never sets this; only set in package settings
     177                 :          0 :         Assert(false);
     178                 :          0 :         break;
     179                 :            :     }
     180                 :            :     }
     181                 :          0 : }
     182                 :            : 
     183         [ +  - ]:          4 : FUZZ_TARGET(tx_pool_standard, .init = initialize_tx_pool)
     184                 :            : {
     185                 :          0 :     FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
     186                 :          0 :     const auto& node = g_setup->m_node;
     187                 :          0 :     auto& chainstate{static_cast<DummyChainState&>(node.chainman->ActiveChainstate())};
     188                 :            : 
     189                 :          0 :     MockTime(fuzzed_data_provider, chainstate);
     190                 :            : 
     191                 :            :     // All RBF-spendable outpoints
     192                 :          0 :     std::set<COutPoint> outpoints_rbf;
     193                 :            :     // All outpoints counting toward the total supply (subset of outpoints_rbf)
     194                 :          0 :     std::set<COutPoint> outpoints_supply;
     195         [ #  # ]:          0 :     for (const auto& outpoint : g_outpoints_coinbase_init_mature) {
     196 [ #  # ][ #  # ]:          0 :         Assert(outpoints_supply.insert(outpoint).second);
     197                 :            :     }
     198         [ #  # ]:          0 :     outpoints_rbf = outpoints_supply;
     199                 :            : 
     200                 :            :     // The sum of the values of all spendable outpoints
     201                 :          0 :     constexpr CAmount SUPPLY_TOTAL{COINBASE_MATURITY * 50 * COIN};
     202                 :            : 
     203         [ #  # ]:          0 :     SetMempoolConstraints(*node.args, fuzzed_data_provider);
     204         [ #  # ]:          0 :     CTxMemPool tx_pool_{MakeMempool(fuzzed_data_provider, node)};
     205                 :          0 :     MockedTxPool& tx_pool = *static_cast<MockedTxPool*>(&tx_pool_);
     206                 :            : 
     207         [ #  # ]:          0 :     chainstate.SetMempool(&tx_pool);
     208                 :            : 
     209                 :            :     // Helper to query an amount
     210 [ #  # ][ #  # ]:          0 :     const CCoinsViewMemPool amount_view{WITH_LOCK(::cs_main, return &chainstate.CoinsTip()), tx_pool};
         [ #  # ][ #  # ]
     211                 :          0 :     const auto GetAmount = [&](const COutPoint& outpoint) {
     212                 :          0 :         Coin c;
     213 [ #  # ][ #  # ]:          0 :         Assert(amount_view.GetCoin(outpoint, c));
     214                 :          0 :         return c.out.nValue;
     215                 :          0 :     };
     216                 :            : 
     217 [ #  # ][ #  # ]:          0 :     LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 300)
                 [ #  # ]
     218                 :            :     {
     219                 :            :         {
     220                 :            :             // Total supply is the mempool fee + all outpoints
     221 [ #  # ][ #  # ]:          0 :             CAmount supply_now{WITH_LOCK(tx_pool.cs, return tx_pool.GetTotalFee())};
                 [ #  # ]
     222         [ #  # ]:          0 :             for (const auto& op : outpoints_supply) {
     223         [ #  # ]:          0 :                 supply_now += GetAmount(op);
     224                 :            :             }
     225         [ #  # ]:          0 :             Assert(supply_now == SUPPLY_TOTAL);
     226                 :            :         }
     227         [ #  # ]:          0 :         Assert(!outpoints_supply.empty());
     228                 :            : 
     229                 :            :         // Create transaction to add to the mempool
     230         [ #  # ]:          0 :         const CTransactionRef tx = [&] {
     231                 :          0 :             CMutableTransaction tx_mut;
     232                 :          0 :             tx_mut.nVersion = CTransaction::CURRENT_VERSION;
     233 [ #  # ][ #  # ]:          0 :             tx_mut.nLockTime = fuzzed_data_provider.ConsumeBool() ? 0 : fuzzed_data_provider.ConsumeIntegral<uint32_t>();
                 [ #  # ]
     234         [ #  # ]:          0 :             const auto num_in = fuzzed_data_provider.ConsumeIntegralInRange<int>(1, outpoints_rbf.size());
     235         [ #  # ]:          0 :             const auto num_out = fuzzed_data_provider.ConsumeIntegralInRange<int>(1, outpoints_rbf.size() * 2);
     236                 :            : 
     237                 :          0 :             CAmount amount_in{0};
     238         [ #  # ]:          0 :             for (int i = 0; i < num_in; ++i) {
     239                 :            :                 // Pop random outpoint
     240                 :          0 :                 auto pop = outpoints_rbf.begin();
     241 [ #  # ][ #  # ]:          0 :                 std::advance(pop, fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, outpoints_rbf.size() - 1));
     242                 :          0 :                 const auto outpoint = *pop;
     243         [ #  # ]:          0 :                 outpoints_rbf.erase(pop);
     244         [ #  # ]:          0 :                 amount_in += GetAmount(outpoint);
     245                 :            : 
     246                 :            :                 // Create input
     247                 :          0 :                 const auto sequence = ConsumeSequence(fuzzed_data_provider);
     248         [ #  # ]:          0 :                 const auto script_sig = CScript{};
     249 [ #  # ][ #  # ]:          0 :                 const auto script_wit_stack = std::vector<std::vector<uint8_t>>{WITNESS_STACK_ELEM_OP_TRUE};
     250         [ #  # ]:          0 :                 CTxIn in;
     251                 :          0 :                 in.prevout = outpoint;
     252                 :          0 :                 in.nSequence = sequence;
     253         [ #  # ]:          0 :                 in.scriptSig = script_sig;
     254         [ #  # ]:          0 :                 in.scriptWitness.stack = script_wit_stack;
     255                 :            : 
     256         [ #  # ]:          0 :                 tx_mut.vin.push_back(in);
     257                 :          0 :             }
     258         [ #  # ]:          0 :             const auto amount_fee = fuzzed_data_provider.ConsumeIntegralInRange<CAmount>(-1000, amount_in);
     259                 :          0 :             const auto amount_out = (amount_in - amount_fee) / num_out;
     260         [ #  # ]:          0 :             for (int i = 0; i < num_out; ++i) {
     261         [ #  # ]:          0 :                 tx_mut.vout.emplace_back(amount_out, P2WSH_OP_TRUE);
     262                 :          0 :             }
     263         [ #  # ]:          0 :             auto tx = MakeTransactionRef(tx_mut);
     264                 :            :             // Restore previously removed outpoints
     265         [ #  # ]:          0 :             for (const auto& in : tx->vin) {
     266 [ #  # ][ #  # ]:          0 :                 Assert(outpoints_rbf.insert(in.prevout).second);
     267                 :            :             }
     268                 :          0 :             return tx;
     269         [ #  # ]:          0 :         }();
     270                 :            : 
     271 [ #  # ][ #  # ]:          0 :         if (fuzzed_data_provider.ConsumeBool()) {
     272         [ #  # ]:          0 :             MockTime(fuzzed_data_provider, chainstate);
     273                 :          0 :         }
     274 [ #  # ][ #  # ]:          0 :         if (fuzzed_data_provider.ConsumeBool()) {
     275         [ #  # ]:          0 :             tx_pool.RollingFeeUpdate();
     276                 :          0 :         }
     277 [ #  # ][ #  # ]:          0 :         if (fuzzed_data_provider.ConsumeBool()) {
     278 [ #  # ][ #  # ]:          0 :             const auto& txid = fuzzed_data_provider.ConsumeBool() ?
     279         [ #  # ]:          0 :                                    tx->GetHash() :
     280         [ #  # ]:          0 :                                    PickValue(fuzzed_data_provider, outpoints_rbf).hash;
     281         [ #  # ]:          0 :             const auto delta = fuzzed_data_provider.ConsumeIntegralInRange<CAmount>(-50 * COIN, +50 * COIN);
     282 [ #  # ][ #  # ]:          0 :             tx_pool.PrioritiseTransaction(txid.ToUint256(), delta);
     283                 :          0 :         }
     284                 :            : 
     285                 :            :         // Remember all removed and added transactions
     286                 :          0 :         std::set<CTransactionRef> removed;
     287                 :          0 :         std::set<CTransactionRef> added;
     288         [ #  # ]:          0 :         auto txr = std::make_shared<TransactionsDelta>(removed, added);
     289         [ #  # ]:          0 :         RegisterSharedValidationInterface(txr);
     290         [ #  # ]:          0 :         const bool bypass_limits = fuzzed_data_provider.ConsumeBool();
     291                 :            : 
     292                 :            :         // Make sure ProcessNewPackage on one transaction works.
     293                 :            :         // The result is not guaranteed to be the same as what is returned by ATMP.
     294 [ #  # ][ #  # ]:          0 :         const auto result_package = WITH_LOCK(::cs_main,
         [ #  # ][ #  # ]
     295                 :            :                                     return ProcessNewPackage(chainstate, tx_pool, {tx}, true));
     296                 :            :         // If something went wrong due to a package-specific policy, it might not return a
     297                 :            :         // validation result for the transaction.
     298 [ #  # ][ #  # ]:          0 :         if (result_package.m_state.GetResult() != PackageValidationResult::PCKG_POLICY) {
     299 [ #  # ][ #  # ]:          0 :             auto it = result_package.m_tx_results.find(tx->GetWitnessHash());
                 [ #  # ]
     300         [ #  # ]:          0 :             Assert(it != result_package.m_tx_results.end());
     301 [ #  # ][ #  # ]:          0 :             Assert(it->second.m_result_type == MempoolAcceptResult::ResultType::VALID ||
     302                 :            :                    it->second.m_result_type == MempoolAcceptResult::ResultType::INVALID);
     303                 :          0 :         }
     304                 :            : 
     305 [ #  # ][ #  # ]:          0 :         const auto res = WITH_LOCK(::cs_main, return AcceptToMemoryPool(chainstate, tx, GetTime(), bypass_limits, /*test_accept=*/false));
         [ #  # ][ #  # ]
     306                 :          0 :         const bool accepted = res.m_result_type == MempoolAcceptResult::ResultType::VALID;
     307         [ #  # ]:          0 :         SyncWithValidationInterfaceQueue();
     308         [ #  # ]:          0 :         UnregisterSharedValidationInterface(txr);
     309                 :            : 
     310 [ #  # ][ #  # ]:          0 :         bool txid_in_mempool = tx_pool.exists(GenTxid::Txid(tx->GetHash()));
         [ #  # ][ #  # ]
     311 [ #  # ][ #  # ]:          0 :         bool wtxid_in_mempool = tx_pool.exists(GenTxid::Wtxid(tx->GetWitnessHash()));
         [ #  # ][ #  # ]
     312         [ #  # ]:          0 :         CheckATMPInvariants(res, txid_in_mempool, wtxid_in_mempool);
     313                 :            : 
     314         [ #  # ]:          0 :         Assert(accepted != added.empty());
     315         [ #  # ]:          0 :         if (accepted) {
     316         [ #  # ]:          0 :             Assert(added.size() == 1); // For now, no package acceptance
     317         [ #  # ]:          0 :             Assert(tx == *added.begin());
     318                 :          0 :         } else {
     319                 :            :             // Do not consider rejected transaction removed
     320         [ #  # ]:          0 :             removed.erase(tx);
     321                 :            :         }
     322                 :            : 
     323                 :            :         // Helper to insert spent and created outpoints of a tx into collections
     324                 :            :         using Sets = std::vector<std::reference_wrapper<std::set<COutPoint>>>;
     325                 :          0 :         const auto insert_tx = [](Sets created_by_tx, Sets consumed_by_tx, const auto& tx) {
     326         [ #  # ]:          0 :             for (size_t i{0}; i < tx.vout.size(); ++i) {
     327         [ #  # ]:          0 :                 for (auto& set : created_by_tx) {
     328                 :          0 :                     Assert(set.get().emplace(tx.GetHash(), i).second);
     329                 :            :                 }
     330                 :          0 :             }
     331         [ #  # ]:          0 :             for (const auto& in : tx.vin) {
     332         [ #  # ]:          0 :                 for (auto& set : consumed_by_tx) {
     333                 :          0 :                     Assert(set.get().insert(in.prevout).second);
     334                 :            :                 }
     335                 :            :             }
     336                 :          0 :         };
     337                 :            :         // Add created outpoints, remove spent outpoints
     338                 :            :         {
     339                 :            :             // Outpoints that no longer exist at all
     340                 :          0 :             std::set<COutPoint> consumed_erased;
     341                 :            :             // Outpoints that no longer count toward the total supply
     342                 :          0 :             std::set<COutPoint> consumed_supply;
     343         [ #  # ]:          0 :             for (const auto& removed_tx : removed) {
     344 [ #  # ][ #  # ]:          0 :                 insert_tx(/*created_by_tx=*/{consumed_erased}, /*consumed_by_tx=*/{outpoints_supply}, /*tx=*/*removed_tx);
                 [ #  # ]
     345                 :            :             }
     346         [ #  # ]:          0 :             for (const auto& added_tx : added) {
     347 [ #  # ][ #  # ]:          0 :                 insert_tx(/*created_by_tx=*/{outpoints_supply, outpoints_rbf}, /*consumed_by_tx=*/{consumed_supply}, /*tx=*/*added_tx);
                 [ #  # ]
     348                 :            :             }
     349         [ #  # ]:          0 :             for (const auto& p : consumed_erased) {
     350 [ #  # ][ #  # ]:          0 :                 Assert(outpoints_supply.erase(p) == 1);
     351 [ #  # ][ #  # ]:          0 :                 Assert(outpoints_rbf.erase(p) == 1);
     352                 :            :             }
     353         [ #  # ]:          0 :             for (const auto& p : consumed_supply) {
     354 [ #  # ][ #  # ]:          0 :                 Assert(outpoints_supply.erase(p) == 1);
     355                 :            :             }
     356                 :          0 :         }
     357                 :          0 :     }
     358         [ #  # ]:          0 :     Finish(fuzzed_data_provider, tx_pool, chainstate);
     359                 :          0 : }
     360                 :            : 
     361         [ +  - ]:          4 : FUZZ_TARGET(tx_pool, .init = initialize_tx_pool)
     362                 :            : {
     363                 :          0 :     FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
     364                 :          0 :     const auto& node = g_setup->m_node;
     365                 :          0 :     auto& chainstate{static_cast<DummyChainState&>(node.chainman->ActiveChainstate())};
     366                 :            : 
     367                 :          0 :     MockTime(fuzzed_data_provider, chainstate);
     368                 :            : 
     369                 :          0 :     std::vector<Txid> txids;
     370         [ #  # ]:          0 :     txids.reserve(g_outpoints_coinbase_init_mature.size());
     371         [ #  # ]:          0 :     for (const auto& outpoint : g_outpoints_coinbase_init_mature) {
     372         [ #  # ]:          0 :         txids.push_back(outpoint.hash);
     373                 :            :     }
     374         [ #  # ]:          0 :     for (int i{0}; i <= 3; ++i) {
     375                 :            :         // Add some immature and non-existent outpoints
     376 [ #  # ][ #  # ]:          0 :         txids.push_back(g_outpoints_coinbase_init_immature.at(i).hash);
     377 [ #  # ][ #  # ]:          0 :         txids.push_back(Txid::FromUint256(ConsumeUInt256(fuzzed_data_provider)));
     378                 :          0 :     }
     379                 :            : 
     380         [ #  # ]:          0 :     SetMempoolConstraints(*node.args, fuzzed_data_provider);
     381         [ #  # ]:          0 :     CTxMemPool tx_pool_{MakeMempool(fuzzed_data_provider, node)};
     382                 :          0 :     MockedTxPool& tx_pool = *static_cast<MockedTxPool*>(&tx_pool_);
     383                 :            : 
     384                 :          0 :     chainstate.SetMempool(&tx_pool);
     385                 :            : 
     386 [ #  # ][ #  # ]:          0 :     LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 300)
                 [ #  # ]
     387                 :            :     {
     388         [ #  # ]:          0 :         const auto mut_tx = ConsumeTransaction(fuzzed_data_provider, txids);
     389                 :            : 
     390 [ #  # ][ #  # ]:          0 :         if (fuzzed_data_provider.ConsumeBool()) {
     391         [ #  # ]:          0 :             MockTime(fuzzed_data_provider, chainstate);
     392                 :          0 :         }
     393 [ #  # ][ #  # ]:          0 :         if (fuzzed_data_provider.ConsumeBool()) {
     394         [ #  # ]:          0 :             tx_pool.RollingFeeUpdate();
     395                 :          0 :         }
     396 [ #  # ][ #  # ]:          0 :         if (fuzzed_data_provider.ConsumeBool()) {
     397 [ #  # ][ #  # ]:          0 :             const auto txid = fuzzed_data_provider.ConsumeBool() ?
     398         [ #  # ]:          0 :                                    mut_tx.GetHash() :
     399         [ #  # ]:          0 :                                    PickValue(fuzzed_data_provider, txids);
     400                 :          0 :             const auto delta = fuzzed_data_provider.ConsumeIntegralInRange<CAmount>(-50 * COIN, +50 * COIN);
     401         [ #  # ]:          0 :             tx_pool.PrioritiseTransaction(txid.ToUint256(), delta);
     402                 :          0 :         }
     403                 :            : 
     404         [ #  # ]:          0 :         const auto tx = MakeTransactionRef(mut_tx);
     405         [ #  # ]:          0 :         const bool bypass_limits = fuzzed_data_provider.ConsumeBool();
     406 [ #  # ][ #  # ]:          0 :         const auto res = WITH_LOCK(::cs_main, return AcceptToMemoryPool(chainstate, tx, GetTime(), bypass_limits, /*test_accept=*/false));
                 [ #  # ]
     407                 :          0 :         const bool accepted = res.m_result_type == MempoolAcceptResult::ResultType::VALID;
     408         [ #  # ]:          0 :         if (accepted) {
     409         [ #  # ]:          0 :             txids.push_back(tx->GetHash());
     410                 :          0 :         }
     411                 :          0 :     }
     412         [ #  # ]:          0 :     Finish(fuzzed_data_provider, tx_pool, chainstate);
     413                 :          0 : }
     414                 :            : } // namespace

Generated by: LCOV version 1.14