Bitcoin Core 22.99.0
P2P Digital Currency
mempool_stress.cpp
Go to the documentation of this file.
1// Copyright (c) 2011-2020 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>
8#include <txmempool.h>
9#include <validation.h>
10
11#include <vector>
12
13static void AddTx(const CTransactionRef& tx, CTxMemPool& pool) EXCLUSIVE_LOCKS_REQUIRED(cs_main, pool.cs)
14{
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, nTime, nHeight, spendsCoinbase, sigOpCost, lp));
21}
22
23struct Available {
25 size_t vin_left{0};
26 size_t tx_count;
28};
29
30static std::vector<CTransactionRef> CreateOrderedCoins(FastRandomContext& det_rand, int childTxs, int min_ancestors)
31{
32 std::vector<Available> available_coins;
33 std::vector<CTransactionRef> ordered_coins;
34 // Create some base transactions
35 size_t tx_counter = 1;
36 for (auto x = 0; x < 100; ++x) {
38 tx.vin.resize(1);
39 tx.vin[0].scriptSig = CScript() << CScriptNum(tx_counter);
40 tx.vin[0].scriptWitness.stack.push_back(CScriptNum(x).getvch());
41 tx.vout.resize(det_rand.randrange(10)+2);
42 for (auto& out : tx.vout) {
43 out.scriptPubKey = CScript() << CScriptNum(tx_counter) << OP_EQUAL;
44 out.nValue = 10 * COIN;
45 }
46 ordered_coins.emplace_back(MakeTransactionRef(tx));
47 available_coins.emplace_back(ordered_coins.back(), tx_counter++);
48 }
49 for (auto x = 0; x < childTxs && !available_coins.empty(); ++x) {
51 size_t n_ancestors = det_rand.randrange(10)+1;
52 for (size_t ancestor = 0; ancestor < n_ancestors && !available_coins.empty(); ++ancestor){
53 size_t idx = det_rand.randrange(available_coins.size());
54 Available coin = available_coins[idx];
55 uint256 hash = coin.ref->GetHash();
56 // biased towards taking min_ancestors parents, but maybe more
57 size_t n_to_take = det_rand.randrange(2) == 0 ?
58 min_ancestors :
59 min_ancestors + det_rand.randrange(coin.ref->vout.size() - coin.vin_left);
60 for (size_t i = 0; i < n_to_take; ++i) {
61 tx.vin.emplace_back();
62 tx.vin.back().prevout = COutPoint(hash, coin.vin_left++);
63 tx.vin.back().scriptSig = CScript() << coin.tx_count;
64 tx.vin.back().scriptWitness.stack.push_back(CScriptNum(coin.tx_count).getvch());
65 }
66 if (coin.vin_left == coin.ref->vin.size()) {
67 coin = available_coins.back();
68 available_coins.pop_back();
69 }
70 tx.vout.resize(det_rand.randrange(10)+2);
71 for (auto& out : tx.vout) {
72 out.scriptPubKey = CScript() << CScriptNum(tx_counter) << OP_EQUAL;
73 out.nValue = 10 * COIN;
74 }
75 }
76 ordered_coins.emplace_back(MakeTransactionRef(tx));
77 available_coins.emplace_back(ordered_coins.back(), tx_counter++);
78 }
79 return ordered_coins;
80}
81
83{
84 FastRandomContext det_rand{true};
85 int childTxs = 800;
86 if (bench.complexityN() > 1) {
87 childTxs = static_cast<int>(bench.complexityN());
88 }
89 std::vector<CTransactionRef> ordered_coins = CreateOrderedCoins(det_rand, childTxs, /* min_ancestors */ 1);
90 const auto testing_setup = MakeNoLogFileContext<const TestingSetup>(CBaseChainParams::MAIN);
91 CTxMemPool pool;
92 LOCK2(cs_main, pool.cs);
93 bench.run([&]() NO_THREAD_SAFETY_ANALYSIS {
94 for (auto& tx : ordered_coins) {
95 AddTx(tx, pool);
96 }
97 pool.TrimToSize(pool.DynamicMemoryUsage() * 3 / 4);
98 pool.TrimToSize(GetVirtualTransactionSize(*ordered_coins.front()));
99 });
100}
101
103{
104 FastRandomContext det_rand{true};
105 const int childTxs = bench.complexityN() > 1 ? static_cast<int>(bench.complexityN()) : 2000;
106 const std::vector<CTransactionRef> ordered_coins = CreateOrderedCoins(det_rand, childTxs, /* min_ancestors */ 5);
107 const auto testing_setup = MakeNoLogFileContext<const TestingSetup>(CBaseChainParams::MAIN, {"-checkmempool=1"});
108 CTxMemPool pool;
109 LOCK2(cs_main, pool.cs);
110 const CCoinsViewCache& coins_tip = testing_setup.get()->m_node.chainman->ActiveChainstate().CoinsTip();
111 for (auto& tx : ordered_coins) AddTx(tx, pool);
112
113 bench.run([&]() NO_THREAD_SAFETY_ANALYSIS {
114 pool.check(coins_tip, /* spendheight */ 2);
115 });
116}
117
static constexpr CAmount COIN
The amount of satoshis in one BTC.
Definition: amount.h:15
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: validation.cpp:118
static const std::string MAIN
Chain name strings.
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:214
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:27
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:406
std::vector< unsigned char > getvch() const
Definition: script.h:335
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
Definition: txmempool.h:80
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:424
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it.
Definition: txmempool.h:511
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:1141
size_t DynamicMemoryUsage() const
Definition: txmempool.cpp:1038
Fast randomness source.
Definition: random.h:120
uint64_t randrange(uint64_t range) noexcept
Generate a random integer in the range [0..range).
Definition: random.h:190
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
256-bit opaque blob.
Definition: uint256.h:124
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)
BENCHMARK(ComplexMemPool)
bool spendsCoinbase
unsigned int sigOpCost
LockPoints lp
static void ComplexMemPool(benchmark::Bench &bench)
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:285
static CTransactionRef MakeTransactionRef(Tx &&txIn)
Definition: transaction.h:387
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:386
@ OP_EQUAL
Definition: script.h:139
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:345
std::vector< CTxOut > vout
Definition: transaction.h:347
std::vector< CTxIn > vin
Definition: transaction.h:346
#define LOCK2(cs1, cs2)
Definition: sync.h:227
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
#define NO_THREAD_SAFETY_ANALYSIS
Definition: threadsafety.h:51