Bitcoin ABC  0.24.7
P2P Digital Currency
mempool_stress.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2019 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 <random.h>
8 #include <test/util/setup_common.h>
9 #include <txmempool.h>
10 
11 #include <vector>
12 
13 static void AddTx(const CTransactionRef &tx, CTxMemPool &pool)
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 * SATOSHI, nTime, nHeight,
22 }
23 
24 struct Available {
26  size_t vin_left{0};
27  size_t tx_count;
28  Available(CTransactionRef &_ref, size_t _tx_count)
29  : ref(_ref), tx_count(_tx_count) {}
30 };
31 
32 static void ComplexMemPool(benchmark::Bench &bench) {
33  int childTxs = 800;
34  if (bench.complexityN() > 1) {
35  childTxs = static_cast<int>(bench.complexityN());
36  }
37 
38  FastRandomContext det_rand{true};
39  std::vector<Available> available_coins;
40  std::vector<CTransactionRef> ordered_coins;
41  // Create some base transactions
42  size_t tx_counter = 1;
43  for (auto x = 0; x < 100; ++x) {
45  tx.vin.resize(1);
46  tx.vin[0].scriptSig = CScript() << CScriptNum(tx_counter);
47  tx.vout.resize(det_rand.randrange(10) + 2);
48  for (auto &out : tx.vout) {
49  out.scriptPubKey = CScript() << CScriptNum(tx_counter) << OP_EQUAL;
50  out.nValue = 10 * COIN;
51  }
52  ordered_coins.emplace_back(MakeTransactionRef(tx));
53  available_coins.emplace_back(ordered_coins.back(), tx_counter++);
54  }
55  for (auto x = 0; x < childTxs && !available_coins.empty(); ++x) {
57  size_t n_ancestors = det_rand.randrange(10) + 1;
58  for (size_t ancestor = 0;
59  ancestor < n_ancestors && !available_coins.empty(); ++ancestor) {
60  size_t idx = det_rand.randrange(available_coins.size());
61  Available coin = available_coins[idx];
62  TxId txid = coin.ref->GetId();
63  // biased towards taking just one ancestor, but maybe more
64  size_t n_to_take =
65  det_rand.randrange(2) == 0
66  ? 1
67  : 1 + det_rand.randrange(coin.ref->vout.size() -
68  coin.vin_left);
69  for (size_t i = 0; i < n_to_take; ++i) {
70  tx.vin.emplace_back();
71  tx.vin.back().prevout = COutPoint(txid, coin.vin_left++);
72  tx.vin.back().scriptSig = CScript() << coin.tx_count;
73  }
74  if (coin.vin_left == coin.ref->vin.size()) {
75  coin = available_coins.back();
76  available_coins.pop_back();
77  }
78  tx.vout.resize(det_rand.randrange(10) + 2);
79  for (auto &out : tx.vout) {
80  out.scriptPubKey = CScript()
81  << CScriptNum(tx_counter) << OP_EQUAL;
82  out.nValue = 10 * COIN;
83  }
84  }
85  ordered_coins.emplace_back(MakeTransactionRef(tx));
86  available_coins.emplace_back(ordered_coins.back(), tx_counter++);
87  }
88  TestingSetup test_setup;
89  CTxMemPool pool;
90  LOCK2(cs_main, pool.cs);
91  bench.run([&]() NO_THREAD_SAFETY_ANALYSIS {
92  for (auto &tx : ordered_coins) {
93  AddTx(tx, pool);
94  }
95  pool.TrimToSize(pool.DynamicMemoryUsage() * 3 / 4);
96  pool.TrimToSize(GetVirtualTransactionSize(*ordered_coins.front()));
97  });
98 }
99 
LOCK2
#define LOCK2(cs1, cs2)
Definition: sync.h:244
policy.h
MakeTransactionRef
static CTransactionRef MakeTransactionRef()
Definition: transaction.h:320
Available
Definition: mempool_stress.cpp:24
CTxMemPool
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:495
Available::Available
Available(CTransactionRef &_ref, size_t _tx_count)
Definition: mempool_stress.cpp:28
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:1202
ankerl::nanobench::Bench
Main entry point to nanobench's benchmarking facility.
Definition: nanobench.h:583
COIN
static constexpr Amount COIN
Definition: amount.h:154
txmempool.h
lp
LockPoints lp
Definition: mempool_stress.cpp:19
CScriptNum
Definition: script.h:215
SATOSHI
static constexpr Amount SATOSHI
Definition: amount.h:153
NO_THREAD_SAFETY_ANALYSIS
#define NO_THREAD_SAFETY_ANALYSIS
Definition: threadsafety.h:58
cs
static void pool cs
Definition: mempool_stress.cpp:14
random.h
CTxMemPool::cs
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it.
Definition: txmempool.h:577
CTxMemPool::addUnchecked
void addUnchecked(const CTxMemPoolEntry &entry) EXCLUSIVE_LOCKS_REQUIRED(cs
Definition: txmempool.cpp:1142
cs_main
RecursiveMutex cs_main
Global state.
Definition: validation.cpp:103
sigOpCost
unsigned int sigOpCost
Definition: mempool_stress.cpp:18
spendsCoinbase
bool spendsCoinbase
Definition: mempool_stress.cpp:17
ankerl::nanobench::Bench::run
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1134
Available::tx_count
size_t tx_count
Definition: mempool_stress.cpp:27
TxId
A TxId is the identifier of a transaction.
Definition: txid.h:14
CScript
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:430
bench.h
ankerl::nanobench::Bench::complexityN
Bench & complexityN(T b) noexcept
Definition: nanobench.h:1165
Available::ref
CTransactionRef ref
Definition: mempool_stress.cpp:25
CMutableTransaction::vout
std::vector< CTxOut > vout
Definition: transaction.h:281
EXCLUSIVE_LOCKS_REQUIRED
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:56
CTxMemPoolEntry
Definition: txmempool.h:79
ComplexMemPool
static void ComplexMemPool(benchmark::Bench &bench)
Definition: mempool_stress.cpp:32
LockPoints
Definition: txmempool.h:41
OP_EQUAL
@ OP_EQUAL
Definition: script.h:118
Available::vin_left
size_t vin_left
Definition: mempool_stress.cpp:26
CTransactionRef
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:319
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:1077
GetVirtualTransactionSize
int64_t GetVirtualTransactionSize(int64_t nSize, int64_t nSigOpCount, unsigned int bytes_per_sigop)
Compute the virtual transaction size (size, or more if sigops are too dense).
Definition: policy.cpp:162
COutPoint
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:22
BENCHMARK
BENCHMARK(ComplexMemPool)
CMutableTransaction::vin
std::vector< CTxIn > vin
Definition: transaction.h:280
FastRandomContext
Fast randomness source.
Definition: random.h:129
CMutableTransaction
A mutable version of CTransaction.
Definition: transaction.h:278