checkblock.cpp
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.h> 7 8 #include <chainparams.h> 9 #include <common/args.h> 10 #include <consensus/validation.h> 11 #include <streams.h> 12 #include <util/chaintype.h> 13 #include <validation.h> 14 15 // These are the two major time-sinks which happen after we have fully received 16 // a block off the wire, but before we can relay the block on to peers using 17 // compact block relay. 18 19 static void DeserializeBlockTest(benchmark::Bench& bench) 20 { 21 DataStream stream(benchmark::data::block413567); 22 std::byte a{0}; 23 stream.write({&a, 1}); // Prevent compaction 24 25 bench.unit("block").run([&] { 26 CBlock block; 27 stream >> TX_WITH_WITNESS(block); 28 bool rewound = stream.Rewind(benchmark::data::block413567.size()); 29 assert(rewound); 30 }); 31 } 32 33 static void DeserializeAndCheckBlockTest(benchmark::Bench& bench) 34 { 35 DataStream stream(benchmark::data::block413567); 36 std::byte a{0}; 37 stream.write({&a, 1}); // Prevent compaction 38 39 ArgsManager bench_args; 40 const auto chainParams = CreateChainParams(bench_args, ChainType::MAIN); 41 42 bench.unit("block").run([&] { 43 CBlock block; // Note that CBlock caches its checked state, so we need to recreate it here 44 stream >> TX_WITH_WITNESS(block); 45 bool rewound = stream.Rewind(benchmark::data::block413567.size()); 46 assert(rewound); 47 48 BlockValidationState validationState; 49 bool checked = CheckBlock(block, validationState, chainParams->GetConsensus()); 50 assert(checked); 51 }); 52 } 53 54 BENCHMARK(DeserializeBlockTest, benchmark::PriorityLevel::HIGH); 55 BENCHMARK(DeserializeAndCheckBlockTest, benchmark::PriorityLevel::HIGH);