6 #include <chainparams.h> 30 std::vector<std::unique_ptr<CBlockIndex>> blocks;
31 const uint32_t fixed_time = fuzzed_data_provider.ConsumeIntegral<uint32_t>();
32 const uint32_t fixed_bits = fuzzed_data_provider.ConsumeIntegral<uint32_t>();
33 LIMITED_WHILE(fuzzed_data_provider.remaining_bytes() > 0, 10000) {
34 const std::optional<CBlockHeader> block_header = ConsumeDeserializable<CBlockHeader>(fuzzed_data_provider);
39 *blocks.emplace_back(std::make_unique<CBlockIndex>(*block_header))};
41 CBlockIndex* previous_block = blocks.empty() ? nullptr :
PickValue(fuzzed_data_provider, blocks).get();
42 const int current_height = (previous_block !=
nullptr && previous_block->
nHeight != std::numeric_limits<int>::max()) ? previous_block->
nHeight + 1 : 0;
43 if (fuzzed_data_provider.ConsumeBool()) {
44 current_block.pprev = previous_block;
46 if (fuzzed_data_provider.ConsumeBool()) {
47 current_block.
nHeight = current_height;
49 if (fuzzed_data_provider.ConsumeBool()) {
50 const uint32_t seconds = current_height * consensus_params.nPowTargetSpacing;
52 current_block.nTime = fixed_time + seconds;
55 if (fuzzed_data_provider.ConsumeBool()) {
56 current_block.nBits = fixed_bits;
58 if (fuzzed_data_provider.ConsumeBool()) {
66 (void)
CalculateNextWorkRequired(¤t_block, fuzzed_data_provider.ConsumeIntegralInRange<int64_t>(0, std::numeric_limits<int64_t>::max()), consensus_params);
67 if (current_block.nHeight != std::numeric_limits<int>::max() && current_block.nHeight - (consensus_params.DifficultyAdjustmentInterval() - 1) >= 0) {
72 const auto& to =
PickValue(fuzzed_data_provider, blocks);
73 const auto& from =
PickValue(fuzzed_data_provider, blocks);
74 const auto& tip =
PickValue(fuzzed_data_provider, blocks);
81 const std::optional<uint256> hash = ConsumeDeserializable<uint256>(fuzzed_data_provider);
83 (void)
CheckProofOfWork(*hash, fuzzed_data_provider.ConsumeIntegral<
unsigned int>(), consensus_params);
94 std::vector<std::unique_ptr<CBlockIndex>> blocks;
96 const uint32_t old_time{fuzzed_data_provider.ConsumeIntegral<uint32_t>()};
97 const uint32_t new_time{fuzzed_data_provider.ConsumeIntegral<uint32_t>()};
98 const int32_t version{fuzzed_data_provider.ConsumeIntegral<int32_t>()};
99 uint32_t nbits{fuzzed_data_provider.ConsumeIntegral<uint32_t>()};
104 if (old_target > pow_limit) {
108 for (
int height = 0; height < consensus_params.DifficultyAdjustmentInterval(); ++height) {
111 header.
nTime = old_time;
112 header.
nBits = nbits;
113 if (height == consensus_params.DifficultyAdjustmentInterval() - 1) {
114 header.
nTime = new_time;
116 auto current_block{std::make_unique<CBlockIndex>(header)};
117 current_block->pprev = blocks.empty() ? nullptr : blocks.back().get();
118 current_block->nHeight = height;
119 blocks.emplace_back(std::move(current_block));
121 auto last_block{blocks.back().get()};
arith_uint256 nChainWork
(memory only) Total amount of work (expected number of hashes) in the chain up to and including this ...
bool PermittedDifficultyTransition(const Consensus::Params ¶ms, int64_t height, uint32_t old_nbits, uint32_t new_nbits)
Return false if the proof-of-work requirement specified by new_nbits at a given height is not possibl...
bool AdditionOverflow(const T i, const T j) noexcept
#define LIMITED_WHILE(condition, limit)
Can be used to limit a theoretically unbounded loop.
unsigned int CalculateNextWorkRequired(const CBlockIndex *pindexLast, int64_t nFirstBlockTime, const Consensus::Params ¶ms)
uint32_t GetCompact(bool fNegative=false) const
arith_uint256 UintToArith256(const uint256 &a)
FUZZ_TARGET(pow,.init=initialize_pow)
unsigned int GetNextWorkRequired(const CBlockIndex *pindexLast, const CBlockHeader *pblock, const Consensus::Params ¶ms)
Parameters that influence chain consensus.
bool CheckProofOfWork(uint256 hash, unsigned int nBits, const Consensus::Params ¶ms)
Check whether a block hash satisfies the proof-of-work requirement specified by nBits.
256-bit unsigned big integer.
The block chain is a tree shaped structure starting with the genesis block at the root...
const CChainParams & Params()
Return the currently selected parameters.
arith_uint256 & SetCompact(uint32_t nCompact, bool *pfNegative=nullptr, bool *pfOverflow=nullptr)
The "compact" format is a representation of a whole number N using an unsigned 32bit number similar t...
arith_uint256 GetBlockProof(const CBlockIndex &block)
auto & PickValue(FuzzedDataProvider &fuzzed_data_provider, Collection &col)
int nHeight
height of the entry in the chain. The genesis block has height 0
const Consensus::Params & GetConsensus() const
arith_uint256 ConsumeArithUInt256(FuzzedDataProvider &fuzzed_data_provider) noexcept
void SelectParams(const ChainType chain)
Sets the params returned by Params() to those for the given chain type.
#define Assert(val)
Identity function.
int64_t GetBlockProofEquivalentTime(const CBlockIndex &to, const CBlockIndex &from, const CBlockIndex &tip, const Consensus::Params ¶ms)
Return the time it would take to redo the work difference between from and to, assuming the current h...