Bitcoin ABC 0.26.3
P2P Digital Currency
Loading...
Searching...
No Matches
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 <validation.h>
12
13#include <vector>
14
15static void AddTx(const CTransactionRef &tx, CTxMemPool &pool)
17 int64_t nTime = 0;
18 unsigned int nHeight = 1;
19 unsigned int sigChecks = 1;
23}
24
32
33static std::vector<CTransactionRef>
35 int min_ancestors) {
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.vout.resize(det_rand.randrange(10) + 2);
45 for (auto &out : tx.vout) {
46 out.scriptPubKey = CScript() << CScriptNum(tx_counter) << OP_EQUAL;
47 out.nValue = 10 * COIN;
48 }
49 ordered_coins.emplace_back(MakeTransactionRef(tx));
50 available_coins.emplace_back(ordered_coins.back(), tx_counter++);
51 }
52 for (auto x = 0; x < childTxs && !available_coins.empty(); ++x) {
54 size_t n_ancestors = det_rand.randrange(10) + 1;
55 for (size_t ancestor = 0;
57 size_t idx = det_rand.randrange(available_coins.size());
58 Available coin = available_coins[idx];
59 TxId txid = coin.ref->GetId();
60 // biased towards taking min_ancestors parents, but maybe more
61 size_t n_to_take =
62 det_rand.randrange(2) == 0
64 : min_ancestors + det_rand.randrange(coin.ref->vout.size() -
65 coin.vin_left);
66 for (size_t i = 0; i < n_to_take; ++i) {
67 tx.vin.emplace_back();
68 tx.vin.back().prevout = COutPoint(txid, coin.vin_left++);
69 tx.vin.back().scriptSig = CScript() << coin.tx_count;
70 }
71 if (coin.vin_left == coin.ref->vin.size()) {
72 coin = available_coins.back();
73 available_coins.pop_back();
74 }
75 tx.vout.resize(det_rand.randrange(10) + 2);
76 for (auto &out : tx.vout) {
77 out.scriptPubKey = CScript()
79 out.nValue = 10 * COIN;
80 }
81 }
82 ordered_coins.emplace_back(MakeTransactionRef(tx));
83 available_coins.emplace_back(ordered_coins.back(), tx_counter++);
84 }
85 return ordered_coins;
86}
87
90 int childTxs = 800;
91 if (bench.complexityN() > 1) {
92 childTxs = static_cast<int>(bench.complexityN());
93 }
94 std::vector<CTransactionRef> ordered_coins =
95 CreateOrderedCoins(det_rand, childTxs, /* min_ancestors */ 1);
96 const auto testing_setup =
98 CTxMemPool &pool = *testing_setup.get()->m_node.mempool;
99 LOCK2(cs_main, pool.cs);
101 for (auto &tx : ordered_coins) {
102 AddTx(tx, pool);
103 }
104 pool.TrimToSize(pool.DynamicMemoryUsage() * 3 / 4);
106 });
107}
108
111 auto testing_setup = MakeNoLogFileContext<TestChain100Setup>(
112 CBaseChainParams::REGTEST, {"-checkmempool=1"});
113 CTxMemPool &pool = *testing_setup.get()->m_node.mempool;
114 LOCK2(cs_main, pool.cs);
115 testing_setup->PopulateMempool(det_rand, 400, true);
117 testing_setup.get()->m_node.chainman->ActiveChainstate().CoinsTip();
118
120 // Bump up the spendheight so we don't hit premature coinbase spend
121 // errors.
122 pool.check(coins_tip, /*spendheight=*/300);
123 });
124}
125
static constexpr Amount SATOSHI
Definition amount.h:143
static constexpr Amount COIN
Definition amount.h:144
#define BENCHMARK(n)
Definition bench.h:65
static const std::string REGTEST
static const std::string MAIN
BIP70 chain name strings (main, test or regtest)
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition coins.h:221
A mutable version of CTransaction.
std::vector< CTxOut > vout
std::vector< CTxIn > vin
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition transaction.h:20
Serialized script, used inside transaction inputs and outputs.
Definition script.h:431
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition txmempool.h:212
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it.
Definition txmempool.h:307
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.
size_t DynamicMemoryUsage() const
CTransactionRef get(const TxId &txid) const
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:372
Fast randomness source.
Definition random.h:156
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
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)
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()
std::shared_ptr< const CTransaction > CTransactionRef
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
Definition random.h:85
@ OP_EQUAL
Definition script.h:119
Available(CTransactionRef &_ref, size_t _tx_count)
CTransactionRef ref
A TxId is the identifier of a transaction.
Definition txid.h:14
#define LOCK2(cs1, cs2)
Definition sync.h:309
#define EXCLUSIVE_LOCKS_REQUIRED(...)
#define NO_THREAD_SAFETY_ANALYSIS