Bitcoin ABC  0.26.3
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 #include <validation.h>
11 
12 #include <vector>
13 
14 static void AddTx(const CTransactionRef &tx, CTxMemPool &pool)
16  int64_t nTime = 0;
17  unsigned int nHeight = 1;
18  bool spendsCoinbase = false;
19  unsigned int sigChecks = 1;
21  pool.addUnchecked(CTxMemPoolEntry(tx, 1000 * SATOSHI, nTime, nHeight,
23 }
24 
25 struct Available {
27  size_t vin_left{0};
28  size_t tx_count;
29  Available(CTransactionRef &_ref, size_t _tx_count)
30  : ref(_ref), tx_count(_tx_count) {}
31 };
32 
33 static std::vector<CTransactionRef>
34 CreateOrderedCoins(FastRandomContext &det_rand, int childTxs,
35  int min_ancestors) {
36  std::vector<Available> available_coins;
37  std::vector<CTransactionRef> ordered_coins;
38  // Create some base transactions
39  size_t tx_counter = 1;
40  for (auto x = 0; x < 100; ++x) {
42  tx.vin.resize(1);
43  tx.vin[0].scriptSig = CScript() << CScriptNum(tx_counter);
44  tx.vout.resize(det_rand.randrange(10) + 2);
45  for (auto &out : tx.vout) {
46  out.scriptPubKey = CScript() << CScriptNum(tx_counter) << OP_EQUAL;
47  out.nValue = 10 * COIN;
48  }
49  ordered_coins.emplace_back(MakeTransactionRef(tx));
50  available_coins.emplace_back(ordered_coins.back(), tx_counter++);
51  }
52  for (auto x = 0; x < childTxs && !available_coins.empty(); ++x) {
54  size_t n_ancestors = det_rand.randrange(10) + 1;
55  for (size_t ancestor = 0;
56  ancestor < n_ancestors && !available_coins.empty(); ++ancestor) {
57  size_t idx = det_rand.randrange(available_coins.size());
58  Available coin = available_coins[idx];
59  TxId txid = coin.ref->GetId();
60  // biased towards taking min_ancestors parents, but maybe more
61  size_t n_to_take =
62  det_rand.randrange(2) == 0
63  ? min_ancestors
64  : min_ancestors + det_rand.randrange(coin.ref->vout.size() -
65  coin.vin_left);
66  for (size_t i = 0; i < n_to_take; ++i) {
67  tx.vin.emplace_back();
68  tx.vin.back().prevout = COutPoint(txid, coin.vin_left++);
69  tx.vin.back().scriptSig = CScript() << coin.tx_count;
70  }
71  if (coin.vin_left == coin.ref->vin.size()) {
72  coin = available_coins.back();
73  available_coins.pop_back();
74  }
75  tx.vout.resize(det_rand.randrange(10) + 2);
76  for (auto &out : tx.vout) {
77  out.scriptPubKey = CScript()
78  << CScriptNum(tx_counter) << OP_EQUAL;
79  out.nValue = 10 * COIN;
80  }
81  }
82  ordered_coins.emplace_back(MakeTransactionRef(tx));
83  available_coins.emplace_back(ordered_coins.back(), tx_counter++);
84  }
85  return ordered_coins;
86 }
87 
88 static void ComplexMemPool(benchmark::Bench &bench) {
89  FastRandomContext det_rand{true};
90  int childTxs = 800;
91  if (bench.complexityN() > 1) {
92  childTxs = static_cast<int>(bench.complexityN());
93  }
94  std::vector<CTransactionRef> ordered_coins =
95  CreateOrderedCoins(det_rand, childTxs, /* min_ancestors */ 1);
96  TestingSetup test_setup;
97 
98  CTxMemPool pool;
99  LOCK2(cs_main, pool.cs);
100  bench.run([&]() NO_THREAD_SAFETY_ANALYSIS {
101  for (auto &tx : ordered_coins) {
102  AddTx(tx, pool);
103  }
104  pool.TrimToSize(pool.DynamicMemoryUsage() * 3 / 4);
105  pool.TrimToSize(GetVirtualTransactionSize(*ordered_coins.front()));
106  });
107 }
108 
109 static void MempoolCheck(benchmark::Bench &bench) {
110  FastRandomContext det_rand{true};
111  const int childTxs =
112  bench.complexityN() > 1 ? static_cast<int>(bench.complexityN()) : 2000;
113  const std::vector<CTransactionRef> ordered_coins =
114  CreateOrderedCoins(det_rand, childTxs, /* min_ancestors */ 5);
115  const auto testing_setup =
116  TestingSetup(CBaseChainParams::MAIN, {"-checkmempool=1"});
117  CTxMemPool pool;
118  LOCK2(cs_main, pool.cs);
119  const CCoinsViewCache &coins_tip =
120  testing_setup.m_node.chainman->ActiveChainstate().CoinsTip();
121  for (auto &tx : ordered_coins) {
122  AddTx(tx, pool);
123  }
124 
125  bench.run([&]() NO_THREAD_SAFETY_ANALYSIS {
126  pool.check(coins_tip, /* spendheight */ 2);
127  });
128 }
129 
static constexpr Amount SATOSHI
Definition: amount.h:143
static constexpr Amount COIN
Definition: amount.h:144
RecursiveMutex cs_main
Global state.
Definition: validation.cpp:113
static const std::string MAIN
BIP70 chain name strings (main, test or regtest)
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:203
A mutable version of CTransaction.
Definition: transaction.h:274
std::vector< CTxOut > vout
Definition: transaction.h:277
std::vector< CTxIn > vin
Definition: transaction.h:276
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:20
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:431
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
Definition: txmempool.h:84
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:299
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it.
Definition: txmempool.h:385
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:795
size_t DynamicMemoryUsage() const
Definition: txmempool.cpp:677
void check(const CCoinsViewCache &active_coins_tip, int64_t spendheight) const EXCLUSIVE_LOCKS_REQUIRED(void addUnchecked(const CTxMemPoolEntry &entry) EXCLUSIVE_LOCKS_REQUIRED(cs
If sanity-checking is turned on, check makes sure the pool is consistent (does not contain two transa...
Definition: txmempool.h:440
Fast randomness source.
Definition: random.h:129
uint64_t randrange(uint64_t range) noexcept
Generate a random integer in the range [0..range).
Definition: random.h:204
Main entry point to nanobench's benchmarking facility.
Definition: nanobench.h:616
Bench & complexityN(T b) noexcept
Definition: nanobench.h:1214
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1183
static void AddTx(const CTransactionRef &tx, CTxMemPool &pool) EXCLUSIVE_LOCKS_REQUIRED(cs_main
static void MempoolCheck(benchmark::Bench &bench)
unsigned int sigChecks
unsigned int nHeight
BENCHMARK(ComplexMemPool)
static std::vector< CTransactionRef > CreateOrderedCoins(FastRandomContext &det_rand, int childTxs, int min_ancestors)
bool spendsCoinbase
LockPoints lp
static void pool cs
static void ComplexMemPool(benchmark::Bench &bench)
int64_t GetVirtualTransactionSize(int64_t nSize, int64_t nSigChecks, unsigned int bytes_per_sigCheck)
Compute the virtual transaction size (size, or more if sigChecks are too dense).
Definition: policy.cpp:165
static CTransactionRef MakeTransactionRef()
Definition: transaction.h:316
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:315
@ OP_EQUAL
Definition: script.h:119
Available(CTransactionRef &_ref, size_t _tx_count)
size_t tx_count
CTransactionRef ref
size_t vin_left
A TxId is the identifier of a transaction.
Definition: txid.h:14
#define LOCK2(cs1, cs2)
Definition: sync.h:247
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:56
#define NO_THREAD_SAFETY_ANALYSIS
Definition: threadsafety.h:58