Raven Core  3.0.0
P2P Digital Currency
mempool_eviction.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2016 The Bitcoin Core developers
2 // Copyright (c) 2017-2019 The Raven Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include "bench.h"
7 #include "policy/policy.h"
8 #include "txmempool.h"
9 
10 #include <list>
11 #include <vector>
12 
13 static void AddTx(const CTransaction& tx, const CAmount& nFee, CTxMemPool& pool)
14 {
15  int64_t nTime = 0;
16  unsigned int nHeight = 1;
17  bool spendsCoinbase = false;
18  unsigned int sigOpCost = 4;
19  LockPoints lp;
21  MakeTransactionRef(tx), nFee, nTime, nHeight,
22  spendsCoinbase, sigOpCost, lp));
23 }
24 
25 // Right now this is only testing eviction performance in an extremely small
26 // mempool. Code needs to be written to generate a much wider variety of
27 // unique transactions for a more meaningful performance measurement.
28 static void MempoolEviction(benchmark::State& state)
29 {
31  tx1.vin.resize(1);
32  tx1.vin[0].scriptSig = CScript() << OP_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.vout.resize(1);
41  tx2.vout[0].scriptPubKey = CScript() << OP_2 << OP_EQUAL;
42  tx2.vout[0].nValue = 10 * COIN;
43 
45  tx3.vin.resize(1);
46  tx3.vin[0].prevout = COutPoint(tx2.GetHash(), 0);
47  tx3.vin[0].scriptSig = CScript() << OP_2;
48  tx3.vout.resize(1);
49  tx3.vout[0].scriptPubKey = CScript() << OP_3 << OP_EQUAL;
50  tx3.vout[0].nValue = 10 * COIN;
51 
53  tx4.vin.resize(2);
54  tx4.vin[0].prevout.SetNull();
55  tx4.vin[0].scriptSig = CScript() << OP_4;
56  tx4.vin[1].prevout.SetNull();
57  tx4.vin[1].scriptSig = CScript() << OP_4;
58  tx4.vout.resize(2);
59  tx4.vout[0].scriptPubKey = CScript() << OP_4 << OP_EQUAL;
60  tx4.vout[0].nValue = 10 * COIN;
61  tx4.vout[1].scriptPubKey = CScript() << OP_4 << OP_EQUAL;
62  tx4.vout[1].nValue = 10 * COIN;
63 
65  tx5.vin.resize(2);
66  tx5.vin[0].prevout = COutPoint(tx4.GetHash(), 0);
67  tx5.vin[0].scriptSig = CScript() << OP_4;
68  tx5.vin[1].prevout.SetNull();
69  tx5.vin[1].scriptSig = CScript() << OP_5;
70  tx5.vout.resize(2);
71  tx5.vout[0].scriptPubKey = CScript() << OP_5 << OP_EQUAL;
72  tx5.vout[0].nValue = 10 * COIN;
73  tx5.vout[1].scriptPubKey = CScript() << OP_5 << OP_EQUAL;
74  tx5.vout[1].nValue = 10 * COIN;
75 
77  tx6.vin.resize(2);
78  tx6.vin[0].prevout = COutPoint(tx4.GetHash(), 1);
79  tx6.vin[0].scriptSig = CScript() << OP_4;
80  tx6.vin[1].prevout.SetNull();
81  tx6.vin[1].scriptSig = CScript() << OP_6;
82  tx6.vout.resize(2);
83  tx6.vout[0].scriptPubKey = CScript() << OP_6 << OP_EQUAL;
84  tx6.vout[0].nValue = 10 * COIN;
85  tx6.vout[1].scriptPubKey = CScript() << OP_6 << OP_EQUAL;
86  tx6.vout[1].nValue = 10 * COIN;
87 
89  tx7.vin.resize(2);
90  tx7.vin[0].prevout = COutPoint(tx5.GetHash(), 0);
91  tx7.vin[0].scriptSig = CScript() << OP_5;
92  tx7.vin[1].prevout = COutPoint(tx6.GetHash(), 0);
93  tx7.vin[1].scriptSig = CScript() << OP_6;
94  tx7.vout.resize(2);
95  tx7.vout[0].scriptPubKey = CScript() << OP_7 << OP_EQUAL;
96  tx7.vout[0].nValue = 10 * COIN;
97  tx7.vout[1].scriptPubKey = CScript() << OP_7 << OP_EQUAL;
98  tx7.vout[1].nValue = 10 * COIN;
99 
100  CTxMemPool pool;
101 
102  while (state.KeepRunning()) {
103  AddTx(tx1, 10000LL, pool);
104  AddTx(tx2, 5000LL, pool);
105  AddTx(tx3, 20000LL, pool);
106  AddTx(tx4, 7000LL, pool);
107  AddTx(tx5, 1000LL, pool);
108  AddTx(tx6, 1100LL, pool);
109  AddTx(tx7, 9000LL, pool);
110  pool.TrimToSize(pool.DynamicMemoryUsage() * 3 / 4);
112  }
113 }
114 
115 BENCHMARK(MempoolEviction);
Definition: script.h:65
int64_t GetVirtualTransactionSize(int64_t nWeight, int64_t nSigOpCost)
Compute the virtual transaction size (weight reinterpreted as bytes).
Definition: policy.cpp:266
size_t DynamicMemoryUsage() const
Definition: txmempool.cpp:1281
std::vector< CTxIn > vin
Definition: transaction.h:391
Definition: script.h:63
Definition: script.h:64
bool KeepRunning()
Definition: bench.cpp:44
void TrimToSize(size_t sizelimit, std::vector< COutPoint > *pvNoSpendsRemaining=nullptr)
Remove transactions from the mempool until its dynamic size is <= sizelimit.
Definition: txmempool.cpp:1389
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
Definition: txmempool.h:68
int64_t CAmount
Amount in corbies (Can be negative)
Definition: amount.h:13
Definition: script.h:66
const uint256 & GetHash() const
Definition: transaction.h:320
Definition: script.h:68
Definition: script.h:61
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:22
std::vector< CTxOut > vout
Definition: transaction.h:392
BENCHMARK(MempoolEviction)
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:416
uint256 GetHash() const
Compute the hash of this CMutableTransaction.
Definition: transaction.cpp:69
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:396
Definition: script.h:67
A mutable version of CTransaction.
Definition: transaction.h:389
bool addUnchecked(const uint256 &hash, const CTxMemPoolEntry &entry, bool validFeeEstimate=true)
Definition: txmempool.cpp:1311
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:270