LCOV - code coverage report
Current view: top level - src/test/fuzz - rbf.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 99.2 % 130 129
Test Date: 2025-06-01 05:27:21 Functions: 100.0 % 6 6
Branches: 63.1 % 222 140

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2020-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 <node/mempool_args.h>
       6                 :             : #include <policy/rbf.h>
       7                 :             : #include <primitives/transaction.h>
       8                 :             : #include <sync.h>
       9                 :             : #include <test/fuzz/FuzzedDataProvider.h>
      10                 :             : #include <test/fuzz/fuzz.h>
      11                 :             : #include <test/fuzz/util.h>
      12                 :             : #include <test/fuzz/util/mempool.h>
      13                 :             : #include <test/util/setup_common.h>
      14                 :             : #include <test/util/txmempool.h>
      15                 :             : #include <txmempool.h>
      16                 :             : #include <util/check.h>
      17                 :             : #include <util/translation.h>
      18                 :             : 
      19                 :             : #include <cstdint>
      20                 :             : #include <optional>
      21                 :             : #include <string>
      22                 :             : #include <vector>
      23                 :             : 
      24                 :             : namespace {
      25                 :             : const BasicTestingSetup* g_setup;
      26                 :             : } // namespace
      27                 :             : 
      28                 :             : const int NUM_ITERS = 10000;
      29                 :             : 
      30                 :             : std::vector<COutPoint> g_outpoints;
      31                 :             : 
      32                 :           1 : void initialize_rbf()
      33                 :             : {
      34   [ +  -  +  - ]:           3 :     static const auto testing_setup = MakeNoLogFileContext<>();
      35                 :           1 :     g_setup = testing_setup.get();
      36         [ +  - ]:           2 : }
      37                 :             : 
      38                 :           1 : void initialize_package_rbf()
      39                 :             : {
      40   [ +  -  +  - ]:           2 :     static const auto testing_setup = MakeNoLogFileContext<>();
      41                 :           1 :     g_setup = testing_setup.get();
      42                 :             : 
      43                 :             :     // Create a fixed set of unique "UTXOs" to source parents from
      44                 :             :     // to avoid fuzzer giving circular references
      45         [ +  + ]:       10001 :     for (int i = 0; i < NUM_ITERS; ++i) {
      46                 :       10000 :         g_outpoints.emplace_back();
      47                 :       10000 :         g_outpoints.back().n = i;
      48                 :             :     }
      49                 :             : 
      50         [ +  - ]:           2 : }
      51                 :             : 
      52         [ +  - ]:        1027 : FUZZ_TARGET(rbf, .init = initialize_rbf)
      53                 :             : {
      54                 :         585 :     SeedRandomStateForTest(SeedRand::ZEROS);
      55                 :         585 :     FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
      56                 :         585 :     SetMockTime(ConsumeTime(fuzzed_data_provider));
      57                 :         585 :     std::optional<CMutableTransaction> mtx = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS);
      58         [ +  + ]:         585 :     if (!mtx) {
      59                 :          33 :         return;
      60                 :             :     }
      61                 :             : 
      62         [ +  - ]:         552 :     bilingual_str error;
      63   [ +  -  +  - ]:         552 :     CTxMemPool pool{MemPoolOptionsForTest(g_setup->m_node), error};
      64         [ +  - ]:         552 :     Assert(error.empty());
      65                 :             : 
      66   [ +  +  +  + ]:      294908 :     LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), NUM_ITERS)
      67                 :             :     {
      68                 :      294580 :         const std::optional<CMutableTransaction> another_mtx = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS);
      69         [ +  + ]:      294580 :         if (!another_mtx) {
      70                 :             :             break;
      71                 :             :         }
      72         [ +  - ]:      294356 :         const CTransaction another_tx{*another_mtx};
      73   [ +  +  +  + ]:      294356 :         if (fuzzed_data_provider.ConsumeBool() && !mtx->vin.empty()) {
      74                 :       19314 :             mtx->vin[0].prevout = COutPoint{another_tx.GetHash(), 0};
      75                 :             :         }
      76   [ +  -  +  - ]:      294356 :         LOCK2(cs_main, pool.cs);
      77   [ +  -  +  + ]:      294356 :         if (!pool.GetIter(another_tx.GetHash())) {
      78         [ +  - ]:       90175 :             AddToMempool(pool, ConsumeTxMemPoolEntry(fuzzed_data_provider, another_tx));
      79                 :             :         }
      80   [ +  -  +  - ]:     1177648 :     }
      81         [ +  - ]:         552 :     const CTransaction tx{*mtx};
      82         [ +  + ]:         552 :     if (fuzzed_data_provider.ConsumeBool()) {
      83   [ +  -  +  - ]:         262 :         LOCK2(cs_main, pool.cs);
      84   [ +  -  +  + ]:         262 :         if (!pool.GetIter(tx.GetHash())) {
      85         [ +  - ]:         214 :             AddToMempool(pool, ConsumeTxMemPoolEntry(fuzzed_data_provider, tx));
      86                 :             :         }
      87         [ +  - ]:         524 :     }
      88                 :         552 :     {
      89         [ +  - ]:         552 :         LOCK(pool.cs);
      90         [ +  - ]:         552 :         (void)IsRBFOptIn(tx, pool);
      91                 :         552 :     }
      92         [ +  - ]:        1689 : }
      93                 :             : 
      94         [ +  - ]:        1145 : FUZZ_TARGET(package_rbf, .init = initialize_package_rbf)
      95                 :             : {
      96                 :         703 :     SeedRandomStateForTest(SeedRand::ZEROS);
      97                 :         703 :     FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
      98                 :         703 :     SetMockTime(ConsumeTime(fuzzed_data_provider));
      99                 :             : 
     100                 :             :     // "Real" virtual size is not important for this test since ConsumeTxMemPoolEntry generates its own virtual size values
     101                 :             :     // so we construct small transactions for performance reasons. Child simply needs an input for later to perhaps connect to parent.
     102                 :         703 :     CMutableTransaction child;
     103         [ +  - ]:         703 :     child.vin.resize(1);
     104                 :             : 
     105         [ +  - ]:         703 :     bilingual_str error;
     106   [ +  -  +  - ]:         703 :     CTxMemPool pool{MemPoolOptionsForTest(g_setup->m_node), error};
     107         [ +  - ]:         703 :     Assert(error.empty());
     108                 :             : 
     109                 :             :     // Add a bunch of parent-child pairs to the mempool, and remember them.
     110                 :         703 :     std::vector<CTransaction> mempool_txs;
     111                 :         703 :     uint32_t iter{0};
     112                 :             : 
     113                 :             :     // Keep track of the total vsize of CTxMemPoolEntry's being added to the mempool to avoid overflow
     114                 :             :     // Add replacement_vsize since this is added to new diagram during RBF check
     115                 :         703 :     std::optional<CMutableTransaction> replacement_tx = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS);
     116         [ +  + ]:         703 :     if (!replacement_tx) {
     117                 :          82 :         return;
     118                 :             :     }
     119         [ +  - ]:         621 :     replacement_tx->vin.resize(1);
     120   [ +  -  +  - ]:         621 :     replacement_tx->vin[0].prevout = g_outpoints.at(iter++);
     121         [ +  - ]:         621 :     CTransaction replacement_tx_final{*replacement_tx};
     122                 :         621 :     auto replacement_entry = ConsumeTxMemPoolEntry(fuzzed_data_provider, replacement_tx_final);
     123         [ +  - ]:         621 :     int32_t replacement_vsize = replacement_entry.GetTxSize();
     124                 :         621 :     int64_t running_vsize_total{replacement_vsize};
     125                 :             : 
     126   [ +  -  +  - ]:         621 :     LOCK2(cs_main, pool.cs);
     127                 :             : 
     128         [ +  + ]:      594533 :     while (fuzzed_data_provider.ConsumeBool()) {
     129         [ +  + ]:      593951 :         if (iter >= NUM_ITERS) break;
     130                 :             : 
     131                 :             :         // Make sure txns only have one input, and that a unique input is given to avoid circular references
     132         [ +  - ]:      593949 :         CMutableTransaction parent;
     133         [ +  - ]:      593949 :         parent.vin.resize(1);
     134         [ +  - ]:      593949 :         parent.vin[0].prevout = g_outpoints.at(iter++);
     135         [ +  - ]:      593949 :         parent.vout.emplace_back(0, CScript());
     136                 :             : 
     137         [ +  - ]:      593949 :         mempool_txs.emplace_back(parent);
     138                 :      593949 :         const auto parent_entry = ConsumeTxMemPoolEntry(fuzzed_data_provider, mempool_txs.back());
     139         [ +  - ]:      593949 :         running_vsize_total += parent_entry.GetTxSize();
     140         [ +  + ]:      593949 :         if (running_vsize_total > std::numeric_limits<int32_t>::max()) {
     141                 :             :             // We aren't adding this final tx to mempool, so we don't want to conflict with it
     142                 :          22 :             mempool_txs.pop_back();
     143                 :          22 :             break;
     144                 :             :         }
     145   [ +  -  -  + ]:      593927 :         assert(!pool.GetIter(parent_entry.GetTx().GetHash()));
     146         [ +  - ]:      593927 :         AddToMempool(pool, parent_entry);
     147         [ +  + ]:      593927 :         if (fuzzed_data_provider.ConsumeBool()) {
     148                 :      571371 :             child.vin[0].prevout = COutPoint{mempool_txs.back().GetHash(), 0};
     149                 :             :         }
     150         [ +  - ]:      593927 :         mempool_txs.emplace_back(child);
     151                 :      593927 :         const auto child_entry = ConsumeTxMemPoolEntry(fuzzed_data_provider, mempool_txs.back());
     152         [ +  - ]:      593927 :         running_vsize_total += child_entry.GetTxSize();
     153         [ +  + ]:      593927 :         if (running_vsize_total > std::numeric_limits<int32_t>::max()) {
     154                 :             :             // We aren't adding this final tx to mempool, so we don't want to conflict with it
     155                 :          15 :             mempool_txs.pop_back();
     156                 :          15 :             break;
     157                 :             :         }
     158   [ +  -  +  + ]:      593912 :         if (!pool.GetIter(child_entry.GetTx().GetHash())) {
     159         [ +  - ]:      571429 :             AddToMempool(pool, child_entry);
     160                 :             :         }
     161                 :             : 
     162         [ +  + ]:      593912 :         if (fuzzed_data_provider.ConsumeBool()) {
     163         [ +  - ]:      591465 :             pool.PrioritiseTransaction(mempool_txs.back().GetHash().ToUint256(), fuzzed_data_provider.ConsumeIntegralInRange<int32_t>(-100000, 100000));
     164                 :             :         }
     165                 :     1187898 :     }
     166                 :             : 
     167                 :             :     // Pick some transactions at random to be the direct conflicts
     168                 :         621 :     CTxMemPool::setEntries direct_conflicts;
     169         [ +  + ]:     1188460 :     for (auto& tx : mempool_txs) {
     170         [ +  + ]:     1187839 :         if (fuzzed_data_provider.ConsumeBool()) {
     171   [ +  -  +  - ]:      852757 :             direct_conflicts.insert(*pool.GetIter(tx.GetHash()));
     172                 :             :         }
     173                 :             :     }
     174                 :             : 
     175                 :             :     // Calculate all conflicts:
     176                 :         621 :     CTxMemPool::setEntries all_conflicts;
     177         [ +  + ]:      843667 :     for (auto& txiter : direct_conflicts) {
     178         [ +  - ]:      843046 :         pool.CalculateDescendants(txiter, all_conflicts);
     179                 :             :     }
     180                 :             : 
     181                 :         621 :     CAmount replacement_fees = ConsumeMoney(fuzzed_data_provider);
     182         [ +  - ]:         621 :     auto changeset = pool.GetChangeSet();
     183         [ +  + ]:      868407 :     for (auto& txiter : all_conflicts) {
     184         [ +  - ]:      867786 :         changeset->StageRemoval(txiter);
     185                 :             :     }
     186   [ +  -  +  - ]:        1242 :     changeset->StageAddition(replacement_entry.GetSharedTx(), replacement_fees,
     187         [ +  - ]:         621 :             replacement_entry.GetTime().count(), replacement_entry.GetHeight(),
     188         [ +  - ]:         621 :             replacement_entry.GetSequence(), replacement_entry.GetSpendsCoinbase(),
     189         [ +  - ]:         621 :             replacement_entry.GetSigOpCost(), replacement_entry.GetLockPoints());
     190                 :             :     // Calculate the chunks for a replacement.
     191         [ +  - ]:         621 :     auto calc_results{changeset->CalculateChunksForRBF()};
     192                 :             : 
     193         [ +  - ]:         621 :     if (calc_results.has_value()) {
     194                 :             :         // Sanity checks on the chunks.
     195                 :             : 
     196                 :             :         // Feerates are monotonically decreasing.
     197                 :         621 :         FeeFrac first_sum;
     198         [ +  + ]:      670304 :         for (size_t i = 0; i < calc_results->first.size(); ++i) {
     199         [ +  + ]:      669683 :             first_sum += calc_results->first[i];
     200   [ +  +  -  + ]:      669683 :             if (i) assert(!(calc_results->first[i - 1] << calc_results->first[i]));
     201                 :             :         }
     202                 :         621 :         FeeFrac second_sum;
     203         [ +  + ]:       53432 :         for (size_t i = 0; i < calc_results->second.size(); ++i) {
     204         [ +  + ]:       52811 :             second_sum += calc_results->second[i];
     205   [ +  +  -  + ]:       52811 :             if (i) assert(!(calc_results->second[i - 1] << calc_results->second[i]));
     206                 :             :         }
     207                 :             : 
     208                 :         621 :         FeeFrac replaced;
     209         [ +  + ]:      868407 :         for (auto txiter : all_conflicts) {
     210         [ +  - ]:      867786 :             replaced.fee += txiter->GetModifiedFee();
     211         [ +  - ]:      867786 :             replaced.size += txiter->GetTxSize();
     212                 :             :         }
     213                 :             :         // The total fee & size of the new diagram minus replaced fee & size should be the total
     214                 :             :         // fee & size of the old diagram minus replacement fee & size.
     215         [ +  - ]:         621 :         assert((first_sum - replaced) == (second_sum - FeeFrac{replacement_fees, replacement_vsize}));
     216                 :             :     }
     217                 :             : 
     218                 :             :     // If internals report error, wrapper should too
     219         [ +  - ]:         621 :     auto err_tuple{ImprovesFeerateDiagram(*changeset)};
     220         [ -  + ]:         621 :     if (!calc_results.has_value()) {
     221   [ #  #  #  # ]:           0 :          assert(err_tuple.value().first == DiagramCheckError::UNCALCULABLE);
     222                 :             :     } else {
     223                 :             :         // Diagram check succeeded
     224                 :         621 :         auto old_sum = std::accumulate(calc_results->first.begin(), calc_results->first.end(), FeeFrac{});
     225                 :         621 :         auto new_sum = std::accumulate(calc_results->second.begin(), calc_results->second.end(), FeeFrac{});
     226         [ +  + ]:         621 :         if (!err_tuple.has_value()) {
     227                 :             :             // New diagram's final fee should always match or exceed old diagram's
     228         [ -  + ]:          54 :             assert(old_sum.fee <= new_sum.fee);
     229         [ +  + ]:         567 :         } else if (old_sum.fee > new_sum.fee) {
     230                 :             :             // Or it failed, and if old diagram had higher fees, it should be a failure
     231         [ -  + ]:         392 :             assert(err_tuple.value().first == DiagramCheckError::FAILURE);
     232                 :             :         }
     233                 :             :     }
     234   [ +  -  +  -  :        4593 : }
                   +  - ]
        

Generated by: LCOV version 2.0-1