Bitcoin Core  24.99.0
P2P Digital Currency
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>
6 #include <kernel/mempool_entry.h>
7 #include <policy/policy.h>
9 #include <txmempool.h>
10 
11 
12 static 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  bool spendsCoinbase = false;
17  unsigned int sigOpCost = 4;
19  pool.addUnchecked(CTxMemPoolEntry(
20  tx, nFee, nTime, nHeight,
22 }
23 
24 // Right now this is only testing eviction performance in an extremely small
25 // mempool. Code needs to be written to generate a much wider variety of
26 // unique transactions for a more meaningful performance measurement.
27 static void MempoolEviction(benchmark::Bench& bench)
28 {
29  const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
30 
32  tx1.vin.resize(1);
33  tx1.vin[0].scriptSig = CScript() << OP_1;
34  tx1.vin[0].scriptWitness.stack.push_back({1});
35  tx1.vout.resize(1);
36  tx1.vout[0].scriptPubKey = CScript() << OP_1 << OP_EQUAL;
37  tx1.vout[0].nValue = 10 * COIN;
38 
40  tx2.vin.resize(1);
41  tx2.vin[0].scriptSig = CScript() << OP_2;
42  tx2.vin[0].scriptWitness.stack.push_back({2});
43  tx2.vout.resize(1);
44  tx2.vout[0].scriptPubKey = CScript() << OP_2 << OP_EQUAL;
45  tx2.vout[0].nValue = 10 * COIN;
46 
48  tx3.vin.resize(1);
49  tx3.vin[0].prevout = COutPoint(tx2.GetHash(), 0);
50  tx3.vin[0].scriptSig = CScript() << OP_2;
51  tx3.vin[0].scriptWitness.stack.push_back({3});
52  tx3.vout.resize(1);
53  tx3.vout[0].scriptPubKey = CScript() << OP_3 << OP_EQUAL;
54  tx3.vout[0].nValue = 10 * COIN;
55 
57  tx4.vin.resize(2);
58  tx4.vin[0].prevout.SetNull();
59  tx4.vin[0].scriptSig = CScript() << OP_4;
60  tx4.vin[0].scriptWitness.stack.push_back({4});
61  tx4.vin[1].prevout.SetNull();
62  tx4.vin[1].scriptSig = CScript() << OP_4;
63  tx4.vin[1].scriptWitness.stack.push_back({4});
64  tx4.vout.resize(2);
65  tx4.vout[0].scriptPubKey = CScript() << OP_4 << OP_EQUAL;
66  tx4.vout[0].nValue = 10 * COIN;
67  tx4.vout[1].scriptPubKey = CScript() << OP_4 << OP_EQUAL;
68  tx4.vout[1].nValue = 10 * COIN;
69 
71  tx5.vin.resize(2);
72  tx5.vin[0].prevout = COutPoint(tx4.GetHash(), 0);
73  tx5.vin[0].scriptSig = CScript() << OP_4;
74  tx5.vin[0].scriptWitness.stack.push_back({4});
75  tx5.vin[1].prevout.SetNull();
76  tx5.vin[1].scriptSig = CScript() << OP_5;
77  tx5.vin[1].scriptWitness.stack.push_back({5});
78  tx5.vout.resize(2);
79  tx5.vout[0].scriptPubKey = CScript() << OP_5 << OP_EQUAL;
80  tx5.vout[0].nValue = 10 * COIN;
81  tx5.vout[1].scriptPubKey = CScript() << OP_5 << OP_EQUAL;
82  tx5.vout[1].nValue = 10 * COIN;
83 
85  tx6.vin.resize(2);
86  tx6.vin[0].prevout = COutPoint(tx4.GetHash(), 1);
87  tx6.vin[0].scriptSig = CScript() << OP_4;
88  tx6.vin[0].scriptWitness.stack.push_back({4});
89  tx6.vin[1].prevout.SetNull();
90  tx6.vin[1].scriptSig = CScript() << OP_6;
91  tx6.vin[1].scriptWitness.stack.push_back({6});
92  tx6.vout.resize(2);
93  tx6.vout[0].scriptPubKey = CScript() << OP_6 << OP_EQUAL;
94  tx6.vout[0].nValue = 10 * COIN;
95  tx6.vout[1].scriptPubKey = CScript() << OP_6 << OP_EQUAL;
96  tx6.vout[1].nValue = 10 * COIN;
97 
99  tx7.vin.resize(2);
100  tx7.vin[0].prevout = COutPoint(tx5.GetHash(), 0);
101  tx7.vin[0].scriptSig = CScript() << OP_5;
102  tx7.vin[0].scriptWitness.stack.push_back({5});
103  tx7.vin[1].prevout = COutPoint(tx6.GetHash(), 0);
104  tx7.vin[1].scriptSig = CScript() << OP_6;
105  tx7.vin[1].scriptWitness.stack.push_back({6});
106  tx7.vout.resize(2);
107  tx7.vout[0].scriptPubKey = CScript() << OP_7 << OP_EQUAL;
108  tx7.vout[0].nValue = 10 * COIN;
109  tx7.vout[1].scriptPubKey = CScript() << OP_7 << OP_EQUAL;
110  tx7.vout[1].nValue = 10 * COIN;
111 
112  CTxMemPool& pool = *Assert(testing_setup->m_node.mempool);
113  LOCK2(cs_main, pool.cs);
114  // Create transaction references outside the "hot loop"
115  const CTransactionRef tx1_r{MakeTransactionRef(tx1)};
116  const CTransactionRef tx2_r{MakeTransactionRef(tx2)};
117  const CTransactionRef tx3_r{MakeTransactionRef(tx3)};
118  const CTransactionRef tx4_r{MakeTransactionRef(tx4)};
119  const CTransactionRef tx5_r{MakeTransactionRef(tx5)};
120  const CTransactionRef tx6_r{MakeTransactionRef(tx6)};
121  const CTransactionRef tx7_r{MakeTransactionRef(tx7)};
122 
123  bench.run([&]() NO_THREAD_SAFETY_ANALYSIS {
124  AddTx(tx1_r, 10000LL, pool);
125  AddTx(tx2_r, 5000LL, pool);
126  AddTx(tx3_r, 20000LL, pool);
127  AddTx(tx4_r, 7000LL, pool);
128  AddTx(tx5_r, 1000LL, pool);
129  AddTx(tx6_r, 1100LL, pool);
130  AddTx(tx7_r, 9000LL, pool);
131  pool.TrimToSize(pool.DynamicMemoryUsage() * 3 / 4);
133  });
134 }
135 
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 Assert(val)
Identity function.
Definition: check.h:73
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:36
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:411
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:316
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it.
Definition: txmempool.h:405
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:1055
size_t DynamicMemoryUsage() const
Definition: txmempool.cpp:955
Main entry point to nanobench's benchmarking facility.
Definition: nanobench.h:623
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1230
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: cs_main.cpp:8
BENCHMARK(MempoolEviction, benchmark::PriorityLevel::HIGH)
unsigned int nHeight
bool spendsCoinbase
static void MempoolEviction(benchmark::Bench &bench)
unsigned int sigOpCost
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:295
static CTransactionRef MakeTransactionRef(Tx &&txIn)
Definition: transaction.h:422
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:421
@ OP_2
Definition: script.h:81
@ OP_EQUAL
Definition: script.h:142
@ OP_4
Definition: script.h:83
@ OP_1
Definition: script.h:79
@ OP_3
Definition: script.h:82
@ OP_6
Definition: script.h:85
@ OP_7
Definition: script.h:86
@ OP_5
Definition: script.h:84
A mutable version of CTransaction.
Definition: transaction.h:380
uint256 GetHash() const
Compute the hash of this CMutableTransaction.
Definition: transaction.cpp:68
std::vector< CTxOut > vout
Definition: transaction.h:382
std::vector< CTxIn > vin
Definition: transaction.h:381
#define LOCK2(cs1, cs2)
Definition: sync.h:259
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
#define NO_THREAD_SAFETY_ANALYSIS
Definition: threadsafety.h:51