Bitcoin Core
31.1.0
P2P Digital Currency
Toggle main menu visibility
Loading...
Searching...
No Matches
src
bench
mempool_eviction.cpp
Go to the documentation of this file.
1
// Copyright (c) 2011-present 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/cs_main.h
>
8
#include <
policy/policy.h
>
9
#include <
primitives/transaction.h
>
10
#include <
script/script.h
>
11
#include <
sync.h
>
12
#include <
test/util/setup_common.h
>
13
#include <
test/util/txmempool.h
>
14
#include <
txmempool.h
>
15
#include <
util/check.h
>
16
17
#include <cstdint>
18
#include <memory>
19
#include <vector>
20
21
22
static
void
AddTx
(
const
CTransactionRef
& tx,
const
CAmount
& nFee,
CTxMemPool
& pool)
EXCLUSIVE_LOCKS_REQUIRED
(
cs_main
, pool.cs)
23
{
24
int64_t nTime = 0;
25
unsigned
int
nHeight
= 1;
26
uint64_t
sequence
= 0;
27
bool
spendsCoinbase
=
false
;
28
unsigned
int
sigOpCost
= 4;
29
LockPoints
lp
;
30
TryAddToMempool
(pool,
CTxMemPoolEntry
(
31
tx, nFee, nTime,
nHeight
,
sequence
,
32
spendsCoinbase
,
sigOpCost
,
lp
));
33
}
34
35
// Right now this is only testing eviction performance in an extremely small
36
// mempool. Code needs to be written to generate a much wider variety of
37
// unique transactions for a more meaningful performance measurement.
38
static
void
MempoolEviction
(
benchmark::Bench
& bench)
39
{
40
const
auto
testing_setup =
MakeNoLogFileContext<const TestingSetup>
();
41
42
CMutableTransaction
tx1 =
CMutableTransaction
();
43
tx1.
vin
.resize(1);
44
tx1.
vin
[0].scriptSig =
CScript
() <<
OP_1
;
45
tx1.
vin
[0].scriptWitness.stack.push_back({1});
46
tx1.
vout
.resize(1);
47
tx1.
vout
[0].scriptPubKey =
CScript
() <<
OP_1
<<
OP_EQUAL
;
48
tx1.
vout
[0].nValue = 10 * COIN;
49
50
CMutableTransaction
tx2 =
CMutableTransaction
();
51
tx2.
vin
.resize(1);
52
tx2.
vin
[0].scriptSig =
CScript
() <<
OP_2
;
53
tx2.
vin
[0].scriptWitness.stack.push_back({2});
54
tx2.
vout
.resize(1);
55
tx2.
vout
[0].scriptPubKey =
CScript
() <<
OP_2
<<
OP_EQUAL
;
56
tx2.
vout
[0].nValue = 10 * COIN;
57
58
CMutableTransaction
tx3 =
CMutableTransaction
();
59
tx3.
vin
.resize(1);
60
tx3.
vin
[0].prevout =
COutPoint
(tx2.
GetHash
(), 0);
61
tx3.
vin
[0].scriptSig =
CScript
() <<
OP_2
;
62
tx3.
vin
[0].scriptWitness.stack.push_back({3});
63
tx3.
vout
.resize(1);
64
tx3.
vout
[0].scriptPubKey =
CScript
() <<
OP_3
<<
OP_EQUAL
;
65
tx3.
vout
[0].nValue = 10 * COIN;
66
67
CMutableTransaction
tx4 =
CMutableTransaction
();
68
tx4.
vin
.resize(2);
69
tx4.
vin
[0].prevout.SetNull();
70
tx4.
vin
[0].scriptSig =
CScript
() <<
OP_4
;
71
tx4.
vin
[0].scriptWitness.stack.push_back({4});
72
tx4.
vin
[1].prevout.SetNull();
73
tx4.
vin
[1].scriptSig =
CScript
() <<
OP_4
;
74
tx4.
vin
[1].scriptWitness.stack.push_back({4});
75
tx4.
vout
.resize(2);
76
tx4.
vout
[0].scriptPubKey =
CScript
() <<
OP_4
<<
OP_EQUAL
;
77
tx4.
vout
[0].nValue = 10 * COIN;
78
tx4.
vout
[1].scriptPubKey =
CScript
() <<
OP_4
<<
OP_EQUAL
;
79
tx4.
vout
[1].nValue = 10 * COIN;
80
81
CMutableTransaction
tx5 =
CMutableTransaction
();
82
tx5.
vin
.resize(2);
83
tx5.
vin
[0].prevout =
COutPoint
(tx4.
GetHash
(), 0);
84
tx5.
vin
[0].scriptSig =
CScript
() <<
OP_4
;
85
tx5.
vin
[0].scriptWitness.stack.push_back({4});
86
tx5.
vin
[1].prevout.SetNull();
87
tx5.
vin
[1].scriptSig =
CScript
() <<
OP_5
;
88
tx5.
vin
[1].scriptWitness.stack.push_back({5});
89
tx5.
vout
.resize(2);
90
tx5.
vout
[0].scriptPubKey =
CScript
() <<
OP_5
<<
OP_EQUAL
;
91
tx5.
vout
[0].nValue = 10 * COIN;
92
tx5.
vout
[1].scriptPubKey =
CScript
() <<
OP_5
<<
OP_EQUAL
;
93
tx5.
vout
[1].nValue = 10 * COIN;
94
95
CMutableTransaction
tx6 =
CMutableTransaction
();
96
tx6.
vin
.resize(2);
97
tx6.
vin
[0].prevout =
COutPoint
(tx4.
GetHash
(), 1);
98
tx6.
vin
[0].scriptSig =
CScript
() <<
OP_4
;
99
tx6.
vin
[0].scriptWitness.stack.push_back({4});
100
tx6.
vin
[1].prevout.SetNull();
101
tx6.
vin
[1].scriptSig =
CScript
() <<
OP_6
;
102
tx6.
vin
[1].scriptWitness.stack.push_back({6});
103
tx6.
vout
.resize(2);
104
tx6.
vout
[0].scriptPubKey =
CScript
() <<
OP_6
<<
OP_EQUAL
;
105
tx6.
vout
[0].nValue = 10 * COIN;
106
tx6.
vout
[1].scriptPubKey =
CScript
() <<
OP_6
<<
OP_EQUAL
;
107
tx6.
vout
[1].nValue = 10 * COIN;
108
109
CMutableTransaction
tx7 =
CMutableTransaction
();
110
tx7.
vin
.resize(2);
111
tx7.
vin
[0].prevout =
COutPoint
(tx5.
GetHash
(), 0);
112
tx7.
vin
[0].scriptSig =
CScript
() <<
OP_5
;
113
tx7.
vin
[0].scriptWitness.stack.push_back({5});
114
tx7.
vin
[1].prevout =
COutPoint
(tx6.
GetHash
(), 0);
115
tx7.
vin
[1].scriptSig =
CScript
() <<
OP_6
;
116
tx7.
vin
[1].scriptWitness.stack.push_back({6});
117
tx7.
vout
.resize(2);
118
tx7.
vout
[0].scriptPubKey =
CScript
() <<
OP_7
<<
OP_EQUAL
;
119
tx7.
vout
[0].nValue = 10 * COIN;
120
tx7.
vout
[1].scriptPubKey =
CScript
() <<
OP_7
<<
OP_EQUAL
;
121
tx7.
vout
[1].nValue = 10 * COIN;
122
123
CTxMemPool
& pool = *
Assert
(testing_setup->m_node.mempool);
124
LOCK2
(
cs_main
, pool.
cs
);
125
// Create transaction references outside the "hot loop"
126
const
CTransactionRef
tx1_r{MakeTransactionRef(tx1)};
127
const
CTransactionRef
tx2_r{MakeTransactionRef(tx2)};
128
const
CTransactionRef
tx3_r{MakeTransactionRef(tx3)};
129
const
CTransactionRef
tx4_r{MakeTransactionRef(tx4)};
130
const
CTransactionRef
tx5_r{MakeTransactionRef(tx5)};
131
const
CTransactionRef
tx6_r{MakeTransactionRef(tx6)};
132
const
CTransactionRef
tx7_r{MakeTransactionRef(tx7)};
133
134
bench.
run
([&]()
NO_THREAD_SAFETY_ANALYSIS
{
135
AddTx
(tx1_r, 10000LL, pool);
136
AddTx
(tx2_r, 5000LL, pool);
137
AddTx
(tx3_r, 20000LL, pool);
138
AddTx
(tx4_r, 7000LL, pool);
139
AddTx
(tx5_r, 1000LL, pool);
140
AddTx
(tx6_r, 1100LL, pool);
141
AddTx
(tx7_r, 9000LL, pool);
142
pool.
TrimToSize
(pool.
DynamicMemoryUsage
() * 3 / 4);
143
pool.
TrimToSize
(
GetVirtualTransactionSize
(*tx1_r));
144
});
145
}
146
147
BENCHMARK
(
MempoolEviction
);
amount.h
CAmount
int64_t CAmount
Amount in satoshis (Can be negative).
Definition
amount.h:12
bench.h
BENCHMARK
#define BENCHMARK(n)
Definition
bench.h:68
TryAddToMempool
TryAddToMempool(pool, CTxMemPoolEntry(tx, fee, 0, 1, 0, false, 4, lp))
check.h
Assert
#define Assert(val)
Identity function.
Definition
check.h:113
COutPoint::COutPoint
COutPoint()
Definition
transaction.h:36
CScript::CScript
CScript()=default
CTxMemPoolEntry::CTxMemPoolEntry
CTxMemPoolEntry(const CTxMemPoolEntry &)=delete
CTxMemPool
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition
txmempool.h:187
CTxMemPool::cs
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it.
Definition
txmempool.h:258
CTxMemPool::TrimToSize
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:861
CTxMemPool::DynamicMemoryUsage
size_t DynamicMemoryUsage() const
Definition
txmempool.cpp:778
ankerl::nanobench::Bench
Main entry point to nanobench's benchmarking facility.
Definition
nanobench.h:627
ankerl::nanobench::Bench::run
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition
nanobench.h:1234
cs_main
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition
cs_main.cpp:8
cs_main.h
nHeight
unsigned int nHeight
Definition
mempool_ephemeral_spends.cpp:26
spendsCoinbase
bool spendsCoinbase
Definition
mempool_ephemeral_spends.cpp:28
sigOpCost
unsigned int sigOpCost
Definition
mempool_ephemeral_spends.cpp:29
sequence
uint64_t sequence
Definition
mempool_ephemeral_spends.cpp:27
lp
LockPoints lp
Definition
mempool_ephemeral_spends.cpp:31
MempoolEviction
static void MempoolEviction(benchmark::Bench &bench)
Definition
mempool_eviction.cpp:38
AddTx
static void AddTx(const CTransactionRef &tx, const CAmount &nFee, CTxMemPool &pool) EXCLUSIVE_LOCKS_REQUIRED(cs_main
GetVirtualTransactionSize
int64_t GetVirtualTransactionSize(int64_t nWeight, int64_t nSigOpCost, unsigned int bytes_per_sigop)
Compute the virtual transaction size (weight reinterpreted as bytes).
Definition
policy.cpp:381
policy.h
transaction.h
CTransactionRef
std::shared_ptr< const CTransaction > CTransactionRef
Definition
transaction.h:403
script.h
OP_2
@ OP_2
Definition
script.h:85
OP_EQUAL
@ OP_EQUAL
Definition
script.h:146
OP_4
@ OP_4
Definition
script.h:87
OP_1
@ OP_1
Definition
script.h:83
OP_3
@ OP_3
Definition
script.h:86
OP_6
@ OP_6
Definition
script.h:89
OP_7
@ OP_7
Definition
script.h:90
OP_5
@ OP_5
Definition
script.h:88
setup_common.h
MakeNoLogFileContext
std::unique_ptr< T > MakeNoLogFileContext(const ChainType chain_type=ChainType::REGTEST, TestOpts opts={})
Make a test setup that has disk access to the debug.log file disabled.
Definition
setup_common.h:250
CMutableTransaction
A mutable version of CTransaction.
Definition
transaction.h:358
CMutableTransaction::CMutableTransaction
CMutableTransaction()
Definition
transaction.cpp:66
CMutableTransaction::vout
std::vector< CTxOut > vout
Definition
transaction.h:360
CMutableTransaction::GetHash
Txid GetHash() const
Compute the hash of this CMutableTransaction.
Definition
transaction.cpp:69
CMutableTransaction::vin
std::vector< CTxIn > vin
Definition
transaction.h:359
LockPoints
Definition
mempool_entry.h:26
sync.h
LOCK2
#define LOCK2(cs1, cs2)
Definition
sync.h:259
txmempool.h
EXCLUSIVE_LOCKS_REQUIRED
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition
threadsafety.h:51
NO_THREAD_SAFETY_ANALYSIS
#define NO_THREAD_SAFETY_ANALYSIS
Definition
threadsafety.h:53
txmempool.h
Generated on
for Bitcoin Core by
1.18.0