9#include <chainparams.h>
15static constexpr int64_t
LN2_32 = 2977044472;
17static constexpr int64_t
POW2_32 = int64_t(1) << 32;
26 const int64_t refBlockInterval =
27 (pindexRef->
pprev ==
nullptr)
30 const int64_t refInterval =
31 refBlockInterval + pindexTip->
GetBlockTime() - refBlockTime;
32 const int64_t refIntervalSize = pindexTip->
nHeight - pindexRef->
nHeight;
33 const int64_t timeOffset =
34 refInterval - (targetBlockTime + refIntervalSize * expectedBlockTime);
37 const int64_t tau32 = 288 * expectedBlockTime *
LN2_32;
38 const int64_t x32 = (timeOffset *
POW2_32) / (tau32 >> 32);
39 const int32_t xi = x32 >> 32;
40 const uint32_t xd = x32 & uint32_t(-1);
52 return lastBlockTarget << 32;
53 }
else if (xi <= -32) {
54 return lastBlockTarget >> 32;
58 return (lastBlockTarget * e31) >> (31 - xi);
91 pindexPrev = pindexPrev->
pprev;
99 if (nextTarget > powLimit) {
112 const int64_t lastBlockTime = pindexPrev->
GetBlockTime();
114 const int64_t expectedTime = pindexPrev->
nHeight * powTargetSpacing +
116 const int64_t drift = expectedTime - lastBlockTime;
122 static constexpr int64_t X_CLIP = 729822324;
125 const uint32_t x = std::max(std::min(x32, X_CLIP), -X_CLIP);
137 return (powTargetSpacing + (offsetTime32 >> 32)) >> (x32 < 0);
145 const uint32_t bucket = n >> 30;
155 const uint32_t x = n & 0x3fffffff;
157 constexpr uint32_t scales[4] = {
167 const uint32_t scale = scales[bucket];
172 P = ((P + 45037112) * x) >> 32;
173 P = ((P + 237575834) * x) >> 32;
174 P = ((P + 1031834945) * x) >> 32;
175 P = ((P + 2977042554) * x) >> 32;
arith_uint256 UintToArith256(const uint256 &a)
The block chain is a tree shaped structure starting with the genesis block at the root,...
CBlockIndex * pprev
pointer to the index of the predecessor of this block
int64_t GetBlockTime() const
int nHeight
height of the entry in the chain. The genesis block has height 0
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
const CBlock & GenesisBlock() const
const Consensus::Params & GetConsensus() const
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
static const uint8_t tau[]
static constexpr int64_t POW2_32
uint32_t GetNextGrasbergWorkRequired(const CBlockIndex *pindexPrev, const CBlockHeader *pblock, const CChainParams &chainParams)
Compute the next required proof of work using a relative target based ASERT algorithm.
static constexpr int64_t LN2_32
static arith_uint256 ComputeNextTarget(const CBlockIndex *pindexTip, const CBlockIndex *pindexRef, const CChainParams ¶ms)
int64_t computeTargetBlockTime(const CBlockIndex *pindexPrev, const CChainParams &chainParams)
Compute the block time we are aiming for.
uint32_t deterministicExp2(const uint32_t n)
Computes exp2(n) = 2^32 * (2^(n/2^32) - 1)
Parameters that influence chain consensus.
int64_t nPowTargetTimespan
uint256 powLimit
Proof of work parameters.
int64_t nPowTargetSpacing
bool fPowAllowMinDifficultyBlocks