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