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 <kernel/mempool_entry.h>
8 #include <policy/policy.h>
9 #include <test/util/setup_common.h>
10 #include <txmempool.h>
11 
12 static void AddTx(const CTransactionRef &tx, const Amount &nFee,
14  int64_t nTime = 0;
15  unsigned int nHeight = 1;
16  unsigned int nSigChecks = 1;
18  pool.addUnchecked(
19  CTxMemPoolEntryRef::make(tx, nFee, nTime, nHeight, 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  const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
27 
29  tx1.vin.resize(1);
30  tx1.vin[0].scriptSig = CScript() << OP_1;
31  tx1.vout.resize(1);
32  tx1.vout[0].scriptPubKey = CScript() << OP_1 << OP_EQUAL;
33  tx1.vout[0].nValue = 10 * COIN;
34 
36  tx2.vin.resize(1);
37  tx2.vin[0].scriptSig = CScript() << OP_2;
38  tx2.vout.resize(1);
39  tx2.vout[0].scriptPubKey = CScript() << OP_2 << OP_EQUAL;
40  tx2.vout[0].nValue = 10 * COIN;
41 
43  tx3.vin.resize(1);
44  tx3.vin[0].prevout = COutPoint(tx2.GetId(), 0);
45  tx3.vin[0].scriptSig = CScript() << OP_2;
46  tx3.vout.resize(1);
47  tx3.vout[0].scriptPubKey = CScript() << OP_3 << OP_EQUAL;
48  tx3.vout[0].nValue = 10 * COIN;
49 
51  tx4.vin.resize(2);
52  tx4.vin[0].prevout = COutPoint();
53  tx4.vin[0].scriptSig = CScript() << OP_4;
54  tx4.vin[1].prevout = COutPoint();
55  tx4.vin[1].scriptSig = CScript() << OP_4;
56  tx4.vout.resize(2);
57  tx4.vout[0].scriptPubKey = CScript() << OP_4 << OP_EQUAL;
58  tx4.vout[0].nValue = 10 * COIN;
59  tx4.vout[1].scriptPubKey = CScript() << OP_4 << OP_EQUAL;
60  tx4.vout[1].nValue = 10 * COIN;
61 
63  tx5.vin.resize(2);
64  tx5.vin[0].prevout = COutPoint(tx4.GetId(), 0);
65  tx5.vin[0].scriptSig = CScript() << OP_4;
66  tx5.vin[1].prevout = COutPoint();
67  tx5.vin[1].scriptSig = CScript() << OP_5;
68  tx5.vout.resize(2);
69  tx5.vout[0].scriptPubKey = CScript() << OP_5 << OP_EQUAL;
70  tx5.vout[0].nValue = 10 * COIN;
71  tx5.vout[1].scriptPubKey = CScript() << OP_5 << OP_EQUAL;
72  tx5.vout[1].nValue = 10 * COIN;
73 
75  tx6.vin.resize(2);
76  tx6.vin[0].prevout = COutPoint(tx4.GetId(), 1);
77  tx6.vin[0].scriptSig = CScript() << OP_4;
78  tx6.vin[1].prevout = COutPoint();
79  tx6.vin[1].scriptSig = CScript() << OP_6;
80  tx6.vout.resize(2);
81  tx6.vout[0].scriptPubKey = CScript() << OP_6 << OP_EQUAL;
82  tx6.vout[0].nValue = 10 * COIN;
83  tx6.vout[1].scriptPubKey = CScript() << OP_6 << OP_EQUAL;
84  tx6.vout[1].nValue = 10 * COIN;
85 
87  tx7.vin.resize(2);
88  tx7.vin[0].prevout = COutPoint(tx5.GetId(), 0);
89  tx7.vin[0].scriptSig = CScript() << OP_5;
90  tx7.vin[1].prevout = COutPoint(tx6.GetId(), 0);
91  tx7.vin[1].scriptSig = CScript() << OP_6;
92  tx7.vout.resize(2);
93  tx7.vout[0].scriptPubKey = CScript() << OP_7 << OP_EQUAL;
94  tx7.vout[0].nValue = 10 * COIN;
95  tx7.vout[1].scriptPubKey = CScript() << OP_7 << OP_EQUAL;
96  tx7.vout[1].nValue = 10 * COIN;
97 
98  CTxMemPool &pool = *Assert(testing_setup->m_node.mempool);
99  LOCK2(cs_main, pool.cs);
100  // Create transaction references outside the "hot loop"
101  const CTransactionRef tx1_r{MakeTransactionRef(tx1)};
102  const CTransactionRef tx2_r{MakeTransactionRef(tx2)};
103  const CTransactionRef tx3_r{MakeTransactionRef(tx3)};
104  const CTransactionRef tx4_r{MakeTransactionRef(tx4)};
105  const CTransactionRef tx5_r{MakeTransactionRef(tx5)};
106  const CTransactionRef tx6_r{MakeTransactionRef(tx6)};
107  const CTransactionRef tx7_r{MakeTransactionRef(tx7)};
108 
109  bench.run([&]() NO_THREAD_SAFETY_ANALYSIS {
110  AddTx(tx1_r, 10000 * SATOSHI, pool);
111  AddTx(tx2_r, 5000 * SATOSHI, pool);
112  AddTx(tx3_r, 20000 * SATOSHI, pool);
113  AddTx(tx4_r, 7000 * SATOSHI, pool);
114  AddTx(tx5_r, 1000 * SATOSHI, pool);
115  AddTx(tx6_r, 1100 * SATOSHI, pool);
116  AddTx(tx7_r, 9000 * SATOSHI, pool);
117  pool.TrimToSize(pool.DynamicMemoryUsage() * 3 / 4);
119  });
120 }
121 
static constexpr Amount SATOSHI
Definition: amount.h:143
static constexpr Amount COIN
Definition: amount.h:144
#define Assert(val)
Identity function.
Definition: check.h:84
A mutable version of CTransaction.
Definition: transaction.h:274
TxId GetId() const
Compute the id and hash of this CMutableTransaction.
Definition: transaction.cpp:52
std::vector< CTxOut > vout
Definition: transaction.h:277
std::vector< CTxIn > vin
Definition: transaction.h:276
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:20
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:431
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:209
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it.
Definition: txmempool.h:296
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:757
size_t DynamicMemoryUsage() const
Definition: txmempool.cpp:637
static RCUPtr make(Args &&...args)
Construct a new object that is owned by the pointer.
Definition: rcu.h:112
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
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: cs_main.cpp:7
BENCHMARK(MempoolEviction)
unsigned int nHeight
static void AddTx(const CTransactionRef &tx, const Amount &nFee, CTxMemPool &pool) EXCLUSIVE_LOCKS_REQUIRED(cs_main
unsigned int nSigChecks
static void MempoolEviction(benchmark::Bench &bench)
LockPoints lp
static CTransactionRef MakeTransactionRef()
Definition: transaction.h:316
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:315
@ 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:1258
Definition: amount.h:19
#define LOCK2(cs1, cs2)
Definition: sync.h:309
#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