Bitcoin ABC  0.28.12
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 <kernel/mempool_entry.h>
7 #include <policy/policy.h>
8 #include <random.h>
9 #include <test/util/setup_common.h>
10 #include <txmempool.h>
11 #include <validation.h>
12 
13 #include <vector>
14 
15 static void AddTx(const CTransactionRef &tx, CTxMemPool &pool)
17  int64_t nTime = 0;
18  unsigned int nHeight = 1;
19  unsigned int sigChecks = 1;
22  nHeight, sigChecks, lp));
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  const auto testing_setup =
97  MakeNoLogFileContext<const TestingSetup>(CBaseChainParams::MAIN);
98  CTxMemPool &pool = *testing_setup.get()->m_node.mempool;
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  auto testing_setup = MakeNoLogFileContext<TestChain100Setup>(
112  CBaseChainParams::REGTEST, {"-checkmempool=1"});
113  CTxMemPool &pool = *testing_setup.get()->m_node.mempool;
114  LOCK2(cs_main, pool.cs);
115  testing_setup->PopulateMempool(det_rand, 400, true);
116  const CCoinsViewCache &coins_tip =
117  testing_setup.get()->m_node.chainman->ActiveChainstate().CoinsTip();
118 
119  bench.run([&]() NO_THREAD_SAFETY_ANALYSIS {
120  // Bump up the spendheight so we don't hit premature coinbase spend
121  // errors.
122  pool.check(coins_tip, /*spendheight=*/300);
123  });
124 }
125 
static constexpr Amount SATOSHI
Definition: amount.h:143
static constexpr Amount COIN
Definition: amount.h:144
static const std::string REGTEST
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
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:209
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it.
Definition: txmempool.h:296
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:742
size_t DynamicMemoryUsage() const
Definition: txmempool.cpp:622
CTransactionRef get(const TxId &txid) const
Definition: txmempool.cpp:490
void check(const CCoinsViewCache &active_coins_tip, int64_t spendheight) const EXCLUSIVE_LOCKS_REQUIRED(void addUnchecked(CTxMemPoolEntryRef 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:361
Fast randomness source.
Definition: random.h:156
uint64_t randrange(uint64_t range) noexcept
Generate a random integer in the range [0..range).
Definition: random.h:231
static RCUPtr make(Args &&...args)
Construct a new object that is owned by the pointer.
Definition: rcu.h:112
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
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: cs_main.cpp:7
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)
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:309
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:56
#define NO_THREAD_SAFETY_ANALYSIS
Definition: threadsafety.h:58