Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2025-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.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>11#include <primitives/transaction.h>12#include <random.h>13#include <script/script.h>14#include <sync.h>15#include <test/util/setup_common.h>16#include <test/util/txmempool.h>17#include <txmempool.h>18#include <uint256.h>19#include <util/check.h>21#include <algorithm>22#include <array>23#include <cstddef>24#include <memory>25#include <span>26#include <utility>27#include <vector>30static void AddTx(const CTransactionRef& tx, const CAmount& fee, CTxMemPool& pool) EXCLUSIVE_LOCKS_REQUIRED(cs_main, pool.cs)31{32LockPoints lp;33TryAddToMempool(pool, CTxMemPoolEntry(tx, fee, /*time=*/0, /*entry_height=*/1, /*entry_sequence=*/0, /*spends_coinbase=*/false, /*sigops_cost=*/4, lp));34}36namespace {37class BenchCBHAST : public CBlockHeaderAndShortTxIDs38{39private:40static CBlock DummyBlock()41{42CBlock block;43block.nVersion = 5;44block.hashPrevBlock.SetNull();45block.hashMerkleRoot.SetNull();46block.nTime = 1231006505;47block.nBits = 0x1d00ffff;48block.nNonce = 2083236893;49block.fChecked = false;50CMutableTransaction tx;51tx.vin.resize(1);52tx.vout.resize(1);53block.vtx.emplace_back(MakeTransactionRef(tx)); // dummy coinbase54return block;55}57public:58BenchCBHAST(InsecureRandomContext& rng, int txs) : CBlockHeaderAndShortTxIDs(DummyBlock(), rng.rand64())59{60shorttxids.reserve(txs);61while (txs-- > 0) {62shorttxids.push_back(rng.randbits<SHORTTXIDS_LENGTH*8>());63}64}65};66} // anon namespace68static void BlockEncodingBench(benchmark::Bench& bench, size_t n_pool, size_t n_extra)69{70const auto testing_setup = MakeNoLogFileContext<const ChainTestingSetup>(ChainType::MAIN);71CTxMemPool& pool = *Assert(testing_setup->m_node.mempool);72InsecureRandomContext rng(11);74LOCK2(cs_main, pool.cs);76std::vector<std::pair<Wtxid, CTransactionRef>> extratxn;77extratxn.reserve(n_extra);79// bump up the size of txs80std::array<std::byte,200> sigspam;81sigspam.fill(std::byte(42));83// a reasonably large mempool of 50k txs, ~10MB total84std::vector<CTransactionRef> refs;85refs.reserve(n_pool + n_extra);86for (size_t i = 0; i < n_pool + n_extra; ++i) {87CMutableTransaction tx = CMutableTransaction();88tx.vin.resize(1);89tx.vin[0].scriptSig = CScript() << sigspam;90tx.vin[0].scriptWitness.stack.push_back({1});91tx.vout = {CTxOut{CAmount(i), CScript() << OP_1 << OP_EQUAL}};92refs.push_back(MakeTransactionRef(tx));93}95// ensure mempool ordering is different to memory ordering of transactions,96// to simulate a mempool that has changed over time97std::shuffle(refs.begin(), refs.end(), rng);99for (size_t i = 0; i < n_pool; ++i) {100AddTx(refs[i], /*fee=*/refs[i]->vout[0].nValue, pool);101}102for (size_t i = n_pool; i < n_pool + n_extra; ++i) {103extratxn.emplace_back(refs[i]->GetWitnessHash(), refs[i]);104}106BenchCBHAST cmpctblock{rng, 3000};108bench.run([&] {109PartiallyDownloadedBlock pdb{&pool};110auto res = pdb.InitData(cmpctblock, extratxn);112// if there were duplicates the benchmark will be invalid113// (eg, extra txns will be skipped) and we will receive114// READ_STATUS_FAILED115assert(res == READ_STATUS_OK);116});117}119static void BlockEncodingNoExtra(benchmark::Bench& bench)120{121BlockEncodingBench(bench, 50000, 0);122}124static void BlockEncodingStdExtra(benchmark::Bench& bench)125{126static_assert(DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN == 100);127BlockEncodingBench(bench, 50000, 100);128}130static void BlockEncodingLargeExtra(benchmark::Bench& bench)131{132BlockEncodingBench(bench, 50000, 5000);133}135BENCHMARK(BlockEncodingNoExtra);136BENCHMARK(BlockEncodingStdExtra);137BENCHMARK(BlockEncodingLargeExtra);
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.