Bitcoin ABC  0.24.7
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 }
arith_uint256.h
GetNextEDAWorkRequired
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
pow.h
eda.h
CBlockHeader
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:22
IsDAAEnabled
static bool IsDAAEnabled(const Consensus::Params &params, int nHeight)
Definition: activation.cpp:24
CBlockIndex::nBits
uint32_t nBits
Definition: blockindex.h:82
arith_uint256
256-bit unsigned big integer.
Definition: arith_uint256.h:285
CChainParams
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
Definition: chainparams.h:47
blockhash.h
CChainParams::GetConsensus
const Consensus::Params & GetConsensus() const
Definition: chainparams.h:59
GetNextDAAWorkRequired
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
GetNextWorkRequired
uint32_t GetNextWorkRequired(const CBlockIndex *pindexPrev, const CBlockHeader *pblock, const CChainParams &chainParams)
Definition: pow.cpp:21
chainparams.h
Consensus::Params
Parameters that influence chain consensus.
Definition: params.h:59
CheckProofOfWork
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
activation.h
Consensus::Params::powLimit
uint256 powLimit
Proof of work parameters.
Definition: params.h:104
BlockHash
A BlockHash is a unqiue identifier for a block.
Definition: blockhash.h:13
IsAxionEnabled
bool IsAxionEnabled(const Consensus::Params &params, const CBlockIndex *pindexPrev)
Check if November 15th, 2020 protocol upgrade has activated.
Definition: activation.cpp:78
Consensus::Params::fPowNoRetargeting
bool fPowNoRetargeting
Definition: params.h:106
system.h
aserti32d.h
UintToArith256
arith_uint256 UintToArith256(const uint256 &a)
Definition: arith_uint256.cpp:256
GetNextASERTWorkRequired
uint32_t GetNextASERTWorkRequired(const CBlockIndex *pindexPrev, const CBlockHeader *pblock, const Consensus::Params &params) noexcept
Definition: aserti32d.cpp:90
arith_uint256::SetCompact
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...
Definition: arith_uint256.cpp:204
grasberg.h
params.h
daa.h
CBlockIndex
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: blockindex.h:23