Bitcoin Core 28.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>
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 <util/chaintype.h>
15#include <validation.h>
16
17#include <cassert>
18#include <string>
19
21{
23}
24
26{
27 DataStream ds{buffer};
28 CBlock block;
29 try {
30 ds >> TX_WITH_WITNESS(block);
31 } catch (const std::ios_base::failure&) {
32 return;
33 }
34 const Consensus::Params& consensus_params = Params().GetConsensus();
35 BlockValidationState validation_state_pow_and_merkle;
36 const bool valid_incl_pow_and_merkle = CheckBlock(block, validation_state_pow_and_merkle, consensus_params, /* fCheckPOW= */ true, /* fCheckMerkleRoot= */ true);
37 assert(validation_state_pow_and_merkle.IsValid() || validation_state_pow_and_merkle.IsInvalid() || validation_state_pow_and_merkle.IsError());
38 (void)validation_state_pow_and_merkle.Error("");
39 BlockValidationState validation_state_pow;
40 const bool valid_incl_pow = CheckBlock(block, validation_state_pow, consensus_params, /* fCheckPOW= */ true, /* fCheckMerkleRoot= */ false);
41 assert(validation_state_pow.IsValid() || validation_state_pow.IsInvalid() || validation_state_pow.IsError());
42 BlockValidationState validation_state_merkle;
43 const bool valid_incl_merkle = CheckBlock(block, validation_state_merkle, consensus_params, /* fCheckPOW= */ false, /* fCheckMerkleRoot= */ true);
44 assert(validation_state_merkle.IsValid() || validation_state_merkle.IsInvalid() || validation_state_merkle.IsError());
45 BlockValidationState validation_state_none;
46 const bool valid_incl_none = CheckBlock(block, validation_state_none, consensus_params, /* fCheckPOW= */ false, /* fCheckMerkleRoot= */ false);
47 assert(validation_state_none.IsValid() || validation_state_none.IsInvalid() || validation_state_none.IsError());
48 if (valid_incl_pow_and_merkle) {
49 assert(valid_incl_pow && valid_incl_merkle && valid_incl_none);
50 } else if (valid_incl_merkle || valid_incl_pow) {
51 assert(valid_incl_none);
52 }
53 (void)block.GetHash();
54 (void)block.ToString();
55 (void)BlockMerkleRoot(block);
56 if (!block.vtx.empty()) {
57 (void)BlockWitnessMerkleRoot(block);
58 }
59 (void)GetBlockWeight(block);
60 (void)GetWitnessCommitmentIndex(block);
61 const size_t raw_memory_size = RecursiveDynamicUsage(block);
62 const size_t raw_memory_size_as_shared_ptr = RecursiveDynamicUsage(std::make_shared<CBlock>(block));
63 assert(raw_memory_size_as_shared_ptr > raw_memory_size);
64 CBlock block_copy = block;
65 block_copy.SetNull();
66 const bool is_null = block_copy.IsNull();
67 assert(is_null);
68}
void SelectParams(const ChainType chain)
Sets the params returned by Params() to those for the given chain type.
const CChainParams & Params()
Return the currently selected parameters.
uint256 GetHash() const
Definition: block.cpp:11
bool IsNull() const
Definition: block.h:49
Definition: block.h:69
void SetNull()
Definition: block.h:95
std::string ToString() const
Definition: block.cpp:16
std::vector< CTransactionRef > vtx
Definition: block.h:72
const Consensus::Params & GetConsensus() const
Definition: chainparams.h:93
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:147
bool IsValid() const
Definition: validation.h:106
bool Error(const std::string &reject_reason)
Definition: validation.h:99
bool IsError() const
Definition: validation.h:108
bool IsInvalid() const
Definition: validation.h:107
static int64_t GetBlockWeight(const CBlock &block)
Definition: validation.h:137
int GetWitnessCommitmentIndex(const CBlock &block)
Compute at which vout of the block's coinbase transaction the witness commitment occurs,...
Definition: validation.h:148
static size_t RecursiveDynamicUsage(const CScript &script)
Definition: core_memusage.h:12
uint256 BlockMerkleRoot(const CBlock &block, bool *mutated)
Definition: merkle.cpp:66
uint256 BlockWitnessMerkleRoot(const CBlock &block, bool *mutated)
Definition: merkle.cpp:76
static constexpr TransactionSerParams TX_WITH_WITNESS
Definition: transaction.h:195
Parameters that influence chain consensus.
Definition: params.h:74
FUZZ_TARGET(block,.init=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())