Bitcoin Core 28.99.0
P2P Digital Currency
mempool_stress.cpp
Go to the documentation of this file.
1// Copyright (c) 2011-2022 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 <consensus/amount.h>
7#include <policy/policy.h>
9#include <random.h>
10#include <script/script.h>
11#include <sync.h>
13#include <test/util/txmempool.h>
14#include <txmempool.h>
15#include <validation.h>
16
17#include <cstddef>
18#include <cstdint>
19#include <memory>
20#include <vector>
21
22class CCoinsViewCache;
23
24static void AddTx(const CTransactionRef& tx, CTxMemPool& pool) EXCLUSIVE_LOCKS_REQUIRED(cs_main, pool.cs)
25{
26 int64_t nTime = 0;
27 unsigned int nHeight = 1;
28 uint64_t sequence = 0;
29 bool spendsCoinbase = false;
30 unsigned int sigOpCost = 4;
33}
34
35struct Available {
37 size_t vin_left{0};
38 size_t tx_count;
40};
41
42static std::vector<CTransactionRef> CreateOrderedCoins(FastRandomContext& det_rand, int childTxs, int min_ancestors)
43{
44 std::vector<Available> available_coins;
45 std::vector<CTransactionRef> ordered_coins;
46 // Create some base transactions
47 size_t tx_counter = 1;
48 for (auto x = 0; x < 100; ++x) {
50 tx.vin.resize(1);
51 tx.vin[0].scriptSig = CScript() << CScriptNum(tx_counter);
52 tx.vin[0].scriptWitness.stack.push_back(CScriptNum(x).getvch());
53 tx.vout.resize(det_rand.randrange(10)+2);
54 for (auto& out : tx.vout) {
55 out.scriptPubKey = CScript() << CScriptNum(tx_counter) << OP_EQUAL;
56 out.nValue = 10 * COIN;
57 }
58 ordered_coins.emplace_back(MakeTransactionRef(tx));
59 available_coins.emplace_back(ordered_coins.back(), tx_counter++);
60 }
61 for (auto x = 0; x < childTxs && !available_coins.empty(); ++x) {
63 size_t n_ancestors = det_rand.randrange(10)+1;
64 for (size_t ancestor = 0; ancestor < n_ancestors && !available_coins.empty(); ++ancestor){
65 size_t idx = det_rand.randrange(available_coins.size());
66 Available coin = available_coins[idx];
67 Txid hash = coin.ref->GetHash();
68 // biased towards taking min_ancestors parents, but maybe more
69 size_t n_to_take = det_rand.randrange(2) == 0 ?
70 min_ancestors :
71 min_ancestors + det_rand.randrange(coin.ref->vout.size() - coin.vin_left);
72 for (size_t i = 0; i < n_to_take; ++i) {
73 tx.vin.emplace_back();
74 tx.vin.back().prevout = COutPoint(hash, coin.vin_left++);
75 tx.vin.back().scriptSig = CScript() << coin.tx_count;
76 tx.vin.back().scriptWitness.stack.push_back(CScriptNum(coin.tx_count).getvch());
77 }
78 if (coin.vin_left == coin.ref->vin.size()) {
79 coin = available_coins.back();
80 available_coins.pop_back();
81 }
82 tx.vout.resize(det_rand.randrange(10)+2);
83 for (auto& out : tx.vout) {
84 out.scriptPubKey = CScript() << CScriptNum(tx_counter) << OP_EQUAL;
85 out.nValue = 10 * COIN;
86 }
87 }
88 ordered_coins.emplace_back(MakeTransactionRef(tx));
89 available_coins.emplace_back(ordered_coins.back(), tx_counter++);
90 }
91 return ordered_coins;
92}
93
95{
96 FastRandomContext det_rand{true};
97 int childTxs = 800;
98 if (bench.complexityN() > 1) {
99 childTxs = static_cast<int>(bench.complexityN());
100 }
101 std::vector<CTransactionRef> ordered_coins = CreateOrderedCoins(det_rand, childTxs, /*min_ancestors=*/1);
102 const auto testing_setup = MakeNoLogFileContext<const TestingSetup>(ChainType::MAIN);
103 CTxMemPool& pool = *testing_setup.get()->m_node.mempool;
104 LOCK2(cs_main, pool.cs);
105 bench.run([&]() NO_THREAD_SAFETY_ANALYSIS {
106 for (auto& tx : ordered_coins) {
107 AddTx(tx, pool);
108 }
109 pool.TrimToSize(pool.DynamicMemoryUsage() * 3 / 4);
110 pool.TrimToSize(GetVirtualTransactionSize(*ordered_coins.front()));
111 });
112}
113
115{
116 FastRandomContext det_rand{true};
117 auto testing_setup = MakeNoLogFileContext<TestChain100Setup>(ChainType::REGTEST, {.extra_args = {"-checkmempool=1"}});
118 CTxMemPool& pool = *testing_setup.get()->m_node.mempool;
119 LOCK2(cs_main, pool.cs);
120 testing_setup->PopulateMempool(det_rand, 400, true);
121 const CCoinsViewCache& coins_tip = testing_setup.get()->m_node.chainman->ActiveChainstate().CoinsTip();
122
123 bench.run([&]() NO_THREAD_SAFETY_ANALYSIS {
124 // Bump up the spendheight so we don't hit premature coinbase spend errors.
125 pool.check(coins_tip, /*spendheight=*/300);
126 });
127}
128
static constexpr CAmount COIN
The amount of satoshis in one BTC.
Definition: amount.h:15
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:363
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:29
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:415
std::vector< unsigned char > getvch() const
Definition: script.h:344
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
Definition: mempool_entry.h:66
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:304
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it.
Definition: txmempool.h:390
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:1158
CTransactionRef get(const uint256 &hash) const
Definition: txmempool.cpp:884
size_t DynamicMemoryUsage() const
Definition: txmempool.cpp:1063
Fast randomness source.
Definition: random.h:377
I randrange(I range) noexcept
Generate a random integer in the range [0..range), with range > 0.
Definition: random.h:254
Main entry point to nanobench's benchmarking facility.
Definition: nanobench.h:627
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1234
Bench & complexityN(T n) noexcept
Definition: nanobench.h:1265
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: cs_main.cpp:8
static void AddTx(const CTransactionRef &tx, CTxMemPool &pool) EXCLUSIVE_LOCKS_REQUIRED(cs_main
AddToMempool(pool, CTxMemPoolEntry(tx, 1000, nTime, nHeight, sequence, spendsCoinbase, sigOpCost, lp))
static void MempoolCheck(benchmark::Bench &bench)
unsigned int nHeight
BENCHMARK(ComplexMemPool, benchmark::PriorityLevel::HIGH)
static std::vector< CTransactionRef > CreateOrderedCoins(FastRandomContext &det_rand, int childTxs, int min_ancestors)
bool spendsCoinbase
unsigned int sigOpCost
uint64_t sequence
LockPoints lp
static void ComplexMemPool(benchmark::Bench &bench)
@ HIGH
Definition: bench.h:48
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:312
static CTransactionRef MakeTransactionRef(Tx &&txIn)
Definition: transaction.h:424
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:423
@ OP_EQUAL
Definition: script.h:146
Available(CTransactionRef &ref, size_t tx_count)
size_t tx_count
CTransactionRef ref
size_t vin_left
A mutable version of CTransaction.
Definition: transaction.h:378
std::vector< CTxOut > vout
Definition: transaction.h:380
std::vector< CTxIn > vin
Definition: transaction.h:379
#define LOCK2(cs1, cs2)
Definition: sync.h:258
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
#define NO_THREAD_SAFETY_ANALYSIS
Definition: threadsafety.h:51