Skip to content

Instantly share code, notes, and snippets.

@GeertJohan
Created January 7, 2015 00:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GeertJohan/0694cb20b5fa48f7820b to your computer and use it in GitHub Desktop.
Save GeertJohan/0694cb20b5fa48f7820b to your computer and use it in GitHub Desktop.
// 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();
}
@ny2cafuse
Copy link

Looks clean! Kudos, mate!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment