Bitcoin Core  0.19.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 <txmempool.h>
8 
9 #include <vector>
10 
11 static void AddTx(const CTransactionRef& tx, CTxMemPool& pool) EXCLUSIVE_LOCKS_REQUIRED(cs_main, pool.cs)
12 {
13  int64_t nTime = 0;
14  unsigned int nHeight = 1;
15  bool spendsCoinbase = false;
16  unsigned int sigOpCost = 4;
18  pool.addUnchecked(CTxMemPoolEntry(tx, 1000, nTime, nHeight, spendsCoinbase, sigOpCost, lp));
19 }
20 
21 struct Available {
23  size_t vin_left{0};
24  size_t tx_count;
25  Available(CTransactionRef& ref, size_t tx_count) : ref(ref), tx_count(tx_count){}
26 };
27 
28 static void ComplexMemPool(benchmark::State& state)
29 {
30  FastRandomContext det_rand{true};
31  std::vector<Available> available_coins;
32  std::vector<CTransactionRef> ordered_coins;
33  // Create some base transactions
34  size_t tx_counter = 1;
35  for (auto x = 0; x < 100; ++x) {
37  tx.vin.resize(1);
38  tx.vin[0].scriptSig = CScript() << CScriptNum(tx_counter);
39  tx.vin[0].scriptWitness.stack.push_back(CScriptNum(x).getvch());
40  tx.vout.resize(det_rand.randrange(10)+2);
41  for (auto& out : tx.vout) {
42  out.scriptPubKey = CScript() << CScriptNum(tx_counter) << OP_EQUAL;
43  out.nValue = 10 * COIN;
44  }
45  ordered_coins.emplace_back(MakeTransactionRef(tx));
46  available_coins.emplace_back(ordered_coins.back(), tx_counter++);
47  }
48  for (auto x = 0; x < 800 && !available_coins.empty(); ++x) {
50  size_t n_ancestors = det_rand.randrange(10)+1;
51  for (size_t ancestor = 0; ancestor < n_ancestors && !available_coins.empty(); ++ancestor){
52  size_t idx = det_rand.randrange(available_coins.size());
53  Available coin = available_coins[idx];
54  uint256 hash = coin.ref->GetHash();
55  // biased towards taking just one ancestor, but maybe more
56  size_t n_to_take = det_rand.randrange(2) == 0 ? 1 : 1+det_rand.randrange(coin.ref->vout.size() - coin.vin_left);
57  for (size_t i = 0; i < n_to_take; ++i) {
58  tx.vin.emplace_back();
59  tx.vin.back().prevout = COutPoint(hash, coin.vin_left++);
60  tx.vin.back().scriptSig = CScript() << coin.tx_count;
61  tx.vin.back().scriptWitness.stack.push_back(CScriptNum(coin.tx_count).getvch());
62  }
63  if (coin.vin_left == coin.ref->vin.size()) {
64  coin = available_coins.back();
65  available_coins.pop_back();
66  }
67  tx.vout.resize(det_rand.randrange(10)+2);
68  for (auto& out : tx.vout) {
69  out.scriptPubKey = CScript() << CScriptNum(tx_counter) << OP_EQUAL;
70  out.nValue = 10 * COIN;
71  }
72  }
73  ordered_coins.emplace_back(MakeTransactionRef(tx));
74  available_coins.emplace_back(ordered_coins.back(), tx_counter++);
75  }
76  CTxMemPool pool;
77  LOCK2(cs_main, pool.cs);
78  while (state.KeepRunning()) {
79  for (auto& tx : ordered_coins) {
80  AddTx(tx, pool);
81  }
82  pool.TrimToSize(pool.DynamicMemoryUsage() * 3 / 4);
83  pool.TrimToSize(GetVirtualTransactionSize(*ordered_coins.front()));
84  }
85 }
86 
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:408
static void ComplexMemPool(benchmark::State &state)
size_t DynamicMemoryUsage() const
Definition: txmempool.cpp:915
std::vector< CTxIn > vin
Definition: transaction.h:368
static const CAmount COIN
Definition: amount.h:14
static CTransactionRef MakeTransactionRef()
Definition: transaction.h:409
bool KeepRunning()
Definition: bench.h:72
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:65
Available(CTransactionRef &ref, size_t tx_count)
#define LOCK2(cs1, cs2)
Definition: sync.h:180
Fast randomness source.
Definition: random.h:106
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:106
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:369
256-bit opaque blob.
Definition: uint256.h:120
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:51
unsigned int nHeight
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:444
CTransactionRef ref
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:390
A mutable version of CTransaction.
Definition: transaction.h:366
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:1023
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:521
static void AddTx(const CTransactionRef &tx, CTxMemPool &pool) EXCLUSIVE_LOCKS_REQUIRED(cs_main