Bitcoin Core  24.1.0
P2P Digital Currency
mempool_stress.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2021 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 <bench/bench.h>
6 #include <policy/policy.h>
8 #include <txmempool.h>
9 #include <validation.h>
10 
11 #include <vector>
12 
13 static void AddTx(const CTransactionRef& tx, CTxMemPool& pool) EXCLUSIVE_LOCKS_REQUIRED(cs_main, pool.cs)
14 {
15  int64_t nTime = 0;
16  unsigned int nHeight = 1;
17  bool spendsCoinbase = false;
18  unsigned int sigOpCost = 4;
20  pool.addUnchecked(CTxMemPoolEntry(tx, 1000, nTime, nHeight, spendsCoinbase, sigOpCost, lp));
21 }
22 
23 struct Available {
25  size_t vin_left{0};
26  size_t tx_count;
28 };
29 
30 static std::vector<CTransactionRef> CreateOrderedCoins(FastRandomContext& det_rand, int childTxs, int min_ancestors)
31 {
32  std::vector<Available> available_coins;
33  std::vector<CTransactionRef> ordered_coins;
34  // Create some base transactions
35  size_t tx_counter = 1;
36  for (auto x = 0; x < 100; ++x) {
38  tx.vin.resize(1);
39  tx.vin[0].scriptSig = CScript() << CScriptNum(tx_counter);
40  tx.vin[0].scriptWitness.stack.push_back(CScriptNum(x).getvch());
41  tx.vout.resize(det_rand.randrange(10)+2);
42  for (auto& out : tx.vout) {
43  out.scriptPubKey = CScript() << CScriptNum(tx_counter) << OP_EQUAL;
44  out.nValue = 10 * COIN;
45  }
46  ordered_coins.emplace_back(MakeTransactionRef(tx));
47  available_coins.emplace_back(ordered_coins.back(), tx_counter++);
48  }
49  for (auto x = 0; x < childTxs && !available_coins.empty(); ++x) {
51  size_t n_ancestors = det_rand.randrange(10)+1;
52  for (size_t ancestor = 0; ancestor < n_ancestors && !available_coins.empty(); ++ancestor){
53  size_t idx = det_rand.randrange(available_coins.size());
54  Available coin = available_coins[idx];
55  uint256 hash = coin.ref->GetHash();
56  // biased towards taking min_ancestors parents, but maybe more
57  size_t n_to_take = det_rand.randrange(2) == 0 ?
58  min_ancestors :
59  min_ancestors + det_rand.randrange(coin.ref->vout.size() - coin.vin_left);
60  for (size_t i = 0; i < n_to_take; ++i) {
61  tx.vin.emplace_back();
62  tx.vin.back().prevout = COutPoint(hash, coin.vin_left++);
63  tx.vin.back().scriptSig = CScript() << coin.tx_count;
64  tx.vin.back().scriptWitness.stack.push_back(CScriptNum(coin.tx_count).getvch());
65  }
66  if (coin.vin_left == coin.ref->vin.size()) {
67  coin = available_coins.back();
68  available_coins.pop_back();
69  }
70  tx.vout.resize(det_rand.randrange(10)+2);
71  for (auto& out : tx.vout) {
72  out.scriptPubKey = CScript() << CScriptNum(tx_counter) << OP_EQUAL;
73  out.nValue = 10 * COIN;
74  }
75  }
76  ordered_coins.emplace_back(MakeTransactionRef(tx));
77  available_coins.emplace_back(ordered_coins.back(), tx_counter++);
78  }
79  return ordered_coins;
80 }
81 
82 static void ComplexMemPool(benchmark::Bench& bench)
83 {
84  FastRandomContext det_rand{true};
85  int childTxs = 800;
86  if (bench.complexityN() > 1) {
87  childTxs = static_cast<int>(bench.complexityN());
88  }
89  std::vector<CTransactionRef> ordered_coins = CreateOrderedCoins(det_rand, childTxs, /*min_ancestors=*/1);
90  const auto testing_setup = MakeNoLogFileContext<const TestingSetup>(CBaseChainParams::MAIN);
91  CTxMemPool& pool = *testing_setup.get()->m_node.mempool;
92  LOCK2(cs_main, pool.cs);
93  bench.run([&]() NO_THREAD_SAFETY_ANALYSIS {
94  for (auto& tx : ordered_coins) {
95  AddTx(tx, pool);
96  }
97  pool.TrimToSize(pool.DynamicMemoryUsage() * 3 / 4);
98  pool.TrimToSize(GetVirtualTransactionSize(*ordered_coins.front()));
99  });
100 }
101 
102 static void MempoolCheck(benchmark::Bench& bench)
103 {
104  FastRandomContext det_rand{true};
105  auto testing_setup = MakeNoLogFileContext<TestChain100Setup>(CBaseChainParams::REGTEST, {"-checkmempool=1"});
106  CTxMemPool& pool = *testing_setup.get()->m_node.mempool;
107  LOCK2(cs_main, pool.cs);
108  testing_setup->PopulateMempool(det_rand, 400, true);
109  const CCoinsViewCache& coins_tip = testing_setup.get()->m_node.chainman->ActiveChainstate().CoinsTip();
110 
111  bench.run([&]() NO_THREAD_SAFETY_ANALYSIS {
112  // Bump up the spendheight so we don't hit premature coinbase spend errors.
113  pool.check(coins_tip, /*spendheight=*/300);
114  });
115 }
116 
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:414
#define NO_THREAD_SAFETY_ANALYSIS
Definition: threadsafety.h:51
static const std::string REGTEST
std::vector< CTxIn > vin
Definition: transaction.h:374
int64_t GetVirtualTransactionSize(int64_t nWeight, int64_t nSigOpCost, unsigned int bytes_per_sigop)
Compute the virtual transaction size (weight reinterpreted as bytes).
Definition: policy.cpp:295
LockPoints lp
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
Definition: txmempool.h:88
static const std::string MAIN
Chain name strings.
Available(CTransactionRef &ref, size_t tx_count)
#define LOCK2(cs1, cs2)
Definition: sync.h:262
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1183
BENCHMARK(ComplexMemPool)
static std::vector< CTransactionRef > CreateOrderedCoins(FastRandomContext &det_rand, int childTxs, int min_ancestors)
Fast randomness source.
Definition: random.h:142
Bench & complexityN(T b) noexcept
Definition: nanobench.h:1214
size_t tx_count
std::vector< unsigned char > getvch() const
Definition: script.h:340
static void MempoolCheck(benchmark::Bench &bench)
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:34
std::vector< CTxOut > vout
Definition: transaction.h:375
static CTransactionRef MakeTransactionRef(Tx &&txIn)
Definition: transaction.h:415
256-bit opaque blob.
Definition: uint256.h:119
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
unsigned int nHeight
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:431
CTransactionRef ref
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:410
static void ComplexMemPool(benchmark::Bench &bench)
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate...
Definition: validation.cpp:121
A mutable version of CTransaction.
Definition: transaction.h:372
CTransactionRef get(const uint256 &hash) const
Definition: txmempool.cpp:887
size_t vin_left
Main entry point to nanobench&#39;s benchmarking facility.
Definition: nanobench.h:616
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:212
uint64_t randrange(uint64_t range) noexcept
Generate a random integer in the range [0..range).
Definition: random.h:213
unsigned int sigOpCost
bool spendsCoinbase
static constexpr CAmount COIN
The amount of satoshis in one BTC.
Definition: amount.h:15
static void AddTx(const CTransactionRef &tx, CTxMemPool &pool) EXCLUSIVE_LOCKS_REQUIRED(cs_main