LCOV - code coverage report
Current view: top level - src/test/fuzz - tx_pool.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 97.1 % 280 272
Test Date: 2025-12-28 04:16:35 Functions: 100.0 % 25 25
Branches: 59.5 % 439 261

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2021-present 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                 :      140800 :     void RollingFeeUpdate() EXCLUSIVE_LOCKS_REQUIRED(!cs)
      36                 :             :     {
      37                 :      140800 :         LOCK(cs);
      38         [ +  - ]:      140800 :         lastRollingFeeUpdate = GetTime();
      39         [ +  - ]:      140800 :         blockSinceLastRollingFeeBump = true;
      40                 :      140800 :     }
      41                 :             : };
      42                 :             : 
      43                 :           2 : void initialize_tx_pool()
      44                 :             : {
      45   [ +  -  +  -  :           2 :     static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
                   +  - ]
      46                 :           2 :     g_setup = testing_setup.get();
      47   [ +  -  +  - ]:           6 :     SetMockTime(WITH_LOCK(g_setup->m_node.chainman->GetMutex(), return g_setup->m_node.chainman->ActiveTip()->Time()));
      48                 :             : 
      49                 :           2 :     BlockAssembler::Options options;
      50                 :           2 :     options.coinbase_output_script = P2WSH_OP_TRUE;
      51                 :             : 
      52         [ +  + ]:         402 :     for (int i = 0; i < 2 * COINBASE_MATURITY; ++i) {
      53         [ +  - ]:         400 :         COutPoint prevout{MineBlock(g_setup->m_node, options)};
      54                 :             :         // Remember the txids to avoid expensive disk access later on
      55         [ +  + ]:         400 :         auto& outpoints = i < COINBASE_MATURITY ?
      56                 :             :                               g_outpoints_coinbase_init_mature :
      57                 :             :                               g_outpoints_coinbase_init_immature;
      58         [ +  - ]:         400 :         outpoints.push_back(prevout);
      59                 :             :     }
      60         [ +  - ]:           2 :     g_setup->m_node.validation_signals->SyncWithValidationInterfaceQueue();
      61                 :           2 : }
      62                 :             : 
      63                 :             : struct TransactionsDelta final : public CValidationInterface {
      64                 :             :     std::set<CTransactionRef>& m_removed;
      65                 :             :     std::set<CTransactionRef>& m_added;
      66                 :             : 
      67                 :       13354 :     explicit TransactionsDelta(std::set<CTransactionRef>& r, std::set<CTransactionRef>& a)
      68                 :       13354 :         : m_removed{r}, m_added{a} {}
      69                 :             : 
      70                 :         949 :     void TransactionAddedToMempool(const NewMempoolTransactionInfo& tx, uint64_t /* mempool_sequence */) override
      71                 :             :     {
      72         [ -  + ]:         949 :         Assert(m_added.insert(tx.info.m_tx).second);
      73                 :         949 :     }
      74                 :             : 
      75                 :         447 :     void TransactionRemovedFromMempool(const CTransactionRef& tx, MemPoolRemovalReason reason, uint64_t /* mempool_sequence */) override
      76                 :             :     {
      77         [ -  + ]:         447 :         Assert(m_removed.insert(tx).second);
      78                 :         447 :     }
      79                 :             : };
      80                 :             : 
      81                 :        8417 : void SetMempoolConstraints(ArgsManager& args, FuzzedDataProvider& fuzzed_data_provider)
      82                 :             : {
      83   [ +  -  +  - ]:       16834 :     args.ForceSetArg("-limitclustercount",
      84                 :        8417 :                      ToString(fuzzed_data_provider.ConsumeIntegralInRange<unsigned>(1, 64)));
      85   [ +  -  +  - ]:       16834 :     args.ForceSetArg("-limitclustersize",
      86                 :        8417 :                      ToString(fuzzed_data_provider.ConsumeIntegralInRange<unsigned>(1, 250)));
      87   [ +  -  +  - ]:       16834 :     args.ForceSetArg("-maxmempool",
      88                 :        8417 :                      ToString(fuzzed_data_provider.ConsumeIntegralInRange<unsigned>(0, 200)));
      89   [ +  -  +  - ]:       16834 :     args.ForceSetArg("-mempoolexpiry",
      90                 :        8417 :                      ToString(fuzzed_data_provider.ConsumeIntegralInRange<unsigned>(0, 999)));
      91                 :        8417 : }
      92                 :             : 
      93                 :        8417 : void Finish(FuzzedDataProvider& fuzzed_data_provider, MockedTxPool& tx_pool, Chainstate& chainstate)
      94                 :             : {
      95   [ -  +  +  -  :       25251 :     WITH_LOCK(::cs_main, tx_pool.check(chainstate.CoinsTip(), chainstate.m_chain.Height() + 1));
                   +  - ]
      96                 :        8417 :     {
      97                 :        8417 :         BlockAssembler::Options options;
      98                 :        8417 :         options.nBlockMaxWeight = fuzzed_data_provider.ConsumeIntegralInRange(0U, MAX_BLOCK_WEIGHT);
      99         [ +  - ]:        8417 :         options.blockMinFeeRate = CFeeRate{ConsumeMoney(fuzzed_data_provider, /*max=*/COIN)};
     100         [ +  - ]:        8417 :         auto assembler = BlockAssembler{chainstate, &tx_pool, options};
     101         [ +  - ]:        8417 :         auto block_template = assembler.CreateNewBlock();
     102   [ -  +  -  + ]:        8417 :         Assert(block_template->block.vtx.size() >= 1);
     103                 :             : 
     104                 :             :         // Try updating the mempool for this block, as though it were mined.
     105   [ +  -  +  - ]:        8417 :         LOCK2(::cs_main, tx_pool.cs);
     106   [ -  +  +  - ]:        8417 :         tx_pool.removeForBlock(block_template->block.vtx, chainstate.m_chain.Height() + 1);
     107                 :             : 
     108                 :             :         // Now try to add those transactions back, as though a reorg happened.
     109                 :        8417 :         std::vector<Txid> hashes_to_update;
     110         [ +  + ]:       33167 :         for (const auto& tx : block_template->block.vtx) {
     111   [ +  -  +  - ]:       24750 :             const auto res = AcceptToMemoryPool(chainstate, tx, GetTime(), true, /*test_accept=*/false);
     112         [ +  + ]:       24750 :             if (res.m_result_type == MempoolAcceptResult::ResultType::VALID) {
     113         [ +  - ]:       16333 :                 hashes_to_update.push_back(tx->GetHash());
     114                 :             :             } else {
     115         [ +  - ]:        8417 :                 tx_pool.removeRecursive(*tx, MemPoolRemovalReason::REORG);
     116                 :             :             }
     117                 :       24750 :         }
     118         [ +  - ]:        8417 :         tx_pool.UpdateTransactionsFromBlock(hashes_to_update);
     119   [ +  -  +  - ]:       33668 :     }
     120                 :        8417 :     const auto info_all = tx_pool.infoAll();
     121         [ +  + ]:        8417 :     if (!info_all.empty()) {
     122         [ +  - ]:        1897 :         const auto& tx_to_remove = *PickValue(fuzzed_data_provider, info_all).tx;
     123   [ +  -  +  - ]:        5691 :         WITH_LOCK(tx_pool.cs, tx_pool.removeRecursive(tx_to_remove, MemPoolRemovalReason::BLOCK /* dummy */));
     124   [ +  -  -  +  :        1897 :         assert(tx_pool.size() < info_all.size());
                   -  + ]
     125                 :             :     }
     126                 :             : 
     127         [ +  + ]:        8417 :     if (fuzzed_data_provider.ConsumeBool()) {
     128                 :             :         // Try eviction
     129   [ +  -  +  - ]:        2615 :         LOCK2(::cs_main, tx_pool.cs);
     130   [ +  -  +  - ]:        2615 :         tx_pool.TrimToSize(fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0U, tx_pool.DynamicMemoryUsage() * 2));
     131         [ +  - ]:        5230 :     }
     132         [ +  + ]:        8417 :     if (fuzzed_data_provider.ConsumeBool()) {
     133                 :             :         // Try expiry
     134   [ +  -  +  - ]:        4012 :         LOCK2(::cs_main, tx_pool.cs);
     135   [ +  -  +  - ]:        4012 :         tx_pool.Expire(GetMockTime() - std::chrono::seconds(fuzzed_data_provider.ConsumeIntegral<uint32_t>()));
     136         [ +  - ]:        8024 :     }
     137   [ +  -  -  +  :       25251 :     WITH_LOCK(::cs_main, tx_pool.check(chainstate.CoinsTip(), chainstate.m_chain.Height() + 1));
             +  -  +  - ]
     138         [ +  - ]:        8417 :     g_setup->m_node.validation_signals->SyncWithValidationInterfaceQueue();
     139                 :        8417 : }
     140                 :             : 
     141                 :      135717 : void MockTime(FuzzedDataProvider& fuzzed_data_provider, const Chainstate& chainstate)
     142                 :             : {
     143                 :      135717 :     const auto time = ConsumeTime(fuzzed_data_provider,
     144                 :      135717 :                                   chainstate.m_chain.Tip()->GetMedianTimePast() + 1,
     145         [ -  + ]:      135717 :                                   std::numeric_limits<decltype(chainstate.m_chain.Tip()->nTime)>::max());
     146                 :      135717 :     SetMockTime(time);
     147                 :      135717 : }
     148                 :             : 
     149                 :        8417 : std::unique_ptr<CTxMemPool> MakeMempool(FuzzedDataProvider& fuzzed_data_provider, const NodeContext& node)
     150                 :             : {
     151                 :             :     // Take the default options for tests...
     152                 :        8417 :     CTxMemPool::Options mempool_opts{MemPoolOptionsForTest(node)};
     153                 :             : 
     154                 :             :     // ...override specific options for this specific fuzz suite
     155                 :        8417 :     mempool_opts.check_ratio = 1;
     156                 :        8417 :     mempool_opts.require_standard = fuzzed_data_provider.ConsumeBool();
     157                 :             : 
     158                 :             :     // ...and construct a CTxMemPool from it
     159         [ +  - ]:        8417 :     bilingual_str error;
     160         [ +  - ]:        8417 :     auto mempool{std::make_unique<CTxMemPool>(std::move(mempool_opts), error)};
     161                 :             :     // ... ignore the error since it might be beneficial to fuzz even when the
     162                 :             :     // mempool size is unreasonably small
     163   [ +  +  +  -  :        8819 :     Assert(error.empty() || error.original.starts_with("-maxmempool must be at least "));
                   -  + ]
     164                 :        8417 :     return mempool;
     165                 :        8417 : }
     166                 :             : 
     167                 :       13354 : void CheckATMPInvariants(const MempoolAcceptResult& res, bool txid_in_mempool, bool wtxid_in_mempool)
     168                 :             : {
     169                 :             : 
     170   [ +  +  -  -  :       13354 :     switch (res.m_result_type) {
                      - ]
     171                 :         949 :     case MempoolAcceptResult::ResultType::VALID:
     172                 :         949 :     {
     173         [ -  + ]:         949 :         Assert(txid_in_mempool);
     174         [ -  + ]:         949 :         Assert(wtxid_in_mempool);
     175         [ -  + ]:         949 :         Assert(res.m_state.IsValid());
     176         [ -  + ]:         949 :         Assert(!res.m_state.IsInvalid());
     177         [ -  + ]:         949 :         Assert(res.m_vsize);
     178         [ -  + ]:         949 :         Assert(res.m_base_fees);
     179         [ -  + ]:         949 :         Assert(res.m_effective_feerate);
     180         [ -  + ]:         949 :         Assert(res.m_wtxids_fee_calculations);
     181         [ -  + ]:         949 :         Assert(!res.m_other_wtxid);
     182                 :             :         break;
     183                 :             :     }
     184                 :       12405 :     case MempoolAcceptResult::ResultType::INVALID:
     185                 :       12405 :     {
     186                 :             :         // It may be already in the mempool since in ATMP cases we don't set MEMPOOL_ENTRY or DIFFERENT_WITNESS
     187         [ -  + ]:       12405 :         Assert(!res.m_state.IsValid());
     188         [ -  + ]:       12405 :         Assert(res.m_state.IsInvalid());
     189                 :             : 
     190         [ -  + ]:       12405 :         const bool is_reconsiderable{res.m_state.GetResult() == TxValidationResult::TX_RECONSIDERABLE};
     191         [ -  + ]:       12405 :         Assert(!res.m_vsize);
     192         [ -  + ]:       12405 :         Assert(!res.m_base_fees);
     193                 :             :         // Fee information is provided if the failure is TX_RECONSIDERABLE.
     194                 :             :         // In other cases, validation may be unable or unwilling to calculate the fees.
     195         [ -  + ]:       12405 :         Assert(res.m_effective_feerate.has_value() == is_reconsiderable);
     196         [ -  + ]:       12405 :         Assert(res.m_wtxids_fee_calculations.has_value() == is_reconsiderable);
     197         [ -  + ]:       12405 :         Assert(!res.m_other_wtxid);
     198                 :             :         break;
     199                 :             :     }
     200                 :           0 :     case MempoolAcceptResult::ResultType::MEMPOOL_ENTRY:
     201                 :           0 :     {
     202                 :             :         // ATMP never sets this; only set in package settings
     203                 :           0 :         Assert(false);
     204                 :           0 :         break;
     205                 :             :     }
     206                 :           0 :     case MempoolAcceptResult::ResultType::DIFFERENT_WITNESS:
     207                 :           0 :     {
     208                 :             :         // ATMP never sets this; only set in package settings
     209                 :           0 :         Assert(false);
     210                 :           0 :         break;
     211                 :             :     }
     212                 :             :     }
     213                 :       13354 : }
     214                 :             : 
     215         [ +  - ]:        1868 : FUZZ_TARGET(tx_pool_standard, .init = initialize_tx_pool)
     216                 :             : {
     217                 :        1416 :     SeedRandomStateForTest(SeedRand::ZEROS);
     218                 :        1416 :     FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
     219                 :        1416 :     const auto& node = g_setup->m_node;
     220                 :        1416 :     auto& chainstate{static_cast<DummyChainState&>(node.chainman->ActiveChainstate())};
     221                 :             : 
     222                 :        1416 :     MockTime(fuzzed_data_provider, chainstate);
     223                 :             : 
     224                 :             :     // All RBF-spendable outpoints
     225                 :        1416 :     std::set<COutPoint> outpoints_rbf;
     226                 :             :     // All outpoints counting toward the total supply (subset of outpoints_rbf)
     227                 :        1416 :     std::set<COutPoint> outpoints_supply;
     228         [ +  + ]:      143016 :     for (const auto& outpoint : g_outpoints_coinbase_init_mature) {
     229         [ +  - ]:      283200 :         Assert(outpoints_supply.insert(outpoint).second);
     230                 :             :     }
     231         [ +  - ]:        1416 :     outpoints_rbf = outpoints_supply;
     232                 :             : 
     233                 :             :     // The sum of the values of all spendable outpoints
     234                 :        1416 :     constexpr CAmount SUPPLY_TOTAL{COINBASE_MATURITY * 50 * COIN};
     235                 :             : 
     236         [ +  - ]:        1416 :     SetMempoolConstraints(*node.args, fuzzed_data_provider);
     237         [ +  - ]:        1416 :     auto tx_pool_{MakeMempool(fuzzed_data_provider, node)};
     238         [ +  - ]:        1416 :     MockedTxPool& tx_pool = *static_cast<MockedTxPool*>(tx_pool_.get());
     239                 :             : 
     240         [ +  - ]:        1416 :     chainstate.SetMempool(&tx_pool);
     241                 :             : 
     242                 :             :     // Helper to query an amount
     243   [ +  -  +  - ]:        4248 :     const CCoinsViewMemPool amount_view{WITH_LOCK(::cs_main, return &chainstate.CoinsTip()), tx_pool};
     244                 :     1864281 :     const auto GetAmount = [&](const COutPoint& outpoint) {
     245         [ +  - ]:     1862865 :         auto coin{amount_view.GetCoin(outpoint).value()};
     246                 :     1862865 :         return coin.out.nValue;
     247                 :     1864281 :     };
     248                 :             : 
     249   [ +  +  +  + ]:       14770 :     LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 100)
     250                 :             :     {
     251                 :       13354 :         {
     252                 :             :             // Total supply is the mempool fee + all outpoints
     253         [ +  - ]:       26708 :             CAmount supply_now{WITH_LOCK(tx_pool.cs, return tx_pool.GetTotalFee())};
     254         [ +  + ]:     1515098 :             for (const auto& op : outpoints_supply) {
     255         [ +  - ]:     1501744 :                 supply_now += GetAmount(op);
     256                 :             :             }
     257         [ -  + ]:       13354 :             Assert(supply_now == SUPPLY_TOTAL);
     258                 :             :         }
     259         [ -  + ]:       13354 :         Assert(!outpoints_supply.empty());
     260                 :             : 
     261                 :             :         // Create transaction to add to the mempool
     262                 :       26708 :         const CTransactionRef tx = [&] {
     263                 :       13354 :             CMutableTransaction tx_mut;
     264         [ +  + ]:       13354 :             tx_mut.version = fuzzed_data_provider.ConsumeBool() ? TRUC_VERSION : CTransaction::CURRENT_VERSION;
     265         [ +  + ]:       13354 :             tx_mut.nLockTime = fuzzed_data_provider.ConsumeBool() ? 0 : fuzzed_data_provider.ConsumeIntegral<uint32_t>();
     266                 :       13354 :             const auto num_in = fuzzed_data_provider.ConsumeIntegralInRange<int>(1, outpoints_rbf.size());
     267                 :       13354 :             const auto num_out = fuzzed_data_provider.ConsumeIntegralInRange<int>(1, outpoints_rbf.size() * 2);
     268                 :             : 
     269                 :       13354 :             CAmount amount_in{0};
     270         [ +  + ]:      374475 :             for (int i = 0; i < num_in; ++i) {
     271                 :             :                 // Pop random outpoint
     272                 :      361121 :                 auto pop = outpoints_rbf.begin();
     273                 :      361121 :                 std::advance(pop, fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, outpoints_rbf.size() - 1));
     274                 :      361121 :                 const auto outpoint = *pop;
     275                 :      361121 :                 outpoints_rbf.erase(pop);
     276         [ +  - ]:      361121 :                 amount_in += GetAmount(outpoint);
     277                 :             : 
     278                 :             :                 // Create input
     279                 :      361121 :                 const auto sequence = ConsumeSequence(fuzzed_data_provider);
     280                 :      361121 :                 const auto script_sig = CScript{};
     281   [ -  +  +  +  :      722242 :                 const auto script_wit_stack = std::vector<std::vector<uint8_t>>{WITNESS_STACK_ELEM_OP_TRUE};
                   -  - ]
     282                 :      361121 :                 CTxIn in;
     283                 :      361121 :                 in.prevout = outpoint;
     284                 :      361121 :                 in.nSequence = sequence;
     285                 :      361121 :                 in.scriptSig = script_sig;
     286         [ +  - ]:      361121 :                 in.scriptWitness.stack = script_wit_stack;
     287                 :             : 
     288         [ +  - ]:      361121 :                 tx_mut.vin.push_back(in);
     289                 :      361121 :             }
     290                 :             : 
     291                 :             :             // Check sigops in mempool + block template creation
     292                 :       13354 :             bool add_sigops{fuzzed_data_provider.ConsumeBool()};
     293                 :             : 
     294                 :       13354 :             const auto amount_fee = fuzzed_data_provider.ConsumeIntegralInRange<CAmount>(-1000, amount_in);
     295                 :       13354 :             const auto amount_out = (amount_in - amount_fee) / num_out;
     296         [ +  + ]:      697290 :             for (int i = 0; i < num_out; ++i) {
     297         [ +  + ]:      683936 :                 if (i == 0 && add_sigops) {
     298   [ +  -  +  -  :       36672 :                     tx_mut.vout.emplace_back(amount_out, CScript() << std::vector<unsigned char>(33, 0x02) << OP_CHECKSIG);
                   +  - ]
     299                 :             :                 } else {
     300         [ +  - ]:      671712 :                     tx_mut.vout.emplace_back(amount_out, P2WSH_OP_TRUE);
     301                 :             :                 }
     302                 :             :             }
     303                 :             : 
     304         [ +  - ]:       13354 :             auto tx = MakeTransactionRef(tx_mut);
     305                 :             :             // Restore previously removed outpoints
     306         [ +  + ]:      374475 :             for (const auto& in : tx->vin) {
     307         [ +  - ]:      722242 :                 Assert(outpoints_rbf.insert(in.prevout).second);
     308                 :             :             }
     309                 :       13354 :             return tx;
     310         [ +  - ]:      387829 :         }();
     311                 :             : 
     312         [ +  + ]:       13354 :         if (fuzzed_data_provider.ConsumeBool()) {
     313         [ +  - ]:       12790 :             MockTime(fuzzed_data_provider, chainstate);
     314                 :             :         }
     315         [ +  + ]:       13354 :         if (fuzzed_data_provider.ConsumeBool()) {
     316         [ +  - ]:       12470 :             tx_pool.RollingFeeUpdate();
     317                 :             :         }
     318         [ +  + ]:       13354 :         if (fuzzed_data_provider.ConsumeBool()) {
     319         [ +  + ]:       12688 :             const auto& txid = fuzzed_data_provider.ConsumeBool() ?
     320                 :       12460 :                                    tx->GetHash() :
     321                 :         228 :                                    PickValue(fuzzed_data_provider, outpoints_rbf).hash;
     322                 :       12688 :             const auto delta = fuzzed_data_provider.ConsumeIntegralInRange<CAmount>(-50 * COIN, +50 * COIN);
     323         [ +  - ]:       12688 :             tx_pool.PrioritiseTransaction(txid, delta);
     324                 :             :         }
     325                 :             : 
     326                 :             :         // Remember all removed and added transactions
     327         [ +  - ]:       13354 :         std::set<CTransactionRef> removed;
     328                 :       13354 :         std::set<CTransactionRef> added;
     329         [ +  - ]:       13354 :         auto txr = std::make_shared<TransactionsDelta>(removed, added);
     330   [ +  -  +  - ]:       26708 :         node.validation_signals->RegisterSharedValidationInterface(txr);
     331                 :             : 
     332                 :             :         // Make sure ProcessNewPackage on one transaction works.
     333                 :             :         // The result is not guaranteed to be the same as what is returned by ATMP.
     334   [ +  -  +  -  :       66770 :         const auto result_package = WITH_LOCK(::cs_main,
          +  -  +  +  +  
             -  -  -  -  
                      - ]
     335                 :             :                                     return ProcessNewPackage(chainstate, tx_pool, {tx}, true, /*client_maxfeerate=*/{}));
     336                 :             :         // If something went wrong due to a package-specific policy, it might not return a
     337                 :             :         // validation result for the transaction.
     338         [ +  + ]:       13354 :         if (result_package.m_state.GetResult() != PackageValidationResult::PCKG_POLICY) {
     339                 :       12950 :             auto it = result_package.m_tx_results.find(tx->GetWitnessHash());
     340         [ -  + ]:       12950 :             Assert(it != result_package.m_tx_results.end());
     341   [ +  +  +  -  :       25140 :             Assert(it->second.m_result_type == MempoolAcceptResult::ResultType::VALID ||
                   -  + ]
     342                 :             :                    it->second.m_result_type == MempoolAcceptResult::ResultType::INVALID);
     343                 :             :         }
     344                 :             : 
     345   [ +  -  +  - ]:       40062 :         const auto res = WITH_LOCK(::cs_main, return AcceptToMemoryPool(chainstate, tx, GetTime(), /*bypass_limits=*/false, /*test_accept=*/false));
     346                 :       13354 :         const bool accepted = res.m_result_type == MempoolAcceptResult::ResultType::VALID;
     347         [ +  - ]:       13354 :         node.validation_signals->SyncWithValidationInterfaceQueue();
     348   [ +  -  +  - ]:       26708 :         node.validation_signals->UnregisterSharedValidationInterface(txr);
     349                 :             : 
     350         [ +  - ]:       13354 :         bool txid_in_mempool = tx_pool.exists(tx->GetHash());
     351         [ +  - ]:       13354 :         bool wtxid_in_mempool = tx_pool.exists(tx->GetWitnessHash());
     352         [ +  - ]:       13354 :         CheckATMPInvariants(res, txid_in_mempool, wtxid_in_mempool);
     353                 :             : 
     354         [ -  + ]:       13354 :         Assert(accepted != added.empty());
     355         [ +  + ]:       13354 :         if (accepted) {
     356         [ -  + ]:         949 :             Assert(added.size() == 1); // For now, no package acceptance
     357         [ -  + ]:         949 :             Assert(tx == *added.begin());
     358         [ +  - ]:         949 :             CheckMempoolTRUCInvariants(tx_pool);
     359                 :             :         } else {
     360                 :             :             // Do not consider rejected transaction removed
     361                 :       12405 :             removed.erase(tx);
     362                 :             :         }
     363                 :             : 
     364                 :             :         // Helper to insert spent and created outpoints of a tx into collections
     365                 :       13354 :         using Sets = std::vector<std::reference_wrapper<std::set<COutPoint>>>;
     366                 :       14681 :         const auto insert_tx = [](Sets created_by_tx, Sets consumed_by_tx, const auto& tx) {
     367   [ -  +  +  + ]:       18026 :             for (size_t i{0}; i < tx.vout.size(); ++i) {
     368         [ +  + ]:       47931 :                 for (auto& set : created_by_tx) {
     369         [ -  + ]:       31232 :                     Assert(set.get().emplace(tx.GetHash(), i).second);
     370                 :             :                 }
     371                 :             :             }
     372         [ +  + ]:        9654 :             for (const auto& in : tx.vin) {
     373         [ +  + ]:       16654 :                 for (auto& set : consumed_by_tx) {
     374         [ -  + ]:        8327 :                     Assert(set.get().insert(in.prevout).second);
     375                 :             :                 }
     376                 :             :             }
     377                 :        1327 :         };
     378                 :             :         // Add created outpoints, remove spent outpoints
     379                 :       13354 :         {
     380                 :             :             // Outpoints that no longer exist at all
     381                 :       13354 :             std::set<COutPoint> consumed_erased;
     382                 :             :             // Outpoints that no longer count toward the total supply
     383                 :       13354 :             std::set<COutPoint> consumed_supply;
     384         [ +  + ]:       13732 :             for (const auto& removed_tx : removed) {
     385   [ +  -  +  -  :         756 :                 insert_tx(/*created_by_tx=*/{consumed_erased}, /*consumed_by_tx=*/{outpoints_supply}, /*tx=*/*removed_tx);
                   +  - ]
     386                 :             :             }
     387         [ +  + ]:       14303 :             for (const auto& added_tx : added) {
     388   [ +  -  +  -  :        1898 :                 insert_tx(/*created_by_tx=*/{outpoints_supply, outpoints_rbf}, /*consumed_by_tx=*/{consumed_supply}, /*tx=*/*added_tx);
                   +  - ]
     389                 :             :             }
     390         [ +  + ]:       15520 :             for (const auto& p : consumed_erased) {
     391         [ -  + ]:        2166 :                 Assert(outpoints_supply.erase(p) == 1);
     392         [ -  + ]:        2166 :                 Assert(outpoints_rbf.erase(p) == 1);
     393                 :             :             }
     394         [ +  + ]:       19898 :             for (const auto& p : consumed_supply) {
     395         [ -  + ]:        6544 :                 Assert(outpoints_supply.erase(p) == 1);
     396                 :             :             }
     397                 :       13354 :         }
     398   [ +  -  +  - ]:       40062 :     }
     399         [ +  - ]:        1416 :     Finish(fuzzed_data_provider, tx_pool, chainstate);
     400                 :        1416 : }
     401                 :             : 
     402         [ +  - ]:        7453 : FUZZ_TARGET(tx_pool, .init = initialize_tx_pool)
     403                 :             : {
     404                 :        7001 :     SeedRandomStateForTest(SeedRand::ZEROS);
     405                 :        7001 :     FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
     406                 :        7001 :     const auto& node = g_setup->m_node;
     407                 :        7001 :     auto& chainstate{static_cast<DummyChainState&>(node.chainman->ActiveChainstate())};
     408                 :             : 
     409                 :        7001 :     MockTime(fuzzed_data_provider, chainstate);
     410                 :             : 
     411                 :        7001 :     std::vector<Txid> txids;
     412   [ -  +  +  - ]:        7001 :     txids.reserve(g_outpoints_coinbase_init_mature.size());
     413         [ +  + ]:      707101 :     for (const auto& outpoint : g_outpoints_coinbase_init_mature) {
     414         [ +  - ]:      700100 :         txids.push_back(outpoint.hash);
     415                 :             :     }
     416         [ +  + ]:       35005 :     for (int i{0}; i <= 3; ++i) {
     417                 :             :         // Add some immature and non-existent outpoints
     418   [ +  -  +  - ]:       28004 :         txids.push_back(g_outpoints_coinbase_init_immature.at(i).hash);
     419         [ +  - ]:       28004 :         txids.push_back(Txid::FromUint256(ConsumeUInt256(fuzzed_data_provider)));
     420                 :             :     }
     421                 :             : 
     422         [ +  - ]:        7001 :     SetMempoolConstraints(*node.args, fuzzed_data_provider);
     423         [ +  - ]:        7001 :     auto tx_pool_{MakeMempool(fuzzed_data_provider, node)};
     424                 :        7001 :     MockedTxPool& tx_pool = *static_cast<MockedTxPool*>(tx_pool_.get());
     425                 :             : 
     426                 :        7001 :     chainstate.SetMempool(&tx_pool);
     427                 :             : 
     428                 :             :     // If we ever bypass limits, do not do TRUC invariants checks
     429                 :        7001 :     bool ever_bypassed_limits{false};
     430                 :             : 
     431   [ +  +  +  + ]:      213255 :     LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 300)
     432                 :             :     {
     433         [ +  - ]:      206254 :         const auto mut_tx = ConsumeTransaction(fuzzed_data_provider, txids);
     434                 :             : 
     435         [ +  + ]:      206254 :         if (fuzzed_data_provider.ConsumeBool()) {
     436         [ +  - ]:      114510 :             MockTime(fuzzed_data_provider, chainstate);
     437                 :             :         }
     438         [ +  + ]:      206254 :         if (fuzzed_data_provider.ConsumeBool()) {
     439         [ +  - ]:      128330 :             tx_pool.RollingFeeUpdate();
     440                 :             :         }
     441         [ +  + ]:      206254 :         if (fuzzed_data_provider.ConsumeBool()) {
     442         [ +  + ]:      125517 :             const auto txid = fuzzed_data_provider.ConsumeBool() ?
     443         [ +  - ]:      100903 :                                    mut_tx.GetHash() :
     444                 :       24614 :                                    PickValue(fuzzed_data_provider, txids);
     445                 :      125517 :             const auto delta = fuzzed_data_provider.ConsumeIntegralInRange<CAmount>(-50 * COIN, +50 * COIN);
     446         [ +  - ]:      125517 :             tx_pool.PrioritiseTransaction(txid, delta);
     447                 :             :         }
     448                 :             : 
     449                 :      206254 :         const bool bypass_limits{fuzzed_data_provider.ConsumeBool()};
     450                 :      206254 :         ever_bypassed_limits |= bypass_limits;
     451                 :             : 
     452         [ +  - ]:      206254 :         const auto tx = MakeTransactionRef(mut_tx);
     453   [ +  -  +  - ]:      618762 :         const auto res = WITH_LOCK(::cs_main, return AcceptToMemoryPool(chainstate, tx, GetTime(), bypass_limits, /*test_accept=*/false));
     454                 :      206254 :         const bool accepted = res.m_result_type == MempoolAcceptResult::ResultType::VALID;
     455         [ +  + ]:      206254 :         if (accepted) {
     456         [ +  - ]:       36953 :             txids.push_back(tx->GetHash());
     457         [ +  + ]:       36953 :             if (!ever_bypassed_limits) {
     458         [ +  - ]:        1456 :                 CheckMempoolTRUCInvariants(tx_pool);
     459                 :             :             }
     460                 :             :         }
     461         [ +  - ]:      618762 :     }
     462         [ +  - ]:        7001 :     Finish(fuzzed_data_provider, tx_pool, chainstate);
     463                 :        7001 : }
     464                 :             : } // namespace
        

Generated by: LCOV version 2.0-1