Bitcoin ABC  0.22.13
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 bool CheckProofOfWork(const BlockHash &hash, uint32_t nBits,
46  const Consensus::Params &params) {
47  bool fNegative;
48  bool fOverflow;
49  arith_uint256 bnTarget;
50 
51  bnTarget.SetCompact(nBits, &fNegative, &fOverflow);
52 
53  // Check range
54  if (fNegative || bnTarget == 0 || fOverflow ||
55  bnTarget > UintToArith256(params.powLimit)) {
56  return false;
57  }
58 
59  // Check proof of work matches claimed amount
60  if (UintToArith256(hash) > bnTarget) {
61  return false;
62  }
63 
64  return true;
65 }
bool IsAxionEnabled(const Consensus::Params &params, const CBlockIndex *pindexPrev)
Check if November 15th, 2020 protocol upgrade has activated.
Definition: activation.cpp:78
uint32_t GetNextASERTWorkRequired(const CBlockIndex *pindexPrev, const CBlockHeader *pblock, const Consensus::Params &params) noexcept
Definition: aserti32d.cpp:90
bool fPowNoRetargeting
Definition: params.h:102
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
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system...
Definition: chainparams.h:47
static bool IsDAAEnabled(const Consensus::Params &params, int nHeight)
Definition: activation.cpp:24
arith_uint256 UintToArith256(const uint256 &a)
uint256 powLimit
Proof of work parameters.
Definition: params.h:100
Parameters that influence chain consensus.
Definition: params.h:59
256-bit unsigned big integer.
uint32_t GetNextWorkRequired(const CBlockIndex *pindexPrev, const CBlockHeader *pblock, const CChainParams &chainParams)
Definition: pow.cpp:21
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
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:45
A BlockHash is a unqiue identifier for a block.
Definition: blockhash.h:13
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: blockindex.h:23
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...
const Consensus::Params & GetConsensus() const
Definition: chainparams.h:59
uint32_t nBits
Definition: blockindex.h:82
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:22