Bitcoin Core  0.18.99
P2P Digital Currency
mempool_eviction.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2018 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 <txmempool.h>
8 
9 #include <list>
10 #include <vector>
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,
21  spendsCoinbase, sigOpCost, lp));
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::State& state)
28 {
30  tx1.vin.resize(1);
31  tx1.vin[0].scriptSig = CScript() << OP_1;
32  tx1.vin[0].scriptWitness.stack.push_back({1});
33  tx1.vout.resize(1);
34  tx1.vout[0].scriptPubKey = CScript() << OP_1 << OP_EQUAL;
35  tx1.vout[0].nValue = 10 * COIN;
36 
38  tx2.vin.resize(1);
39  tx2.vin[0].scriptSig = CScript() << OP_2;
40  tx2.vin[0].scriptWitness.stack.push_back({2});
41  tx2.vout.resize(1);
42  tx2.vout[0].scriptPubKey = CScript() << OP_2 << OP_EQUAL;
43  tx2.vout[0].nValue = 10 * COIN;
44 
46  tx3.vin.resize(1);
47  tx3.vin[0].prevout = COutPoint(tx2.GetHash(), 0);
48  tx3.vin[0].scriptSig = CScript() << OP_2;
49  tx3.vin[0].scriptWitness.stack.push_back({3});
50  tx3.vout.resize(1);
51  tx3.vout[0].scriptPubKey = CScript() << OP_3 << OP_EQUAL;
52  tx3.vout[0].nValue = 10 * COIN;
53 
55  tx4.vin.resize(2);
56  tx4.vin[0].prevout.SetNull();
57  tx4.vin[0].scriptSig = CScript() << OP_4;
58  tx4.vin[0].scriptWitness.stack.push_back({4});
59  tx4.vin[1].prevout.SetNull();
60  tx4.vin[1].scriptSig = CScript() << OP_4;
61  tx4.vin[1].scriptWitness.stack.push_back({4});
62  tx4.vout.resize(2);
63  tx4.vout[0].scriptPubKey = CScript() << OP_4 << OP_EQUAL;
64  tx4.vout[0].nValue = 10 * COIN;
65  tx4.vout[1].scriptPubKey = CScript() << OP_4 << OP_EQUAL;
66  tx4.vout[1].nValue = 10 * COIN;
67 
69  tx5.vin.resize(2);
70  tx5.vin[0].prevout = COutPoint(tx4.GetHash(), 0);
71  tx5.vin[0].scriptSig = CScript() << OP_4;
72  tx5.vin[0].scriptWitness.stack.push_back({4});
73  tx5.vin[1].prevout.SetNull();
74  tx5.vin[1].scriptSig = CScript() << OP_5;
75  tx5.vin[1].scriptWitness.stack.push_back({5});
76  tx5.vout.resize(2);
77  tx5.vout[0].scriptPubKey = CScript() << OP_5 << OP_EQUAL;
78  tx5.vout[0].nValue = 10 * COIN;
79  tx5.vout[1].scriptPubKey = CScript() << OP_5 << OP_EQUAL;
80  tx5.vout[1].nValue = 10 * COIN;
81 
83  tx6.vin.resize(2);
84  tx6.vin[0].prevout = COutPoint(tx4.GetHash(), 1);
85  tx6.vin[0].scriptSig = CScript() << OP_4;
86  tx6.vin[0].scriptWitness.stack.push_back({4});
87  tx6.vin[1].prevout.SetNull();
88  tx6.vin[1].scriptSig = CScript() << OP_6;
89  tx6.vin[1].scriptWitness.stack.push_back({6});
90  tx6.vout.resize(2);
91  tx6.vout[0].scriptPubKey = CScript() << OP_6 << OP_EQUAL;
92  tx6.vout[0].nValue = 10 * COIN;
93  tx6.vout[1].scriptPubKey = CScript() << OP_6 << OP_EQUAL;
94  tx6.vout[1].nValue = 10 * COIN;
95 
97  tx7.vin.resize(2);
98  tx7.vin[0].prevout = COutPoint(tx5.GetHash(), 0);
99  tx7.vin[0].scriptSig = CScript() << OP_5;
100  tx7.vin[0].scriptWitness.stack.push_back({5});
101  tx7.vin[1].prevout = COutPoint(tx6.GetHash(), 0);
102  tx7.vin[1].scriptSig = CScript() << OP_6;
103  tx7.vin[1].scriptWitness.stack.push_back({6});
104  tx7.vout.resize(2);
105  tx7.vout[0].scriptPubKey = CScript() << OP_7 << OP_EQUAL;
106  tx7.vout[0].nValue = 10 * COIN;
107  tx7.vout[1].scriptPubKey = CScript() << OP_7 << OP_EQUAL;
108  tx7.vout[1].nValue = 10 * COIN;
109 
110  CTxMemPool pool;
111  LOCK2(cs_main, pool.cs);
112  // Create transaction references outside the "hot loop"
113  const CTransactionRef tx1_r{MakeTransactionRef(tx1)};
114  const CTransactionRef tx2_r{MakeTransactionRef(tx2)};
115  const CTransactionRef tx3_r{MakeTransactionRef(tx3)};
116  const CTransactionRef tx4_r{MakeTransactionRef(tx4)};
117  const CTransactionRef tx5_r{MakeTransactionRef(tx5)};
118  const CTransactionRef tx6_r{MakeTransactionRef(tx6)};
119  const CTransactionRef tx7_r{MakeTransactionRef(tx7)};
120 
121  while (state.KeepRunning()) {
122  AddTx(tx1_r, 10000LL, pool);
123  AddTx(tx2_r, 5000LL, pool);
124  AddTx(tx3_r, 20000LL, pool);
125  AddTx(tx4_r, 7000LL, pool);
126  AddTx(tx5_r, 1000LL, pool);
127  AddTx(tx6_r, 1100LL, pool);
128  AddTx(tx7_r, 9000LL, pool);
129  pool.TrimToSize(pool.DynamicMemoryUsage() * 3 / 4);
131  }
132 }
133 
134 BENCHMARK(MempoolEviction, 41000);
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:408
Definition: script.h:68
static void AddTx(const CTransactionRef &tx, const CAmount &nFee, CTxMemPool &pool) EXCLUSIVE_LOCKS_REQUIRED(cs_main
BENCHMARK(MempoolEviction, 41000)
size_t DynamicMemoryUsage() const
Definition: txmempool.cpp:906
std::vector< CTxIn > vin
Definition: transaction.h:368
static const CAmount COIN
Definition: amount.h:14
unsigned int nHeight
Definition: script.h:66
static CTransactionRef MakeTransactionRef()
Definition: transaction.h:409
Definition: script.h:67
bool KeepRunning()
Definition: bench.h:70
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:67
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
Definition: script.h:69
#define LOCK2(cs1, cs2)
Definition: sync.h:183
CCriticalSection cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate...
Definition: validation.cpp:107
Definition: script.h:71
Definition: script.h:64
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:369
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:51
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:442
static void MempoolEviction(benchmark::State &state)
uint256 GetHash() const
Compute the hash of this CMutableTransaction.
Definition: transaction.cpp:60
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:366
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:1013
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it...
Definition: txmempool.h:517
LockPoints lp