Bitcoin Core  24.99.0
P2P Digital Currency
block.cpp
Go to the documentation of this file.
1 // Copyright (c) 2019-2021 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 {
23 }
24 
26 {
28  CBlock block;
29  try {
30  int nVersion;
31  ds >> nVersion;
32  ds.SetVersion(nVersion);
33  ds >> block;
34  } catch (const std::ios_base::failure&) {
35  return;
36  }
37  const Consensus::Params& consensus_params = Params().GetConsensus();
38  BlockValidationState validation_state_pow_and_merkle;
39  const bool valid_incl_pow_and_merkle = CheckBlock(block, validation_state_pow_and_merkle, consensus_params, /* fCheckPOW= */ true, /* fCheckMerkleRoot= */ true);
40  assert(validation_state_pow_and_merkle.IsValid() || validation_state_pow_and_merkle.IsInvalid() || validation_state_pow_and_merkle.IsError());
41  (void)validation_state_pow_and_merkle.Error("");
42  BlockValidationState validation_state_pow;
43  const bool valid_incl_pow = CheckBlock(block, validation_state_pow, consensus_params, /* fCheckPOW= */ true, /* fCheckMerkleRoot= */ false);
44  assert(validation_state_pow.IsValid() || validation_state_pow.IsInvalid() || validation_state_pow.IsError());
45  BlockValidationState validation_state_merkle;
46  const bool valid_incl_merkle = CheckBlock(block, validation_state_merkle, consensus_params, /* fCheckPOW= */ false, /* fCheckMerkleRoot= */ true);
47  assert(validation_state_merkle.IsValid() || validation_state_merkle.IsInvalid() || validation_state_merkle.IsError());
48  BlockValidationState validation_state_none;
49  const bool valid_incl_none = CheckBlock(block, validation_state_none, consensus_params, /* fCheckPOW= */ false, /* fCheckMerkleRoot= */ false);
50  assert(validation_state_none.IsValid() || validation_state_none.IsInvalid() || validation_state_none.IsError());
51  if (valid_incl_pow_and_merkle) {
52  assert(valid_incl_pow && valid_incl_merkle && valid_incl_none);
53  } else if (valid_incl_merkle || valid_incl_pow) {
54  assert(valid_incl_none);
55  }
56  (void)block.GetHash();
57  (void)block.ToString();
58  (void)BlockMerkleRoot(block);
59  if (!block.vtx.empty()) {
60  (void)BlockWitnessMerkleRoot(block);
61  }
62  (void)GetBlockWeight(block);
63  (void)GetWitnessCommitmentIndex(block);
64  const size_t raw_memory_size = RecursiveDynamicUsage(block);
65  const size_t raw_memory_size_as_shared_ptr = RecursiveDynamicUsage(std::make_shared<CBlock>(block));
66  assert(raw_memory_size_as_shared_ptr > raw_memory_size);
67  CBlock block_copy = block;
68  block_copy.SetNull();
69  const bool is_null = block_copy.IsNull();
70  assert(is_null);
71 }
void SelectParams(const std::string &network)
Sets the params returned by Params() to those for the given chain name.
const CChainParams & Params()
Return the currently selected parameters.
Definition: chainparams.cpp:94
static const std::string REGTEST
uint256 GetHash() const
Definition: block.cpp:11
bool IsNull() const
Definition: block.h:49
Definition: block.h:69
void SetNull()
Definition: block.h:94
std::string ToString() const
Definition: block.cpp:16
std::vector< CTransactionRef > vtx
Definition: block.h:72
const Consensus::Params & GetConsensus() const
Definition: chainparams.h:88
void SetVersion(int n)
Definition: streams.h:362
bool IsValid() const
Definition: validation.h:121
bool Error(const std::string &reject_reason)
Definition: validation.h:114
bool IsError() const
Definition: validation.h:123
bool IsInvalid() const
Definition: validation.h:122
static int64_t GetBlockWeight(const CBlock &block)
Definition: validation.h:152
int GetWitnessCommitmentIndex(const CBlock &block)
Compute at which vout of the block's coinbase transaction the witness commitment occurs,...
Definition: validation.h:163
static size_t RecursiveDynamicUsage(const CScript &script)
Definition: core_memusage.h:12
uint256 BlockMerkleRoot(const CBlock &block, bool *mutated)
Definition: merkle.cpp:65
uint256 BlockWitnessMerkleRoot(const CBlock &block, bool *mutated)
Definition: merkle.cpp:75
@ SER_NETWORK
Definition: serialize.h:131
Parameters that influence chain consensus.
Definition: params.h:74
FUZZ_TARGET_INIT(block, initialize_block)
Definition: block.cpp:25
void initialize_block()
Definition: block.cpp:20
bool CheckBlock(const CBlock &block, BlockValidationState &state, const Consensus::Params &consensusParams, bool fCheckPOW, bool fCheckMerkleRoot)
Functions for validating blocks and updating the block tree.
assert(!tx.IsCoinBase())
static const int INIT_PROTO_VERSION
initial proto version, to be increased after version/verack negotiation
Definition: version.h:15