|
| 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 <primitives/blockhash.h> |
| 7 | + |
| 8 | +#include <test/fuzz/FuzzedDataProvider.h> |
| 9 | +#include <test/fuzz/fuzz.h> |
| 10 | +#include <test/fuzz/util.h> |
| 11 | + |
| 12 | +#include <cstdint> |
| 13 | +#include <optional> |
| 14 | +#include <vector> |
| 15 | + |
| 16 | +void test_one_input(const std::vector<uint8_t> &buffer) { |
| 17 | + FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size()); |
| 18 | + std::optional<CDiskBlockIndex> disk_block_index = |
| 19 | + ConsumeDeserializable<CDiskBlockIndex>(fuzzed_data_provider); |
| 20 | + if (!disk_block_index) { |
| 21 | + return; |
| 22 | + } |
| 23 | + |
| 24 | + const BlockHash zero{}; |
| 25 | + disk_block_index->phashBlock = &zero; |
| 26 | + (void)disk_block_index->GetBlockHash(); |
| 27 | + (void)disk_block_index->GetBlockPos(); |
| 28 | + (void)disk_block_index->GetBlockTime(); |
| 29 | + (void)disk_block_index->GetBlockTimeMax(); |
| 30 | + (void)disk_block_index->GetChainSize(); |
| 31 | + (void)disk_block_index->GetChainTxCount(); |
| 32 | + (void)disk_block_index->GetHeaderReceivedTime(); |
| 33 | + (void)disk_block_index->GetMedianTimePast(); |
| 34 | + (void)disk_block_index->GetReceivedTimeDiff(); |
| 35 | + (void)disk_block_index->GetUndoPos(); |
| 36 | + (void)disk_block_index->HaveTxsDownloaded(); |
| 37 | + (void)disk_block_index->IsValid(); |
| 38 | + (void)disk_block_index->ToString(); |
| 39 | + (void)disk_block_index->UpdateChainStats(); |
| 40 | + |
| 41 | + const CBlockHeader block_header = disk_block_index->GetBlockHeader(); |
| 42 | + (void)CDiskBlockIndex{*disk_block_index}; |
| 43 | + (void)disk_block_index->BuildSkip(); |
| 44 | + |
| 45 | + while (fuzzed_data_provider.ConsumeBool()) { |
| 46 | + const BlockValidity block_validity = |
| 47 | + fuzzed_data_provider.PickValueInArray({ |
| 48 | + BlockValidity::UNKNOWN, |
| 49 | + BlockValidity::RESERVED, |
| 50 | + BlockValidity::TREE, |
| 51 | + BlockValidity::TRANSACTIONS, |
| 52 | + BlockValidity::CHAIN, |
| 53 | + BlockValidity::SCRIPTS, |
| 54 | + }); |
| 55 | + const BlockStatus base; |
| 56 | + bool has_data = fuzzed_data_provider.ConsumeBool(); |
| 57 | + bool has_undo = fuzzed_data_provider.ConsumeBool(); |
| 58 | + bool has_failed = fuzzed_data_provider.ConsumeBool(); |
| 59 | + bool has_failed_parent = fuzzed_data_provider.ConsumeBool(); |
| 60 | + bool is_parked = fuzzed_data_provider.ConsumeBool(); |
| 61 | + bool has_parked_parent = fuzzed_data_provider.ConsumeBool(); |
| 62 | + const BlockStatus block_status = |
| 63 | + base.withValidity(block_validity) |
| 64 | + .withData(has_data) |
| 65 | + .withUndo(has_undo) |
| 66 | + .withFailed(has_failed) |
| 67 | + .withFailedParent(has_failed_parent) |
| 68 | + .withParked(is_parked) |
| 69 | + .withParkedParent(has_parked_parent); |
| 70 | + |
| 71 | + assert(block_status.hasData() == has_data); |
| 72 | + assert(block_status.hasUndo() == has_undo); |
| 73 | + assert(block_status.hasFailed() == has_failed); |
| 74 | + assert(block_status.hasFailedParent() == has_failed_parent); |
| 75 | + assert(block_status.isParked() == is_parked); |
| 76 | + assert(block_status.hasParkedParent() == has_parked_parent); |
| 77 | + |
| 78 | + assert(block_status.isInvalid() == has_failed || has_failed_parent); |
| 79 | + const BlockStatus valid_block = block_status.withClearedFailureFlags(); |
| 80 | + assert(!valid_block.isInvalid()); |
| 81 | + |
| 82 | + assert(block_status.isOnParkedChain() == is_parked || |
| 83 | + has_parked_parent); |
| 84 | + const BlockStatus unparked_block = |
| 85 | + block_status.withClearedParkedFlags(); |
| 86 | + assert(!unparked_block.isOnParkedChain()); |
| 87 | + |
| 88 | + if (!block_status.isValid()) { |
| 89 | + continue; |
| 90 | + } |
| 91 | + (void)disk_block_index->RaiseValidity(block_validity); |
| 92 | + } |
| 93 | + |
| 94 | + CBlockIndex block_index{block_header}; |
| 95 | + block_index.phashBlock = &zero; |
| 96 | + (void)block_index.GetBlockHash(); |
| 97 | + (void)block_index.ToString(); |
| 98 | +} |
0 commit comments