Bitcoin Core  22.99.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;
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  const int childTxs = bench.complexityN() > 1 ? static_cast<int>(bench.complexityN()) : 2000;
106  const std::vector<CTransactionRef> ordered_coins = CreateOrderedCoins(det_rand, childTxs, /* min_ancestors */ 5);
107  const auto testing_setup = MakeNoLogFileContext<const TestingSetup>(CBaseChainParams::MAIN, {"-checkmempool=1"});
108  CTxMemPool pool;
109  LOCK2(cs_main, pool.cs);
110  const CCoinsViewCache& coins_tip = testing_setup.get()->m_node.chainman->ActiveChainstate().CoinsTip();
111  for (auto& tx : ordered_coins) AddTx(tx, pool);
112 
113  bench.run([&]() NO_THREAD_SAFETY_ANALYSIS {
114  pool.check(coins_tip, /* spendheight */ 2);
115  });
116 }
117 
LOCK2
#define LOCK2(cs1, cs2)
Definition: sync.h:227
policy.h
CMutableTransaction::vin
std::vector< CTxIn > vin
Definition: transaction.h:346
Available
Definition: mempool_stress.cpp:23
setup_common.h
CTxMemPool
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:429
CTxMemPool::TrimToSize
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:1138
ankerl::nanobench::Bench
Main entry point to nanobench's benchmarking facility.
Definition: nanobench.h:616
MakeTransactionRef
static CTransactionRef MakeTransactionRef(Tx &&txIn)
Definition: transaction.h:387
CTransactionRef
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:386
CreateOrderedCoins
static std::vector< CTransactionRef > CreateOrderedCoins(FastRandomContext &det_rand, int childTxs, int min_ancestors)
Definition: mempool_stress.cpp:30
txmempool.h
lp
LockPoints lp
Definition: mempool_stress.cpp:19
CScriptNum
Definition: script.h:219
Available::Available
Available(CTransactionRef &ref, size_t tx_count)
Definition: mempool_stress.cpp:27
NO_THREAD_SAFETY_ANALYSIS
#define NO_THREAD_SAFETY_ANALYSIS
Definition: threadsafety.h:51
CTxMemPool::cs
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it.
Definition: txmempool.h:517
sigOpCost
unsigned int sigOpCost
Definition: mempool_stress.cpp:18
spendsCoinbase
bool spendsCoinbase
Definition: mempool_stress.cpp:17
OP_EQUAL
@ OP_EQUAL
Definition: script.h:139
ankerl::nanobench::Bench::run
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1183
MempoolCheck
static void MempoolCheck(benchmark::Bench &bench)
Definition: mempool_stress.cpp:102
Available::tx_count
size_t tx_count
Definition: mempool_stress.cpp:26
uint256
256-bit opaque blob.
Definition: uint256.h:124
CScript
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:405
bench.h
CScriptNum::getvch
std::vector< unsigned char > getvch() const
Definition: script.h:335
CBaseChainParams::MAIN
static const std::string MAIN
Chain name strings.
Definition: chainparamsbase.h:22
CMutableTransaction::vout
std::vector< CTxOut > vout
Definition: transaction.h:347
ankerl::nanobench::Bench::complexityN
Bench & complexityN(T b) noexcept
Definition: nanobench.h:1214
Available::ref
CTransactionRef ref
Definition: mempool_stress.cpp:24
cs_main
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: validation.cpp:138
CCoinsViewCache
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:213
EXCLUSIVE_LOCKS_REQUIRED
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
CTxMemPoolEntry
Definition: txmempool.h:85
ComplexMemPool
static void ComplexMemPool(benchmark::Bench &bench)
Definition: mempool_stress.cpp:82
LockPoints
Definition: txmempool.h:41
Available::vin_left
size_t vin_left
Definition: mempool_stress.cpp:25
FastRandomContext::randrange
uint64_t randrange(uint64_t range) noexcept
Generate a random integer in the range [0..range).
Definition: random.h:202
nHeight
unsigned int nHeight
Definition: mempool_stress.cpp:16
AddTx
static void AddTx(const CTransactionRef &tx, CTxMemPool &pool) EXCLUSIVE_LOCKS_REQUIRED(cs_main
CTxMemPool::DynamicMemoryUsage
size_t DynamicMemoryUsage() const
Definition: txmempool.cpp:1035
COutPoint
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:26
COIN
static constexpr CAmount COIN
The amount of satoshis in one BTC.
Definition: amount.h:15
CMutableTransaction
A mutable version of CTransaction.
Definition: transaction.h:344
BENCHMARK
BENCHMARK(ComplexMemPool)
GetVirtualTransactionSize
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:282
FastRandomContext
Fast randomness source.
Definition: random.h:131