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