Bitcoin ABC 0.31.8
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>
7#include <policy/policy.h>
8#include <random.h>
9#include <test/util/setup_common.h>
10#include <txmempool.h>
11#include <util/chaintype.h>
12#include <validation.h>
13
14#include <vector>
15
16static void AddTx(const CTransactionRef &tx, CTxMemPool &pool)
18 int64_t nTime = 0;
19 unsigned int nHeight = 1;
20 unsigned int sigChecks = 1;
24}
25
26struct Available {
28 size_t vin_left{0};
29 size_t tx_count;
30 Available(CTransactionRef &_ref, size_t _tx_count)
31 : ref(_ref), tx_count(_tx_count) {}
32};
33
34static std::vector<CTransactionRef>
35CreateOrderedCoins(FastRandomContext &det_rand, int childTxs,
36 int min_ancestors) {
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.vout.resize(det_rand.randrange(10) + 2);
46 for (auto &out : tx.vout) {
47 out.scriptPubKey = CScript() << CScriptNum(tx_counter) << OP_EQUAL;
48 out.nValue = 10 * COIN;
49 }
50 ordered_coins.emplace_back(MakeTransactionRef(tx));
51 available_coins.emplace_back(ordered_coins.back(), tx_counter++);
52 }
53 for (auto x = 0; x < childTxs && !available_coins.empty(); ++x) {
55 size_t n_ancestors = det_rand.randrange(10) + 1;
56 for (size_t ancestor = 0;
57 ancestor < n_ancestors && !available_coins.empty(); ++ancestor) {
58 size_t idx = det_rand.randrange(available_coins.size());
59 Available coin = available_coins[idx];
60 TxId txid = coin.ref->GetId();
61 // biased towards taking min_ancestors parents, but maybe more
62 size_t n_to_take =
63 det_rand.randrange(2) == 0
64 ? min_ancestors
65 : min_ancestors + det_rand.randrange(coin.ref->vout.size() -
66 coin.vin_left);
67 for (size_t i = 0; i < n_to_take; ++i) {
68 tx.vin.emplace_back();
69 tx.vin.back().prevout = COutPoint(txid, coin.vin_left++);
70 tx.vin.back().scriptSig = CScript() << coin.tx_count;
71 }
72 if (coin.vin_left == coin.ref->vin.size()) {
73 coin = available_coins.back();
74 available_coins.pop_back();
75 }
76 tx.vout.resize(det_rand.randrange(10) + 2);
77 for (auto &out : tx.vout) {
78 out.scriptPubKey = CScript()
79 << CScriptNum(tx_counter) << OP_EQUAL;
80 out.nValue = 10 * COIN;
81 }
82 }
83 ordered_coins.emplace_back(MakeTransactionRef(tx));
84 available_coins.emplace_back(ordered_coins.back(), tx_counter++);
85 }
86 return ordered_coins;
87}
88
89static void ComplexMemPool(benchmark::Bench &bench) {
90 FastRandomContext det_rand{true};
91 int childTxs = 800;
92 if (bench.complexityN() > 1) {
93 childTxs = static_cast<int>(bench.complexityN());
94 }
95 std::vector<CTransactionRef> ordered_coins =
96 CreateOrderedCoins(det_rand, childTxs, /* min_ancestors */ 1);
97 const auto testing_setup =
98 MakeNoLogFileContext<const TestingSetup>(ChainType::MAIN);
99 CTxMemPool &pool = *testing_setup.get()->m_node.mempool;
100 LOCK2(cs_main, pool.cs);
101 bench.run([&]() NO_THREAD_SAFETY_ANALYSIS {
102 for (auto &tx : ordered_coins) {
103 AddTx(tx, pool);
104 }
105 pool.TrimToSize(pool.DynamicMemoryUsage() * 3 / 4);
106 pool.TrimToSize(GetVirtualTransactionSize(*ordered_coins.front()));
107 });
108}
109
110static void MempoolCheck(benchmark::Bench &bench) {
111 FastRandomContext det_rand{true};
112 auto testing_setup = MakeNoLogFileContext<TestChain100Setup>(
113 ChainType::REGTEST, {"-checkmempool=1"});
114 CTxMemPool &pool = *testing_setup.get()->m_node.mempool;
115 LOCK2(cs_main, pool.cs);
116 testing_setup->PopulateMempool(det_rand, 400, true);
117 const CCoinsViewCache &coins_tip =
118 testing_setup.get()->m_node.chainman->ActiveChainstate().CoinsTip();
119
120 bench.run([&]() NO_THREAD_SAFETY_ANALYSIS {
121 // Bump up the spendheight so we don't hit premature coinbase spend
122 // errors.
123 pool.check(coins_tip, /*spendheight=*/300);
124 });
125}
126
static constexpr Amount SATOSHI
Definition: amount.h:143
static constexpr Amount COIN
Definition: amount.h:144
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:221
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
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:219
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it.
Definition: txmempool.h:315
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:915
size_t DynamicMemoryUsage() const
Definition: txmempool.cpp:783
CTransactionRef get(const TxId &txid) const
Definition: txmempool.cpp:645
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:380
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
static std::vector< CTransactionRef > CreateOrderedCoins(FastRandomContext &det_rand, int childTxs, int min_ancestors)
BENCHMARK(ComplexMemPool)
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