| 22 | } |
| 23 | |
| 24 | FUZZ_TARGET_INIT(pow, initialize_pow) |
| 25 | { |
| 26 | FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size()); |
| 27 | const Consensus::Params& consensus_params = Params().GetConsensus(); |
| 28 | std::vector<CBlockIndex> blocks; |
| 29 | const uint32_t fixed_time = fuzzed_data_provider.ConsumeIntegral<uint32_t>(); |
| 30 | const uint32_t fixed_bits = fuzzed_data_provider.ConsumeIntegral<uint32_t>(); |
| 31 | LIMITED_WHILE(fuzzed_data_provider.remaining_bytes() > 0, 10000) { |
| 32 | const std::optional<CBlockHeader> block_header = ConsumeDeserializable<CBlockHeader>(fuzzed_data_provider); |
| 33 | if (!block_header) { |
| 34 | continue; |
| 35 | } |
| 36 | CBlockIndex current_block{*block_header}; |
| 37 | { |
| 38 | CBlockIndex* previous_block = blocks.empty() ? nullptr : &PickValue(fuzzed_data_provider, blocks); |
| 39 | const int current_height = (previous_block != nullptr && previous_block->nHeight != std::numeric_limits<int>::max()) ? previous_block->nHeight + 1 : 0; |
| 40 | if (fuzzed_data_provider.ConsumeBool()) { |
| 41 | current_block.pprev = previous_block; |
| 42 | } |
| 43 | if (fuzzed_data_provider.ConsumeBool()) { |
| 44 | current_block.nHeight = current_height; |
| 45 | } |
| 46 | if (fuzzed_data_provider.ConsumeBool()) { |
| 47 | const uint32_t seconds = current_height * consensus_params.nPowTargetSpacing; |
| 48 | if (!AdditionOverflow(fixed_time, seconds)) { |
| 49 | current_block.nTime = fixed_time + seconds; |
| 50 | } |
| 51 | } |
| 52 | if (fuzzed_data_provider.ConsumeBool()) { |
| 53 | current_block.nBits = fixed_bits; |
| 54 | } |
| 55 | if (fuzzed_data_provider.ConsumeBool()) { |
| 56 | current_block.nChainWork = previous_block != nullptr ? previous_block->nChainWork + GetBlockProof(*previous_block) : arith_uint256{0}; |
| 57 | } else { |
| 58 | current_block.nChainWork = ConsumeArithUInt256(fuzzed_data_provider); |
| 59 | } |
| 60 | blocks.push_back(current_block); |
| 61 | } |
| 62 | { |
| 63 | (void)GetBlockProof(current_block); |
| 64 | (void)CalculateNextWorkRequired(¤t_block, fuzzed_data_provider.ConsumeIntegralInRange<int64_t>(0, std::numeric_limits<int64_t>::max()), consensus_params); |
| 65 | if (current_block.nHeight != std::numeric_limits<int>::max() && current_block.nHeight - (consensus_params.DifficultyAdjustmentInterval() - 1) >= 0) { |
| 66 | (void)GetNextWorkRequired(¤t_block, &(*block_header), consensus_params); |
| 67 | } |
| 68 | } |
| 69 | { |
| 70 | const CBlockIndex* to = &PickValue(fuzzed_data_provider, blocks); |
| 71 | const CBlockIndex* from = &PickValue(fuzzed_data_provider, blocks); |
| 72 | const CBlockIndex* tip = &PickValue(fuzzed_data_provider, blocks); |
| 73 | try { |
| 74 | (void)GetBlockProofEquivalentTime(*to, *from, *tip, consensus_params); |
| 75 | } catch (const uint_error&) { |
| 76 | } |
| 77 | } |
| 78 | { |
| 79 | const std::optional<uint256> hash = ConsumeDeserializable<uint256>(fuzzed_data_provider); |
| 80 | if (hash) { |
| 81 | (void)CheckProofOfWork(*hash, fuzzed_data_provider.ConsumeIntegral<unsigned int>(), consensus_params); |
nothing calls this directly
no test coverage detected