Bitcoin ABC  0.26.3
P2P Digital Currency
pow.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 The Bitcoin Core developers
3 // Copyright (c) 2017-2020 The Bitcoin developers
4 // Distributed under the MIT software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 
7 #include <pow/pow.h>
8 
9 #include <arith_uint256.h>
10 #include <chain.h>
11 #include <chainparams.h>
12 #include <consensus/activation.h>
13 #include <consensus/params.h>
14 #include <pow/aserti32d.h>
15 #include <pow/daa.h>
16 #include <pow/eda.h>
17 #include <pow/grasberg.h>
18 #include <primitives/blockhash.h>
19 #include <util/system.h>
20 
21 uint32_t GetNextWorkRequired(const CBlockIndex *pindexPrev,
22  const CBlockHeader *pblock,
23  const CChainParams &chainParams) {
24  // GetNextWorkRequired should never be called on the genesis block
25  assert(pindexPrev != nullptr);
26 
27  const Consensus::Params &params = chainParams.GetConsensus();
28 
29  // Special rule for regtest: we never retarget.
30  if (params.fPowNoRetargeting) {
31  return pindexPrev->nBits;
32  }
33 
34  if (IsAxionEnabled(params, pindexPrev)) {
35  return GetNextASERTWorkRequired(pindexPrev, pblock, params);
36  }
37 
38  if (IsDAAEnabled(params, pindexPrev)) {
39  return GetNextDAAWorkRequired(pindexPrev, pblock, params);
40  }
41 
42  return GetNextEDAWorkRequired(pindexPrev, pblock, params);
43 }
44 
45 // Check that on difficulty adjustments, the new difficulty does not increase
46 // or decrease beyond the permitted limits.
48  int64_t height, uint32_t old_nbits,
49  uint32_t new_nbits) {
50  if (params.fPowAllowMinDifficultyBlocks) {
51  return true;
52  }
53 
54  // Keeping the same difficulty as the prev block is always permitted,
55  // assuming the initial difficulty was valid, so bail out early.
56  // The initial difficulty is valid because we start from the genesis block
57  // and we stop calling this function as soon as it returns false.
58  // This avoids further computation for most blocks prior to the BCH fork.
59  if (old_nbits == new_nbits) {
60  return true;
61  }
62 
63  // Prior to the UAHF, the difficulty could change only every 2016 blocks,
64  // so we can bail out early if we observe a difficulty change at an
65  // unexpected block height.
66  if (!IsUAHFenabled(params, height - 1) &&
67  height % params.DifficultyAdjustmentInterval() != 0) {
68  return false;
69  }
70 
71  // Check [0, powLimit] range for all DAA algorithms.
72  bool fNegative;
73  bool fOverflow;
74  arith_uint256 observed_new_target;
75  observed_new_target.SetCompact(new_nbits, &fNegative, &fOverflow);
76  if (fNegative || observed_new_target == 0 || fOverflow ||
77  observed_new_target > UintToArith256(params.powLimit)) {
78  return false;
79  }
80 
81  // The newer difficulty adjustement algorithms (CW-144 and Aserti32d) do
82  // not have other hard rules for permitted difficulty transitions.
83  if (IsDAAEnabled(params, height - 1)) {
84  return true;
85  }
86 
87  return PermittedEDADifficultyTransition(params, old_nbits,
88  observed_new_target);
89 }
90 
91 bool CheckProofOfWork(const BlockHash &hash, uint32_t nBits,
92  const Consensus::Params &params) {
93  bool fNegative;
94  bool fOverflow;
95  arith_uint256 bnTarget;
96 
97  bnTarget.SetCompact(nBits, &fNegative, &fOverflow);
98 
99  // Check range
100  if (fNegative || bnTarget == 0 || fOverflow ||
101  bnTarget > UintToArith256(params.powLimit)) {
102  return false;
103  }
104 
105  // Check proof of work matches claimed amount
106  if (UintToArith256(hash) > bnTarget) {
107  return false;
108  }
109 
110  return true;
111 }
bool IsDAAEnabled(const Consensus::Params &params, int nHeight)
Definition: activation.cpp:24
bool IsUAHFenabled(const Consensus::Params &params, int nHeight)
Definition: activation.cpp:11
static bool IsAxionEnabled(const Consensus::Params &params, int32_t nHeight)
Definition: activation.cpp:78
arith_uint256 UintToArith256(const uint256 &a)
uint32_t GetNextASERTWorkRequired(const CBlockIndex *pindexPrev, const CBlockHeader *pblock, const Consensus::Params &params) noexcept
Definition: aserti32d.cpp:9
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:23
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: blockindex.h:26
uint32_t nBits
Definition: blockindex.h:94
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
Definition: chainparams.h:74
const Consensus::Params & GetConsensus() const
Definition: chainparams.h:86
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 GetNextDAAWorkRequired(const CBlockIndex *pindexPrev, const CBlockHeader *pblock, const Consensus::Params &params)
Compute the next required proof of work using a weighted average of the estimated hashrate per block.
Definition: daa.cpp:91
uint32_t GetNextEDAWorkRequired(const CBlockIndex *pindexPrev, const CBlockHeader *pblock, const Consensus::Params &params)
Compute the next required proof of work using the legacy Bitcoin difficulty adjustment + Emergency Di...
Definition: eda.cpp:45
bool PermittedEDADifficultyTransition(const Consensus::Params &params, uint32_t old_nbits, arith_uint256 new_target)
Return false if the proof-of-work requirement specified by new_target is not possible,...
Definition: eda.cpp:119
bool CheckProofOfWork(const BlockHash &hash, uint32_t nBits, const Consensus::Params &params)
Check whether a block hash satisfies the proof-of-work requirement specified by nBits.
Definition: pow.cpp:91
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:47
uint32_t GetNextWorkRequired(const CBlockIndex *pindexPrev, const CBlockHeader *pblock, const CChainParams &chainParams)
Definition: pow.cpp:21
A BlockHash is a unqiue identifier for a block.
Definition: blockhash.h:13
Parameters that influence chain consensus.
Definition: params.h:34
int64_t DifficultyAdjustmentInterval() const
Definition: params.h:85
bool fPowNoRetargeting
Definition: params.h:78
uint256 powLimit
Proof of work parameters.
Definition: params.h:76
bool fPowAllowMinDifficultyBlocks
Definition: params.h:77
assert(!tx.IsCoinBase())