|
|
@ -10,10 +10,6 @@ |
|
|
|
#include "main.h" |
|
|
|
#include "main.h" |
|
|
|
#include "uint256.h" |
|
|
|
#include "uint256.h" |
|
|
|
|
|
|
|
|
|
|
|
static const int64_t nTargetTimespan = 14 * 24 * 60 * 60; // two weeks
|
|
|
|
|
|
|
|
static const int64_t nTargetSpacing = 10 * 60; |
|
|
|
|
|
|
|
static const int64_t nInterval = nTargetTimespan / nTargetSpacing; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader *pblock) |
|
|
|
unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader *pblock) |
|
|
|
{ |
|
|
|
{ |
|
|
|
unsigned int nProofOfWorkLimit = Params().ProofOfWorkLimit().GetCompact(); |
|
|
|
unsigned int nProofOfWorkLimit = Params().ProofOfWorkLimit().GetCompact(); |
|
|
@ -23,20 +19,20 @@ unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHead |
|
|
|
return nProofOfWorkLimit; |
|
|
|
return nProofOfWorkLimit; |
|
|
|
|
|
|
|
|
|
|
|
// Only change once per interval
|
|
|
|
// Only change once per interval
|
|
|
|
if ((pindexLast->nHeight+1) % nInterval != 0) |
|
|
|
if ((pindexLast->nHeight+1) % Params().Interval() != 0) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (Params().AllowMinDifficultyBlocks()) |
|
|
|
if (Params().AllowMinDifficultyBlocks()) |
|
|
|
{ |
|
|
|
{ |
|
|
|
// 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->nTime > pindexLast->nTime + nTargetSpacing*2) |
|
|
|
if (pblock->nTime > pindexLast->nTime + Params().TargetSpacing()*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 % nInterval != 0 && pindex->nBits == nProofOfWorkLimit) |
|
|
|
while (pindex->pprev && pindex->nHeight % Params().Interval() != 0 && pindex->nBits == nProofOfWorkLimit) |
|
|
|
pindex = pindex->pprev; |
|
|
|
pindex = pindex->pprev; |
|
|
|
return pindex->nBits; |
|
|
|
return pindex->nBits; |
|
|
|
} |
|
|
|
} |
|
|
@ -46,17 +42,17 @@ 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 < nInterval-1; i++) |
|
|
|
for (int i = 0; pindexFirst && i < Params().Interval()-1; i++) |
|
|
|
pindexFirst = pindexFirst->pprev; |
|
|
|
pindexFirst = pindexFirst->pprev; |
|
|
|
assert(pindexFirst); |
|
|
|
assert(pindexFirst); |
|
|
|
|
|
|
|
|
|
|
|
// Limit adjustment step
|
|
|
|
// Limit adjustment step
|
|
|
|
int64_t nActualTimespan = pindexLast->GetBlockTime() - pindexFirst->GetBlockTime(); |
|
|
|
int64_t nActualTimespan = pindexLast->GetBlockTime() - pindexFirst->GetBlockTime(); |
|
|
|
LogPrintf(" nActualTimespan = %d before bounds\n", nActualTimespan); |
|
|
|
LogPrintf(" nActualTimespan = %d before bounds\n", nActualTimespan); |
|
|
|
if (nActualTimespan < nTargetTimespan/4) |
|
|
|
if (nActualTimespan < Params().TargetTimespan()/4) |
|
|
|
nActualTimespan = nTargetTimespan/4; |
|
|
|
nActualTimespan = Params().TargetTimespan()/4; |
|
|
|
if (nActualTimespan > nTargetTimespan*4) |
|
|
|
if (nActualTimespan > Params().TargetTimespan()*4) |
|
|
|
nActualTimespan = nTargetTimespan*4; |
|
|
|
nActualTimespan = Params().TargetTimespan()*4; |
|
|
|
|
|
|
|
|
|
|
|
// Retarget
|
|
|
|
// Retarget
|
|
|
|
uint256 bnNew; |
|
|
|
uint256 bnNew; |
|
|
@ -64,14 +60,14 @@ unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHead |
|
|
|
bnNew.SetCompact(pindexLast->nBits); |
|
|
|
bnNew.SetCompact(pindexLast->nBits); |
|
|
|
bnOld = bnNew; |
|
|
|
bnOld = bnNew; |
|
|
|
bnNew *= nActualTimespan; |
|
|
|
bnNew *= nActualTimespan; |
|
|
|
bnNew /= nTargetTimespan; |
|
|
|
bnNew /= Params().TargetTimespan(); |
|
|
|
|
|
|
|
|
|
|
|
if (bnNew > Params().ProofOfWorkLimit()) |
|
|
|
if (bnNew > Params().ProofOfWorkLimit()) |
|
|
|
bnNew = Params().ProofOfWorkLimit(); |
|
|
|
bnNew = Params().ProofOfWorkLimit(); |
|
|
|
|
|
|
|
|
|
|
|
/// debug print
|
|
|
|
/// debug print
|
|
|
|
LogPrintf("GetNextWorkRequired RETARGET\n"); |
|
|
|
LogPrintf("GetNextWorkRequired RETARGET\n"); |
|
|
|
LogPrintf("nTargetTimespan = %d nActualTimespan = %d\n", nTargetTimespan, nActualTimespan); |
|
|
|
LogPrintf("Params().TargetTimespan() = %d nActualTimespan = %d\n", Params().TargetTimespan(), 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()); |
|
|
|
|
|
|
|
|
|
|
@ -104,8 +100,8 @@ unsigned int ComputeMinWork(unsigned int nBase, int64_t nTime) |
|
|
|
{ |
|
|
|
{ |
|
|
|
const uint256 &bnLimit = Params().ProofOfWorkLimit(); |
|
|
|
const uint256 &bnLimit = Params().ProofOfWorkLimit(); |
|
|
|
// Testnet has min-difficulty blocks
|
|
|
|
// Testnet has min-difficulty blocks
|
|
|
|
// after nTargetSpacing*2 time between blocks:
|
|
|
|
// after Params().TargetSpacing()*2 time between blocks:
|
|
|
|
if (Params().AllowMinDifficultyBlocks() && nTime > nTargetSpacing*2) |
|
|
|
if (Params().AllowMinDifficultyBlocks() && nTime > Params().TargetSpacing()*2) |
|
|
|
return bnLimit.GetCompact(); |
|
|
|
return bnLimit.GetCompact(); |
|
|
|
|
|
|
|
|
|
|
|
uint256 bnResult; |
|
|
|
uint256 bnResult; |
|
|
@ -115,7 +111,7 @@ unsigned int ComputeMinWork(unsigned int nBase, int64_t nTime) |
|
|
|
// Maximum 400% adjustment...
|
|
|
|
// Maximum 400% adjustment...
|
|
|
|
bnResult *= 4; |
|
|
|
bnResult *= 4; |
|
|
|
// ... in best-case exactly 4-times-normal target time
|
|
|
|
// ... in best-case exactly 4-times-normal target time
|
|
|
|
nTime -= nTargetTimespan*4; |
|
|
|
nTime -= Params().TargetTimespan()*4; |
|
|
|
} |
|
|
|
} |
|
|
|
if (bnResult > bnLimit) |
|
|
|
if (bnResult > bnLimit) |
|
|
|
bnResult = bnLimit; |
|
|
|
bnResult = bnLimit; |
|
|
|