mempool_stress.cpp raw

   1  // Copyright (c) 2011-2022 The Limenka 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 <bench/bench.h>
   6  #include <consensus/amount.h>
   7  #include <policy/policy.h>
   8  #include <primitives/transaction.h>
   9  #include <random.h>
  10  #include <script/script.h>
  11  #include <sync.h>
  12  #include <test/util/setup_common.h>
  13  #include <test/util/txmempool.h>
  14  #include <txmempool.h>
  15  #include <validation.h>
  16  
  17  #include <cstddef>
  18  #include <cstdint>
  19  #include <memory>
  20  #include <vector>
  21  
  22  class CCoinsViewCache;
  23  
  24  static void AddTx(const CTransactionRef& tx, CTxMemPool& pool) EXCLUSIVE_LOCKS_REQUIRED(cs_main, pool.cs)
  25  {
  26      int64_t nTime = 0;
  27      constexpr uint64_t coin_age{10};
  28      unsigned int nHeight = 1;
  29      uint64_t sequence = 0;
  30      bool spendsCoinbase = false;
  31      unsigned int sigOpCost = 4;
  32      LockPoints lp;
  33      AddToMempool(pool, CTxMemPoolEntry(tx, 1000, nTime, nHeight, sequence, {
  34          .inputs_coin_age = coin_age,
  35          .in_chain_input_value = tx->GetValueOut(),
  36      }, spendsCoinbase, /*extra_weight=*/0, sigOpCost, lp));
  37  }
  38  
  39  struct Available {
  40      CTransactionRef ref;
  41      size_t vin_left{0};
  42      size_t tx_count;
  43      Available(CTransactionRef& ref, size_t tx_count) : ref(ref), tx_count(tx_count){}
  44  };
  45  
  46  static std::vector<CTransactionRef> CreateOrderedCoins(FastRandomContext& det_rand, int childTxs, int min_ancestors)
  47  {
  48      std::vector<Available> available_coins;
  49      std::vector<CTransactionRef> ordered_coins;
  50      // Create some base transactions
  51      size_t tx_counter = 1;
  52      for (auto x = 0; x < 100; ++x) {
  53          CMutableTransaction tx = CMutableTransaction();
  54          tx.vin.resize(1);
  55          tx.vin[0].scriptSig = CScript() << CScriptNum(tx_counter);
  56          tx.vin[0].scriptWitness.stack.push_back(CScriptNum(x).getvch());
  57          tx.vout.resize(det_rand.randrange(10)+2);
  58          for (auto& out : tx.vout) {
  59              out.scriptPubKey = CScript() << CScriptNum(tx_counter) << OP_EQUAL;
  60              out.nValue = 10 * COIN;
  61          }
  62          ordered_coins.emplace_back(MakeTransactionRef(tx));
  63          available_coins.emplace_back(ordered_coins.back(), tx_counter++);
  64      }
  65      for (auto x = 0; x < childTxs && !available_coins.empty(); ++x) {
  66          CMutableTransaction tx = CMutableTransaction();
  67          size_t n_ancestors = det_rand.randrange(10)+1;
  68          for (size_t ancestor = 0; ancestor < n_ancestors && !available_coins.empty(); ++ancestor){
  69              size_t idx = det_rand.randrange(available_coins.size());
  70              Available coin = available_coins[idx];
  71              Txid hash = coin.ref->GetHash();
  72              // biased towards taking min_ancestors parents, but maybe more
  73              size_t n_to_take = det_rand.randrange(2) == 0 ?
  74                                 min_ancestors :
  75                                 min_ancestors + det_rand.randrange(coin.ref->vout.size() - coin.vin_left);
  76              for (size_t i = 0; i < n_to_take; ++i) {
  77                  tx.vin.emplace_back();
  78                  tx.vin.back().prevout = COutPoint(hash, coin.vin_left++);
  79                  tx.vin.back().scriptSig = CScript() << coin.tx_count;
  80                  tx.vin.back().scriptWitness.stack.push_back(CScriptNum(coin.tx_count).getvch());
  81              }
  82              if (coin.vin_left == coin.ref->vin.size()) {
  83                  coin = available_coins.back();
  84                  available_coins.pop_back();
  85              }
  86              tx.vout.resize(det_rand.randrange(10)+2);
  87              for (auto& out : tx.vout) {
  88                  out.scriptPubKey = CScript() << CScriptNum(tx_counter) << OP_EQUAL;
  89                  out.nValue = 10 * COIN;
  90              }
  91          }
  92          ordered_coins.emplace_back(MakeTransactionRef(tx));
  93          available_coins.emplace_back(ordered_coins.back(), tx_counter++);
  94      }
  95      return ordered_coins;
  96  }
  97  
  98  static void ComplexMemPool(benchmark::Bench& bench)
  99  {
 100      FastRandomContext det_rand{true};
 101      int childTxs = 800;
 102      if (bench.complexityN() > 1) {
 103          childTxs = static_cast<int>(bench.complexityN());
 104      }
 105      std::vector<CTransactionRef> ordered_coins = CreateOrderedCoins(det_rand, childTxs, /*min_ancestors=*/1);
 106      const auto testing_setup = MakeNoLogFileContext<const TestingSetup>(ChainType::MAIN);
 107      CTxMemPool& pool = *testing_setup.get()->m_node.mempool;
 108      LOCK2(cs_main, pool.cs);
 109      bench.run([&]() NO_THREAD_SAFETY_ANALYSIS {
 110          for (auto& tx : ordered_coins) {
 111              AddTx(tx, pool);
 112          }
 113          pool.TrimToSize(pool.DynamicMemoryUsage() * 3 / 4);
 114          pool.TrimToSize(GetVirtualTransactionSize(*ordered_coins.front()));
 115      });
 116  }
 117  
 118  static void MempoolCheck(benchmark::Bench& bench)
 119  {
 120      FastRandomContext det_rand{true};
 121      auto testing_setup = MakeNoLogFileContext<TestChain100Setup>(ChainType::REGTEST, {.extra_args = {"-checkmempool=1"}});
 122      CTxMemPool& pool = *testing_setup.get()->m_node.mempool;
 123      LOCK2(cs_main, pool.cs);
 124      testing_setup->PopulateMempool(det_rand, 400, true);
 125      const CCoinsViewCache& coins_tip = testing_setup.get()->m_node.chainman->ActiveChainstate().CoinsTip();
 126  
 127      bench.run([&]() NO_THREAD_SAFETY_ANALYSIS {
 128          // Bump up the spendheight so we don't hit premature coinbase spend errors.
 129          pool.check(coins_tip, /*spendheight=*/300);
 130      });
 131  }
 132  
 133  BENCHMARK(ComplexMemPool, benchmark::PriorityLevel::HIGH);
 134  BENCHMARK(MempoolCheck, benchmark::PriorityLevel::HIGH);
 135