Bitcoin ABC  0.22.13
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 
11 #include <vector>
12 
13 static void AddTx(const CTransactionRef &tx, CTxMemPool &pool)
15  int64_t nTime = 0;
16  unsigned int nHeight = 1;
17  bool spendsCoinbase = false;
18  unsigned int sigOpCost = 4;
20  pool.addUnchecked(CTxMemPoolEntry(tx, 1000 * SATOSHI, nTime, nHeight,
21  spendsCoinbase, sigOpCost, lp));
22 }
23 
24 struct Available {
26  size_t vin_left{0};
27  size_t tx_count;
28  Available(CTransactionRef &_ref, size_t _tx_count)
29  : ref(_ref), tx_count(_tx_count) {}
30 };
31 
32 static void ComplexMemPool(benchmark::State &state) {
33  FastRandomContext det_rand{true};
34  std::vector<Available> available_coins;
35  std::vector<CTransactionRef> ordered_coins;
36  // Create some base transactions
37  size_t tx_counter = 1;
38  for (auto x = 0; x < 100; ++x) {
40  tx.vin.resize(1);
41  tx.vin[0].scriptSig = CScript() << CScriptNum(tx_counter);
42  tx.vout.resize(det_rand.randrange(10) + 2);
43  for (auto &out : tx.vout) {
44  out.scriptPubKey = CScript() << CScriptNum(tx_counter) << OP_EQUAL;
45  out.nValue = 10 * COIN;
46  }
47  ordered_coins.emplace_back(MakeTransactionRef(tx));
48  available_coins.emplace_back(ordered_coins.back(), tx_counter++);
49  }
50  for (auto x = 0; x < 800 && !available_coins.empty(); ++x) {
52  size_t n_ancestors = det_rand.randrange(10) + 1;
53  for (size_t ancestor = 0;
54  ancestor < n_ancestors && !available_coins.empty(); ++ancestor) {
55  size_t idx = det_rand.randrange(available_coins.size());
56  Available coin = available_coins[idx];
57  TxId txid = coin.ref->GetId();
58  // biased towards taking just one ancestor, but maybe more
59  size_t n_to_take =
60  det_rand.randrange(2) == 0
61  ? 1
62  : 1 + det_rand.randrange(coin.ref->vout.size() -
63  coin.vin_left);
64  for (size_t i = 0; i < n_to_take; ++i) {
65  tx.vin.emplace_back();
66  tx.vin.back().prevout = COutPoint(txid, coin.vin_left++);
67  tx.vin.back().scriptSig = CScript() << coin.tx_count;
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()
76  << CScriptNum(tx_counter) << OP_EQUAL;
77  out.nValue = 10 * COIN;
78  }
79  }
80  ordered_coins.emplace_back(MakeTransactionRef(tx));
81  available_coins.emplace_back(ordered_coins.back(), tx_counter++);
82  }
83  TestingSetup test_setup;
84  CTxMemPool pool;
85  LOCK2(cs_main, pool.cs);
86  while (state.KeepRunning()) {
87  for (auto &tx : ordered_coins) {
88  AddTx(tx, pool);
89  }
90  pool.TrimToSize(pool.DynamicMemoryUsage() * 3 / 4);
91  pool.TrimToSize(GetVirtualTransactionSize(*ordered_coins.front()));
92  }
93 }
94 
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:338
static void ComplexMemPool(benchmark::State &state)
Available(CTransactionRef &_ref, size_t _tx_count)
size_t DynamicMemoryUsage() const
Definition: txmempool.cpp:1058
static CTransactionRef MakeTransactionRef()
Definition: transaction.h:339
bool KeepRunning()
Definition: bench.h:71
static constexpr Amount SATOSHI
Definition: amount.h:151
std::vector< CTxOut > vout
Definition: transaction.h:300
LockPoints lp
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
Definition: txmempool.h:65
std::vector< CTxIn > vin
Definition: transaction.h:299
#define LOCK2(cs1, cs2)
Definition: sync.h:233
static constexpr Amount COIN
Definition: amount.h:153
Fast randomness source.
Definition: random.h:113
size_t tx_count
RecursiveMutex cs_main
Global state.
Definition: validation.cpp:95
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:22
static void pool cs
void addUnchecked(const CTxMemPoolEntry &entry) EXCLUSIVE_LOCKS_REQUIRED(cs
Definition: txmempool.cpp:1124
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:56
unsigned int nHeight
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:468
CTransactionRef ref
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:430
A TxId is the identifier of a transaction.
Definition: txid.h:14
A mutable version of CTransaction.
Definition: transaction.h:297
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:1198
size_t vin_left
BENCHMARK(ComplexMemPool, 1)
int64_t GetVirtualTransactionSize(int64_t nSize, int64_t nSigOpCount, unsigned int bytes_per_sigop)
Compute the virtual transaction size (size, or more if sigops are too dense).
Definition: policy.cpp:161
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:545
static void AddTx(const CTransactionRef &tx, CTxMemPool &pool) EXCLUSIVE_LOCKS_REQUIRED(cs_main