Bitcoin Core 28.0.0
P2P Digital Currency
Loading...
Searching...
No Matches
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>
7#include <policy/policy.h>
8#include <random.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) EXCLUSIVE_LOCKS_REQUIRED(cs_main, pool.cs)
17{
18 int64_t nTime = 0;
19 unsigned int nHeight = 1;
20 uint64_t sequence = 0;
21 bool spendsCoinbase = false;
22 unsigned int sigOpCost = 4;
24 pool.addUnchecked(CTxMemPoolEntry(tx, 1000, nTime, nHeight, sequence, spendsCoinbase, sigOpCost, lp));
25}
26
33
34static std::vector<CTransactionRef> CreateOrderedCoins(FastRandomContext& det_rand, int childTxs, int min_ancestors)
35{
36 std::vector<Available> available_coins;
37 std::vector<CTransactionRef> ordered_coins;
38 // Create some base transactions
39 size_t tx_counter = 1;
40 for (auto x = 0; x < 100; ++x) {
42 tx.vin.resize(1);
43 tx.vin[0].scriptSig = CScript() << CScriptNum(tx_counter);
44 tx.vin[0].scriptWitness.stack.push_back(CScriptNum(x).getvch());
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; ancestor < n_ancestors && !available_coins.empty(); ++ancestor){
57 size_t idx = det_rand.randrange(available_coins.size());
58 Available coin = available_coins[idx];
59 Txid hash = coin.ref->GetHash();
60 // biased towards taking min_ancestors parents, but maybe more
61 size_t n_to_take = det_rand.randrange(2) == 0 ?
62 min_ancestors :
63 min_ancestors + det_rand.randrange(coin.ref->vout.size() - 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(hash, coin.vin_left++);
67 tx.vin.back().scriptSig = CScript() << coin.tx_count;
68 tx.vin.back().scriptWitness.stack.push_back(CScriptNum(coin.tx_count).getvch());
69 }
70 if (coin.vin_left == coin.ref->vin.size()) {
71 coin = available_coins.back();
72 available_coins.pop_back();
73 }
74 tx.vout.resize(det_rand.randrange(10)+2);
75 for (auto& out : tx.vout) {
76 out.scriptPubKey = CScript() << 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 return ordered_coins;
84}
85
87{
88 FastRandomContext det_rand{true};
89 int childTxs = 800;
90 if (bench.complexityN() > 1) {
91 childTxs = static_cast<int>(bench.complexityN());
92 }
93 std::vector<CTransactionRef> ordered_coins = CreateOrderedCoins(det_rand, childTxs, /*min_ancestors=*/1);
95 CTxMemPool& pool = *testing_setup.get()->m_node.mempool;
96 LOCK2(cs_main, pool.cs);
97 bench.run([&]() NO_THREAD_SAFETY_ANALYSIS {
98 for (auto& tx : ordered_coins) {
99 AddTx(tx, pool);
100 }
101 pool.TrimToSize(pool.DynamicMemoryUsage() * 3 / 4);
102 pool.TrimToSize(GetVirtualTransactionSize(*ordered_coins.front()));
103 });
104}
105
107{
108 FastRandomContext det_rand{true};
109 auto testing_setup = MakeNoLogFileContext<TestChain100Setup>(ChainType::REGTEST, {.extra_args = {"-checkmempool=1"}});
110 CTxMemPool& pool = *testing_setup.get()->m_node.mempool;
111 LOCK2(cs_main, pool.cs);
112 testing_setup->PopulateMempool(det_rand, 400, true);
113 const CCoinsViewCache& coins_tip = testing_setup.get()->m_node.chainman->ActiveChainstate().CoinsTip();
114
115 bench.run([&]() NO_THREAD_SAFETY_ANALYSIS {
116 // Bump up the spendheight so we don't hit premature coinbase spend errors.
117 pool.check(coins_tip, /*spendheight=*/300);
118 });
119}
120
static constexpr CAmount COIN
The amount of satoshis in one BTC.
Definition amount.h:15
#define BENCHMARK(n, priority_level)
Definition bench.h:79
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition coins.h:360
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:414
std::vector< unsigned char > getvch() const
Definition script.h:343
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
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.
CTransactionRef get(const uint256 &hash) const
size_t DynamicMemoryUsage() const
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
static void MempoolCheck(benchmark::Bench &bench)
unsigned int nHeight
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:47
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:300
static CTransactionRef MakeTransactionRef(Tx &&txIn)
std::shared_ptr< const CTransaction > CTransactionRef
@ OP_EQUAL
Definition script.h:145
std::unique_ptr< T > MakeNoLogFileContext(const ChainType chain_type=ChainType::REGTEST, TestOpts opts={})
Make a test setup that has disk access to the debug.log file disabled.
Available(CTransactionRef &ref, size_t tx_count)
CTransactionRef ref
A mutable version of CTransaction.
std::vector< CTxOut > vout
std::vector< CTxIn > vin
#define LOCK2(cs1, cs2)
Definition sync.h:258
#define EXCLUSIVE_LOCKS_REQUIRED(...)
#define NO_THREAD_SAFETY_ANALYSIS