blockencodings.cpp
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; 42 CMutableTransaction tx; 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) { 79 CMutableTransaction tx = CMutableTransaction(); 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 113 static void BlockEncodingNoExtra(benchmark::Bench& bench) 114 { 115 BlockEncodingBench(bench, 50000, 0); 116 } 117 118 static void BlockEncodingStdExtra(benchmark::Bench& bench) 119 { 120 static_assert(DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN == 100); 121 BlockEncodingBench(bench, 50000, 100); 122 } 123 124 static void BlockEncodingLargeExtra(benchmark::Bench& bench) 125 { 126 BlockEncodingBench(bench, 50000, 5000); 127 } 128 129 BENCHMARK(BlockEncodingNoExtra); 130 BENCHMARK(BlockEncodingStdExtra); 131 BENCHMARK(BlockEncodingLargeExtra);