BSHA3  0.17.99
P2P Blockchain, based on Bitcoin
checkblock.cpp
Go to the documentation of this file.
1 // Copyright (c) 2016-2018 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 
7 #include <chainparams.h>
8 #include <validation.h>
9 #include <streams.h>
10 #include <consensus/validation.h>
11 
12 namespace block_bench {
13 #include <bench/data/block413567.raw.h>
14 } // namespace block_bench
15 
16 // These are the two major time-sinks which happen after we have fully received
17 // a block off the wire, but before we can relay the block on to peers using
18 // compact block relay.
19 
20 static void DeserializeBlockTest(benchmark::State& state)
21 {
22  CDataStream stream((const char*)block_bench::block413567,
23  (const char*)block_bench::block413567 + sizeof(block_bench::block413567),
24  SER_NETWORK, PROTOCOL_VERSION);
25  char a = '\0';
26  stream.write(&a, 1); // Prevent compaction
27 
28  while (state.KeepRunning()) {
29  CBlock block;
30  stream >> block;
31  bool rewound = stream.Rewind(sizeof(block_bench::block413567));
32  assert(rewound);
33  }
34 }
35 
36 static void DeserializeAndCheckBlockTest(benchmark::State& state)
37 {
38  CDataStream stream((const char*)block_bench::block413567,
39  (const char*)block_bench::block413567 + sizeof(block_bench::block413567),
40  SER_NETWORK, PROTOCOL_VERSION);
41  char a = '\0';
42  stream.write(&a, 1); // Prevent compaction
43 
44  const auto chainParams = CreateChainParams(CBaseChainParams::MAIN);
45 
46  while (state.KeepRunning()) {
47  CBlock block; // Note that CBlock caches its checked state, so we need to recreate it here
48  stream >> block;
49  bool rewound = stream.Rewind(sizeof(block_bench::block413567));
50  assert(rewound);
51 
52  CValidationState validationState;
53  bool checked = CheckBlock(block, validationState, chainParams->GetConsensus());
54  assert(checked);
55  }
56 }
57 
58 BENCHMARK(DeserializeBlockTest, 130);
59 BENCHMARK(DeserializeAndCheckBlockTest, 160);
std::unique_ptr< const CChainParams > CreateChainParams(const std::string &chain)
Creates and returns a std::unique_ptr<CChainParams> of the chosen chain.
Definition: block.h:74
bool KeepRunning()
Definition: bench.h:70
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:221
static const std::string MAIN
BIP70 chain name strings (main, test or regtest)
bool CheckBlock(const CBlock &block, CValidationState &state, const Consensus::Params &consensusParams, bool fCheckPOW, bool fCheckMerkleRoot)
Functions for validating blocks and updating the block tree.
Capture information about block/transaction validation.
Definition: validation.h:26
BENCHMARK(DeserializeBlockTest, 130)