Created
January 7, 2015 00:41
-
-
Save GeertJohan/0694cb20b5fa48f7820b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Digi algorithm should never be used until at least 2 blocks are mined. | |
// Contains code by RealSolid & WDC | |
// Cleaned up for use in Guldencoin by GeertJohan (dead code removal since Guldencoin retargets every block) | |
unsigned int static GetNextWorkRequired_DIGI(const CBlockIndex* pindexLast, const CBlockHeader *pblock){ | |
// retarget timespan is set to a single block spacing because there is a retarget every block | |
int64 retargetTimespan = nTargetSpacing; | |
// get previous block | |
const CBlockIndex* pindexPrev = pindexLast->pprev; | |
assert(pindexPrev); | |
// calculate actual timestpan between last block and previous block | |
int64 nActualTimespan = pindexLast->GetBlockTime() - pindexPrev->GetBlockTime(); | |
printf("Digishield retarget\n"); | |
printf("nActualTimespan = %"PRI64d" before bounds\n", nActualTimespan); | |
// limit difficulty changes between 50% and 125% (human view) | |
if (nActualTimespan < (retargetTimespan - (retargetTimespan/4)) ) nActualTimespan = (retargetTimespan - (retargetTimespan/4)); | |
if (nActualTimespan > (retargetTimespan + (retargetTimespan/2)) ) nActualTimespan = (retargetTimespan + (retargetTimespan/2)); | |
// calculate new difficulty | |
CBigNum bnNew; | |
bnNew.SetCompact(pindexLast->nBits); | |
bnNew *= nActualTimespan; | |
bnNew /= retargetTimespan; | |
// difficulty should never go below (human view) the starting difficulty | |
if (bnNew > bnProofOfWorkLimit) | |
bnNew = bnProofOfWorkLimit; | |
/// debug print | |
printf("nTargetTimespan = %"PRI64d" nActualTimespan = %"PRI64d"\n" , retargetTimespan, nActualTimespan); | |
printf("Before: %08x %s\n", pindexLast->nBits, CBigNum().SetCompact(pindexLast->nBits).getuint256().ToString().c_str()); | |
printf("After: %08x %s\n", bnNew.GetCompact(), bnNew.getuint256().ToString().c_str()); | |
// return compact (uint) difficulty | |
return bnNew.GetCompact(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Looks clean! Kudos, mate!