Bitcoin Core 28.0.0
P2P Digital Currency
Loading...
Searching...
No Matches
mempool_eviction.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>
9#include <txmempool.h>
10
11
12static void AddTx(const CTransactionRef& tx, const CAmount& nFee, CTxMemPool& pool) EXCLUSIVE_LOCKS_REQUIRED(cs_main, pool.cs)
13{
14 int64_t nTime = 0;
15 unsigned int nHeight = 1;
16 uint64_t sequence = 0;
17 bool spendsCoinbase = false;
18 unsigned int sigOpCost = 4;
20 pool.addUnchecked(CTxMemPoolEntry(
21 tx, nFee, nTime, nHeight, sequence,
23}
24
25// Right now this is only testing eviction performance in an extremely small
26// mempool. Code needs to be written to generate a much wider variety of
27// unique transactions for a more meaningful performance measurement.
29{
30 const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
31
33 tx1.vin.resize(1);
34 tx1.vin[0].scriptSig = CScript() << OP_1;
35 tx1.vin[0].scriptWitness.stack.push_back({1});
36 tx1.vout.resize(1);
37 tx1.vout[0].scriptPubKey = CScript() << OP_1 << OP_EQUAL;
38 tx1.vout[0].nValue = 10 * COIN;
39
41 tx2.vin.resize(1);
42 tx2.vin[0].scriptSig = CScript() << OP_2;
43 tx2.vin[0].scriptWitness.stack.push_back({2});
44 tx2.vout.resize(1);
45 tx2.vout[0].scriptPubKey = CScript() << OP_2 << OP_EQUAL;
46 tx2.vout[0].nValue = 10 * COIN;
47
49 tx3.vin.resize(1);
50 tx3.vin[0].prevout = COutPoint(tx2.GetHash(), 0);
51 tx3.vin[0].scriptSig = CScript() << OP_2;
52 tx3.vin[0].scriptWitness.stack.push_back({3});
53 tx3.vout.resize(1);
54 tx3.vout[0].scriptPubKey = CScript() << OP_3 << OP_EQUAL;
55 tx3.vout[0].nValue = 10 * COIN;
56
58 tx4.vin.resize(2);
59 tx4.vin[0].prevout.SetNull();
60 tx4.vin[0].scriptSig = CScript() << OP_4;
61 tx4.vin[0].scriptWitness.stack.push_back({4});
62 tx4.vin[1].prevout.SetNull();
63 tx4.vin[1].scriptSig = CScript() << OP_4;
64 tx4.vin[1].scriptWitness.stack.push_back({4});
65 tx4.vout.resize(2);
66 tx4.vout[0].scriptPubKey = CScript() << OP_4 << OP_EQUAL;
67 tx4.vout[0].nValue = 10 * COIN;
68 tx4.vout[1].scriptPubKey = CScript() << OP_4 << OP_EQUAL;
69 tx4.vout[1].nValue = 10 * COIN;
70
72 tx5.vin.resize(2);
73 tx5.vin[0].prevout = COutPoint(tx4.GetHash(), 0);
74 tx5.vin[0].scriptSig = CScript() << OP_4;
75 tx5.vin[0].scriptWitness.stack.push_back({4});
76 tx5.vin[1].prevout.SetNull();
77 tx5.vin[1].scriptSig = CScript() << OP_5;
78 tx5.vin[1].scriptWitness.stack.push_back({5});
79 tx5.vout.resize(2);
80 tx5.vout[0].scriptPubKey = CScript() << OP_5 << OP_EQUAL;
81 tx5.vout[0].nValue = 10 * COIN;
82 tx5.vout[1].scriptPubKey = CScript() << OP_5 << OP_EQUAL;
83 tx5.vout[1].nValue = 10 * COIN;
84
86 tx6.vin.resize(2);
87 tx6.vin[0].prevout = COutPoint(tx4.GetHash(), 1);
88 tx6.vin[0].scriptSig = CScript() << OP_4;
89 tx6.vin[0].scriptWitness.stack.push_back({4});
90 tx6.vin[1].prevout.SetNull();
91 tx6.vin[1].scriptSig = CScript() << OP_6;
92 tx6.vin[1].scriptWitness.stack.push_back({6});
93 tx6.vout.resize(2);
94 tx6.vout[0].scriptPubKey = CScript() << OP_6 << OP_EQUAL;
95 tx6.vout[0].nValue = 10 * COIN;
96 tx6.vout[1].scriptPubKey = CScript() << OP_6 << OP_EQUAL;
97 tx6.vout[1].nValue = 10 * COIN;
98
100 tx7.vin.resize(2);
101 tx7.vin[0].prevout = COutPoint(tx5.GetHash(), 0);
102 tx7.vin[0].scriptSig = CScript() << OP_5;
103 tx7.vin[0].scriptWitness.stack.push_back({5});
104 tx7.vin[1].prevout = COutPoint(tx6.GetHash(), 0);
105 tx7.vin[1].scriptSig = CScript() << OP_6;
106 tx7.vin[1].scriptWitness.stack.push_back({6});
107 tx7.vout.resize(2);
108 tx7.vout[0].scriptPubKey = CScript() << OP_7 << OP_EQUAL;
109 tx7.vout[0].nValue = 10 * COIN;
110 tx7.vout[1].scriptPubKey = CScript() << OP_7 << OP_EQUAL;
111 tx7.vout[1].nValue = 10 * COIN;
112
113 CTxMemPool& pool = *Assert(testing_setup->m_node.mempool);
114 LOCK2(cs_main, pool.cs);
115 // Create transaction references outside the "hot loop"
116 const CTransactionRef tx1_r{MakeTransactionRef(tx1)};
117 const CTransactionRef tx2_r{MakeTransactionRef(tx2)};
118 const CTransactionRef tx3_r{MakeTransactionRef(tx3)};
119 const CTransactionRef tx4_r{MakeTransactionRef(tx4)};
120 const CTransactionRef tx5_r{MakeTransactionRef(tx5)};
121 const CTransactionRef tx6_r{MakeTransactionRef(tx6)};
122 const CTransactionRef tx7_r{MakeTransactionRef(tx7)};
123
124 bench.run([&]() NO_THREAD_SAFETY_ANALYSIS {
125 AddTx(tx1_r, 10000LL, pool);
126 AddTx(tx2_r, 5000LL, pool);
127 AddTx(tx3_r, 20000LL, pool);
128 AddTx(tx4_r, 7000LL, pool);
129 AddTx(tx5_r, 1000LL, pool);
130 AddTx(tx6_r, 1100LL, pool);
131 AddTx(tx7_r, 9000LL, pool);
132 pool.TrimToSize(pool.DynamicMemoryUsage() * 3 / 4);
134 });
135}
136
int64_t CAmount
Amount in satoshis (Can be negative)
Definition amount.h:12
static constexpr CAmount COIN
The amount of satoshis in one BTC.
Definition amount.h:15
#define BENCHMARK(n, priority_level)
Definition bench.h:79
#define Assert(val)
Identity function.
Definition check.h:77
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
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.
size_t DynamicMemoryUsage() const
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
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition cs_main.cpp:8
unsigned int nHeight
bool spendsCoinbase
static void MempoolEviction(benchmark::Bench &bench)
unsigned int sigOpCost
uint64_t sequence
static void AddTx(const CTransactionRef &tx, const CAmount &nFee, CTxMemPool &pool) EXCLUSIVE_LOCKS_REQUIRED(cs_main
LockPoints lp
@ 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_2
Definition script.h:84
@ OP_EQUAL
Definition script.h:145
@ OP_4
Definition script.h:86
@ OP_1
Definition script.h:82
@ OP_3
Definition script.h:85
@ OP_6
Definition script.h:88
@ OP_7
Definition script.h:89
@ OP_5
Definition script.h:87
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.
A mutable version of CTransaction.
std::vector< CTxOut > vout
Txid GetHash() const
Compute the hash of this CMutableTransaction.
std::vector< CTxIn > vin
#define LOCK2(cs1, cs2)
Definition sync.h:258
#define EXCLUSIVE_LOCKS_REQUIRED(...)
#define NO_THREAD_SAFETY_ANALYSIS