Bitcoin ABC 0.30.5
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 <common/system.h>
14#include <consensus/params.h>
15#include <pow/aserti32d.h>
16#include <pow/daa.h>
17#include <pow/eda.h>
18#include <pow/grasberg.h>
20
21uint32_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) {
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 arith_uint256 observed_new_target;
72 // Check [0, powLimit] range for all DAA algorithms.
73 if (!NBitsToTarget(params, new_nbits, observed_new_target)) {
74 return false;
75 }
76
77 // The newer difficulty adjustement algorithms (CW-144 and Aserti32d) do
78 // not have other hard rules for permitted difficulty transitions.
79 if (IsDAAEnabled(params, height - 1)) {
80 return true;
81 }
82
83 return PermittedEDADifficultyTransition(params, old_nbits,
84 observed_new_target);
85}
86
87bool CheckProofOfWork(const BlockHash &hash, uint32_t nBits,
88 const Consensus::Params &params) {
89 arith_uint256 bnTarget;
90 if (!NBitsToTarget(params, nBits, bnTarget)) {
91 return false;
92 }
93
94 // Check proof of work matches claimed amount
95 if (UintToArith256(hash) > bnTarget) {
96 return false;
97 }
98
99 return true;
100}
101
102bool NBitsToTarget(const Consensus::Params &params, uint32_t nBits,
103 arith_uint256 &target) {
104 bool fNegative;
105 bool fOverflow;
106
107 target.SetCompact(nBits, &fNegative, &fOverflow);
108
109 return !(fNegative || target == 0 || fOverflow ||
110 target > UintToArith256(params.powLimit));
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:25
uint32_t nBits
Definition: blockindex.h:93
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
Definition: chainparams.h:80
const Consensus::Params & GetConsensus() const
Definition: chainparams.h:92
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:87
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
bool NBitsToTarget(const Consensus::Params &params, uint32_t nBits, arith_uint256 &target)
Convert a header bits difficulty representation to a 256 bits hash target.
Definition: pow.cpp:102
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())