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