Skip to content

Commit

Permalink
Merge pull request namecoin#53 from khalahan/namecoinq-testnet
Browse files Browse the repository at this point in the history
Testnet difficulty calculation changes, to take effect Mar 15 2014
  • Loading branch information
khalahan committed Mar 14, 2014
2 parents 4ca2b07 + 8f64b47 commit 8324841
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
29 changes: 25 additions & 4 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -746,19 +746,40 @@ int64 static GetBlockValue(int nHeight, int64 nFees)
return nSubsidy + nFees;
}

unsigned int static GetNextWorkRequired(const CBlockIndex* pindexLast)
unsigned int static GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlock *pblock)
{
const int64 nTargetTimespan = 14 * 24 * 60 * 60; // two weeks
const int64 nTargetSpacing = 10 * 60;
const int64 nInterval = nTargetTimespan / nTargetSpacing;

unsigned int nProofOfWorkLimit = bnProofOfWorkLimit.GetCompact();

// Genesis block
if (pindexLast == NULL)
return bnProofOfWorkLimit.GetCompact();
return nProofOfWorkLimit;

// Only change once per interval
if ((pindexLast->nHeight+1) % nInterval != 0)
{
// Special rules for testnet after 15 Mar 2014:
if (fTestNet && pblock->nTime > 1394838000)
{
// 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;
}

// Go back the full period unless it's the first retarget after genesis. Code courtesy of ArtForz

Expand Down Expand Up @@ -1526,7 +1547,7 @@ bool CBlock::AcceptBlock()
int nHeight = pindexPrev->nHeight+1;

// Check proof of work
if (nBits != GetNextWorkRequired(pindexPrev))
if (nBits != GetNextWorkRequired(pindexPrev, this))
return error("AcceptBlock() : incorrect proof of work");

// Check timestamp against prev
Expand Down Expand Up @@ -3125,7 +3146,7 @@ CBlock* CreateNewBlock(CReserveKey& reservekey)
pblock->hashPrevBlock = pindexPrev->GetBlockHash();
pblock->hashMerkleRoot = pblock->BuildMerkleTree();
pblock->nTime = max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime());
pblock->nBits = GetNextWorkRequired(pindexPrev);
pblock->nBits = GetNextWorkRequired(pindexPrev, pblock.get());
pblock->nNonce = 0;

return pblock.release();
Expand Down
2 changes: 1 addition & 1 deletion src/serialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class CDataStream;
class CAutoFile;
static const unsigned int MAX_SIZE = 0x02000000;

static const int VERSION = 37200;
static const int VERSION = 37300;
static const char* pszSubVer = "";
static const bool VERSION_IS_BETA = false;

Expand Down

0 comments on commit 8324841

Please sign in to comment.