Bitcoin ABC 0.26.3
P2P Digital Currency
Loading...
Searching...
No Matches
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>
8#include <test/util/setup_common.h>
9#include <validation.h>
10
25 const auto testing_setup{
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"};
32 auto params{testing_setup->m_node.chainman->GetParams()};
33 ss << params.DiskMagic();
35 // We can't use the streaming serialization
36 // (ss << benchmark::data::block413567)
37 // because that first writes a compact size.
39
40 // Create the test file.
41 {
42 // "wb+" is "binary, O_RDWR | O_CREAT | O_TRUNC".
43 FILE *file{fsbridge::fopen(blkfile, "wb+")};
44 // Make the test block file about 128 MB in length.
45 for (size_t i = 0; i < node::MAX_BLOCKFILE_SIZE / ss.size(); ++i) {
46 if (fwrite(ss.data(), 1, ss.size(), file) != ss.size()) {
47 throw std::runtime_error("write to test file failed\n");
48 }
49 }
50 fclose(file);
51 }
52
53 Chainstate &chainstate{testing_setup->m_node.chainman->ActiveChainstate()};
54 std::multimap<BlockHash, FlatFilePos> blocks_with_unknown_parent;
55 FlatFilePos pos;
56 bench.run([&] {
57 // "rb" is "binary, O_RDONLY", positioned to the start of the file.
58 // The file will be closed by LoadExternalBlockFile().
59 FILE *file{fsbridge::fopen(blkfile, "rb")};
60 chainstate.LoadExternalBlockFile(file, &pos,
62 });
63 fs::remove(blkfile);
64}
65
#define BENCHMARK(n)
Definition bench.h:65
static const std::string MAIN
BIP70 chain name strings (main, test or regtest)
Double ended buffer combining vector and stream-like interfaces.
Definition streams.h:177
Chainstate stores and provides an API to update our local knowledge of the current best chain.
Definition validation.h:693
Main entry point to nanobench's benchmarking facility.
Definition nanobench.h:616
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition fs.h:30
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
FILE * fopen(const fs::path &p, const char *mode)
Definition fs.cpp:30
static const unsigned int MAX_BLOCKFILE_SIZE
The maximum size of a blk?????.dat file (since 0.8)
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
Definition random.h:85
@ SER_DISK
Definition serialize.h:153
Span< const std::byte > MakeByteSpan(V &&v) noexcept
Definition span.h:301