Bitcoin Core  22.99.0
P2P Digital Currency
pow.cpp
Go to the documentation of this file.
1 // Copyright (c) 2020-2021 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 <pow.h>
8 #include <primitives/block.h>
10 #include <test/fuzz/fuzz.h>
11 #include <test/fuzz/util.h>
12 
13 #include <cstdint>
14 #include <optional>
15 #include <string>
16 #include <vector>
17 
19 {
21 }
22 
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 std::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() ? nullptr : &PickValue(fuzzed_data_provider, blocks);
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  const uint32_t seconds = current_height * consensus_params.nPowTargetSpacing;
47  if (!AdditionOverflow(fixed_time, seconds)) {
48  current_block.nTime = fixed_time + seconds;
49  }
50  }
51  if (fuzzed_data_provider.ConsumeBool()) {
52  current_block.nBits = fixed_bits;
53  }
54  if (fuzzed_data_provider.ConsumeBool()) {
55  current_block.nChainWork = previous_block != nullptr ? previous_block->nChainWork + GetBlockProof(*previous_block) : arith_uint256{0};
56  } else {
57  current_block.nChainWork = ConsumeArithUInt256(fuzzed_data_provider);
58  }
59  blocks.push_back(current_block);
60  }
61  {
62  (void)GetBlockProof(current_block);
63  (void)CalculateNextWorkRequired(&current_block, fuzzed_data_provider.ConsumeIntegralInRange<int64_t>(0, std::numeric_limits<int64_t>::max()), consensus_params);
64  if (current_block.nHeight != std::numeric_limits<int>::max() && current_block.nHeight - (consensus_params.DifficultyAdjustmentInterval() - 1) >= 0) {
65  (void)GetNextWorkRequired(&current_block, &(*block_header), consensus_params);
66  }
67  }
68  {
69  const CBlockIndex* to = &PickValue(fuzzed_data_provider, blocks);
70  const CBlockIndex* from = &PickValue(fuzzed_data_provider, blocks);
71  const CBlockIndex* tip = &PickValue(fuzzed_data_provider, blocks);
72  try {
73  (void)GetBlockProofEquivalentTime(*to, *from, *tip, consensus_params);
74  } catch (const uint_error&) {
75  }
76  }
77  {
78  const std::optional<uint256> hash = ConsumeDeserializable<uint256>(fuzzed_data_provider);
79  if (hash) {
80  (void)CheckProofOfWork(*hash, fuzzed_data_provider.ConsumeIntegral<unsigned int>(), consensus_params);
81  }
82  }
83  }
84 }
block.h
GetNextWorkRequired
unsigned int GetNextWorkRequired(const CBlockIndex *pindexLast, const CBlockHeader *pblock, const Consensus::Params &params)
Definition: pow.cpp:13
pow.h
if
if(na.IsAddrV1Compatible())
Definition: deserialize.cpp:213
initialize_pow
void initialize_pow()
Definition: pow.cpp:18
arith_uint256
256-bit unsigned big integer.
Definition: arith_uint256.h:250
CBlockIndex::nHeight
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: chain.h:158
CalculateNextWorkRequired
unsigned int CalculateNextWorkRequired(const CBlockIndex *pindexLast, int64_t nFirstBlockTime, const Consensus::Params &params)
Definition: pow.cpp:49
util.h
CBlockIndex::nChainWork
arith_uint256 nChainWork
(memory only) Total amount of work (expected number of hashes) in the chain up to and including this ...
Definition: chain.h:170
CheckProofOfWork
bool CheckProofOfWork(uint256 hash, unsigned int nBits, const Consensus::Params &params)
Check whether a block hash satisfies the proof-of-work requirement specified by nBits.
Definition: pow.cpp:74
CChainParams::GetConsensus
const Consensus::Params & GetConsensus() const
Definition: chainparams.h:82
chainparams.h
AdditionOverflow
bool AdditionOverflow(const T i, const T j) noexcept
Definition: util.h:195
Consensus::Params::nPowTargetSpacing
int64_t nPowTargetSpacing
Definition: params.h:103
Consensus::Params
Parameters that influence chain consensus.
Definition: params.h:70
FuzzedDataProvider.h
ConsumeArithUInt256
arith_uint256 ConsumeArithUInt256(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: util.h:162
SelectParams
void SelectParams(const std::string &network)
Sets the params returned by Params() to those for the given chain name.
Definition: chainparams.cpp:580
CBaseChainParams::MAIN
static const std::string MAIN
Chain name strings.
Definition: chainparamsbase.h:22
FuzzedDataProvider::remaining_bytes
size_t remaining_bytes()
Definition: FuzzedDataProvider.h:84
fuzz.h
FuzzedDataProvider
Definition: FuzzedDataProvider.h:31
FuzzedDataProvider::ConsumeIntegral
T ConsumeIntegral()
Definition: FuzzedDataProvider.h:194
Params
const CChainParams & Params()
Return the currently selected parameters.
Definition: chainparams.cpp:561
GetBlockProof
arith_uint256 GetBlockProof(const CBlockIndex &block)
Definition: chain.cpp:122
FUZZ_TARGET_INIT
FUZZ_TARGET_INIT(pow, initialize_pow)
Definition: pow.cpp:23
uint_error
Definition: arith_uint256.h:17
FuzzedDataProvider::ConsumeBool
bool ConsumeBool()
Definition: FuzzedDataProvider.h:288
CBlockIndex
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:145
Consensus::Params::DifficultyAdjustmentInterval
int64_t DifficultyAdjustmentInterval() const
Definition: params.h:105
GetBlockProofEquivalentTime
int64_t GetBlockProofEquivalentTime(const CBlockIndex &to, const CBlockIndex &from, const CBlockIndex &tip, const Consensus::Params &params)
Return the time it would take to redo the work difference between from and to, assuming the current h...
Definition: chain.cpp:137
FuzzedDataProvider::ConsumeIntegralInRange
T ConsumeIntegralInRange(T min, T max)
Definition: FuzzedDataProvider.h:204
PickValue
auto & PickValue(FuzzedDataProvider &fuzzed_data_provider, Collection &col)
Definition: util.h:52