1 // Copyright (c) 2011-2020 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 
AddTx(const CTransactionRef & tx,const CAmount & nFee,CTxMemPool & pool)11 static void AddTx(const CTransactionRef& tx, const CAmount& nFee, CTxMemPool& pool) EXCLUSIVE_LOCKS_REQUIRED(cs_main, pool.cs)
12 {
13     int64_t nTime = 0;
14     unsigned int nHeight = 1;
15     bool spendsCoinbase = false;
16     unsigned int sigOpCost = 4;
17     LockPoints lp;
18     pool.addUnchecked(CTxMemPoolEntry(
19         tx, nFee, nTime, nHeight,
20         spendsCoinbase, sigOpCost, lp));
21 }
22 
23 // Right now this is only testing eviction performance in an extremely small
24 // mempool. Code needs to be written to generate a much wider variety of
25 // unique transactions for a more meaningful performance measurement.
MempoolEviction(benchmark::Bench & bench)26 static void MempoolEviction(benchmark::Bench& bench)
27 {
28     const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
29 
30     CMutableTransaction tx1 = CMutableTransaction();
31     tx1.vin.resize(1);
32     tx1.vin[0].scriptSig = CScript() << OP_1;
33     tx1.vin[0].scriptWitness.stack.push_back({1});
34     tx1.vout.resize(1);
35     tx1.vout[0].scriptPubKey = CScript() << OP_1 << OP_EQUAL;
36     tx1.vout[0].nValue = 10 * COIN;
37 
38     CMutableTransaction tx2 = CMutableTransaction();
39     tx2.vin.resize(1);
40     tx2.vin[0].scriptSig = CScript() << OP_2;
41     tx2.vin[0].scriptWitness.stack.push_back({2});
42     tx2.vout.resize(1);
43     tx2.vout[0].scriptPubKey = CScript() << OP_2 << OP_EQUAL;
44     tx2.vout[0].nValue = 10 * COIN;
45 
46     CMutableTransaction tx3 = CMutableTransaction();
47     tx3.vin.resize(1);
48     tx3.vin[0].prevout = COutPoint(tx2.GetHash(), 0);
49     tx3.vin[0].scriptSig = CScript() << OP_2;
50     tx3.vin[0].scriptWitness.stack.push_back({3});
51     tx3.vout.resize(1);
52     tx3.vout[0].scriptPubKey = CScript() << OP_3 << OP_EQUAL;
53     tx3.vout[0].nValue = 10 * COIN;
54 
55     CMutableTransaction tx4 = CMutableTransaction();
56     tx4.vin.resize(2);
57     tx4.vin[0].prevout.SetNull();
58     tx4.vin[0].scriptSig = CScript() << OP_4;
59     tx4.vin[0].scriptWitness.stack.push_back({4});
60     tx4.vin[1].prevout.SetNull();
61     tx4.vin[1].scriptSig = CScript() << OP_4;
62     tx4.vin[1].scriptWitness.stack.push_back({4});
63     tx4.vout.resize(2);
64     tx4.vout[0].scriptPubKey = CScript() << OP_4 << OP_EQUAL;
65     tx4.vout[0].nValue = 10 * COIN;
66     tx4.vout[1].scriptPubKey = CScript() << OP_4 << OP_EQUAL;
67     tx4.vout[1].nValue = 10 * COIN;
68 
69     CMutableTransaction tx5 = CMutableTransaction();
70     tx5.vin.resize(2);
71     tx5.vin[0].prevout = COutPoint(tx4.GetHash(), 0);
72     tx5.vin[0].scriptSig = CScript() << OP_4;
73     tx5.vin[0].scriptWitness.stack.push_back({4});
74     tx5.vin[1].prevout.SetNull();
75     tx5.vin[1].scriptSig = CScript() << OP_5;
76     tx5.vin[1].scriptWitness.stack.push_back({5});
77     tx5.vout.resize(2);
78     tx5.vout[0].scriptPubKey = CScript() << OP_5 << OP_EQUAL;
79     tx5.vout[0].nValue = 10 * COIN;
80     tx5.vout[1].scriptPubKey = CScript() << OP_5 << OP_EQUAL;
81     tx5.vout[1].nValue = 10 * COIN;
82 
83     CMutableTransaction tx6 = CMutableTransaction();
84     tx6.vin.resize(2);
85     tx6.vin[0].prevout = COutPoint(tx4.GetHash(), 1);
86     tx6.vin[0].scriptSig = CScript() << OP_4;
87     tx6.vin[0].scriptWitness.stack.push_back({4});
88     tx6.vin[1].prevout.SetNull();
89     tx6.vin[1].scriptSig = CScript() << OP_6;
90     tx6.vin[1].scriptWitness.stack.push_back({6});
91     tx6.vout.resize(2);
92     tx6.vout[0].scriptPubKey = CScript() << OP_6 << OP_EQUAL;
93     tx6.vout[0].nValue = 10 * COIN;
94     tx6.vout[1].scriptPubKey = CScript() << OP_6 << OP_EQUAL;
95     tx6.vout[1].nValue = 10 * COIN;
96 
97     CMutableTransaction tx7 = CMutableTransaction();
98     tx7.vin.resize(2);
99     tx7.vin[0].prevout = COutPoint(tx5.GetHash(), 0);
100     tx7.vin[0].scriptSig = CScript() << OP_5;
101     tx7.vin[0].scriptWitness.stack.push_back({5});
102     tx7.vin[1].prevout = COutPoint(tx6.GetHash(), 0);
103     tx7.vin[1].scriptSig = CScript() << OP_6;
104     tx7.vin[1].scriptWitness.stack.push_back({6});
105     tx7.vout.resize(2);
106     tx7.vout[0].scriptPubKey = CScript() << OP_7 << OP_EQUAL;
107     tx7.vout[0].nValue = 10 * COIN;
108     tx7.vout[1].scriptPubKey = CScript() << OP_7 << OP_EQUAL;
109     tx7.vout[1].nValue = 10 * COIN;
110 
111     CTxMemPool pool;
112     LOCK2(cs_main, pool.cs);
113     // Create transaction references outside the "hot loop"
114     const CTransactionRef tx1_r{MakeTransactionRef(tx1)};
115     const CTransactionRef tx2_r{MakeTransactionRef(tx2)};
116     const CTransactionRef tx3_r{MakeTransactionRef(tx3)};
117     const CTransactionRef tx4_r{MakeTransactionRef(tx4)};
118     const CTransactionRef tx5_r{MakeTransactionRef(tx5)};
119     const CTransactionRef tx6_r{MakeTransactionRef(tx6)};
120     const CTransactionRef tx7_r{MakeTransactionRef(tx7)};
121 
122     bench.run([&]() NO_THREAD_SAFETY_ANALYSIS {
123         AddTx(tx1_r, 10000LL, pool);
124         AddTx(tx2_r, 5000LL, pool);
125         AddTx(tx3_r, 20000LL, pool);
126         AddTx(tx4_r, 7000LL, pool);
127         AddTx(tx5_r, 1000LL, pool);
128         AddTx(tx6_r, 1100LL, pool);
129         AddTx(tx7_r, 9000LL, pool);
130         pool.TrimToSize(pool.DynamicMemoryUsage() * 3 / 4);
131         pool.TrimToSize(GetVirtualTransactionSize(*tx1_r));
132     });
133 }
134 
135 BENCHMARK(MempoolEviction);
136