Bitcoin Core  21.99.0
P2P Digital Currency
mempool_stress.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2020 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>
7 #include <test/util/setup_common.h>
8 #include <txmempool.h>
9 
10 #include <vector>
11 
12 static void AddTx(const CTransactionRef& tx, CTxMemPool& pool) EXCLUSIVE_LOCKS_REQUIRED(cs_main, pool.cs)
13 {
14  int64_t nTime = 0;
15  unsigned int nHeight = 1;
16  bool spendsCoinbase = false;
17  unsigned int sigOpCost = 4;
19  pool.addUnchecked(CTxMemPoolEntry(tx, 1000, nTime, nHeight, spendsCoinbase, sigOpCost, lp));
20 }
21 
22 struct Available {
24  size_t vin_left{0};
25  size_t tx_count;
27 };
28 
29 static void ComplexMemPool(benchmark::Bench& bench)
30 {
31  int childTxs = 800;
32  if (bench.complexityN() > 1) {
33  childTxs = static_cast<int>(bench.complexityN());
34  }
35 
36  FastRandomContext det_rand{true};
37  std::vector<Available> available_coins;
38  std::vector<CTransactionRef> ordered_coins;
39  // Create some base transactions
40  size_t tx_counter = 1;
41  for (auto x = 0; x < 100; ++x) {
43  tx.vin.resize(1);
44  tx.vin[0].scriptSig = CScript() << CScriptNum(tx_counter);
45  tx.vin[0].scriptWitness.stack.push_back(CScriptNum(x).getvch());
46  tx.vout.resize(det_rand.randrange(10)+2);
47  for (auto& out : tx.vout) {
48  out.scriptPubKey = CScript() << CScriptNum(tx_counter) << OP_EQUAL;
49  out.nValue = 10 * COIN;
50  }
51  ordered_coins.emplace_back(MakeTransactionRef(tx));
52  available_coins.emplace_back(ordered_coins.back(), tx_counter++);
53  }
54  for (auto x = 0; x < childTxs && !available_coins.empty(); ++x) {
56  size_t n_ancestors = det_rand.randrange(10)+1;
57  for (size_t ancestor = 0; ancestor < n_ancestors && !available_coins.empty(); ++ancestor){
58  size_t idx = det_rand.randrange(available_coins.size());
59  Available coin = available_coins[idx];
60  uint256 hash = coin.ref->GetHash();
61  // biased towards taking just one ancestor, but maybe more
62  size_t n_to_take = det_rand.randrange(2) == 0 ? 1 : 1+det_rand.randrange(coin.ref->vout.size() - coin.vin_left);
63  for (size_t i = 0; i < n_to_take; ++i) {
64  tx.vin.emplace_back();
65  tx.vin.back().prevout = COutPoint(hash, coin.vin_left++);
66  tx.vin.back().scriptSig = CScript() << coin.tx_count;
67  tx.vin.back().scriptWitness.stack.push_back(CScriptNum(coin.tx_count).getvch());
68  }
69  if (coin.vin_left == coin.ref->vin.size()) {
70  coin = available_coins.back();
71  available_coins.pop_back();
72  }
73  tx.vout.resize(det_rand.randrange(10)+2);
74  for (auto& out : tx.vout) {
75  out.scriptPubKey = CScript() << CScriptNum(tx_counter) << OP_EQUAL;
76  out.nValue = 10 * COIN;
77  }
78  }
79  ordered_coins.emplace_back(MakeTransactionRef(tx));
80  available_coins.emplace_back(ordered_coins.back(), tx_counter++);
81  }
82  const auto testing_setup = MakeNoLogFileContext<const TestingSetup>(CBaseChainParams::MAIN);
83  CTxMemPool pool;
84  LOCK2(cs_main, pool.cs);
85  bench.run([&]() NO_THREAD_SAFETY_ANALYSIS {
86  for (auto& tx : ordered_coins) {
87  AddTx(tx, pool);
88  }
89  pool.TrimToSize(pool.DynamicMemoryUsage() * 3 / 4);
90  pool.TrimToSize(GetVirtualTransactionSize(*ordered_coins.front()));
91  });
92 }
93 
LOCK2
#define LOCK2(cs1, cs2)
Definition: sync.h:233
policy.h
CMutableTransaction::vin
std::vector< CTxIn > vin
Definition: transaction.h:346
Available
Definition: mempool_stress.cpp:22
CTxMemPool
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:477
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:1054
ankerl::nanobench::Bench
Main entry point to nanobench's benchmarking facility.
Definition: nanobench.h:614
MakeTransactionRef
static CTransactionRef MakeTransactionRef(Tx &&txIn)
Definition: transaction.h:387
CTransactionRef
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:386
txmempool.h
lp
LockPoints lp
Definition: mempool_stress.cpp:18
CScriptNum
Definition: script.h:218
Available::Available
Available(CTransactionRef &ref, size_t tx_count)
Definition: mempool_stress.cpp:26
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:565
sigOpCost
unsigned int sigOpCost
Definition: mempool_stress.cpp:17
spendsCoinbase
bool spendsCoinbase
Definition: mempool_stress.cpp:16
ankerl::nanobench::Bench::run
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1181
Available::tx_count
size_t tx_count
Definition: mempool_stress.cpp:25
uint256
256-bit opaque blob.
Definition: uint256.h:124
CScript
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:404
bench.h
CScriptNum::getvch
std::vector< unsigned char > getvch() const
Definition: script.h:334
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:1212
Available::ref
CTransactionRef ref
Definition: mempool_stress.cpp:23
cs_main
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: validation.cpp:114
EXCLUSIVE_LOCKS_REQUIRED
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
CTxMemPoolEntry
Definition: txmempool.h:81
ComplexMemPool
static void ComplexMemPool(benchmark::Bench &bench)
Definition: mempool_stress.cpp:29
LockPoints
Definition: txmempool.h:39
OP_EQUAL
@ OP_EQUAL
Definition: script.h:138
Available::vin_left
size_t vin_left
Definition: mempool_stress.cpp:24
nHeight
unsigned int nHeight
Definition: mempool_stress.cpp:15
AddTx
static void AddTx(const CTransactionRef &tx, CTxMemPool &pool) EXCLUSIVE_LOCKS_REQUIRED(cs_main
COIN
static const CAmount COIN
Definition: amount.h:14
CTxMemPool::DynamicMemoryUsage
size_t DynamicMemoryUsage() const
Definition: txmempool.cpp:951
COutPoint
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:26
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:280
FastRandomContext
Fast randomness source.
Definition: random.h:119