Bitcoin Core 31.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>
10#include <primitives/block.h>
12#include <random.h>
13#include <script/script.h>
14#include <sync.h>
16#include <test/util/txmempool.h>
17#include <txmempool.h>
18#include <uint256.h>
19#include <util/check.h>
20
21#include <algorithm>
22#include <array>
23#include <cstddef>
24#include <memory>
25#include <span>
26#include <utility>
27#include <vector>
28
29
30static void AddTx(const CTransactionRef& tx, const CAmount& fee, CTxMemPool& pool) EXCLUSIVE_LOCKS_REQUIRED(cs_main, pool.cs)
31{
33 TryAddToMempool(pool, CTxMemPoolEntry(tx, fee, /*time=*/0, /*entry_height=*/1, /*entry_sequence=*/0, /*spends_coinbase=*/false, /*sigops_cost=*/4, lp));
34}
35
36namespace {
37class BenchCBHAST : public CBlockHeaderAndShortTxIDs
38{
39private:
40 static CBlock DummyBlock()
41 {
42 CBlock block;
43 block.nVersion = 5;
44 block.hashPrevBlock.SetNull();
45 block.hashMerkleRoot.SetNull();
46 block.nTime = 1231006505;
47 block.nBits = 0x1d00ffff;
48 block.nNonce = 2083236893;
49 block.fChecked = false;
51 tx.vin.resize(1);
52 tx.vout.resize(1);
53 block.vtx.emplace_back(MakeTransactionRef(tx)); // dummy coinbase
54 return block;
55 }
56
57public:
58 BenchCBHAST(InsecureRandomContext& rng, int txs) : CBlockHeaderAndShortTxIDs(DummyBlock(), rng.rand64())
59 {
60 shorttxids.reserve(txs);
61 while (txs-- > 0) {
62 shorttxids.push_back(rng.randbits<SHORTTXIDS_LENGTH*8>());
63 }
64 }
65};
66} // anon namespace
67
68static void BlockEncodingBench(benchmark::Bench& bench, size_t n_pool, size_t n_extra)
69{
70 const auto testing_setup = MakeNoLogFileContext<const ChainTestingSetup>(ChainType::MAIN);
71 CTxMemPool& pool = *Assert(testing_setup->m_node.mempool);
73
74 LOCK2(cs_main, pool.cs);
75
76 std::vector<std::pair<Wtxid, CTransactionRef>> extratxn;
77 extratxn.reserve(n_extra);
78
79 // bump up the size of txs
80 std::array<std::byte,200> sigspam;
81 sigspam.fill(std::byte(42));
82
83 // a reasonably large mempool of 50k txs, ~10MB total
84 std::vector<CTransactionRef> refs;
85 refs.reserve(n_pool + n_extra);
86 for (size_t i = 0; i < n_pool + n_extra; ++i) {
88 tx.vin.resize(1);
89 tx.vin[0].scriptSig = CScript() << sigspam;
90 tx.vin[0].scriptWitness.stack.push_back({1});
91 tx.vout.resize(1);
92 tx.vout[0].scriptPubKey = CScript() << OP_1 << OP_EQUAL;
93 tx.vout[0].nValue = i;
94 refs.push_back(MakeTransactionRef(tx));
95 }
96
97 // ensure mempool ordering is different to memory ordering of transactions,
98 // to simulate a mempool that has changed over time
99 std::shuffle(refs.begin(), refs.end(), rng);
100
101 for (size_t i = 0; i < n_pool; ++i) {
102 AddTx(refs[i], /*fee=*/refs[i]->vout[0].nValue, pool);
103 }
104 for (size_t i = n_pool; i < n_pool + n_extra; ++i) {
105 extratxn.emplace_back(refs[i]->GetWitnessHash(), refs[i]);
106 }
107
108 BenchCBHAST cmpctblock{rng, 3000};
109
110 bench.run([&] {
111 PartiallyDownloadedBlock pdb{&pool};
112 auto res = pdb.InitData(cmpctblock, extratxn);
113
114 // if there were duplicates the benchmark will be invalid
115 // (eg, extra txns will be skipped) and we will receive
116 // READ_STATUS_FAILED
117 assert(res == READ_STATUS_OK);
118 });
119}
120
122{
123 BlockEncodingBench(bench, 50000, 0);
124}
125
127{
128 static_assert(DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN == 100);
129 BlockEncodingBench(bench, 50000, 100);
130}
131
133{
134 BlockEncodingBench(bench, 50000, 5000);
135}
136
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
static void BlockEncodingNoExtra(benchmark::Bench &bench)
static void BlockEncodingStdExtra(benchmark::Bench &bench)
BENCHMARK(BlockEncodingNoExtra)
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
TryAddToMempool(pool, CTxMemPoolEntry(tx, fee, 0, 1, 0, false, 4, lp))
static void BlockEncodingLargeExtra(benchmark::Bench &bench)
@ READ_STATUS_OK
#define Assert(val)
Identity function.
Definition: check.h:116
uint32_t nNonce
Definition: block.h:35
uint32_t nBits
Definition: block.h:34
uint32_t nTime
Definition: block.h:33
int32_t nVersion
Definition: block.h:30
uint256 hashPrevBlock
Definition: block.h:31
uint256 hashMerkleRoot
Definition: block.h:32
Definition: block.h:74
std::vector< CTransactionRef > vtx
Definition: block.h:77
bool fChecked
Definition: block.h:80
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:406
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:187
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:649
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1308
constexpr void SetNull()
Definition: uint256.h:56
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
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:404
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:403
@ OP_EQUAL
Definition: script.h:147
@ OP_1
Definition: script.h:84
A mutable version of CTransaction.
Definition: transaction.h:358
std::vector< CTxOut > vout
Definition: transaction.h:360
std::vector< CTxIn > vin
Definition: transaction.h:359
#define LOCK2(cs1, cs2)
Definition: sync.h:269
FastRandomContext rng
Definition: dbwrapper.cpp:414
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
assert(!tx.IsCoinBase())