Bitcoin ABC  0.22.13
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::State &state) {
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  while (state.KeepRunning()) {
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 
128 BENCHMARK(MempoolEviction, 41000);
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:338
Definition: script.h:59
BENCHMARK(MempoolEviction, 41000)
TxId GetId() const
Compute the id and hash of this CMutableTransaction.
Definition: transaction.cpp:48
static const std::string REGTEST
size_t DynamicMemoryUsage() const
Definition: txmempool.cpp:1058
unsigned int nHeight
Definition: script.h:57
static CTransactionRef MakeTransactionRef()
Definition: transaction.h:339
Definition: amount.h:17
Definition: script.h:58
bool KeepRunning()
Definition: bench.h:71
static constexpr Amount SATOSHI
Definition: amount.h:151
std::vector< CTxOut > vout
Definition: transaction.h:300
size_t GetSerializeSize(const T &t, int nVersion=0)
Definition: serialize.h:1193
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
Definition: txmempool.h:65
std::vector< CTxIn > vin
Definition: transaction.h:299
Definition: script.h:60
#define LOCK2(cs1, cs2)
Definition: sync.h:233
static constexpr Amount COIN
Definition: amount.h:153
static void AddTx(const CTransactionRef &tx, const Amount &nFee, CTxMemPool &pool) EXCLUSIVE_LOCKS_REQUIRED(cs_main
Definition: script.h:62
Definition: script.h:55
unsigned int nSigOpCount
RecursiveMutex cs_main
Global state.
Definition: validation.cpp:95
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:22
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:56
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:468
static void MempoolEviction(benchmark::State &state)
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:430
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:11
A mutable version of CTransaction.
Definition: transaction.h:297
Definition: script.h:61
bool spendsCoinbase
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:1198
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it...
Definition: txmempool.h:545
LockPoints lp