Bitcoin Core  22.99.0
P2P Digital Currency
block.cpp
Go to the documentation of this file.
1 // Copyright (c) 2019-2020 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 <chainparams.h>
6 #include <consensus/merkle.h>
7 #include <consensus/validation.h>
8 #include <core_io.h>
9 #include <core_memusage.h>
10 #include <primitives/block.h>
11 #include <pubkey.h>
12 #include <streams.h>
13 #include <test/fuzz/fuzz.h>
14 #include <validation.h>
15 #include <version.h>
16 
17 #include <cassert>
18 #include <string>
19 
21 {
22  static const ECCVerifyHandle verify_handle;
24 }
25 
27 {
29  CBlock block;
30  try {
31  int nVersion;
32  ds >> nVersion;
33  ds.SetVersion(nVersion);
34  ds >> block;
35  } catch (const std::ios_base::failure&) {
36  return;
37  }
38  const Consensus::Params& consensus_params = Params().GetConsensus();
39  BlockValidationState validation_state_pow_and_merkle;
40  const bool valid_incl_pow_and_merkle = CheckBlock(block, validation_state_pow_and_merkle, consensus_params, /* fCheckPOW= */ true, /* fCheckMerkleRoot= */ true);
41  assert(validation_state_pow_and_merkle.IsValid() || validation_state_pow_and_merkle.IsInvalid() || validation_state_pow_and_merkle.IsError());
42  (void)validation_state_pow_and_merkle.Error("");
43  BlockValidationState validation_state_pow;
44  const bool valid_incl_pow = CheckBlock(block, validation_state_pow, consensus_params, /* fCheckPOW= */ true, /* fCheckMerkleRoot= */ false);
45  assert(validation_state_pow.IsValid() || validation_state_pow.IsInvalid() || validation_state_pow.IsError());
46  BlockValidationState validation_state_merkle;
47  const bool valid_incl_merkle = CheckBlock(block, validation_state_merkle, consensus_params, /* fCheckPOW= */ false, /* fCheckMerkleRoot= */ true);
48  assert(validation_state_merkle.IsValid() || validation_state_merkle.IsInvalid() || validation_state_merkle.IsError());
49  BlockValidationState validation_state_none;
50  const bool valid_incl_none = CheckBlock(block, validation_state_none, consensus_params, /* fCheckPOW= */ false, /* fCheckMerkleRoot= */ false);
51  assert(validation_state_none.IsValid() || validation_state_none.IsInvalid() || validation_state_none.IsError());
52  if (valid_incl_pow_and_merkle) {
53  assert(valid_incl_pow && valid_incl_merkle && valid_incl_none);
54  } else if (valid_incl_merkle || valid_incl_pow) {
55  assert(valid_incl_none);
56  }
57  (void)block.GetHash();
58  (void)block.ToString();
59  (void)BlockMerkleRoot(block);
60  if (!block.vtx.empty()) {
61  // TODO: Avoid array index out of bounds error in BlockWitnessMerkleRoot
62  // when block.vtx.empty().
63  (void)BlockWitnessMerkleRoot(block);
64  }
65  (void)GetBlockWeight(block);
66  (void)GetWitnessCommitmentIndex(block);
67  const size_t raw_memory_size = RecursiveDynamicUsage(block);
68  const size_t raw_memory_size_as_shared_ptr = RecursiveDynamicUsage(std::make_shared<CBlock>(block));
69  assert(raw_memory_size_as_shared_ptr > raw_memory_size);
70  CBlock block_copy = block;
71  block_copy.SetNull();
72  const bool is_null = block_copy.IsNull();
73  assert(is_null);
74 }
block.h
assert
assert(!tx.IsCoinBase())
CBlockHeader::IsNull
bool IsNull() const
Definition: block.h:48
BlockValidationState
Definition: validation.h:140
streams.h
FUZZ_TARGET_INIT
FUZZ_TARGET_INIT(block, initialize_block)
Definition: block.cpp:26
validation.h
CChainParams::GetConsensus
const Consensus::Params & GetConsensus() const
Definition: chainparams.h:82
pubkey.h
chainparams.h
version.h
core_io.h
CBlockHeader::GetHash
uint256 GetHash() const
Definition: block.cpp:11
ValidationState::IsValid
bool IsValid() const
Definition: validation.h:119
Consensus::Params
Parameters that influence chain consensus.
Definition: params.h:70
RecursiveDynamicUsage
static size_t RecursiveDynamicUsage(const CScript &script)
Definition: core_memusage.h:12
CBaseChainParams::REGTEST
static const std::string REGTEST
Definition: chainparamsbase.h:25
SelectParams
void SelectParams(const std::string &network)
Sets the params returned by Params() to those for the given chain name.
Definition: chainparams.cpp:578
GetWitnessCommitmentIndex
int GetWitnessCommitmentIndex(const CBlock &block)
Compute at which vout of the block's coinbase transaction the witness commitment occurs,...
Definition: validation.h:161
GetBlockWeight
static int64_t GetBlockWeight(const CBlock &block)
Definition: validation.h:150
merkle.h
CDataStream::SetVersion
void SetVersion(int n)
Definition: streams.h:362
CheckBlock
bool CheckBlock(const CBlock &block, BlockValidationState &state, const Consensus::Params &consensusParams, bool fCheckPOW, bool fCheckMerkleRoot)
Functions for validating blocks and updating the block tree.
Definition: validation.cpp:2933
BlockWitnessMerkleRoot
uint256 BlockWitnessMerkleRoot(const CBlock &block, bool *mutated)
Definition: merkle.cpp:75
CBlock
Definition: block.h:62
core_memusage.h
CBlock::vtx
std::vector< CTransactionRef > vtx
Definition: block.h:66
ECCVerifyHandle
Users of this module must hold an ECCVerifyHandle.
Definition: pubkey.h:315
fuzz.h
Params
const CChainParams & Params()
Return the currently selected parameters.
Definition: chainparams.cpp:559
CDataStream
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:204
SER_NETWORK
@ SER_NETWORK
Definition: serialize.h:138
CBlock::ToString
std::string ToString() const
Definition: block.cpp:16
CBlock::SetNull
void SetNull()
Definition: block.h:88
ValidationState::Error
bool Error(const std::string &reject_reason)
Definition: validation.h:112
ValidationState::IsError
bool IsError() const
Definition: validation.h:121
ValidationState::IsInvalid
bool IsInvalid() const
Definition: validation.h:120
initialize_block
void initialize_block()
Definition: block.cpp:20
BlockMerkleRoot
uint256 BlockMerkleRoot(const CBlock &block, bool *mutated)
Definition: merkle.cpp:65
INIT_PROTO_VERSION
static const int INIT_PROTO_VERSION
initial proto version, to be increased after version/verack negotiation
Definition: version.h:15