/ src / bench / checkblock.cpp
checkblock.cpp
 1  // Copyright (c) 2016-present 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  
28  static void DeserializeBlockTest(benchmark::Bench& bench)
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  
42  static void DeserializeAndCheckBlockTest(benchmark::Bench& bench)
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  
63  BENCHMARK(DeserializeBlockTest);
64  BENCHMARK(DeserializeAndCheckBlockTest);