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