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::State& state)
30 {
31  FastRandomContext det_rand{true};
32  std::vector<Available> available_coins;
33  std::vector<CTransactionRef> ordered_coins;
34  // Create some base transactions
35  size_t tx_counter = 1;
36  for (auto x = 0; x < 100; ++x) {
38  tx.vin.resize(1);
39  tx.vin[0].scriptSig = CScript() << CScriptNum(tx_counter);
40  tx.vin[0].scriptWitness.stack.push_back(CScriptNum(x).getvch());
41  tx.vout.resize(det_rand.randrange(10)+2);
42  for (auto& out : tx.vout) {
43  out.scriptPubKey = CScript() << CScriptNum(tx_counter) << OP_EQUAL;
44  out.nValue = 10 * COIN;
45  }
46  ordered_coins.emplace_back(MakeTransactionRef(tx));
47  available_coins.emplace_back(ordered_coins.back(), tx_counter++);
48  }
49  for (auto x = 0; x < 800 && !available_coins.empty(); ++x) {
51  size_t n_ancestors = det_rand.randrange(10)+1;
52  for (size_t ancestor = 0; ancestor < n_ancestors && !available_coins.empty(); ++ancestor){
53  size_t idx = det_rand.randrange(available_coins.size());
54  Available coin = available_coins[idx];
55  uint256 hash = coin.ref->GetHash();
56  // biased towards taking just one ancestor, but maybe more
57  size_t n_to_take = det_rand.randrange(2) == 0 ? 1 : 1+det_rand.randrange(coin.ref->vout.size() - coin.vin_left);
58  for (size_t i = 0; i < n_to_take; ++i) {
59  tx.vin.emplace_back();
60  tx.vin.back().prevout = COutPoint(hash, coin.vin_left++);
61  tx.vin.back().scriptSig = CScript() << coin.tx_count;
62  tx.vin.back().scriptWitness.stack.push_back(CScriptNum(coin.tx_count).getvch());
63  }
64  if (coin.vin_left == coin.ref->vin.size()) {
65  coin = available_coins.back();
66  available_coins.pop_back();
67  }
68  tx.vout.resize(det_rand.randrange(10)+2);
69  for (auto& out : tx.vout) {
70  out.scriptPubKey = CScript() << CScriptNum(tx_counter) << OP_EQUAL;
71  out.nValue = 10 * COIN;
72  }
73  }
74  ordered_coins.emplace_back(MakeTransactionRef(tx));
75  available_coins.emplace_back(ordered_coins.back(), tx_counter++);
76  }
77  TestingSetup test_setup;
78  CTxMemPool pool;
79  LOCK2(cs_main, pool.cs);
80  while (state.KeepRunning()) {
81  for (auto& tx : ordered_coins) {
82  AddTx(tx, pool);
83  }
84  pool.TrimToSize(pool.DynamicMemoryUsage() * 3 / 4);
85  pool.TrimToSize(GetVirtualTransactionSize(*ordered_coins.front()));
86  }
87 }
88 
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:387
static void ComplexMemPool(benchmark::State &state)
size_t DynamicMemoryUsage() const
Definition: txmempool.cpp:918
std::vector< CTxIn > vin
Definition: transaction.h:347
static const CAmount COIN
Definition: amount.h:14
static CTransactionRef MakeTransactionRef()
Definition: transaction.h:388
bool KeepRunning()
Definition: bench.h:69
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:238
LockPoints lp
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
Definition: txmempool.h:64
Available(CTransactionRef &ref, size_t tx_count)
#define LOCK2(cs1, cs2)
Definition: sync.h:227
Fast randomness source.
Definition: random.h:119
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:128
std::vector< unsigned char > getvch() const
Definition: script.h:320
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:18
std::vector< CTxOut > vout
Definition: transaction.h:348
256-bit opaque blob.
Definition: uint256.h:120
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:53
unsigned int nHeight
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:443
CTransactionRef ref
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:390
A mutable version of CTransaction.
Definition: transaction.h:345
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:1035
size_t vin_left
BENCHMARK(ComplexMemPool, 1)
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:520
static void AddTx(const CTransactionRef &tx, CTxMemPool &pool) EXCLUSIVE_LOCKS_REQUIRED(cs_main