Bitcoin Core  31.0.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/transaction.h>
11 #include <script/script.h>
12 #include <sync.h>
13 #include <test/util/setup_common.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 
22 static void AddTx(const CTransactionRef& tx, const CAmount& fee, CTxMemPool& pool) EXCLUSIVE_LOCKS_REQUIRED(cs_main, pool.cs)
23 {
24  LockPoints lp;
25  TryAddToMempool(pool, CTxMemPoolEntry(tx, fee, /*time=*/0, /*entry_height=*/1, /*entry_sequence=*/0, /*spends_coinbase=*/false, /*sigops_cost=*/4, lp));
26 }
27 
28 namespace {
29 class BenchCBHAST : public CBlockHeaderAndShortTxIDs
30 {
31 private:
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 
49 public:
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 
60 static 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);
64  InsecureRandomContext rng(11);
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 
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:403
uint32_t nNonce
Definition: block.h:35
ReadStatus InitData(const CBlockHeaderAndShortTxIDs &cmpctblock, const std::vector< std::pair< Wtxid, CTransactionRef >> &extra_txn)
static void BlockEncodingNoExtra(benchmark::Bench &bench)
assert(!tx.IsCoinBase())
Definition: block.h:73
static void BlockEncodingBench(benchmark::Bench &bench, size_t n_pool, size_t n_extra)
std::vector< CTxIn > vin
Definition: transaction.h:359
TryAddToMempool(pool, CTxMemPoolEntry(tx, fee, 0, 1, 0, false, 4, lp))
static void BlockEncodingStdExtra(benchmark::Bench &bench)
uint32_t nTime
Definition: block.h:33
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
Definition: mempool_entry.h:65
Definition: script.h:83
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
#define LOCK2(cs1, cs2)
Definition: sync.h:259
static void AddTx(const CTransactionRef &tx, const CAmount &fee, CTxMemPool &pool) EXCLUSIVE_LOCKS_REQUIRED(cs_main
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1234
uint256 hashMerkleRoot
Definition: block.h:32
uint256 hashPrevBlock
Definition: block.h:31
xoroshiro128++ PRNG.
Definition: random.h:424
std::vector< CTxOut > vout
Definition: transaction.h:360
static CTransactionRef MakeTransactionRef(Tx &&txIn)
Definition: transaction.h:404
BENCHMARK(BlockEncodingNoExtra)
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:51
std::vector< CTransactionRef > vtx
Definition: block.h:77
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:186
constexpr void SetNull()
Definition: uint256.h:55
static const uint32_t DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN
Default number of non-mempool transactions to keep around for block reconstruction.
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:404
uint64_t randbits(int bits) noexcept
Generate a random (bits)-bit integer.
Definition: random.h:204
A mutable version of CTransaction.
Definition: transaction.h:357
Main entry point to nanobench&#39;s benchmarking facility.
Definition: nanobench.h:627
bool fChecked
Definition: block.h:80
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate...
Definition: cs_main.cpp:8
int32_t nVersion
Definition: block.h:30
LockPoints lp
uint64_t fee
#define Assert(val)
Identity function.
Definition: check.h:113
uint32_t nBits
Definition: block.h:34
static void BlockEncodingLargeExtra(benchmark::Bench &bench)