2022-12-24 23:49:50 +00:00
|
|
|
// Copyright (c) 2020-2022 The Bitcoin Core developers
|
2020-03-22 14:16:40 +00:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#include <chain.h>
|
|
|
|
#include <chainparams.h>
|
|
|
|
#include <pow.h>
|
|
|
|
#include <primitives/block.h>
|
|
|
|
#include <test/fuzz/FuzzedDataProvider.h>
|
|
|
|
#include <test/fuzz/fuzz.h>
|
|
|
|
#include <test/fuzz/util.h>
|
2023-04-17 22:20:59 +02:00
|
|
|
#include <util/chaintype.h>
|
2022-11-23 16:36:58 +01:00
|
|
|
#include <util/check.h>
|
2021-11-02 09:48:10 +01:00
|
|
|
#include <util/overflow.h>
|
2020-03-22 14:16:40 +00:00
|
|
|
|
|
|
|
#include <cstdint>
|
2020-05-10 18:35:55 +00:00
|
|
|
#include <optional>
|
2020-03-22 14:16:40 +00:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2020-12-03 16:42:49 +01:00
|
|
|
void initialize_pow()
|
2020-03-22 14:16:40 +00:00
|
|
|
{
|
2023-04-17 22:20:59 +02:00
|
|
|
SelectParams(ChainType::MAIN);
|
2020-03-22 14:16:40 +00:00
|
|
|
}
|
|
|
|
|
2023-07-11 14:33:31 +02:00
|
|
|
FUZZ_TARGET(pow, .init = initialize_pow)
|
2020-03-22 14:16:40 +00:00
|
|
|
{
|
|
|
|
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
|
|
|
|
const Consensus::Params& consensus_params = Params().GetConsensus();
|
2022-06-08 12:06:55 -04:00
|
|
|
std::vector<std::unique_ptr<CBlockIndex>> blocks;
|
2020-03-22 14:16:40 +00:00
|
|
|
const uint32_t fixed_time = fuzzed_data_provider.ConsumeIntegral<uint32_t>();
|
|
|
|
const uint32_t fixed_bits = fuzzed_data_provider.ConsumeIntegral<uint32_t>();
|
2021-10-25 19:48:22 +00:00
|
|
|
LIMITED_WHILE(fuzzed_data_provider.remaining_bytes() > 0, 10000) {
|
2020-05-10 18:35:55 +00:00
|
|
|
const std::optional<CBlockHeader> block_header = ConsumeDeserializable<CBlockHeader>(fuzzed_data_provider);
|
2020-03-22 14:16:40 +00:00
|
|
|
if (!block_header) {
|
|
|
|
continue;
|
|
|
|
}
|
2022-06-08 12:06:55 -04:00
|
|
|
CBlockIndex& current_block{
|
|
|
|
*blocks.emplace_back(std::make_unique<CBlockIndex>(*block_header))};
|
2020-03-22 14:16:40 +00:00
|
|
|
{
|
2022-06-08 12:06:55 -04:00
|
|
|
CBlockIndex* previous_block = blocks.empty() ? nullptr : PickValue(fuzzed_data_provider, blocks).get();
|
2020-03-22 14:16:40 +00:00
|
|
|
const int current_height = (previous_block != nullptr && previous_block->nHeight != std::numeric_limits<int>::max()) ? previous_block->nHeight + 1 : 0;
|
|
|
|
if (fuzzed_data_provider.ConsumeBool()) {
|
|
|
|
current_block.pprev = previous_block;
|
|
|
|
}
|
|
|
|
if (fuzzed_data_provider.ConsumeBool()) {
|
|
|
|
current_block.nHeight = current_height;
|
|
|
|
}
|
|
|
|
if (fuzzed_data_provider.ConsumeBool()) {
|
2021-01-24 18:45:44 +00:00
|
|
|
const uint32_t seconds = current_height * consensus_params.nPowTargetSpacing;
|
|
|
|
if (!AdditionOverflow(fixed_time, seconds)) {
|
|
|
|
current_block.nTime = fixed_time + seconds;
|
|
|
|
}
|
2020-03-22 14:16:40 +00:00
|
|
|
}
|
|
|
|
if (fuzzed_data_provider.ConsumeBool()) {
|
|
|
|
current_block.nBits = fixed_bits;
|
|
|
|
}
|
|
|
|
if (fuzzed_data_provider.ConsumeBool()) {
|
|
|
|
current_block.nChainWork = previous_block != nullptr ? previous_block->nChainWork + GetBlockProof(*previous_block) : arith_uint256{0};
|
|
|
|
} else {
|
|
|
|
current_block.nChainWork = ConsumeArithUInt256(fuzzed_data_provider);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
(void)GetBlockProof(current_block);
|
|
|
|
(void)CalculateNextWorkRequired(¤t_block, fuzzed_data_provider.ConsumeIntegralInRange<int64_t>(0, std::numeric_limits<int64_t>::max()), consensus_params);
|
|
|
|
if (current_block.nHeight != std::numeric_limits<int>::max() && current_block.nHeight - (consensus_params.DifficultyAdjustmentInterval() - 1) >= 0) {
|
|
|
|
(void)GetNextWorkRequired(¤t_block, &(*block_header), consensus_params);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
2022-06-08 12:06:55 -04:00
|
|
|
const auto& to = PickValue(fuzzed_data_provider, blocks);
|
|
|
|
const auto& from = PickValue(fuzzed_data_provider, blocks);
|
|
|
|
const auto& tip = PickValue(fuzzed_data_provider, blocks);
|
2020-03-22 14:16:40 +00:00
|
|
|
try {
|
|
|
|
(void)GetBlockProofEquivalentTime(*to, *from, *tip, consensus_params);
|
|
|
|
} catch (const uint_error&) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
2020-05-10 18:35:55 +00:00
|
|
|
const std::optional<uint256> hash = ConsumeDeserializable<uint256>(fuzzed_data_provider);
|
2020-03-22 14:16:40 +00:00
|
|
|
if (hash) {
|
|
|
|
(void)CheckProofOfWork(*hash, fuzzed_data_provider.ConsumeIntegral<unsigned int>(), consensus_params);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-05-25 10:16:56 -04:00
|
|
|
|
|
|
|
|
2023-07-11 14:33:31 +02:00
|
|
|
FUZZ_TARGET(pow_transition, .init = initialize_pow)
|
2022-05-25 10:16:56 -04:00
|
|
|
{
|
|
|
|
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
|
|
|
|
const Consensus::Params& consensus_params{Params().GetConsensus()};
|
|
|
|
std::vector<std::unique_ptr<CBlockIndex>> blocks;
|
|
|
|
|
|
|
|
const uint32_t old_time{fuzzed_data_provider.ConsumeIntegral<uint32_t>()};
|
|
|
|
const uint32_t new_time{fuzzed_data_provider.ConsumeIntegral<uint32_t>()};
|
|
|
|
const int32_t version{fuzzed_data_provider.ConsumeIntegral<int32_t>()};
|
|
|
|
uint32_t nbits{fuzzed_data_provider.ConsumeIntegral<uint32_t>()};
|
|
|
|
|
|
|
|
const arith_uint256 pow_limit = UintToArith256(consensus_params.powLimit);
|
|
|
|
arith_uint256 old_target;
|
|
|
|
old_target.SetCompact(nbits);
|
|
|
|
if (old_target > pow_limit) {
|
|
|
|
nbits = pow_limit.GetCompact();
|
|
|
|
}
|
|
|
|
// Create one difficulty adjustment period worth of headers
|
|
|
|
for (int height = 0; height < consensus_params.DifficultyAdjustmentInterval(); ++height) {
|
|
|
|
CBlockHeader header;
|
|
|
|
header.nVersion = version;
|
|
|
|
header.nTime = old_time;
|
|
|
|
header.nBits = nbits;
|
|
|
|
if (height == consensus_params.DifficultyAdjustmentInterval() - 1) {
|
|
|
|
header.nTime = new_time;
|
|
|
|
}
|
|
|
|
auto current_block{std::make_unique<CBlockIndex>(header)};
|
|
|
|
current_block->pprev = blocks.empty() ? nullptr : blocks.back().get();
|
|
|
|
current_block->nHeight = height;
|
2022-09-06 14:49:55 +02:00
|
|
|
blocks.emplace_back(std::move(current_block));
|
2022-05-25 10:16:56 -04:00
|
|
|
}
|
|
|
|
auto last_block{blocks.back().get()};
|
|
|
|
unsigned int new_nbits{GetNextWorkRequired(last_block, nullptr, consensus_params)};
|
|
|
|
Assert(PermittedDifficultyTransition(consensus_params, last_block->nHeight + 1, last_block->nBits, new_nbits));
|
|
|
|
}
|