Bitcoin Core  0.20.99
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 <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;
26  Available(CTransactionRef& ref, size_t tx_count) : ref(ref), tx_count(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  TestingSetup test_setup;
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 
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:395
#define NO_THREAD_SAFETY_ANALYSIS
Definition: threadsafety.h:51
size_t DynamicMemoryUsage() const
Definition: txmempool.cpp:929
std::vector< CTxIn > vin
Definition: transaction.h:355
static const CAmount COIN
Definition: amount.h:14
static CTransactionRef MakeTransactionRef()
Definition: transaction.h:396
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:276
LockPoints lp
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
Definition: txmempool.h:78
Available(CTransactionRef &ref, size_t tx_count)
#define LOCK2(cs1, cs2)
Definition: sync.h:231
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1134
BENCHMARK(ComplexMemPool)
Fast randomness source.
Definition: random.h:119
Bench & complexityN(T b) noexcept
Definition: nanobench.h:1165
size_t tx_count
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate...
Definition: validation.cpp:129
std::vector< unsigned char > getvch() const
Definition: script.h:334
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:26
std::vector< CTxOut > vout
Definition: transaction.h:356
256-bit opaque blob.
Definition: uint256.h:124
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
unsigned int nHeight
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:488
CTransactionRef ref
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:404
static void ComplexMemPool(benchmark::Bench &bench)
A mutable version of CTransaction.
Definition: transaction.h:353
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:1032
size_t vin_left
Main entry point to nanobench&#39;s benchmarking facility.
Definition: nanobench.h:583
unsigned int sigOpCost
bool spendsCoinbase
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it...
Definition: txmempool.h:576
static void AddTx(const CTransactionRef &tx, CTxMemPool &pool) EXCLUSIVE_LOCKS_REQUIRED(cs_main