|
| 1 | +// Copyright (c) 2019 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 <pubkey.h> |
| 11 | +#include <primitives/block.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 | + |
| 20 | +void initialize() |
| 21 | +{ |
| 22 | + const static auto verify_handle = MakeUnique<ECCVerifyHandle>(); |
| 23 | + SelectParams(CBaseChainParams::REGTEST); |
| 24 | +} |
| 25 | + |
| 26 | +void test_one_input(const std::vector<uint8_t>& buffer) |
| 27 | +{ |
| 28 | + CDataStream ds(buffer, SER_NETWORK, INIT_PROTO_VERSION); |
| 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 | + BlockValidationState validation_state_pow; |
| 42 | + const bool valid_incl_pow = CheckBlock(block, validation_state_pow, consensus_params, /* fCheckPOW= */ true, /* fCheckMerkleRoot= */ false); |
| 43 | + BlockValidationState validation_state_merkle; |
| 44 | + const bool valid_incl_merkle = CheckBlock(block, validation_state_merkle, consensus_params, /* fCheckPOW= */ false, /* fCheckMerkleRoot= */ true); |
| 45 | + BlockValidationState validation_state_none; |
| 46 | + const bool valid_incl_none = CheckBlock(block, validation_state_none, consensus_params, /* fCheckPOW= */ false, /* fCheckMerkleRoot= */ false); |
| 47 | + if (valid_incl_pow_and_merkle) { |
| 48 | + assert(valid_incl_pow && valid_incl_merkle && valid_incl_none); |
| 49 | + } else if (valid_incl_merkle || valid_incl_pow) { |
| 50 | + assert(valid_incl_none); |
| 51 | + } |
| 52 | + (void)block.GetHash(); |
| 53 | + (void)block.ToString(); |
| 54 | + (void)BlockMerkleRoot(block); |
| 55 | + if (!block.vtx.empty()) { |
| 56 | + // TODO: Avoid array index out of bounds error in BlockWitnessMerkleRoot |
| 57 | + // when block.vtx.empty(). |
| 58 | + (void)BlockWitnessMerkleRoot(block); |
| 59 | + } |
| 60 | + (void)GetBlockWeight(block); |
| 61 | + (void)GetWitnessCommitmentIndex(block); |
| 62 | + (void)RecursiveDynamicUsage(block); |
| 63 | +} |
0 commit comments