Bitcoin Core 29.99.0
P2P Digital Currency
blockencodings.cpp
Go to the documentation of this file.
1// Copyright (c) 2025-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 <blockencodings.h>
7#include <consensus/amount.h>
8#include <kernel/cs_main.h>
9#include <net_processing.h>
11#include <script/script.h>
12#include <sync.h>
14#include <test/util/txmempool.h>
15#include <txmempool.h>
16#include <util/check.h>
17
18#include <memory>
19#include <vector>
20
21
22static void AddTx(const CTransactionRef& tx, const CAmount& fee, CTxMemPool& pool) EXCLUSIVE_LOCKS_REQUIRED(cs_main, pool.cs)
23{
25 AddToMempool(pool, CTxMemPoolEntry(tx, fee, /*time=*/0, /*entry_height=*/1, /*entry_sequence=*/0, /*spends_coinbase=*/false, /*sigops_cost=*/4, lp));
26}
27
28namespace {
29class BenchCBHAST : public CBlockHeaderAndShortTxIDs
30{
31private:
32 static CBlock DummyBlock()
33 {
34 CBlock block;
35 block.nVersion = 5;
36 block.hashPrevBlock.SetNull();
37 block.hashMerkleRoot.SetNull();
38 block.nTime = 1231006505;
39 block.nBits = 0x1d00ffff;
40 block.nNonce = 2083236893;
41 block.fChecked = false;
43 tx.vin.resize(1);
44 tx.vout.resize(1);
45 block.vtx.emplace_back(MakeTransactionRef(tx)); // dummy coinbase
46 return block;
47 }
48
49public:
50 BenchCBHAST(InsecureRandomContext& rng, int txs) : CBlockHeaderAndShortTxIDs(DummyBlock(), rng.rand64())
51 {
52 shorttxids.reserve(txs);
53 while (txs-- > 0) {
54 shorttxids.push_back(rng.randbits<SHORTTXIDS_LENGTH*8>());
55 }
56 }
57};
58} // anon namespace
59
60static void BlockEncodingBench(benchmark::Bench& bench, size_t n_pool, size_t n_extra)
61{
62 const auto testing_setup = MakeNoLogFileContext<const ChainTestingSetup>(ChainType::MAIN);
63 CTxMemPool& pool = *Assert(testing_setup->m_node.mempool);
65
66 LOCK2(cs_main, pool.cs);
67
68 std::vector<std::pair<Wtxid, CTransactionRef>> extratxn;
69 extratxn.reserve(n_extra);
70
71 // bump up the size of txs
72 std::array<std::byte,200> sigspam;
73 sigspam.fill(std::byte(42));
74
75 // a reasonably large mempool of 50k txs, ~10MB total
76 std::vector<CTransactionRef> refs;
77 refs.reserve(n_pool + n_extra);
78 for (size_t i = 0; i < n_pool + n_extra; ++i) {
80 tx.vin.resize(1);
81 tx.vin[0].scriptSig = CScript() << sigspam;
82 tx.vin[0].scriptWitness.stack.push_back({1});
83 tx.vout.resize(1);
84 tx.vout[0].scriptPubKey = CScript() << OP_1 << OP_EQUAL;
85 tx.vout[0].nValue = i;
86 refs.push_back(MakeTransactionRef(tx));
87 }
88
89 // ensure mempool ordering is different to memory ordering of transactions,
90 // to simulate a mempool that has changed over time
91 std::shuffle(refs.begin(), refs.end(), rng);
92
93 for (size_t i = 0; i < n_pool; ++i) {
94 AddTx(refs[i], /*fee=*/refs[i]->vout[0].nValue, pool);
95 }
96 for (size_t i = n_pool; i < n_pool + n_extra; ++i) {
97 extratxn.emplace_back(refs[i]->GetWitnessHash(), refs[i]);
98 }
99
100 BenchCBHAST cmpctblock{rng, 3000};
101
102 bench.run([&] {
103 PartiallyDownloadedBlock pdb{&pool};
104 auto res = pdb.InitData(cmpctblock, extratxn);
105
106 // if there were duplicates the benchmark will be invalid
107 // (eg, extra txns will be skipped) and we will receive
108 // READ_STATUS_FAILED
109 assert(res == READ_STATUS_OK);
110 });
111}
112
114{
115 BlockEncodingBench(bench, 50000, 0);
116}
117
119{
120 static_assert(DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN == 100);
121 BlockEncodingBench(bench, 50000, 100);
122}
123
125{
126 BlockEncodingBench(bench, 50000, 5000);
127}
128
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
static void BlockEncodingNoExtra(benchmark::Bench &bench)
AddToMempool(pool, CTxMemPoolEntry(tx, fee, 0, 1, 0, false, 4, lp))
static void BlockEncodingStdExtra(benchmark::Bench &bench)
static void BlockEncodingBench(benchmark::Bench &bench, size_t n_pool, size_t n_extra)
static void AddTx(const CTransactionRef &tx, const CAmount &fee, CTxMemPool &pool) EXCLUSIVE_LOCKS_REQUIRED(cs_main
static void BlockEncodingLargeExtra(benchmark::Bench &bench)
BENCHMARK(BlockEncodingNoExtra, benchmark::PriorityLevel::HIGH)
@ READ_STATUS_OK
#define Assert(val)
Identity function.
Definition: check.h:106
uint32_t nNonce
Definition: block.h:30
uint32_t nBits
Definition: block.h:29
uint32_t nTime
Definition: block.h:28
int32_t nVersion
Definition: block.h:25
uint256 hashPrevBlock
Definition: block.h:26
uint256 hashMerkleRoot
Definition: block.h:27
Definition: block.h:69
std::vector< CTransactionRef > vtx
Definition: block.h:72
bool fChecked
Definition: block.h:75
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:413
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
Definition: mempool_entry.h:66
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:281
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it.
Definition: txmempool.h:367
xoroshiro128++ PRNG.
Definition: random.h:425
uint64_t randbits(int bits) noexcept
Generate a random (bits)-bit integer.
Definition: random.h:204
Main entry point to nanobench's benchmarking facility.
Definition: nanobench.h:627
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1234
constexpr void SetNull()
Definition: uint256.h:55
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: cs_main.cpp:8
uint64_t fee
LockPoints lp
@ HIGH
Definition: bench.h:48
static const uint32_t DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN
Default number of non-mempool transactions to keep around for block reconstruction.
static CTransactionRef MakeTransactionRef(Tx &&txIn)
Definition: transaction.h:424
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:423
@ OP_EQUAL
Definition: script.h:146
@ OP_1
Definition: script.h:83
A mutable version of CTransaction.
Definition: transaction.h:378
std::vector< CTxOut > vout
Definition: transaction.h:380
std::vector< CTxIn > vin
Definition: transaction.h:379
#define LOCK2(cs1, cs2)
Definition: sync.h:260
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:51
assert(!tx.IsCoinBase())