Bitcoin Core  29.1.0
P2P Digital Currency
checkblock.cpp
Go to the documentation of this file.
1 // Copyright (c) 2016-2022 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 <bench/data/block413567.raw.h>
7 #include <chainparams.h>
8 #include <common/args.h>
9 #include <consensus/validation.h>
10 #include <primitives/block.h>
11 #include <primitives/transaction.h>
12 #include <serialize.h>
13 #include <span.h>
14 #include <streams.h>
15 #include <util/chaintype.h>
16 #include <validation.h>
17 
18 #include <cassert>
19 #include <cstddef>
20 #include <memory>
21 #include <optional>
22 #include <vector>
23 
24 // These are the two major time-sinks which happen after we have fully received
25 // a block off the wire, but before we can relay the block on to peers using
26 // compact block relay.
27 
29 {
30  DataStream stream(benchmark::data::block413567);
31  std::byte a{0};
32  stream.write({&a, 1}); // Prevent compaction
33 
34  bench.unit("block").run([&] {
35  CBlock block;
36  stream >> TX_WITH_WITNESS(block);
37  bool rewound = stream.Rewind(benchmark::data::block413567.size());
38  assert(rewound);
39  });
40 }
41 
43 {
44  DataStream stream(benchmark::data::block413567);
45  std::byte a{0};
46  stream.write({&a, 1}); // Prevent compaction
47 
48  ArgsManager bench_args;
49  const auto chainParams = CreateChainParams(bench_args, ChainType::MAIN);
50 
51  bench.unit("block").run([&] {
52  CBlock block; // Note that CBlock caches its checked state, so we need to recreate it here
53  stream >> TX_WITH_WITNESS(block);
54  bool rewound = stream.Rewind(benchmark::data::block413567.size());
55  assert(rewound);
56 
57  BlockValidationState validationState;
58  bool checked = CheckBlock(block, validationState, chainParams->GetConsensus());
59  assert(checked);
60  });
61 }
62 
assert(!tx.IsCoinBase())
Definition: block.h:68
static void DeserializeBlockTest(benchmark::Bench &bench)
Definition: checkblock.cpp:28
void write(Span< const value_type > src)
Definition: streams.h:251
bool Rewind(std::optional< size_type > n=std::nullopt)
Definition: streams.h:197
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1234
static void DeserializeAndCheckBlockTest(benchmark::Bench &bench)
Definition: checkblock.cpp:42
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:146
bool CheckBlock(const CBlock &block, BlockValidationState &state, const Consensus::Params &consensusParams, bool fCheckPOW, bool fCheckMerkleRoot)
Functions for validating blocks and updating the block tree.
Bench & unit(char const *unit)
Sets the operation unit.
std::unique_ptr< const CChainParams > CreateChainParams(const ArgsManager &args, const ChainType chain)
Creates and returns a std::unique_ptr<CChainParams> of the chosen chain.
BENCHMARK(DeserializeBlockTest, benchmark::PriorityLevel::HIGH)
Main entry point to nanobench&#39;s benchmarking facility.
Definition: nanobench.h:627
static constexpr TransactionSerParams TX_WITH_WITNESS
Definition: transaction.h:195