Bitcoin Core  0.20.99
P2P Digital Currency
mempool_eviction.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>
6 #include <policy/policy.h>
7 #include <test/util/setup_common.h>
8 #include <txmempool.h>
9 
10 
11 static void AddTx(const CTransactionRef& tx, const CAmount& nFee, CTxMemPool& pool) EXCLUSIVE_LOCKS_REQUIRED(cs_main, pool.cs)
12 {
13  int64_t nTime = 0;
14  unsigned int nHeight = 1;
15  bool spendsCoinbase = false;
16  unsigned int sigOpCost = 4;
18  pool.addUnchecked(CTxMemPoolEntry(
19  tx, nFee, nTime, nHeight,
20  spendsCoinbase, sigOpCost, lp));
21 }
22 
23 // Right now this is only testing eviction performance in an extremely small
24 // mempool. Code needs to be written to generate a much wider variety of
25 // unique transactions for a more meaningful performance measurement.
26 static void MempoolEviction(benchmark::State& state)
27 {
28  TestingSetup test_setup{
30  /* extra_args */ {
31  "-nodebuglogfile",
32  "-nodebug",
33  },
34  };
35 
37  tx1.vin.resize(1);
38  tx1.vin[0].scriptSig = CScript() << OP_1;
39  tx1.vin[0].scriptWitness.stack.push_back({1});
40  tx1.vout.resize(1);
41  tx1.vout[0].scriptPubKey = CScript() << OP_1 << OP_EQUAL;
42  tx1.vout[0].nValue = 10 * COIN;
43 
45  tx2.vin.resize(1);
46  tx2.vin[0].scriptSig = CScript() << OP_2;
47  tx2.vin[0].scriptWitness.stack.push_back({2});
48  tx2.vout.resize(1);
49  tx2.vout[0].scriptPubKey = CScript() << OP_2 << OP_EQUAL;
50  tx2.vout[0].nValue = 10 * COIN;
51 
53  tx3.vin.resize(1);
54  tx3.vin[0].prevout = COutPoint(tx2.GetHash(), 0);
55  tx3.vin[0].scriptSig = CScript() << OP_2;
56  tx3.vin[0].scriptWitness.stack.push_back({3});
57  tx3.vout.resize(1);
58  tx3.vout[0].scriptPubKey = CScript() << OP_3 << OP_EQUAL;
59  tx3.vout[0].nValue = 10 * COIN;
60 
62  tx4.vin.resize(2);
63  tx4.vin[0].prevout.SetNull();
64  tx4.vin[0].scriptSig = CScript() << OP_4;
65  tx4.vin[0].scriptWitness.stack.push_back({4});
66  tx4.vin[1].prevout.SetNull();
67  tx4.vin[1].scriptSig = CScript() << OP_4;
68  tx4.vin[1].scriptWitness.stack.push_back({4});
69  tx4.vout.resize(2);
70  tx4.vout[0].scriptPubKey = CScript() << OP_4 << OP_EQUAL;
71  tx4.vout[0].nValue = 10 * COIN;
72  tx4.vout[1].scriptPubKey = CScript() << OP_4 << OP_EQUAL;
73  tx4.vout[1].nValue = 10 * COIN;
74 
76  tx5.vin.resize(2);
77  tx5.vin[0].prevout = COutPoint(tx4.GetHash(), 0);
78  tx5.vin[0].scriptSig = CScript() << OP_4;
79  tx5.vin[0].scriptWitness.stack.push_back({4});
80  tx5.vin[1].prevout.SetNull();
81  tx5.vin[1].scriptSig = CScript() << OP_5;
82  tx5.vin[1].scriptWitness.stack.push_back({5});
83  tx5.vout.resize(2);
84  tx5.vout[0].scriptPubKey = CScript() << OP_5 << OP_EQUAL;
85  tx5.vout[0].nValue = 10 * COIN;
86  tx5.vout[1].scriptPubKey = CScript() << OP_5 << OP_EQUAL;
87  tx5.vout[1].nValue = 10 * COIN;
88 
90  tx6.vin.resize(2);
91  tx6.vin[0].prevout = COutPoint(tx4.GetHash(), 1);
92  tx6.vin[0].scriptSig = CScript() << OP_4;
93  tx6.vin[0].scriptWitness.stack.push_back({4});
94  tx6.vin[1].prevout.SetNull();
95  tx6.vin[1].scriptSig = CScript() << OP_6;
96  tx6.vin[1].scriptWitness.stack.push_back({6});
97  tx6.vout.resize(2);
98  tx6.vout[0].scriptPubKey = CScript() << OP_6 << OP_EQUAL;
99  tx6.vout[0].nValue = 10 * COIN;
100  tx6.vout[1].scriptPubKey = CScript() << OP_6 << OP_EQUAL;
101  tx6.vout[1].nValue = 10 * COIN;
102 
104  tx7.vin.resize(2);
105  tx7.vin[0].prevout = COutPoint(tx5.GetHash(), 0);
106  tx7.vin[0].scriptSig = CScript() << OP_5;
107  tx7.vin[0].scriptWitness.stack.push_back({5});
108  tx7.vin[1].prevout = COutPoint(tx6.GetHash(), 0);
109  tx7.vin[1].scriptSig = CScript() << OP_6;
110  tx7.vin[1].scriptWitness.stack.push_back({6});
111  tx7.vout.resize(2);
112  tx7.vout[0].scriptPubKey = CScript() << OP_7 << OP_EQUAL;
113  tx7.vout[0].nValue = 10 * COIN;
114  tx7.vout[1].scriptPubKey = CScript() << OP_7 << OP_EQUAL;
115  tx7.vout[1].nValue = 10 * COIN;
116 
117  CTxMemPool pool;
118  LOCK2(cs_main, pool.cs);
119  // Create transaction references outside the "hot loop"
120  const CTransactionRef tx1_r{MakeTransactionRef(tx1)};
121  const CTransactionRef tx2_r{MakeTransactionRef(tx2)};
122  const CTransactionRef tx3_r{MakeTransactionRef(tx3)};
123  const CTransactionRef tx4_r{MakeTransactionRef(tx4)};
124  const CTransactionRef tx5_r{MakeTransactionRef(tx5)};
125  const CTransactionRef tx6_r{MakeTransactionRef(tx6)};
126  const CTransactionRef tx7_r{MakeTransactionRef(tx7)};
127 
128  while (state.KeepRunning()) {
129  AddTx(tx1_r, 10000LL, pool);
130  AddTx(tx2_r, 5000LL, pool);
131  AddTx(tx3_r, 20000LL, pool);
132  AddTx(tx4_r, 7000LL, pool);
133  AddTx(tx5_r, 1000LL, pool);
134  AddTx(tx6_r, 1100LL, pool);
135  AddTx(tx7_r, 9000LL, pool);
136  pool.TrimToSize(pool.DynamicMemoryUsage() * 3 / 4);
138  }
139 }
140 
141 BENCHMARK(MempoolEviction, 41000);
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:387
Definition: script.h:68
static void AddTx(const CTransactionRef &tx, const CAmount &nFee, CTxMemPool &pool) EXCLUSIVE_LOCKS_REQUIRED(cs_main
BENCHMARK(MempoolEviction, 41000)
static const std::string REGTEST
size_t DynamicMemoryUsage() const
Definition: txmempool.cpp:918
std::vector< CTxIn > vin
Definition: transaction.h:347
static const CAmount COIN
Definition: amount.h:14
unsigned int nHeight
Definition: script.h:66
static CTransactionRef MakeTransactionRef()
Definition: transaction.h:388
Definition: script.h:67
bool KeepRunning()
Definition: bench.h:69
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:238
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
Definition: txmempool.h:64
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
Definition: script.h:69
#define LOCK2(cs1, cs2)
Definition: sync.h:227
Definition: script.h:71
Definition: script.h:64
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate...
Definition: validation.cpp:128
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:18
std::vector< CTxOut > vout
Definition: transaction.h:348
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:53
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:443
static void MempoolEviction(benchmark::State &state)
uint256 GetHash() const
Compute the hash of this CMutableTransaction.
Definition: transaction.cpp:62
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:390
unsigned int sigOpCost
Definition: script.h:70
bool spendsCoinbase
A mutable version of CTransaction.
Definition: transaction.h:345
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:1035
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it...
Definition: txmempool.h:520
LockPoints lp