Branch data Line data Source code
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 <util/chaintype.h> 15 : : #include <validation.h> 16 : : #include <version.h> 17 [ + - ]: 2 : 18 [ + - ]: 2 : #include <cassert> 19 : : #include <string> 20 : : 21 : 0 : void initialize_block() 22 : : { 23 : 0 : SelectParams(ChainType::REGTEST); 24 : 0 : } 25 : : 26 [ + - - + ]: 4 : FUZZ_TARGET(block, .init = initialize_block) 27 : : { 28 : 0 : CDataStream ds(buffer, SER_NETWORK, INIT_PROTO_VERSION); 29 [ # # ]: 0 : CBlock block; 30 : : try { 31 : : int nVersion; 32 [ # # ]: 0 : ds >> nVersion; 33 [ # # ]: 0 : ds.SetVersion(nVersion); 34 [ # # ]: 0 : ds >> block; 35 [ # # ]: 0 : } catch (const std::ios_base::failure&) { 36 : : return; 37 [ # # ]: 0 : } 38 [ # # # # ]: 0 : const Consensus::Params& consensus_params = Params().GetConsensus(); 39 : 0 : BlockValidationState validation_state_pow_and_merkle; 40 [ # # ]: 0 : const bool valid_incl_pow_and_merkle = CheckBlock(block, validation_state_pow_and_merkle, consensus_params, /* fCheckPOW= */ true, /* fCheckMerkleRoot= */ true); 41 [ # # # # : 0 : assert(validation_state_pow_and_merkle.IsValid() || validation_state_pow_and_merkle.IsInvalid() || validation_state_pow_and_merkle.IsError()); # # # # # # # # ] 42 [ # # # # ]: 0 : (void)validation_state_pow_and_merkle.Error(""); 43 : 0 : BlockValidationState validation_state_pow; 44 [ # # ]: 0 : const bool valid_incl_pow = CheckBlock(block, validation_state_pow, consensus_params, /* fCheckPOW= */ true, /* fCheckMerkleRoot= */ false); 45 [ # # # # : 0 : assert(validation_state_pow.IsValid() || validation_state_pow.IsInvalid() || validation_state_pow.IsError()); # # # # # # # # ] 46 : 0 : BlockValidationState validation_state_merkle; 47 [ # # ]: 0 : const bool valid_incl_merkle = CheckBlock(block, validation_state_merkle, consensus_params, /* fCheckPOW= */ false, /* fCheckMerkleRoot= */ true); 48 [ # # # # : 0 : assert(validation_state_merkle.IsValid() || validation_state_merkle.IsInvalid() || validation_state_merkle.IsError()); # # # # # # # # ] 49 : 0 : BlockValidationState validation_state_none; 50 [ # # ]: 0 : const bool valid_incl_none = CheckBlock(block, validation_state_none, consensus_params, /* fCheckPOW= */ false, /* fCheckMerkleRoot= */ false); 51 [ # # # # : 0 : assert(validation_state_none.IsValid() || validation_state_none.IsInvalid() || validation_state_none.IsError()); # # # # # # # # ] 52 [ # # ]: 0 : if (valid_incl_pow_and_merkle) { 53 [ # # # # : 0 : assert(valid_incl_pow && valid_incl_merkle && valid_incl_none); # # ] 54 [ # # # # ]: 0 : } else if (valid_incl_merkle || valid_incl_pow) { 55 [ # # ]: 0 : assert(valid_incl_none); 56 : 0 : } 57 [ # # ]: 0 : (void)block.GetHash(); 58 [ # # ]: 0 : (void)block.ToString(); 59 [ # # ]: 0 : (void)BlockMerkleRoot(block); 60 [ # # ]: 0 : if (!block.vtx.empty()) { 61 [ # # ]: 0 : (void)BlockWitnessMerkleRoot(block); 62 : 0 : } 63 [ # # ]: 0 : (void)GetBlockWeight(block); 64 [ # # ]: 0 : (void)GetWitnessCommitmentIndex(block); 65 [ # # ]: 0 : const size_t raw_memory_size = RecursiveDynamicUsage(block); 66 [ # # # # ]: 0 : const size_t raw_memory_size_as_shared_ptr = RecursiveDynamicUsage(std::make_shared<CBlock>(block)); 67 [ # # ]: 0 : assert(raw_memory_size_as_shared_ptr > raw_memory_size); 68 [ # # ]: 0 : CBlock block_copy = block; 69 [ # # ]: 0 : block_copy.SetNull(); 70 [ # # ]: 0 : const bool is_null = block_copy.IsNull(); 71 [ # # ]: 0 : assert(is_null); 72 [ # # ]: 0 : }