DGW3


SUBMITTED BY: mmboulhosa

DATE: March 20, 2016, 8:12 p.m.

FORMAT: Text only

SIZE: 2.6 kB

HITS: 750

  1. unsigned int static DarkGravityWave3(const CBlockIndex* pindexLast, const CBlockHeader *pblock) {
  2. /* current difficulty formula, darkcoin - DarkGravity v3, written by Evan Duffield - evan@darkcoin.io */
  3. const CBlockIndex *BlockLastSolved = pindexLast;
  4. const CBlockIndex *BlockReading = pindexLast;
  5. const CBlockHeader *BlockCreating = pblock;
  6. BlockCreating = BlockCreating;
  7. int64 nActualTimespan = 0;
  8. int64 LastBlockTime = 0;
  9. int64 PastBlocksMin = 24;
  10. int64 PastBlocksMax = 24;
  11. int64 CountBlocks = 0;
  12. CBigNum PastDifficultyAverage;
  13. CBigNum PastDifficultyAveragePrev;
  14. if (BlockLastSolved == NULL || BlockLastSolved->nHeight == 0 || BlockLastSolved->nHeight < PastBlocksMin) {
  15. return bnProofOfWorkLimit.GetCompact();
  16. }
  17. for (unsigned int i = 1; BlockReading && BlockReading->nHeight > 0; i++) {
  18. if (PastBlocksMax > 0 && i > PastBlocksMax) { break; }
  19. CountBlocks++;
  20. if(CountBlocks <= PastBlocksMin) {
  21. if (CountBlocks == 1) { PastDifficultyAverage.SetCompact(BlockReading->nBits); }
  22. else { PastDifficultyAverage = ((PastDifficultyAveragePrev * CountBlocks)+(CBigNum().SetCompact(BlockReading->nBits))) / (CountBlocks+1); }
  23. PastDifficultyAveragePrev = PastDifficultyAverage;
  24. }
  25. if(LastBlockTime > 0){
  26. int64 Diff = (LastBlockTime - BlockReading->GetBlockTime());
  27. nActualTimespan += Diff;
  28. }
  29. LastBlockTime = BlockReading->GetBlockTime();
  30. if (BlockReading->pprev == NULL) { assert(BlockReading); break; }
  31. BlockReading = BlockReading->pprev;
  32. }
  33. CBigNum bnNew(PastDifficultyAverage);
  34. int64 nTargetTimespan = CountBlocks*nTargetSpacing;
  35. if (nActualTimespan < nTargetTimespan/3)
  36. nActualTimespan = nTargetTimespan/3;
  37. if (nActualTimespan > nTargetTimespan*3)
  38. nActualTimespan = nTargetTimespan*3;
  39. // Retarget
  40. bnNew *= nActualTimespan;
  41. bnNew /= nTargetTimespan;
  42. /// debug print
  43. printf("DarkGravityWave3 RETARGET\n");
  44. printf("nTargetTimespan = %"PRI64d" nActualTimespan = %"PRI64d"\n", nTargetTimespan, nActualTimespan);
  45. printf("Before: %08x %s\n", pindexLast->nBits, CBigNum().SetCompact(pindexLast->nBits).getuint256().ToString().c_str());
  46. printf("After: %08x %s\n", bnNew.GetCompact(), bnNew.getuint256().ToString().c_str());
  47. if (bnNew > bnProofOfWorkLimit){
  48. bnNew = bnProofOfWorkLimit;
  49. }
  50. return bnNew.GetCompact();
  51. }

comments powered by Disqus