Browse Source

Testnet difficulty calculation changes, to take effect Feb 15 2012

Allow mining of min-difficulty blocks if 20 minutes have gone by without mining a regular-difficulty block.
Normal rules apply every 2016 blocks, though, so there may be a very-slow-to-confirm block at the difficulty-adjustment blocks.
miguelfreitas
Gavin Andresen 13 years ago
parent
commit
c52296a73e
  1. 33
      src/main.cpp

33
src/main.cpp

@ -768,6 +768,11 @@ static const int64 nInterval = nTargetTimespan / nTargetSpacing;
// //
unsigned int ComputeMinWork(unsigned int nBase, int64 nTime) unsigned int ComputeMinWork(unsigned int nBase, int64 nTime)
{ {
// Testnet has min-difficulty blocks
// after nTargetSpacing*2 time between blocks:
if (fTestNet && nTime > nTargetSpacing*2)
return bnProofOfWorkLimit.GetCompact();
CBigNum bnResult; CBigNum bnResult;
bnResult.SetCompact(nBase); bnResult.SetCompact(nBase);
while (nTime > 0 && bnResult < bnProofOfWorkLimit) while (nTime > 0 && bnResult < bnProofOfWorkLimit)
@ -782,16 +787,36 @@ unsigned int ComputeMinWork(unsigned int nBase, int64 nTime)
return bnResult.GetCompact(); return bnResult.GetCompact();
} }
unsigned int static GetNextWorkRequired(const CBlockIndex* pindexLast) unsigned int static GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlock *pblock)
{ {
unsigned int nProofOfWorkLimit = bnProofOfWorkLimit.GetCompact();
// Genesis block // Genesis block
if (pindexLast == NULL) if (pindexLast == NULL)
return bnProofOfWorkLimit.GetCompact(); return nProofOfWorkLimit;
// Only change once per interval // Only change once per interval
if ((pindexLast->nHeight+1) % nInterval != 0) if ((pindexLast->nHeight+1) % nInterval != 0)
{
// Special rules for testnet after 15 Feb 2012:
if (fTestNet && pblock->nTime > 1329264000)
{
// If the new block's timestamp is more than 2* 10 minutes
// then allow mining of a min-difficulty block.
if (pblock->nTime - pindexLast->nTime > nTargetSpacing*2)
return nProofOfWorkLimit;
else
{
// Return the last non-special-min-difficulty-rules-block
const CBlockIndex* pindex = pindexLast;
while (pindex->pprev && pindex->nHeight % nInterval != 0 && pindex->nBits == nProofOfWorkLimit)
pindex = pindex->pprev;
return pindex->nBits;
}
}
return pindexLast->nBits; return pindexLast->nBits;
}
// 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;
@ -1540,7 +1565,7 @@ bool CBlock::AcceptBlock()
int nHeight = pindexPrev->nHeight+1; int nHeight = pindexPrev->nHeight+1;
// Check proof of work // Check proof of work
if (nBits != GetNextWorkRequired(pindexPrev)) if (nBits != GetNextWorkRequired(pindexPrev, this))
return DoS(100, error("AcceptBlock() : incorrect proof of work")); return DoS(100, error("AcceptBlock() : incorrect proof of work"));
// Check timestamp against prev // Check timestamp against prev
@ -3120,7 +3145,7 @@ CBlock* CreateNewBlock(CReserveKey& reservekey)
pblock->hashPrevBlock = pindexPrev->GetBlockHash(); pblock->hashPrevBlock = pindexPrev->GetBlockHash();
pblock->hashMerkleRoot = pblock->BuildMerkleTree(); pblock->hashMerkleRoot = pblock->BuildMerkleTree();
pblock->nTime = max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime()); pblock->nTime = max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime());
pblock->nBits = GetNextWorkRequired(pindexPrev); pblock->nBits = GetNextWorkRequired(pindexPrev, pblock.get());
pblock->nNonce = 0; pblock->nNonce = 0;
return pblock.release(); return pblock.release();

Loading…
Cancel
Save