Bitcoin ABC  0.26.3
P2P Digital Currency
mempool_eviction.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2016 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 <consensus/amount.h>
7 #include <policy/policy.h>
8 #include <test/util/setup_common.h>
9 #include <txmempool.h>
10 
11 static void AddTx(const CTransactionRef &tx, const Amount &nFee,
13  int64_t nTime = 0;
14  unsigned int nHeight = 1;
15  bool spendsCoinbase = false;
16  unsigned int nSigChecks = 1;
18  pool.addUnchecked(CTxMemPoolEntry(tx, nFee, nTime, nHeight, spendsCoinbase,
19  nSigChecks, lp));
20 }
21 
22 // Right now this is only testing eviction performance in an extremely small
23 // mempool. Code needs to be written to generate a much wider variety of
24 // unique transactions for a more meaningful performance measurement.
25 static void MempoolEviction(benchmark::Bench &bench) {
26  TestingSetup test_setup{
28  /* extra_args */
29  {
30  "-nodebuglogfile",
31  "-nodebug",
32  },
33  };
34 
36  tx1.vin.resize(1);
37  tx1.vin[0].scriptSig = CScript() << OP_1;
38  tx1.vout.resize(1);
39  tx1.vout[0].scriptPubKey = CScript() << OP_1 << OP_EQUAL;
40  tx1.vout[0].nValue = 10 * COIN;
41 
43  tx2.vin.resize(1);
44  tx2.vin[0].scriptSig = CScript() << OP_2;
45  tx2.vout.resize(1);
46  tx2.vout[0].scriptPubKey = CScript() << OP_2 << OP_EQUAL;
47  tx2.vout[0].nValue = 10 * COIN;
48 
50  tx3.vin.resize(1);
51  tx3.vin[0].prevout = COutPoint(tx2.GetId(), 0);
52  tx3.vin[0].scriptSig = CScript() << OP_2;
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 = COutPoint();
60  tx4.vin[0].scriptSig = CScript() << OP_4;
61  tx4.vin[1].prevout = COutPoint();
62  tx4.vin[1].scriptSig = CScript() << OP_4;
63  tx4.vout.resize(2);
64  tx4.vout[0].scriptPubKey = CScript() << OP_4 << OP_EQUAL;
65  tx4.vout[0].nValue = 10 * COIN;
66  tx4.vout[1].scriptPubKey = CScript() << OP_4 << OP_EQUAL;
67  tx4.vout[1].nValue = 10 * COIN;
68 
70  tx5.vin.resize(2);
71  tx5.vin[0].prevout = COutPoint(tx4.GetId(), 0);
72  tx5.vin[0].scriptSig = CScript() << OP_4;
73  tx5.vin[1].prevout = COutPoint();
74  tx5.vin[1].scriptSig = CScript() << OP_5;
75  tx5.vout.resize(2);
76  tx5.vout[0].scriptPubKey = CScript() << OP_5 << OP_EQUAL;
77  tx5.vout[0].nValue = 10 * COIN;
78  tx5.vout[1].scriptPubKey = CScript() << OP_5 << OP_EQUAL;
79  tx5.vout[1].nValue = 10 * COIN;
80 
82  tx6.vin.resize(2);
83  tx6.vin[0].prevout = COutPoint(tx4.GetId(), 1);
84  tx6.vin[0].scriptSig = CScript() << OP_4;
85  tx6.vin[1].prevout = COutPoint();
86  tx6.vin[1].scriptSig = CScript() << OP_6;
87  tx6.vout.resize(2);
88  tx6.vout[0].scriptPubKey = CScript() << OP_6 << OP_EQUAL;
89  tx6.vout[0].nValue = 10 * COIN;
90  tx6.vout[1].scriptPubKey = CScript() << OP_6 << OP_EQUAL;
91  tx6.vout[1].nValue = 10 * COIN;
92 
94  tx7.vin.resize(2);
95  tx7.vin[0].prevout = COutPoint(tx5.GetId(), 0);
96  tx7.vin[0].scriptSig = CScript() << OP_5;
97  tx7.vin[1].prevout = COutPoint(tx6.GetId(), 0);
98  tx7.vin[1].scriptSig = CScript() << OP_6;
99  tx7.vout.resize(2);
100  tx7.vout[0].scriptPubKey = CScript() << OP_7 << OP_EQUAL;
101  tx7.vout[0].nValue = 10 * COIN;
102  tx7.vout[1].scriptPubKey = CScript() << OP_7 << OP_EQUAL;
103  tx7.vout[1].nValue = 10 * COIN;
104 
105  CTxMemPool pool;
106  LOCK2(cs_main, pool.cs);
107  // Create transaction references outside the "hot loop"
108  const CTransactionRef tx1_r{MakeTransactionRef(tx1)};
109  const CTransactionRef tx2_r{MakeTransactionRef(tx2)};
110  const CTransactionRef tx3_r{MakeTransactionRef(tx3)};
111  const CTransactionRef tx4_r{MakeTransactionRef(tx4)};
112  const CTransactionRef tx5_r{MakeTransactionRef(tx5)};
113  const CTransactionRef tx6_r{MakeTransactionRef(tx6)};
114  const CTransactionRef tx7_r{MakeTransactionRef(tx7)};
115 
116  bench.run([&]() NO_THREAD_SAFETY_ANALYSIS {
117  AddTx(tx1_r, 10000 * SATOSHI, pool);
118  AddTx(tx2_r, 5000 * SATOSHI, pool);
119  AddTx(tx3_r, 20000 * SATOSHI, pool);
120  AddTx(tx4_r, 7000 * SATOSHI, pool);
121  AddTx(tx5_r, 1000 * SATOSHI, pool);
122  AddTx(tx6_r, 1100 * SATOSHI, pool);
123  AddTx(tx7_r, 9000 * SATOSHI, pool);
124  pool.TrimToSize(pool.DynamicMemoryUsage() * 3 / 4);
126  });
127 }
128 
static constexpr Amount SATOSHI
Definition: amount.h:143
static constexpr Amount COIN
Definition: amount.h:144
RecursiveMutex cs_main
Global state.
Definition: validation.cpp:113
static const std::string REGTEST
A mutable version of CTransaction.
Definition: transaction.h:280
TxId GetId() const
Compute the id and hash of this CMutableTransaction.
Definition: transaction.cpp:51
std::vector< CTxOut > vout
Definition: transaction.h:283
std::vector< CTxIn > vin
Definition: transaction.h:282
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:22
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:431
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
Definition: txmempool.h:88
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:355
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it.
Definition: txmempool.h:441
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:1116
size_t DynamicMemoryUsage() const
Definition: txmempool.cpp:988
Main entry point to nanobench's benchmarking facility.
Definition: nanobench.h:616
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1183
BENCHMARK(MempoolEviction)
unsigned int nHeight
static void AddTx(const CTransactionRef &tx, const Amount &nFee, CTxMemPool &pool) EXCLUSIVE_LOCKS_REQUIRED(cs_main
unsigned int nSigChecks
bool spendsCoinbase
static void MempoolEviction(benchmark::Bench &bench)
LockPoints lp
static CTransactionRef MakeTransactionRef()
Definition: transaction.h:322
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:321
@ OP_2
Definition: script.h:58
@ OP_EQUAL
Definition: script.h:119
@ OP_4
Definition: script.h:60
@ OP_1
Definition: script.h:56
@ OP_3
Definition: script.h:59
@ OP_6
Definition: script.h:62
@ OP_7
Definition: script.h:63
@ OP_5
Definition: script.h:61
size_t GetSerializeSize(const T &t, int nVersion=0)
Definition: serialize.h:1259
Definition: amount.h:19
#define LOCK2(cs1, cs2)
Definition: sync.h:246
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:56
#define NO_THREAD_SAFETY_ANALYSIS
Definition: threadsafety.h:58
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:11