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) 2022-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or https://www.opensource.org/licenses/mit-license.php.5#include <bench/bench.h>6#include <bench/data/block413567.raw.h>7#include <chainparams.h>8#include <flatfile.h>9#include <node/blockstorage.h>10#include <streams.h>11#include <test/util/setup_common.h>12#include <uint256.h>13#include <util/fs.h>14#include <validation.h>16#include <cstdint>17#include <cstdio>18#include <map>19#include <memory>20#include <span>21#include <stdexcept>23/**24* The LoadExternalBlockFile() function is used during -reindex and -loadblock.25*26* Create a test file that's similar to a datadir/blocks/blk?????.dat file,27* It contains around 134 copies of the same block (typical size of real block files).28* For each block in the file, LoadExternalBlockFile() won't find its parent,29* and so will skip the block. (In the real system, it will re-read the block30* from disk later when it encounters its parent.)31*32* This benchmark measures the performance of deserializing the block (or just33* its header, beginning with PR 16981).34*/35static void LoadExternalBlockFile(benchmark::Bench& bench)36{37const auto testing_setup{MakeNoLogFileContext<const TestingSetup>(ChainType::MAIN)};39// Create a single block as in the blocks files (magic bytes, block size,40// block data) as a stream object.41const fs::path blkfile{testing_setup.get()->m_path_root / "blk.dat"};42DataStream ss{};43auto params{testing_setup->m_node.chainman->GetParams()};44ss << params.MessageStart();45ss << static_cast<uint32_t>(benchmark::data::block413567.size());46// Use span-serialization to avoid writing the size first.47ss << std::span{benchmark::data::block413567};49// Create the test file.50{51// "wb+" is "binary, O_RDWR | O_CREAT | O_TRUNC".52FILE* file{fsbridge::fopen(blkfile, "wb+")};53// Make the test block file about 128 MB in length.54for (size_t i = 0; i < node::MAX_BLOCKFILE_SIZE / ss.size(); ++i) {55if (fwrite(ss.data(), 1, ss.size(), file) != ss.size()) {56throw std::runtime_error("write to test file failed\n");57}58}59fclose(file);60}62std::multimap<uint256, FlatFilePos> blocks_with_unknown_parent;63FlatFilePos pos;64bench.setup([&] {65blocks_with_unknown_parent.clear();66pos = FlatFilePos{};67})68.run([&] {69// "rb" is "binary, O_RDONLY", positioned to the start of the file.70// The file will be closed by LoadExternalBlockFile().71AutoFile file{fsbridge::fopen(blkfile, "rb")};72testing_setup->m_node.chainman->LoadExternalBlockFile(file, &pos, &blocks_with_unknown_parent);73});74fs::remove(blkfile);75}77BENCHMARK(LoadExternalBlockFile);
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.