|
| 1 | +// Copyright (c) 2020 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 <chain.h> |
| 6 | +#include <chainparams.h> |
| 7 | +#include <optional.h> |
| 8 | +#include <pow.h> |
| 9 | +#include <primitives/block.h> |
| 10 | +#include <test/fuzz/FuzzedDataProvider.h> |
| 11 | +#include <test/fuzz/fuzz.h> |
| 12 | +#include <test/fuzz/util.h> |
| 13 | + |
| 14 | +#include <cstdint> |
| 15 | +#include <string> |
| 16 | +#include <vector> |
| 17 | + |
| 18 | +void initialize() |
| 19 | +{ |
| 20 | + SelectParams(CBaseChainParams::MAIN); |
| 21 | +} |
| 22 | + |
| 23 | +void test_one_input(const std::vector<uint8_t>& buffer) |
| 24 | +{ |
| 25 | + FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size()); |
| 26 | + const Consensus::Params& consensus_params = Params().GetConsensus(); |
| 27 | + std::vector<CBlockIndex> blocks; |
| 28 | + const uint32_t fixed_time = fuzzed_data_provider.ConsumeIntegral<uint32_t>(); |
| 29 | + const uint32_t fixed_bits = fuzzed_data_provider.ConsumeIntegral<uint32_t>(); |
| 30 | + while (fuzzed_data_provider.remaining_bytes() > 0) { |
| 31 | + const Optional<CBlockHeader> block_header = ConsumeDeserializable<CBlockHeader>(fuzzed_data_provider); |
| 32 | + if (!block_header) { |
| 33 | + continue; |
| 34 | + } |
| 35 | + CBlockIndex current_block{*block_header}; |
| 36 | + { |
| 37 | + CBlockIndex* previous_block = !blocks.empty() ? &blocks[fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, blocks.size() - 1)] : nullptr; |
| 38 | + const int current_height = (previous_block != nullptr && previous_block->nHeight != std::numeric_limits<int>::max()) ? previous_block->nHeight + 1 : 0; |
| 39 | + if (fuzzed_data_provider.ConsumeBool()) { |
| 40 | + current_block.pprev = previous_block; |
| 41 | + } |
| 42 | + if (fuzzed_data_provider.ConsumeBool()) { |
| 43 | + current_block.nHeight = current_height; |
| 44 | + } |
| 45 | + if (fuzzed_data_provider.ConsumeBool()) { |
| 46 | + current_block.nTime = fixed_time + current_height * consensus_params.nPowTargetSpacing; |
| 47 | + } |
| 48 | + if (fuzzed_data_provider.ConsumeBool()) { |
| 49 | + current_block.nBits = fixed_bits; |
| 50 | + } |
| 51 | + if (fuzzed_data_provider.ConsumeBool()) { |
| 52 | + current_block.nChainWork = previous_block != nullptr ? previous_block->nChainWork + GetBlockProof(*previous_block) : arith_uint256{0}; |
| 53 | + } else { |
| 54 | + current_block.nChainWork = ConsumeArithUInt256(fuzzed_data_provider); |
| 55 | + } |
| 56 | + blocks.push_back(current_block); |
| 57 | + } |
| 58 | + { |
| 59 | + (void)GetBlockProof(current_block); |
| 60 | + (void)CalculateNextWorkRequired(¤t_block, fuzzed_data_provider.ConsumeIntegralInRange<int64_t>(0, std::numeric_limits<int64_t>::max()), consensus_params); |
| 61 | + if (current_block.nHeight != std::numeric_limits<int>::max() && current_block.nHeight - (consensus_params.DifficultyAdjustmentInterval() - 1) >= 0) { |
| 62 | + (void)GetNextWorkRequired(¤t_block, &(*block_header), consensus_params); |
| 63 | + } |
| 64 | + } |
| 65 | + { |
| 66 | + const CBlockIndex* to = &blocks[fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, blocks.size() - 1)]; |
| 67 | + const CBlockIndex* from = &blocks[fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, blocks.size() - 1)]; |
| 68 | + const CBlockIndex* tip = &blocks[fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, blocks.size() - 1)]; |
| 69 | + try { |
| 70 | + (void)GetBlockProofEquivalentTime(*to, *from, *tip, consensus_params); |
| 71 | + } catch (const uint_error&) { |
| 72 | + } |
| 73 | + } |
| 74 | + { |
| 75 | + const Optional<uint256> hash = ConsumeDeserializable<uint256>(fuzzed_data_provider); |
| 76 | + if (hash) { |
| 77 | + (void)CheckProofOfWork(*hash, fuzzed_data_provider.ConsumeIntegral<unsigned int>(), consensus_params); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | +} |
0 commit comments