Bitcoin Core  25.99.0
P2P Digital Currency
load_external.cpp
Go to the documentation of this file.
1 // Copyright (c) 2022 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or https://www.opensource.org/licenses/mit-license.php.
4 
5 #include <bench/bench.h>
6 #include <bench/data.h>
7 #include <chainparams.h>
9 #include <util/chaintype.h>
10 #include <validation.h>
11 
25 {
26  const auto testing_setup{MakeNoLogFileContext<const TestingSetup>(ChainType::MAIN)};
27 
28  // Create a single block as in the blocks files (magic bytes, block size,
29  // block data) as a stream object.
30  const fs::path blkfile{testing_setup.get()->m_path_root / "blk.dat"};
31  DataStream ss{};
32  auto params{testing_setup->m_node.chainman->GetParams()};
33  ss << params.MessageStart();
34  ss << static_cast<uint32_t>(benchmark::data::block413567.size());
35  // We can't use the streaming serialization (ss << benchmark::data::block413567)
36  // because that first writes a compact size.
38 
39  // Create the test file.
40  {
41  // "wb+" is "binary, O_RDWR | O_CREAT | O_TRUNC".
42  FILE* file{fsbridge::fopen(blkfile, "wb+")};
43  // Make the test block file about 128 MB in length.
44  for (size_t i = 0; i < node::MAX_BLOCKFILE_SIZE / ss.size(); ++i) {
45  if (fwrite(ss.data(), 1, ss.size(), file) != ss.size()) {
46  throw std::runtime_error("write to test file failed\n");
47  }
48  }
49  fclose(file);
50  }
51 
52  Chainstate& chainstate{testing_setup->m_node.chainman->ActiveChainstate()};
53  std::multimap<uint256, FlatFilePos> blocks_with_unknown_parent;
54  FlatFilePos pos;
55  bench.run([&] {
56  // "rb" is "binary, O_RDONLY", positioned to the start of the file.
57  // The file will be closed by LoadExternalBlockFile().
58  FILE* file{fsbridge::fopen(blkfile, "rb")};
59  chainstate.LoadExternalBlockFile(file, &pos, &blocks_with_unknown_parent);
60  });
61  fs::remove(blkfile);
62 }
63 
Chainstate stores and provides an API to update our local knowledge of the current best chain.
Definition: validation.h:459
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:186
Main entry point to nanobench's benchmarking facility.
Definition: nanobench.h:623
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1230
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:31
BENCHMARK(LoadExternalBlockFile, benchmark::PriorityLevel::HIGH)
static void LoadExternalBlockFile(benchmark::Bench &bench)
The LoadExternalBlockFile() function is used during -reindex and -loadblock.
const std::vector< uint8_t > block413567
Definition: data.cpp:11
@ HIGH
Definition: bench.h:47
FILE * fopen(const fs::path &p, const char *mode)
Definition: fs.cpp:25
static const unsigned int MAX_BLOCKFILE_SIZE
The maximum size of a blk?????.dat file (since 0.8)
Definition: blockstorage.h:44
Span< const std::byte > MakeByteSpan(V &&v) noexcept
Definition: span.h:264