|
|
@ -7,34 +7,33 @@ |
|
|
|
|
|
|
|
|
|
|
|
#include "arith_uint256.h" |
|
|
|
#include "arith_uint256.h" |
|
|
|
#include "chain.h" |
|
|
|
#include "chain.h" |
|
|
|
#include "chainparams.h" |
|
|
|
|
|
|
|
#include "primitives/block.h" |
|
|
|
#include "primitives/block.h" |
|
|
|
#include "uint256.h" |
|
|
|
#include "uint256.h" |
|
|
|
#include "util.h" |
|
|
|
#include "util.h" |
|
|
|
|
|
|
|
|
|
|
|
unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader *pblock) |
|
|
|
unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader *pblock, const Consensus::Params& params) |
|
|
|
{ |
|
|
|
{ |
|
|
|
unsigned int nProofOfWorkLimit = Params().ProofOfWorkLimit().GetCompact(); |
|
|
|
unsigned int nProofOfWorkLimit = params.powLimit.GetCompact(); |
|
|
|
|
|
|
|
|
|
|
|
// Genesis block
|
|
|
|
// Genesis block
|
|
|
|
if (pindexLast == NULL) |
|
|
|
if (pindexLast == NULL) |
|
|
|
return nProofOfWorkLimit; |
|
|
|
return nProofOfWorkLimit; |
|
|
|
|
|
|
|
|
|
|
|
// Only change once per difficulty adjustment interval
|
|
|
|
// Only change once per difficulty adjustment interval
|
|
|
|
if ((pindexLast->nHeight+1) % Params().DifficultyAdjustmentInterval() != 0) |
|
|
|
if ((pindexLast->nHeight+1) % params.DifficultyAdjustmentInterval() != 0) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (Params().AllowMinDifficultyBlocks()) |
|
|
|
if (params.fPowAllowMinDifficultyBlocks) |
|
|
|
{ |
|
|
|
{ |
|
|
|
// Special difficulty rule for testnet:
|
|
|
|
// Special difficulty rule for testnet:
|
|
|
|
// If the new block's timestamp is more than 2* 10 minutes
|
|
|
|
// If the new block's timestamp is more than 2* 10 minutes
|
|
|
|
// then allow mining of a min-difficulty block.
|
|
|
|
// then allow mining of a min-difficulty block.
|
|
|
|
if (pblock->GetBlockTime() > pindexLast->GetBlockTime() + Params().TargetSpacing()*2) |
|
|
|
if (pblock->GetBlockTime() > pindexLast->GetBlockTime() + params.nPowTargetSpacing*2) |
|
|
|
return nProofOfWorkLimit; |
|
|
|
return nProofOfWorkLimit; |
|
|
|
else |
|
|
|
else |
|
|
|
{ |
|
|
|
{ |
|
|
|
// Return the last non-special-min-difficulty-rules-block
|
|
|
|
// Return the last non-special-min-difficulty-rules-block
|
|
|
|
const CBlockIndex* pindex = pindexLast; |
|
|
|
const CBlockIndex* pindex = pindexLast; |
|
|
|
while (pindex->pprev && pindex->nHeight % Params().DifficultyAdjustmentInterval() != 0 && pindex->nBits == nProofOfWorkLimit) |
|
|
|
while (pindex->pprev && pindex->nHeight % params.DifficultyAdjustmentInterval() != 0 && pindex->nBits == nProofOfWorkLimit) |
|
|
|
pindex = pindex->pprev; |
|
|
|
pindex = pindex->pprev; |
|
|
|
return pindex->nBits; |
|
|
|
return pindex->nBits; |
|
|
|
} |
|
|
|
} |
|
|
@ -44,22 +43,22 @@ unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHead |
|
|
|
|
|
|
|
|
|
|
|
// Go back by what we want to be 14 days worth of blocks
|
|
|
|
// Go back by what we want to be 14 days worth of blocks
|
|
|
|
const CBlockIndex* pindexFirst = pindexLast; |
|
|
|
const CBlockIndex* pindexFirst = pindexLast; |
|
|
|
for (int i = 0; pindexFirst && i < Params().DifficultyAdjustmentInterval()-1; i++) |
|
|
|
for (int i = 0; pindexFirst && i < params.DifficultyAdjustmentInterval()-1; i++) |
|
|
|
pindexFirst = pindexFirst->pprev; |
|
|
|
pindexFirst = pindexFirst->pprev; |
|
|
|
assert(pindexFirst); |
|
|
|
assert(pindexFirst); |
|
|
|
|
|
|
|
|
|
|
|
return CalculateNextWorkRequired(pindexLast, pindexFirst->GetBlockTime()); |
|
|
|
return CalculateNextWorkRequired(pindexLast, pindexFirst->GetBlockTime(), params); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
unsigned int CalculateNextWorkRequired(const CBlockIndex* pindexLast, int64_t nFirstBlockTime) |
|
|
|
unsigned int CalculateNextWorkRequired(const CBlockIndex* pindexLast, int64_t nFirstBlockTime, const Consensus::Params& params) |
|
|
|
{ |
|
|
|
{ |
|
|
|
// Limit adjustment step
|
|
|
|
// Limit adjustment step
|
|
|
|
int64_t nActualTimespan = pindexLast->GetBlockTime() - nFirstBlockTime; |
|
|
|
int64_t nActualTimespan = pindexLast->GetBlockTime() - nFirstBlockTime; |
|
|
|
LogPrintf(" nActualTimespan = %d before bounds\n", nActualTimespan); |
|
|
|
LogPrintf(" nActualTimespan = %d before bounds\n", nActualTimespan); |
|
|
|
if (nActualTimespan < Params().TargetTimespan()/4) |
|
|
|
if (nActualTimespan < params.nPowTargetTimespan/4) |
|
|
|
nActualTimespan = Params().TargetTimespan()/4; |
|
|
|
nActualTimespan = params.nPowTargetTimespan/4; |
|
|
|
if (nActualTimespan > Params().TargetTimespan()*4) |
|
|
|
if (nActualTimespan > params.nPowTargetTimespan*4) |
|
|
|
nActualTimespan = Params().TargetTimespan()*4; |
|
|
|
nActualTimespan = params.nPowTargetTimespan*4; |
|
|
|
|
|
|
|
|
|
|
|
// Retarget
|
|
|
|
// Retarget
|
|
|
|
arith_uint256 bnNew; |
|
|
|
arith_uint256 bnNew; |
|
|
@ -67,21 +66,21 @@ unsigned int CalculateNextWorkRequired(const CBlockIndex* pindexLast, int64_t nF |
|
|
|
bnNew.SetCompact(pindexLast->nBits); |
|
|
|
bnNew.SetCompact(pindexLast->nBits); |
|
|
|
bnOld = bnNew; |
|
|
|
bnOld = bnNew; |
|
|
|
bnNew *= nActualTimespan; |
|
|
|
bnNew *= nActualTimespan; |
|
|
|
bnNew /= Params().TargetTimespan(); |
|
|
|
bnNew /= params.nPowTargetTimespan; |
|
|
|
|
|
|
|
|
|
|
|
if (bnNew > Params().ProofOfWorkLimit()) |
|
|
|
if (bnNew > params.powLimit) |
|
|
|
bnNew = Params().ProofOfWorkLimit(); |
|
|
|
bnNew = params.powLimit; |
|
|
|
|
|
|
|
|
|
|
|
/// debug print
|
|
|
|
/// debug print
|
|
|
|
LogPrintf("GetNextWorkRequired RETARGET\n"); |
|
|
|
LogPrintf("GetNextWorkRequired RETARGET\n"); |
|
|
|
LogPrintf("Params().TargetTimespan() = %d nActualTimespan = %d\n", Params().TargetTimespan(), nActualTimespan); |
|
|
|
LogPrintf("params.nPowTargetTimespan = %d nActualTimespan = %d\n", params.nPowTargetTimespan, nActualTimespan); |
|
|
|
LogPrintf("Before: %08x %s\n", pindexLast->nBits, bnOld.ToString()); |
|
|
|
LogPrintf("Before: %08x %s\n", pindexLast->nBits, bnOld.ToString()); |
|
|
|
LogPrintf("After: %08x %s\n", bnNew.GetCompact(), bnNew.ToString()); |
|
|
|
LogPrintf("After: %08x %s\n", bnNew.GetCompact(), bnNew.ToString()); |
|
|
|
|
|
|
|
|
|
|
|
return bnNew.GetCompact(); |
|
|
|
return bnNew.GetCompact(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool CheckProofOfWork(uint256 hash, unsigned int nBits) |
|
|
|
bool CheckProofOfWork(uint256 hash, unsigned int nBits, const Consensus::Params& params) |
|
|
|
{ |
|
|
|
{ |
|
|
|
bool fNegative; |
|
|
|
bool fNegative; |
|
|
|
bool fOverflow; |
|
|
|
bool fOverflow; |
|
|
@ -90,7 +89,7 @@ bool CheckProofOfWork(uint256 hash, unsigned int nBits) |
|
|
|
bnTarget.SetCompact(nBits, &fNegative, &fOverflow); |
|
|
|
bnTarget.SetCompact(nBits, &fNegative, &fOverflow); |
|
|
|
|
|
|
|
|
|
|
|
// Check range
|
|
|
|
// Check range
|
|
|
|
if (fNegative || bnTarget == 0 || fOverflow || bnTarget > Params().ProofOfWorkLimit()) |
|
|
|
if (fNegative || bnTarget == 0 || fOverflow || bnTarget > params.powLimit) |
|
|
|
return error("CheckProofOfWork(): nBits below minimum work"); |
|
|
|
return error("CheckProofOfWork(): nBits below minimum work"); |
|
|
|
|
|
|
|
|
|
|
|
// Check proof of work matches claimed amount
|
|
|
|
// Check proof of work matches claimed amount
|
|
|
|