LCOV - code coverage report
Current view: top level - src/test/fuzz - package_eval.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 100.0 % 280 280
Test Date: 2025-12-07 04:23:59 Functions: 100.0 % 23 23
Branches: 67.4 % 436 294

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2023 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                 :             : 
      27                 :             : namespace {
      28                 :             : 
      29                 :             : const TestingSetup* g_setup;
      30                 :             : std::vector<COutPoint> g_outpoints_coinbase_init_mature;
      31                 :             : 
      32                 :             : struct MockedTxPool : public CTxMemPool {
      33                 :       42715 :     void RollingFeeUpdate() EXCLUSIVE_LOCKS_REQUIRED(!cs)
      34                 :             :     {
      35                 :       42715 :         LOCK(cs);
      36         [ +  - ]:       42715 :         lastRollingFeeUpdate = GetTime();
      37         [ +  - ]:       42715 :         blockSinceLastRollingFeeBump = true;
      38                 :       42715 :     }
      39                 :             : };
      40                 :             : 
      41                 :           2 : void initialize_tx_pool()
      42                 :             : {
      43   [ +  -  +  -  :           2 :     static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
                   +  - ]
      44                 :           2 :     g_setup = testing_setup.get();
      45   [ +  -  +  - ]:           6 :     SetMockTime(WITH_LOCK(g_setup->m_node.chainman->GetMutex(), return g_setup->m_node.chainman->ActiveTip()->Time()));
      46                 :             : 
      47                 :           2 :     BlockAssembler::Options options;
      48                 :           2 :     options.coinbase_output_script = P2WSH_EMPTY;
      49                 :             : 
      50         [ +  + ]:         402 :     for (int i = 0; i < 2 * COINBASE_MATURITY; ++i) {
      51         [ +  - ]:         400 :         COutPoint prevout{MineBlock(g_setup->m_node, options)};
      52         [ +  + ]:         400 :         if (i < COINBASE_MATURITY) {
      53                 :             :             // Remember the txids to avoid expensive disk access later on
      54         [ +  - ]:         200 :             g_outpoints_coinbase_init_mature.push_back(prevout);
      55                 :             :         }
      56                 :             :     }
      57         [ +  - ]:           2 :     g_setup->m_node.validation_signals->SyncWithValidationInterfaceQueue();
      58                 :           2 : }
      59                 :             : 
      60                 :             : struct OutpointsUpdater final : public CValidationInterface {
      61                 :             :     std::set<COutPoint>& m_mempool_outpoints;
      62                 :             : 
      63                 :        3785 :     explicit OutpointsUpdater(std::set<COutPoint>& r)
      64                 :        3785 :         : m_mempool_outpoints{r} {}
      65                 :             : 
      66                 :      129077 :     void TransactionAddedToMempool(const NewMempoolTransactionInfo& tx, uint64_t /* mempool_sequence */) override
      67                 :             :     {
      68                 :             :         // for coins spent we always want to be able to rbf so they're not removed
      69                 :             : 
      70                 :             :         // outputs from this tx can now be spent
      71   [ -  +  +  + ]:      442473 :         for (uint32_t index{0}; index < tx.info.m_tx->vout.size(); ++index) {
      72                 :      313396 :             m_mempool_outpoints.insert(COutPoint{tx.info.m_tx->GetHash(), index});
      73                 :             :         }
      74                 :      129077 :     }
      75                 :             : 
      76                 :       41841 :     void TransactionRemovedFromMempool(const CTransactionRef& tx, MemPoolRemovalReason reason, uint64_t /* mempool_sequence */) override
      77                 :             :     {
      78                 :             :         // outpoints spent by this tx are now available
      79         [ +  + ]:      116524 :         for (const auto& input : tx->vin) {
      80                 :             :             // Could already exist if this was a replacement
      81                 :       74683 :             m_mempool_outpoints.insert(input.prevout);
      82                 :             :         }
      83                 :             :         // outpoints created by this tx no longer exist
      84   [ -  +  +  + ]:      142022 :         for (uint32_t index{0}; index < tx->vout.size(); ++index) {
      85                 :      100181 :             m_mempool_outpoints.erase(COutPoint{tx->GetHash(), index});
      86                 :             :         }
      87                 :       41841 :     }
      88                 :             : };
      89                 :             : 
      90                 :             : struct TransactionsDelta final : public CValidationInterface {
      91                 :             :     std::set<CTransactionRef>& m_added;
      92                 :             : 
      93                 :      162084 :     explicit TransactionsDelta(std::set<CTransactionRef>& a)
      94                 :      162084 :         : m_added{a} {}
      95                 :             : 
      96                 :       20382 :     void TransactionAddedToMempool(const NewMempoolTransactionInfo& tx, uint64_t /* mempool_sequence */) override
      97                 :             :     {
      98                 :             :         // Transactions may be entered and booted any number of times
      99                 :       20382 :         m_added.insert(tx.info.m_tx);
     100                 :       20382 :     }
     101                 :             : 
     102                 :       16801 :     void TransactionRemovedFromMempool(const CTransactionRef& tx, MemPoolRemovalReason reason, uint64_t /* mempool_sequence */) override
     103                 :             :     {
     104                 :             :         // Transactions may be entered and booted any number of times
     105                 :       16801 :          m_added.erase(tx);
     106                 :       16801 :     }
     107                 :             : };
     108                 :             : 
     109                 :       61736 : void MockTime(FuzzedDataProvider& fuzzed_data_provider, const Chainstate& chainstate)
     110                 :             : {
     111                 :       61736 :     const auto time = ConsumeTime(fuzzed_data_provider,
     112                 :       61736 :                                   chainstate.m_chain.Tip()->GetMedianTimePast() + 1,
     113         [ -  + ]:       61736 :                                   std::numeric_limits<decltype(chainstate.m_chain.Tip()->nTime)>::max());
     114                 :       61736 :     SetMockTime(time);
     115                 :       61736 : }
     116                 :             : 
     117                 :        1664 : std::unique_ptr<CTxMemPool> MakeMempool(FuzzedDataProvider& fuzzed_data_provider, const NodeContext& node)
     118                 :             : {
     119                 :             :     // Take the default options for tests...
     120                 :        1664 :     CTxMemPool::Options mempool_opts{MemPoolOptionsForTest(node)};
     121                 :             : 
     122                 :             : 
     123                 :             :     // ...override specific options for this specific fuzz suite
     124                 :        1664 :     mempool_opts.limits.ancestor_count = fuzzed_data_provider.ConsumeIntegralInRange<unsigned>(0, 50);
     125                 :        1664 :     mempool_opts.limits.descendant_count = fuzzed_data_provider.ConsumeIntegralInRange<unsigned>(0, 50);
     126                 :        1664 :     mempool_opts.max_size_bytes = fuzzed_data_provider.ConsumeIntegralInRange<unsigned>(0, 200) * 1'000'000;
     127                 :        1664 :     mempool_opts.expiry = std::chrono::hours{fuzzed_data_provider.ConsumeIntegralInRange<unsigned>(0, 999)};
     128                 :             :     // Only interested in 2 cases: sigop cost 0 or when single legacy sigop cost is >> 1KvB
     129                 :        1664 :     nBytesPerSigOp = fuzzed_data_provider.ConsumeIntegralInRange<unsigned>(0, 1) * 10'000;
     130                 :             : 
     131                 :        1664 :     mempool_opts.check_ratio = 1;
     132                 :        1664 :     mempool_opts.require_standard = fuzzed_data_provider.ConsumeBool();
     133                 :             : 
     134         [ +  - ]:        1664 :     bilingual_str error;
     135                 :             :     // ...and construct a CTxMemPool from it
     136         [ +  - ]:        1664 :     auto mempool{std::make_unique<CTxMemPool>(std::move(mempool_opts), error)};
     137                 :             :     // ... ignore the error since it might be beneficial to fuzz even when the
     138                 :             :     // mempool size is unreasonably small
     139   [ +  +  +  -  :        1902 :     Assert(error.empty() || error.original.starts_with("-maxmempool must be at least "));
                   -  + ]
     140                 :        1664 :     return mempool;
     141                 :        1664 : }
     142                 :             : 
     143                 :        2121 : std::unique_ptr<CTxMemPool> MakeEphemeralMempool(const NodeContext& node)
     144                 :             : {
     145                 :             :     // Take the default options for tests...
     146                 :        2121 :     CTxMemPool::Options mempool_opts{MemPoolOptionsForTest(node)};
     147                 :             : 
     148                 :        2121 :     mempool_opts.check_ratio = 1;
     149                 :             : 
     150                 :             :     // Require standardness rules otherwise ephemeral dust is no-op
     151                 :        2121 :     mempool_opts.require_standard = true;
     152                 :             : 
     153                 :             :     // And set minrelay to 0 to allow ephemeral parent tx even with non-TRUC
     154         [ +  - ]:        2121 :     mempool_opts.min_relay_feerate = CFeeRate(0);
     155                 :             : 
     156         [ +  - ]:        2121 :     bilingual_str error;
     157                 :             :     // ...and construct a CTxMemPool from it
     158         [ +  - ]:        2121 :     auto mempool{std::make_unique<CTxMemPool>(std::move(mempool_opts), error)};
     159         [ -  + ]:        2121 :     Assert(error.empty());
     160                 :        2121 :     return mempool;
     161                 :        2121 : }
     162                 :             : 
     163                 :             : // Scan mempool for a tx that has spent dust and return a
     164                 :             : // prevout of the child that isn't the dusty parent itself.
     165                 :             : // This is used to double-spend the child out of the mempool,
     166                 :             : // leaving the parent childless.
     167                 :             : // This assumes CheckMempoolEphemeralInvariants has passed for tx_pool.
     168                 :      164829 : std::optional<COutPoint> GetChildEvictingPrevout(const CTxMemPool& tx_pool)
     169                 :             : {
     170                 :      164829 :     LOCK(tx_pool.cs);
     171   [ +  -  +  + ]:     5671273 :     for (const auto& tx_info : tx_pool.infoAll()) {
     172   [ +  -  -  + ]:     5518011 :         const auto& entry = *Assert(tx_pool.GetEntry(tx_info.tx->GetHash()));
     173         [ +  - ]:     5518011 :         std::vector<uint32_t> dust_indexes{GetDust(*tx_info.tx, tx_pool.m_opts.dust_relay_feerate)};
     174         [ +  + ]:     5518011 :         if (!dust_indexes.empty()) {
     175         [ +  - ]:       25543 :             const auto& children = tx_pool.GetChildren(entry);
     176         [ +  + ]:       25543 :             if (!children.empty()) {
     177   [ -  +  -  + ]:       17346 :                 Assert(children.size() == 1);
     178                 :             :                 // Find an input that doesn't spend from parent's txid
     179                 :       17346 :                 const auto& only_child = children.begin()->get().GetTx();
     180         [ +  + ]:       32149 :                 for (const auto& tx_input : only_child.vin) {
     181         [ +  + ]:       26370 :                     if (tx_input.prevout.hash != tx_info.tx->GetHash()) {
     182                 :       11567 :                         return tx_input.prevout;
     183                 :             :                     }
     184                 :             :                 }
     185                 :             :             }
     186                 :       25543 :         }
     187                 :     5671273 :     }
     188                 :             : 
     189                 :      153262 :     return std::nullopt;
     190                 :      164829 : }
     191                 :             : 
     192         [ +  - ]:        2577 : FUZZ_TARGET(ephemeral_package_eval, .init = initialize_tx_pool)
     193                 :             : {
     194                 :        2121 :     SeedRandomStateForTest(SeedRand::ZEROS);
     195                 :        2121 :     FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
     196                 :        2121 :     const auto& node = g_setup->m_node;
     197                 :        2121 :     auto& chainstate{static_cast<DummyChainState&>(node.chainman->ActiveChainstate())};
     198                 :             : 
     199                 :        2121 :     MockTime(fuzzed_data_provider, chainstate);
     200                 :             : 
     201                 :             :     // All RBF-spendable outpoints outside of the unsubmitted package
     202         [ +  - ]:        2121 :     std::set<COutPoint> mempool_outpoints;
     203         [ +  - ]:        2121 :     std::unordered_map<COutPoint, CAmount, SaltedOutpointHasher> outpoints_value;
     204         [ +  + ]:      214221 :     for (const auto& outpoint : g_outpoints_coinbase_init_mature) {
     205         [ +  - ]:      212100 :         Assert(mempool_outpoints.insert(outpoint).second);
     206         [ +  - ]:      212100 :         outpoints_value[outpoint] = 50 * COIN;
     207                 :             :     }
     208                 :             : 
     209         [ +  - ]:        2121 :     auto outpoints_updater = std::make_shared<OutpointsUpdater>(mempool_outpoints);
     210   [ +  -  +  - ]:        4242 :     node.validation_signals->RegisterSharedValidationInterface(outpoints_updater);
     211                 :             : 
     212         [ +  - ]:        2121 :     auto tx_pool_{MakeEphemeralMempool(node)};
     213                 :        2121 :     MockedTxPool& tx_pool = *static_cast<MockedTxPool*>(tx_pool_.get());
     214                 :             : 
     215                 :        2121 :     chainstate.SetMempool(&tx_pool);
     216                 :             : 
     217   [ +  +  +  + ]:      373333 :     LIMITED_WHILE(fuzzed_data_provider.remaining_bytes() > 0, 300)
     218                 :             :     {
     219         [ -  + ]:      371212 :         Assert(!mempool_outpoints.empty());
     220                 :             : 
     221                 :      371212 :         std::vector<CTransactionRef> txs;
     222                 :             : 
     223                 :             :         // Find something we may want to double-spend with two input single tx
     224   [ +  +  +  - ]:      371212 :         std::optional<COutPoint> outpoint_to_rbf{fuzzed_data_provider.ConsumeBool() ? GetChildEvictingPrevout(tx_pool) : std::nullopt};
     225                 :             : 
     226                 :             :         // Make small packages
     227         [ +  + ]:      371212 :         const auto num_txs = outpoint_to_rbf ? 1 : fuzzed_data_provider.ConsumeIntegralInRange<size_t>(1, 4);
     228                 :             : 
     229                 :      371212 :         std::set<COutPoint> package_outpoints;
     230   [ -  +  +  + ]:     1191024 :         while (txs.size() < num_txs) {
     231                 :             :             // Create transaction to add to the mempool
     232         [ +  - ]:     1639624 :             txs.emplace_back([&] {
     233                 :      819812 :                 CMutableTransaction tx_mut;
     234                 :      819812 :                 tx_mut.version = CTransaction::CURRENT_VERSION;
     235                 :      819812 :                 tx_mut.nLockTime = 0;
     236                 :             :                 // Last transaction in a package needs to be a child of parents to get further in validation
     237                 :             :                 // so the last transaction to be generated(in a >1 package) must spend all package-made outputs
     238                 :             :                 // Note that this test currently only spends package outputs in last transaction.
     239   [ +  +  -  +  :      819812 :                 bool last_tx = num_txs > 1 && txs.size() == num_txs - 1;
                   +  + ]
     240         [ +  + ]:      819812 :                 const auto num_in = outpoint_to_rbf ? 2 :
     241         [ +  + ]:      808245 :                     last_tx ? fuzzed_data_provider.ConsumeIntegralInRange<int>(package_outpoints.size()/2 + 1, package_outpoints.size()) :
     242                 :      571867 :                     fuzzed_data_provider.ConsumeIntegralInRange<int>(1, 4);
     243         [ +  + ]:      819812 :                 const auto num_out = outpoint_to_rbf ? 1 : fuzzed_data_provider.ConsumeIntegralInRange<int>(1, 4);
     244                 :             : 
     245         [ +  + ]:      819812 :                 auto& outpoints = last_tx ? package_outpoints : mempool_outpoints;
     246                 :             : 
     247   [ +  -  -  +  :      819812 :                 Assert((int)outpoints.size() >= num_in && num_in > 0);
                   -  + ]
     248                 :             : 
     249                 :             :                 CAmount amount_in{0};
     250         [ +  + ]:     3084122 :                 for (int i = 0; i < num_in; ++i) {
     251                 :             :                     // Pop random outpoint. We erase them to avoid double-spending
     252                 :             :                     // while in this loop, but later add them back (unless last_tx).
     253                 :     2264310 :                     auto pop = outpoints.begin();
     254                 :     2264310 :                     std::advance(pop, fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, outpoints.size() - 1));
     255         [ +  + ]:     2264310 :                     auto outpoint = *pop;
     256                 :             : 
     257   [ +  +  +  + ]:     2264310 :                     if (i == 0 && outpoint_to_rbf) {
     258                 :       11567 :                         outpoint = *outpoint_to_rbf;
     259                 :       11567 :                         outpoints.erase(outpoint);
     260                 :             :                     } else {
     261                 :     2252743 :                         outpoints.erase(pop);
     262                 :             :                     }
     263                 :             :                     // no need to update or erase from outpoints_value
     264         [ +  - ]:     2264310 :                     amount_in += outpoints_value.at(outpoint);
     265                 :             : 
     266                 :             :                     // Create input
     267                 :     2264310 :                     CTxIn in;
     268                 :     2264310 :                     in.prevout = outpoint;
     269         [ +  - ]:     2264310 :                     in.scriptWitness.stack = P2WSH_EMPTY_TRUE_STACK;
     270                 :             : 
     271         [ +  - ]:     2264310 :                     tx_mut.vin.push_back(in);
     272                 :     2264310 :                 }
     273                 :             : 
     274                 :      819812 :                 const auto amount_fee = fuzzed_data_provider.ConsumeIntegralInRange<CAmount>(0, amount_in);
     275                 :      819812 :                 const auto amount_out = (amount_in - amount_fee) / num_out;
     276         [ +  + ]:     2684507 :                 for (int i = 0; i < num_out; ++i) {
     277         [ +  - ]:     1864695 :                     tx_mut.vout.emplace_back(amount_out, P2WSH_EMPTY);
     278                 :             :                 }
     279                 :             : 
     280                 :             :                 // Note output amounts can naturally drop to dust on their own.
     281   [ +  +  +  + ]:      819812 :                 if (!outpoint_to_rbf && fuzzed_data_provider.ConsumeBool()) {
     282                 :      293453 :                     uint32_t dust_index = fuzzed_data_provider.ConsumeIntegralInRange<uint32_t>(0, num_out);
     283   [ +  -  +  - ]:      293453 :                     tx_mut.vout.insert(tx_mut.vout.begin() + dust_index, CTxOut(0, P2WSH_EMPTY));
     284                 :             :                 }
     285                 :             : 
     286         [ +  - ]:      819812 :                 auto tx = MakeTransactionRef(tx_mut);
     287                 :             :                 // Restore previously removed outpoints, except in-package outpoints (to allow RBF)
     288         [ +  + ]:      819812 :                 if (!last_tx) {
     289         [ +  + ]:     1906742 :                     for (const auto& in : tx->vin) {
     290         [ +  - ]:     2646616 :                         Assert(outpoints.insert(in.prevout).second);
     291                 :             :                     }
     292                 :             :                     // Cache the in-package outpoints being made
     293   [ -  +  +  + ]:     2068152 :                     for (size_t i = 0; i < tx->vout.size(); ++i) {
     294         [ +  - ]:     1484718 :                         package_outpoints.emplace(tx->GetHash(), i);
     295                 :             :                     }
     296                 :             :                 }
     297                 :             :                 // We need newly-created values for the duration of this run
     298   [ -  +  +  + ]:     2977960 :                 for (size_t i = 0; i < tx->vout.size(); ++i) {
     299         [ +  - ]:     2158148 :                     outpoints_value[COutPoint(tx->GetHash(), i)] = tx->vout[i].nValue;
     300                 :             :                 }
     301                 :      819812 :                 return tx;
     302         [ +  - ]:     2459436 :             }());
     303                 :             :         }
     304                 :             : 
     305         [ +  + ]:      371212 :         if (fuzzed_data_provider.ConsumeBool()) {
     306         [ +  + ]:      157187 :             const auto& txid = fuzzed_data_provider.ConsumeBool() ?
     307                 :       64274 :                                    txs.back()->GetHash() :
     308                 :       92913 :                                    PickValue(fuzzed_data_provider, mempool_outpoints).hash;
     309                 :      157187 :             const auto delta = fuzzed_data_provider.ConsumeIntegralInRange<CAmount>(-50 * COIN, +50 * COIN);
     310                 :             :             // We only prioritise out of mempool transactions since PrioritiseTransaction doesn't
     311                 :             :             // filter for ephemeral dust
     312   [ +  -  +  + ]:      157187 :             if (tx_pool.exists(txid)) {
     313         [ +  - ]:       51349 :                 const auto tx_info{tx_pool.info(txid)};
     314   [ +  -  +  + ]:       51349 :                 if (GetDust(*tx_info.tx, tx_pool.m_opts.dust_relay_feerate).empty()) {
     315         [ +  - ]:       50107 :                     tx_pool.PrioritiseTransaction(txid, delta);
     316                 :             :                 }
     317                 :       51349 :             }
     318                 :             :         }
     319                 :             : 
     320         [ -  + ]:      371212 :         auto single_submit = txs.size() == 1;
     321                 :             : 
     322         [ +  - ]:     1113636 :         const auto result_package = WITH_LOCK(::cs_main,
     323                 :             :                                     return ProcessNewPackage(chainstate, tx_pool, txs, /*test_accept=*/single_submit, /*client_maxfeerate=*/{}));
     324                 :             : 
     325   [ +  -  +  - ]:     1113636 :         const auto res = WITH_LOCK(::cs_main, return AcceptToMemoryPool(chainstate, txs.back(), GetTime(),
     326                 :             :                                    /*bypass_limits=*/false, /*test_accept=*/!single_submit));
     327                 :             : 
     328   [ +  +  +  + ]:      371212 :         if (!single_submit && result_package.m_state.GetResult() != PackageValidationResult::PCKG_POLICY) {
     329                 :             :             // We don't know anything about the validity since transactions were randomly generated, so
     330                 :             :             // just use result_package.m_state here. This makes the expect_valid check meaningless, but
     331                 :             :             // we can still verify that the contents of m_tx_results are consistent with m_state.
     332         [ +  - ]:      142126 :             const bool expect_valid{result_package.m_state.IsValid()};
     333   [ +  -  -  + ]:      142126 :             Assert(!CheckPackageMempoolAcceptResult(txs, result_package, expect_valid, &tx_pool));
     334                 :             :         }
     335                 :             : 
     336         [ +  - ]:      371212 :         node.validation_signals->SyncWithValidationInterfaceQueue();
     337                 :             : 
     338         [ +  - ]:      371212 :         CheckMempoolEphemeralInvariants(tx_pool);
     339                 :      371212 :     }
     340                 :             : 
     341   [ +  -  +  - ]:        4242 :     node.validation_signals->UnregisterSharedValidationInterface(outpoints_updater);
     342                 :             : 
     343   [ +  +  +  -  :        6363 :     WITH_LOCK(::cs_main, tx_pool.check(chainstate.CoinsTip(), chainstate.m_chain.Height() + 1));
                   +  - ]
     344         [ +  - ]:        4242 : }
     345                 :             : 
     346                 :             : 
     347         [ +  - ]:        2120 : FUZZ_TARGET(tx_package_eval, .init = initialize_tx_pool)
     348                 :             : {
     349                 :        1664 :     SeedRandomStateForTest(SeedRand::ZEROS);
     350                 :        1664 :     FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
     351                 :        1664 :     const auto& node = g_setup->m_node;
     352                 :        1664 :     auto& chainstate{static_cast<DummyChainState&>(node.chainman->ActiveChainstate())};
     353                 :             : 
     354                 :        1664 :     MockTime(fuzzed_data_provider, chainstate);
     355                 :             : 
     356                 :             :     // All RBF-spendable outpoints outside of the unsubmitted package
     357         [ +  - ]:        1664 :     std::set<COutPoint> mempool_outpoints;
     358         [ +  - ]:        1664 :     std::unordered_map<COutPoint, CAmount, SaltedOutpointHasher> outpoints_value;
     359         [ +  + ]:      168064 :     for (const auto& outpoint : g_outpoints_coinbase_init_mature) {
     360         [ +  - ]:      166400 :         Assert(mempool_outpoints.insert(outpoint).second);
     361         [ +  - ]:      166400 :         outpoints_value[outpoint] = 50 * COIN;
     362                 :             :     }
     363                 :             : 
     364         [ +  - ]:        1664 :     auto outpoints_updater = std::make_shared<OutpointsUpdater>(mempool_outpoints);
     365   [ +  -  +  - ]:        3328 :     node.validation_signals->RegisterSharedValidationInterface(outpoints_updater);
     366                 :             : 
     367         [ +  - ]:        1664 :     auto tx_pool_{MakeMempool(fuzzed_data_provider, node)};
     368                 :        1664 :     MockedTxPool& tx_pool = *static_cast<MockedTxPool*>(tx_pool_.get());
     369                 :             : 
     370                 :        1664 :     chainstate.SetMempool(&tx_pool);
     371                 :             : 
     372   [ +  +  +  + ]:      163748 :     LIMITED_WHILE(fuzzed_data_provider.remaining_bytes() > 0, 300)
     373                 :             :     {
     374         [ -  + ]:      162084 :         Assert(!mempool_outpoints.empty());
     375                 :             : 
     376                 :      162084 :         std::vector<CTransactionRef> txs;
     377                 :             : 
     378                 :             :         // Make packages of 1-to-26 transactions
     379                 :      162084 :         const auto num_txs = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(1, 26);
     380                 :      162084 :         std::set<COutPoint> package_outpoints;
     381   [ -  +  +  + ]:      743689 :         while (txs.size() < num_txs) {
     382                 :             :             // Create transaction to add to the mempool
     383         [ +  - ]:     1163210 :             txs.emplace_back([&] {
     384                 :      581605 :                 CMutableTransaction tx_mut;
     385         [ +  + ]:      581605 :                 tx_mut.version = fuzzed_data_provider.ConsumeBool() ? TRUC_VERSION : CTransaction::CURRENT_VERSION;
     386         [ +  + ]:      581605 :                 tx_mut.nLockTime = fuzzed_data_provider.ConsumeBool() ? 0 : fuzzed_data_provider.ConsumeIntegral<uint32_t>();
     387                 :             :                 // Last transaction in a package needs to be a child of parents to get further in validation
     388                 :             :                 // so the last transaction to be generated(in a >1 package) must spend all package-made outputs
     389                 :             :                 // Note that this test currently only spends package outputs in last transaction.
     390   [ +  +  -  +  :      581605 :                 bool last_tx = num_txs > 1 && txs.size() == num_txs - 1;
                   +  + ]
     391                 :      581605 :                 const auto num_in = last_tx ? package_outpoints.size()  : fuzzed_data_provider.ConsumeIntegralInRange<int>(1, mempool_outpoints.size());
     392                 :      581605 :                 auto num_out = fuzzed_data_provider.ConsumeIntegralInRange<int>(1, mempool_outpoints.size() * 2);
     393                 :             : 
     394         [ +  + ]:      581605 :                 auto& outpoints = last_tx ? package_outpoints : mempool_outpoints;
     395                 :             : 
     396         [ -  + ]:      581605 :                 Assert(!outpoints.empty());
     397                 :             : 
     398                 :             :                 CAmount amount_in{0};
     399         [ +  + ]:    10958486 :                 for (size_t i = 0; i < num_in; ++i) {
     400                 :             :                     // Pop random outpoint. We erase them to avoid double-spending
     401                 :             :                     // while in this loop, but later add them back (unless last_tx).
     402                 :    10376881 :                     auto pop = outpoints.begin();
     403                 :    10376881 :                     std::advance(pop, fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, outpoints.size() - 1));
     404                 :    10376881 :                     const auto outpoint = *pop;
     405                 :    10376881 :                     outpoints.erase(pop);
     406                 :             :                     // no need to update or erase from outpoints_value
     407         [ +  - ]:    10376881 :                     amount_in += outpoints_value.at(outpoint);
     408                 :             : 
     409                 :             :                     // Create input
     410                 :    10376881 :                     const auto sequence = ConsumeSequence(fuzzed_data_provider);
     411                 :    10376881 :                     const auto script_sig = CScript{};
     412   [ +  +  +  - ]:    16180430 :                     const auto script_wit_stack = fuzzed_data_provider.ConsumeBool() ? P2WSH_EMPTY_TRUE_STACK : P2WSH_EMPTY_TWO_STACK;
     413                 :             : 
     414                 :    10376881 :                     CTxIn in;
     415                 :    10376881 :                     in.prevout = outpoint;
     416                 :    10376881 :                     in.nSequence = sequence;
     417                 :    10376881 :                     in.scriptSig = script_sig;
     418         [ +  - ]:    10376881 :                     in.scriptWitness.stack = script_wit_stack;
     419                 :             : 
     420         [ +  - ]:    10376881 :                     tx_mut.vin.push_back(in);
     421                 :    10376881 :                 }
     422                 :             : 
     423                 :             :                 // Duplicate an input
     424                 :      581605 :                 bool dup_input = fuzzed_data_provider.ConsumeBool();
     425         [ +  + ]:      581605 :                 if (dup_input) {
     426         [ +  - ]:      238994 :                     tx_mut.vin.push_back(tx_mut.vin.back());
     427                 :             :                 }
     428                 :             : 
     429                 :             :                 // Refer to a non-existent input
     430         [ +  + ]:      581605 :                 if (fuzzed_data_provider.ConsumeBool()) {
     431         [ +  - ]:      196115 :                     tx_mut.vin.emplace_back();
     432                 :             :                 }
     433                 :             : 
     434                 :             :                 // Make a p2pk output to make sigops adjusted vsize to violate TRUC rules, potentially, which is never spent
     435   [ +  +  +  + ]:      581605 :                 if (last_tx && amount_in > 1000 && fuzzed_data_provider.ConsumeBool()) {
     436   [ +  -  +  -  :       88725 :                     tx_mut.vout.emplace_back(1000, CScript() << std::vector<unsigned char>(33, 0x02) << OP_CHECKSIG);
                   +  - ]
     437                 :             :                     // Don't add any other outputs.
     438                 :       29575 :                     num_out = 1;
     439                 :       29575 :                     amount_in -= 1000;
     440                 :             :                 }
     441                 :             : 
     442                 :      581605 :                 const auto amount_fee = fuzzed_data_provider.ConsumeIntegralInRange<CAmount>(0, amount_in);
     443                 :      581605 :                 const auto amount_out = (amount_in - amount_fee) / num_out;
     444         [ +  + ]:    10159769 :                 for (int i = 0; i < num_out; ++i) {
     445         [ +  - ]:     9578164 :                     tx_mut.vout.emplace_back(amount_out, P2WSH_EMPTY);
     446                 :             :                 }
     447         [ +  - ]:      581605 :                 auto tx = MakeTransactionRef(tx_mut);
     448                 :             :                 // Restore previously removed outpoints, except in-package outpoints
     449         [ +  + ]:      581605 :                 if (!last_tx) {
     450         [ +  + ]:     6066220 :                     for (const auto& in : tx->vin) {
     451                 :             :                         // It's a fake input, or a new input, or a duplicate
     452   [ +  +  +  -  :    11323121 :                         Assert(in == CTxIn() || outpoints.insert(in.prevout).second || dup_input);
          +  +  +  -  -  
                      + ]
     453                 :             :                     }
     454                 :             :                     // Cache the in-package outpoints being made
     455   [ -  +  +  + ]:     9406047 :                     for (size_t i = 0; i < tx->vout.size(); ++i) {
     456         [ +  - ]:     8893537 :                         package_outpoints.emplace(tx->GetHash(), i);
     457                 :             :                     }
     458                 :             :                 }
     459                 :             :                 // We need newly-created values for the duration of this run
     460   [ -  +  +  + ]:    10189344 :                 for (size_t i = 0; i < tx->vout.size(); ++i) {
     461         [ +  - ]:     9607739 :                     outpoints_value[COutPoint(tx->GetHash(), i)] = tx->vout[i].nValue;
     462                 :             :                 }
     463                 :      581605 :                 return tx;
     464         [ +  - ]:     1744815 :             }());
     465                 :             :         }
     466                 :             : 
     467         [ +  + ]:      162084 :         if (fuzzed_data_provider.ConsumeBool()) {
     468         [ +  - ]:       57951 :             MockTime(fuzzed_data_provider, chainstate);
     469                 :             :         }
     470         [ +  + ]:      162084 :         if (fuzzed_data_provider.ConsumeBool()) {
     471         [ +  - ]:       42715 :             tx_pool.RollingFeeUpdate();
     472                 :             :         }
     473         [ +  + ]:      162084 :         if (fuzzed_data_provider.ConsumeBool()) {
     474         [ +  + ]:       60976 :             const auto& txid = fuzzed_data_provider.ConsumeBool() ?
     475                 :       27407 :                                    txs.back()->GetHash() :
     476                 :       33569 :                                    PickValue(fuzzed_data_provider, mempool_outpoints).hash;
     477                 :       60976 :             const auto delta = fuzzed_data_provider.ConsumeIntegralInRange<CAmount>(-50 * COIN, +50 * COIN);
     478         [ +  - ]:       60976 :             tx_pool.PrioritiseTransaction(txid, delta);
     479                 :             :         }
     480                 :             : 
     481                 :             :         // Remember all added transactions
     482         [ +  - ]:      162084 :         std::set<CTransactionRef> added;
     483         [ +  - ]:      162084 :         auto txr = std::make_shared<TransactionsDelta>(added);
     484   [ +  -  +  - ]:      324168 :         node.validation_signals->RegisterSharedValidationInterface(txr);
     485                 :             : 
     486                 :             :         // When there are multiple transactions in the package, we call ProcessNewPackage(txs, test_accept=false)
     487                 :             :         // and AcceptToMemoryPool(txs.back(), test_accept=true). When there is only 1 transaction, we might flip it
     488                 :             :         // (the package is a test accept and ATMP is a submission).
     489   [ -  +  +  +  :      255073 :         auto single_submit = txs.size() == 1 && fuzzed_data_provider.ConsumeBool();
                   +  + ]
     490                 :             : 
     491                 :             :         // Exercise client_maxfeerate logic
     492                 :      162084 :         std::optional<CFeeRate> client_maxfeerate{};
     493         [ +  + ]:      162084 :         if (fuzzed_data_provider.ConsumeBool()) {
     494         [ +  - ]:       39693 :             client_maxfeerate = CFeeRate(fuzzed_data_provider.ConsumeIntegralInRange<CAmount>(-1, 50 * COIN), 100);
     495                 :             :         }
     496                 :             : 
     497         [ +  - ]:      486252 :         const auto result_package = WITH_LOCK(::cs_main,
     498                 :             :                                     return ProcessNewPackage(chainstate, tx_pool, txs, /*test_accept=*/single_submit, client_maxfeerate));
     499                 :             : 
     500                 :             :         // Always set bypass_limits to false because it is not supported in ProcessNewPackage and
     501                 :             :         // can be a source of divergence.
     502   [ +  -  +  - ]:      486252 :         const auto res = WITH_LOCK(::cs_main, return AcceptToMemoryPool(chainstate, txs.back(), GetTime(),
     503                 :             :                                    /*bypass_limits=*/false, /*test_accept=*/!single_submit));
     504                 :      162084 :         const bool passed = res.m_result_type == MempoolAcceptResult::ResultType::VALID;
     505                 :             : 
     506         [ +  - ]:      162084 :         node.validation_signals->SyncWithValidationInterfaceQueue();
     507   [ +  -  +  - ]:      324168 :         node.validation_signals->UnregisterSharedValidationInterface(txr);
     508                 :             : 
     509                 :             :         // There is only 1 transaction in the package. We did a test-package-accept and a ATMP
     510         [ +  + ]:      162084 :         if (single_submit) {
     511         [ -  + ]:       16588 :             Assert(passed != added.empty());
     512         [ -  + ]:       16588 :             Assert(passed == res.m_state.IsValid());
     513         [ +  + ]:       16588 :             if (passed) {
     514         [ -  + ]:        2043 :                 Assert(added.size() == 1);
     515         [ -  + ]:        2043 :                 Assert(txs.back() == *added.begin());
     516                 :             :             }
     517         [ +  + ]:      145496 :         } else if (result_package.m_state.GetResult() != PackageValidationResult::PCKG_POLICY) {
     518                 :             :             // We don't know anything about the validity since transactions were randomly generated, so
     519                 :             :             // just use result_package.m_state here. This makes the expect_valid check meaningless, but
     520                 :             :             // we can still verify that the contents of m_tx_results are consistent with m_state.
     521         [ +  - ]:      107470 :             const bool expect_valid{result_package.m_state.IsValid()};
     522   [ +  -  -  + ]:      107470 :             Assert(!CheckPackageMempoolAcceptResult(txs, result_package, expect_valid, &tx_pool));
     523                 :             :         } else {
     524                 :             :             // This is empty if it fails early checks, or "full" if transactions are looked at deeper
     525   [ -  +  +  +  :       73700 :             Assert(result_package.m_tx_results.size() == txs.size() || result_package.m_tx_results.empty());
             +  -  -  + ]
     526                 :             :         }
     527                 :             : 
     528         [ +  - ]:      162084 :         CheckMempoolTRUCInvariants(tx_pool);
     529                 :             : 
     530                 :             :         // Dust checks only make sense when dust is enforced
     531         [ +  + ]:      162084 :         if (tx_pool.m_opts.require_standard) {
     532         [ +  - ]:       82807 :             CheckMempoolEphemeralInvariants(tx_pool);
     533                 :             :         }
     534         [ +  - ]:      324168 :     }
     535                 :             : 
     536   [ +  -  +  - ]:        3328 :     node.validation_signals->UnregisterSharedValidationInterface(outpoints_updater);
     537                 :             : 
     538   [ +  +  +  -  :        4992 :     WITH_LOCK(::cs_main, tx_pool.check(chainstate.CoinsTip(), chainstate.m_chain.Height() + 1));
                   +  - ]
     539         [ +  - ]:        3328 : }
     540                 :             : } // namespace
        

Generated by: LCOV version 2.0-1