Bitcoin Core  24.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 #include <util/overflow.h>
13 
14 #include <cstdint>
15 #include <optional>
16 #include <string>
17 #include <vector>
18 
20 {
22 }
23 
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(&current_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(&current_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);
82  }
83  }
84  }
85 }
86 
87 
89 {
90  FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
91  const Consensus::Params& consensus_params{Params().GetConsensus()};
92  std::vector<std::unique_ptr<CBlockIndex>> blocks;
93 
94  const uint32_t old_time{fuzzed_data_provider.ConsumeIntegral<uint32_t>()};
95  const uint32_t new_time{fuzzed_data_provider.ConsumeIntegral<uint32_t>()};
96  const int32_t version{fuzzed_data_provider.ConsumeIntegral<int32_t>()};
97  uint32_t nbits{fuzzed_data_provider.ConsumeIntegral<uint32_t>()};
98 
99  const arith_uint256 pow_limit = UintToArith256(consensus_params.powLimit);
100  arith_uint256 old_target;
101  old_target.SetCompact(nbits);
102  if (old_target > pow_limit) {
103  nbits = pow_limit.GetCompact();
104  }
105  // Create one difficulty adjustment period worth of headers
106  for (int height = 0; height < consensus_params.DifficultyAdjustmentInterval(); ++height) {
107  CBlockHeader header;
108  header.nVersion = version;
109  header.nTime = old_time;
110  header.nBits = nbits;
111  if (height == consensus_params.DifficultyAdjustmentInterval() - 1) {
112  header.nTime = new_time;
113  }
114  auto current_block{std::make_unique<CBlockIndex>(header)};
115  current_block->pprev = blocks.empty() ? nullptr : blocks.back().get();
116  current_block->nHeight = height;
117  blocks.emplace_back(std::move(current_block));
118  }
119  auto last_block{blocks.back().get()};
120  unsigned int new_nbits{GetNextWorkRequired(last_block, nullptr, consensus_params)};
121  Assert(PermittedDifficultyTransition(consensus_params, last_block->nHeight + 1, last_block->nBits, new_nbits));
122 }
arith_uint256 UintToArith256(const uint256 &a)
if(!SetupNetworking())
arith_uint256 GetBlockProof(const CBlockIndex &block)
Definition: chain.cpp:131
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:146
void SelectParams(const std::string &network)
Sets the params returned by Params() to those for the given chain name.
const CChainParams & Params()
Return the currently selected parameters.
#define Assert(val)
Identity function.
Definition: check.h:74
static const std::string MAIN
Chain name strings.
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:22
uint32_t nBits
Definition: block.h:29
uint32_t nTime
Definition: block.h:28
int32_t nVersion
Definition: block.h:25
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:152
arith_uint256 nChainWork
(memory only) Total amount of work (expected number of hashes) in the chain up to and including this ...
Definition: chain.h:176
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: chain.h:164
const Consensus::Params & GetConsensus() const
Definition: chainparams.h:82
T ConsumeIntegralInRange(T min, T max)
256-bit unsigned big integer.
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...
uint32_t GetCompact(bool fNegative=false) const
#define LIMITED_WHILE(condition, limit)
Can be used to limit a theoretically unbounded loop.
Definition: fuzz.h:18
bool AdditionOverflow(const T i, const T j) noexcept
Definition: overflow.h:13
bool PermittedDifficultyTransition(const Consensus::Params &params, 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...
Definition: pow.cpp:76
unsigned int GetNextWorkRequired(const CBlockIndex *pindexLast, const CBlockHeader *pblock, const Consensus::Params &params)
Definition: pow.cpp:13
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:125
unsigned int CalculateNextWorkRequired(const CBlockIndex *pindexLast, int64_t nFirstBlockTime, const Consensus::Params &params)
Definition: pow.cpp:49
Parameters that influence chain consensus.
Definition: params.h:73
int64_t DifficultyAdjustmentInterval() const
Definition: params.h:117
int64_t nPowTargetSpacing
Definition: params.h:111
FUZZ_TARGET_INIT(pow, initialize_pow)
Definition: pow.cpp:24
void initialize_pow()
Definition: pow.cpp:19
auto & PickValue(FuzzedDataProvider &fuzzed_data_provider, Collection &col)
Definition: util.h:101
arith_uint256 ConsumeArithUInt256(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: util.h:211