unsigned int static GravityWell(const CBlockIndex* pindexLast, const CBlockHeader *pblock, uint64_t TargetBlocksSpacingSeconds, uint64_t PastBlocksMin, uint64_t PastBlocksMax) { const CBlockIndex *BlockLastSolved = pindexLast; const CBlockIndex *BlockReading = pindexLast; uint64_t PastBlocksMass = 0; int64_t PastRateActualSeconds = 0; int64_t PastRateTargetSeconds = 0; double PastRateAdjustmentRatio = double(1); CBigNum PastDifficultyAverage; CBigNum PastDifficultyAveragePrev; double EventHorizonDeviation; double EventHorizonDeviationFast; double EventHorizonDeviationSlow; CBigNum bnLimit = CBigNum(~uint256(0) >> 20); if (BlockLastSolved == NULL || BlockLastSolved->nHeight == 0 || (uint64_t)BlockLastSolved->nHeight < PastBlocksMin) { return bnLimit.GetCompact(); } for (unsigned int i = 1; BlockReading && BlockReading->nHeight > 0; i++) { if (PastBlocksMax > 0 && i > PastBlocksMax) { break; } PastBlocksMass++; if (i == 1) { PastDifficultyAverage.SetCompact(BlockReading->nBits); } else { PastDifficultyAverage = ((CBigNum().SetCompact(BlockReading->nBits) - PastDifficultyAveragePrev) / i) + PastDifficultyAveragePrev; } PastDifficultyAveragePrev = PastDifficultyAverage; PastRateActualSeconds = BlockLastSolved->GetBlockTime() - BlockReading->GetBlockTime(); PastRateTargetSeconds = TargetBlocksSpacingSeconds * PastBlocksMass; PastRateAdjustmentRatio = double(1); if (PastRateActualSeconds < 0) { PastRateActualSeconds = 0; } if (PastRateActualSeconds != 0 && PastRateTargetSeconds != 0) { PastRateAdjustmentRatio = double(PastRateTargetSeconds) / double(PastRateActualSeconds); } EventHorizonDeviation = 1 + (0.7084 * pow((double(PastBlocksMass)/double(144)), -1.228)); EventHorizonDeviationFast = EventHorizonDeviation; EventHorizonDeviationSlow = 1 / EventHorizonDeviation; if (PastBlocksMass >= PastBlocksMin) { if ((PastRateAdjustmentRatio <= EventHorizonDeviationSlow) || (PastRateAdjustmentRatio >= EventHorizonDeviationFast)) { assert(BlockReading); break; } } if (BlockReading->pprev == NULL) { assert(BlockReading); break; } BlockReading = BlockReading->pprev; } CBigNum bnNew(PastDifficultyAverage); if (PastRateActualSeconds != 0 && PastRateTargetSeconds != 0) { bnNew *= PastRateActualSeconds; bnNew /= PastRateTargetSeconds; } if (bnNew > bnLimit) { bnNew = bnLimit; } /// debug print LogPrintf("Difficulty Retarget - Gravity Well\n"); LogPrintf("PastRateAdjustmentRatio = %g\n", PastRateAdjustmentRatio); LogPrintf("Before: %08x %s\n", pindexLast->nBits, CBigNum().SetCompact(pindexLast->nBits).getuint256().ToString()); LogPrintf("After: %08x %s\n", bnNew.GetCompact(), bnNew.getuint256().ToString()); return bnNew.GetCompact(); }
/* current difficulty formula, DarkGravity v3, written by Evan Duffield - [email protected] */ unsigned int static DarkGravityWave3(const CBlockIndex* pindexLast, const CBlockHeader *pblock) { const CBlockIndex *BlockLastSolved = pindexLast; const CBlockIndex *BlockReading = pindexLast; const CBlockHeader *BlockCreating = pblock; BlockCreating = BlockCreating; int64_t nActualTimespan = 0; int64_t LastBlockTime = 0; int64_t PastBlocksMin = 24; int64_t PastBlocksMax = 24; int64_t CountBlocks = 0; uint256 PastDifficultyAverage; uint256 PastDifficultyAveragePrev; if (BlockLastSolved == NULL || BlockLastSolved->nHeight == 0 || BlockLastSolved->nHeight < PastBlocksMin) { return Params().ProofOfWorkLimit().GetCompact(); } for (unsigned int i = 1; BlockReading && BlockReading->nHeight > 0; i++) { if (PastBlocksMax > 0 && i > PastBlocksMax) { break; } CountBlocks++; if (CountBlocks <= PastBlocksMin) { if (CountBlocks == 1) PastDifficultyAverage.SetCompact(BlockReading->nBits); else { PastDifficultyAverage = ((PastDifficultyAveragePrev * CountBlocks)+(uint256().SetCompact(BlockReading->nBits))) / (CountBlocks+1); } PastDifficultyAveragePrev = PastDifficultyAverage; } if (LastBlockTime > 0) { int64_t Diff = (LastBlockTime - BlockReading->GetBlockTime()); nActualTimespan += Diff; } LastBlockTime = BlockReading->GetBlockTime(); if (BlockReading->pprev == NULL) { assert(BlockReading); break; } BlockReading = BlockReading->pprev; } uint256 bnNew(PastDifficultyAverage); int64_t nTargetTimespan = CountBlocks*Params().TargetSpacing(); if (nActualTimespan < nTargetTimespan/3) nActualTimespan = nTargetTimespan/3; if (nActualTimespan > nTargetTimespan*3) nActualTimespan = nTargetTimespan*3; // Retarget bnNew *= nActualTimespan; bnNew /= nTargetTimespan; if (bnNew > Params().ProofOfWorkLimit()) { bnNew = Params().ProofOfWorkLimit(); } return bnNew.GetCompact(); }
unsigned int static DarkGravityWave(const CBlockIndex* pindexLast, const Consensus::Params& params) { /* current difficulty formula, dash - DarkGravity v3, written by Evan Duffield - [email protected] */ const CBlockIndex *BlockLastSolved = pindexLast; const CBlockIndex *BlockReading = pindexLast; int64_t nActualTimespan = 0; int64_t LastBlockTime = 0; int64_t PastBlocksMin = 24; int64_t PastBlocksMax = 24; int64_t CountBlocks = 0; arith_uint256 PastDifficultyAverage; arith_uint256 PastDifficultyAveragePrev; unsigned int nProofOfWorkLimit = UintToArith256(params.powLimit).GetCompact(); const arith_uint256 bnPowLimit = UintToArith256(params.powLimit); if (BlockLastSolved == NULL || BlockLastSolved->nHeight == 0 || BlockLastSolved->nHeight < PastBlocksMin) { return nProofOfWorkLimit; } for (unsigned int i = 1; BlockReading && BlockReading->nHeight > 0; i++) { if (PastBlocksMax > 0 && i > PastBlocksMax) { break; } CountBlocks++; if(CountBlocks <= PastBlocksMin) { if (CountBlocks == 1) { PastDifficultyAverage.SetCompact(BlockReading->nBits); } else { PastDifficultyAverage = ((PastDifficultyAveragePrev * CountBlocks) + (arith_uint256().SetCompact(BlockReading->nBits))) / (CountBlocks + 1); } PastDifficultyAveragePrev = PastDifficultyAverage; } if(LastBlockTime > 0){ int64_t Diff = (LastBlockTime - BlockReading->GetBlockTime()); nActualTimespan += Diff; } LastBlockTime = BlockReading->GetBlockTime(); if (BlockReading->pprev == NULL) { assert(BlockReading); break; } BlockReading = BlockReading->pprev; } arith_uint256 bnNew(PastDifficultyAverage); int64_t _nTargetTimespan = CountBlocks * params.nPowTargetSpacing; if (nActualTimespan < _nTargetTimespan/3) nActualTimespan = _nTargetTimespan/3; if (nActualTimespan > _nTargetTimespan*3) nActualTimespan = _nTargetTimespan*3; // Retarget bnNew *= nActualTimespan; bnNew /= _nTargetTimespan; if (bnNew > bnPowLimit) bnNew = bnPowLimit; return bnNew.GetCompact(); }
unsigned int static DarkGravityWave(const CBlockIndex* pindexLast) { /* current difficulty formula, x11basecoin - DarkGravity v3, written by Evan Duffield - [email protected] */ const CBlockIndex *BlockLastSolved = pindexLast; const CBlockIndex *BlockReading = pindexLast; int64_t nActualTimespan = 0; int64_t LastBlockTime = 0; int64_t PastBlocksMin = 24; int64_t PastBlocksMax = 24; int64_t CountBlocks = 0; uint256 PastDifficultyAverage; uint256 PastDifficultyAveragePrev; if (BlockLastSolved == NULL || BlockLastSolved->nHeight == 0 || BlockLastSolved->nHeight < PastBlocksMin) { return Params().ProofOfWorkLimit().GetCompact(); } for (unsigned int i = 1; BlockReading && BlockReading->nHeight > 0; i++) { if (PastBlocksMax > 0 && i > PastBlocksMax) { break; } CountBlocks++; if(CountBlocks <= PastBlocksMin) { if (CountBlocks == 1) { PastDifficultyAverage.SetCompact(BlockReading->nBits); } else { PastDifficultyAverage = ((PastDifficultyAveragePrev * CountBlocks) + (uint256().SetCompact(BlockReading->nBits))) / (CountBlocks + 1); } PastDifficultyAveragePrev = PastDifficultyAverage; } if(LastBlockTime > 0){ int64_t Diff = (LastBlockTime - BlockReading->GetBlockTime()); nActualTimespan += Diff; } LastBlockTime = BlockReading->GetBlockTime(); if (BlockReading->pprev == NULL) { assert(BlockReading); break; } BlockReading = BlockReading->pprev; } uint256 bnNew(PastDifficultyAverage); int64_t _nTargetTimespan = CountBlocks * Params().TargetSpacing(); if (nActualTimespan < _nTargetTimespan/3) nActualTimespan = _nTargetTimespan/3; if (nActualTimespan > _nTargetTimespan*3) nActualTimespan = _nTargetTimespan*3; // Retarget bnNew *= nActualTimespan; bnNew /= _nTargetTimespan; if (bnNew > Params().ProofOfWorkLimit()){ bnNew = Params().ProofOfWorkLimit(); } return bnNew.GetCompact(); }
unsigned int static DarkGravityWave(const CBlockIndex* pindexLast, const Consensus::Params& params) { /* current difficulty formula, gridcoin - DarkGravity v3, written by Evan Duffield - [email protected] */ const arith_uint256 bnPowLimit = UintToArith256(params.powLimit); int64_t nPastBlocks = 24; // make sure we have at least (nPastBlocks + 1) blocks, otherwise just return powLimit if (!pindexLast || pindexLast->nHeight < nPastBlocks) { return bnPowLimit.GetCompact(); } const CBlockIndex *pindex = pindexLast; arith_uint256 bnPastTargetAvg; for (unsigned int nCountBlocks = 1; nCountBlocks <= nPastBlocks; nCountBlocks++) { arith_uint256 bnTarget = arith_uint256().SetCompact(pindex->nBits); if (nCountBlocks == 1) { bnPastTargetAvg = bnTarget; } else { // NOTE: that's not an average really... bnPastTargetAvg = (bnPastTargetAvg * nCountBlocks + bnTarget) / (nCountBlocks + 1); } if(nCountBlocks != nPastBlocks) { assert(pindex->pprev); // should never fail pindex = pindex->pprev; } } arith_uint256 bnNew(bnPastTargetAvg); int64_t nActualTimespan = pindexLast->GetBlockTime() - pindex->GetBlockTime(); // NOTE: is this accurate? nActualTimespan counts it for (nPastBlocks - 1) blocks only... int64_t nTargetTimespan = nPastBlocks * params.nPowTargetSpacing; if (nActualTimespan < nTargetTimespan/3) nActualTimespan = nTargetTimespan/3; if (nActualTimespan > nTargetTimespan*3) nActualTimespan = nTargetTimespan*3; // Retarget bnNew *= nActualTimespan; bnNew /= nTargetTimespan; if (bnNew > bnPowLimit) { bnNew = bnPowLimit; } return bnNew.GetCompact(); }
unsigned int static KimotoGravityWell(const CBlockIndex* pindexLast) { const CBlockIndex *BlockLastSolved = pindexLast; const CBlockIndex *BlockReading = pindexLast; uint64_t PastBlocksMass = 0; int64_t PastRateActualSeconds = 0; int64_t PastRateTargetSeconds = 0; double PastRateAdjustmentRatio = double(1); uint256 PastDifficultyAverage; uint256 PastDifficultyAveragePrev; double EventHorizonDeviation; double EventHorizonDeviationFast; double EventHorizonDeviationSlow; uint64_t pastSecondsMin = Params().TargetTimespan() * 0.025; uint64_t pastSecondsMax = Params().TargetTimespan() * 7; uint64_t PastBlocksMin = pastSecondsMin / Params().TargetSpacing(); uint64_t PastBlocksMax = pastSecondsMax / Params().TargetSpacing(); if (BlockLastSolved == NULL || BlockLastSolved->nHeight == 0 || (uint64_t)BlockLastSolved->nHeight < PastBlocksMin) { return Params().ProofOfWorkLimit().GetCompact(); } for (unsigned int i = 1; BlockReading && BlockReading->nHeight > 0; i++) { if (PastBlocksMax > 0 && i > PastBlocksMax) { break; } PastBlocksMass++; PastDifficultyAverage.SetCompact(BlockReading->nBits); if (i > 1) { // handle negative uint256 if(PastDifficultyAverage >= PastDifficultyAveragePrev) PastDifficultyAverage = ((PastDifficultyAverage - PastDifficultyAveragePrev) / i) + PastDifficultyAveragePrev; else PastDifficultyAverage = PastDifficultyAveragePrev - ((PastDifficultyAveragePrev - PastDifficultyAverage) / i); } PastDifficultyAveragePrev = PastDifficultyAverage; PastRateActualSeconds = BlockLastSolved->GetBlockTime() - BlockReading->GetBlockTime(); PastRateTargetSeconds = Params().TargetSpacing() * PastBlocksMass; PastRateAdjustmentRatio = double(1); if (PastRateActualSeconds < 0) { PastRateActualSeconds = 0; } if (PastRateActualSeconds != 0 && PastRateTargetSeconds != 0) { PastRateAdjustmentRatio = double(PastRateTargetSeconds) / double(PastRateActualSeconds); } EventHorizonDeviation = 1 + (0.7084 * pow((double(PastBlocksMass)/double(28.2)), -1.228)); EventHorizonDeviationFast = EventHorizonDeviation; EventHorizonDeviationSlow = 1 / EventHorizonDeviation; if (PastBlocksMass >= PastBlocksMin) { if ((PastRateAdjustmentRatio <= EventHorizonDeviationSlow) || (PastRateAdjustmentRatio >= EventHorizonDeviationFast)) { assert(BlockReading); break; } } if (BlockReading->pprev == NULL) { assert(BlockReading); break; } BlockReading = BlockReading->pprev; } uint256 bnNew(PastDifficultyAverage); if (PastRateActualSeconds != 0 && PastRateTargetSeconds != 0) { bnNew *= PastRateActualSeconds; bnNew /= PastRateTargetSeconds; } if (bnNew > Params().ProofOfWorkLimit()) { bnNew = Params().ProofOfWorkLimit(); } return bnNew.GetCompact(); }
unsigned int KimotoGravityWell(const CBlockIndex* pindexLast, uint64_t TargetBlockSpacingSeconds, uint64_t PastBlocksMin, uint64_t PastBlocksMax, const Consensus::Params& params) { const CBlockIndex *BlockLastSolved = pindexLast; const CBlockIndex *BlockReading = pindexLast; uint64_t PastBlocksMass = 0; int64_t PastRateActualSeconds = 0; int64_t PastRateTargetSeconds = 0; double PastRateAdjustmentRatio = double(1); arith_uint256 PastDifficultyAverage, PastDifficultyAveragePrev; double EventHorizonDeviation, EventHorizonDeviationFast, EventHorizonDeviationSlow; if(BlockLastSolved == NULL || BlockLastSolved->nHeight == 0 || (uint64_t) BlockLastSolved->nHeight < PastBlocksMin) return UintToArith256(params.powLimit).GetCompact(); for(unsigned int i = 1; BlockReading && BlockReading->nHeight > 0; i++) { if(PastBlocksMax > 0 && i > PastBlocksMax) break; PastBlocksMass++; PastDifficultyAverage.SetCompact(BlockReading->nBits); if(i > 1) { if(PastDifficultyAverage >= PastDifficultyAveragePrev) { PastDifficultyAverage = ((PastDifficultyAverage - PastDifficultyAveragePrev) / i) + PastDifficultyAveragePrev; } else { PastDifficultyAverage = PastDifficultyAveragePrev - ((PastDifficultyAveragePrev - PastDifficultyAverage) / i); } } PastDifficultyAveragePrev = PastDifficultyAverage; PastRateActualSeconds = BlockLastSolved->GetBlockTime() - BlockReading->GetBlockTime(); PastRateTargetSeconds = TargetBlockSpacingSeconds * PastBlocksMass; PastRateActualSeconds = (PastRateActualSeconds < 0) ? 0 : PastRateActualSeconds; PastRateAdjustmentRatio = (PastRateActualSeconds != 0 && PastRateTargetSeconds != 0) ? double(PastRateTargetSeconds) / double(PastRateActualSeconds) : double(1); EventHorizonDeviation = 1 + (0.7084 * pow((double(PastBlocksMass) / double(144)), -1.228)); EventHorizonDeviationFast = EventHorizonDeviation; EventHorizonDeviationSlow = 1 / EventHorizonDeviation; if((PastBlocksMass >= PastBlocksMin && (PastRateAdjustmentRatio <= EventHorizonDeviationSlow || PastRateAdjustmentRatio >= EventHorizonDeviationFast)) || (BlockReading->pprev == NULL)) { assert(BlockReading); break; } BlockReading = BlockReading->pprev; } arith_uint256 bnNew(PastDifficultyAverage); if(PastRateActualSeconds != 0 && PastRateTargetSeconds != 0) { bnNew *= PastRateActualSeconds; bnNew /= PastRateTargetSeconds; } const arith_uint256 bnPowLimit = UintToArith256(params.powLimit); if(bnNew > bnPowLimit) { bnNew = bnPowLimit; } // Debug /* LogPrintf("Difficulty Retarget - Kimoto Gravity Well [Block: %d]\n", pindexLast->nHeight); LogPrintf("PastRateAdjustmentRatio = %g\n", PastRateAdjustmentRatio); LogPrintf("Before: %08x %s\n", pindexLast->nBits, uint256().SetCompact(pindexLast->nBits).ToString().c_str()); LogPrintf("After: %08x %s\n", bnNew.GetCompact(), bnNew.ToString().c_str()); */ return bnNew.GetCompact(); }
unsigned int KimotoGravityWell(const CBlockIndex* pindexLast, int algo) { unsigned int nProofOfWorkLimit = Params().ProofOfWorkLimit(algo).GetCompact(); if (fDebug){ LogPrintf("Proof Of Work Limit For Algo %i, is % i\n", algo, nProofOfWorkLimit); } // Genesis block if (pindexLast == NULL){ LogPrintf("Genesis Block Difficulty"); return nProofOfWorkLimit; } const CBlockIndex* pindexPrevAlgo = GetLastBlockIndexForAlgo(pindexLast, algo); if (pindexPrevAlgo == NULL){ LogPrintf("pindexPrevAlgo == NULL for Algo %i, is % i\n", algo, nProofOfWorkLimit); return nProofOfWorkLimit; } /* Franko Multi Algo Gravity Well */ const CBlockIndex *BlockLastSolved = pindexPrevAlgo; const CBlockIndex *BlockReading = pindexPrevAlgo; unsigned int AlgoCounter = 0; uint64_t PastBlocksMass = 0; int64_t PastRateActualSeconds = 0; int64_t PastRateTargetSeconds = 0; double PastRateAdjustmentRatio = double(1); uint256 PastDifficultyAverage; uint256 PastDifficultyAveragePrev; uint256 BlockReadingDifficulty; double EventHorizonDeviation; double EventHorizonDeviationFast; double EventHorizonDeviationSlow; static const int64_t TargetBlockSpacing = 60; // == 1 minute unsigned int TimeDaySeconds = 60 * 60 * 24; int64_t PastSecondsMin = TimeDaySeconds * 0.25; // == 6300 Seconds int64_t PastSecondsMax = TimeDaySeconds * 7; // == 604800 Seconds uint64_t PastBlocksMin = PastSecondsMin / TargetBlockSpacing; // == 360 blocks uint64_t PastBlocksMax = PastSecondsMax / TargetBlockSpacing; // == 10080 blocks //loop through and count the blocks found by the algo for (unsigned int i = 1; BlockReading && BlockReading->nHeight > 0; i++) { if (PastBlocksMax > 0 && i > PastBlocksMax) { break; } // Makes sure we are only calculating blocks from the specified algo if (BlockReading->GetAlgo() != algo){ BlockReading = BlockReading->pprev; continue; } AlgoCounter++; BlockReading = BlockReading->pprev; } if (BlockLastSolved == NULL || BlockLastSolved->nHeight == 0 || (uint64_t)BlockLastSolved->nHeight < PastBlocksMin || AlgoCounter < PastBlocksMin) { return Params().ProofOfWorkLimit(algo).GetCompact(); } int64_t LatestBlockTime = BlockLastSolved->GetBlockTime(); BlockReading = pindexPrevAlgo; for (unsigned int i = 1; BlockReading && BlockReading->nHeight > 0; i++) { if (PastBlocksMax > 0 && i > AlgoCounter) { break; } // Makes sure we are only calculating blocks from the specified algo if (BlockReading->GetAlgo() != algo){ BlockReading = BlockReading->pprev; continue; } PastBlocksMass++; if (i == 1) { PastDifficultyAverage.SetCompact(BlockReading->nBits); } else { BlockReadingDifficulty.SetCompact(BlockReading->nBits); if (BlockReadingDifficulty > PastDifficultyAveragePrev) { PastDifficultyAverage = PastDifficultyAveragePrev + ((BlockReadingDifficulty - PastDifficultyAveragePrev) / i); } else { PastDifficultyAverage = PastDifficultyAveragePrev - ((PastDifficultyAveragePrev - BlockReadingDifficulty) / i); } } PastDifficultyAveragePrev = PastDifficultyAverage; if (LatestBlockTime < BlockReading->GetBlockTime()) { LatestBlockTime = BlockReading->GetBlockTime(); } PastRateActualSeconds = LatestBlockTime - BlockReading->GetBlockTime(); PastRateTargetSeconds = TargetBlockSpacing * PastBlocksMass; PastRateAdjustmentRatio = double(1); if (PastRateActualSeconds < 1) { PastRateActualSeconds = 1; } if (PastRateActualSeconds != 0 && PastRateTargetSeconds != 0) { PastRateAdjustmentRatio = double(PastRateTargetSeconds) / double(PastRateActualSeconds); } EventHorizonDeviation = 1 + (0.7084 * pow((double(PastBlocksMass)/double(144)), -1.228)); EventHorizonDeviationFast = EventHorizonDeviation; EventHorizonDeviationSlow = 1 / EventHorizonDeviation; if (PastBlocksMass >= PastBlocksMin) { if ((PastRateAdjustmentRatio <= EventHorizonDeviationSlow) || (PastRateAdjustmentRatio >= EventHorizonDeviationFast)) { assert(BlockReading); break; } } if (BlockReading->pprev == NULL) { assert(BlockReading); break; } BlockReading = BlockReading->pprev; } uint256 bnNew(PastDifficultyAverage); if (PastRateActualSeconds != 0 && PastRateTargetSeconds != 0) { bnNew *= PastRateActualSeconds; bnNew /= PastRateTargetSeconds; } if (bnNew > Params().ProofOfWorkLimit(algo)) { bnNew = Params().ProofOfWorkLimit(algo); } // debug print if (fDebug){ LogPrintf("Franko Multi Algo Gravity Well\n"); LogPrintf("PastRateAdjustmentRatio = %g PastRateTargetSeconds = %d PastRateActualSeconds = %d\n", PastRateAdjustmentRatio, PastRateTargetSeconds, PastRateActualSeconds); LogPrintf("Before: %08x %s\n", BlockLastSolved->nBits, uint256().SetCompact(BlockLastSolved->nBits).ToString()); LogPrintf("After: %08x %s\n", bnNew.GetCompact(), bnNew.ToString()); } return bnNew.GetCompact(); }
unsigned int static DarkGravityWave(const CBlockIndex* pindexLast, const CBlockHeader *pblock, const Consensus::Params& params) { /* current difficulty formula, dash - DarkGravity v3, written by Evan Duffield - [email protected] */ const arith_uint256 bnPowLimit = UintToArith256(params.powLimit); int64_t nPastBlocks = 24; // make sure we have at least (nPastBlocks + 1) blocks, otherwise just return powLimit if (!pindexLast || pindexLast->nHeight < nPastBlocks) { return bnPowLimit.GetCompact(); } if (params.fPowAllowMinDifficultyBlocks && ( // testnet ... (params.hashDevnetGenesisBlock.IsNull() && pindexLast->nChainWork >= UintToArith256(uint256S("0x000000000000000000000000000000000000000000000000003e9ccfe0e03e01"))) || // or devnet !params.hashDevnetGenesisBlock.IsNull())) { // NOTE: 000000000000000000000000000000000000000000000000003e9ccfe0e03e01 is the work of the "wrong" chain, // so this rule activates there immediately and new blocks with high diff from that chain are going // to be rejected by updated nodes. Note, that old nodes are going to reject blocks from updated nodes // after the "right" chain reaches this amount of work too. This is a temporary condition which should // be removed when we decide to hard-fork testnet again. // TODO: remove "testnet+work OR devnet" part on next testnet hard-fork // Special difficulty rule for testnet/devnet: // If the new block's timestamp is more than 2* 2.5 minutes // then allow mining of a min-difficulty block. // start using smoother adjustment on testnet when total work hits // 000000000000000000000000000000000000000000000000003ff00000000000 if (pindexLast->nChainWork >= UintToArith256(uint256S("0x000000000000000000000000000000000000000000000000003ff00000000000")) // and immediately on devnet || !params.hashDevnetGenesisBlock.IsNull()) { // recent block is more than 2 hours old if (pblock->GetBlockTime() > pindexLast->GetBlockTime() + 2 * 60 * 60) { return bnPowLimit.GetCompact(); } // recent block is more than 10 minutes old if (pblock->GetBlockTime() > pindexLast->GetBlockTime() + params.nPowTargetSpacing*4) { arith_uint256 bnNew = arith_uint256().SetCompact(pindexLast->nBits) * 10; if (bnNew > bnPowLimit) { bnNew = bnPowLimit; } return bnNew.GetCompact(); } } else { // old stuff if (pblock->GetBlockTime() > pindexLast->GetBlockTime() + params.nPowTargetSpacing*2) { return bnPowLimit.GetCompact(); } } } const CBlockIndex *pindex = pindexLast; arith_uint256 bnPastTargetAvg; for (unsigned int nCountBlocks = 1; nCountBlocks <= nPastBlocks; nCountBlocks++) { arith_uint256 bnTarget = arith_uint256().SetCompact(pindex->nBits); if (nCountBlocks == 1) { bnPastTargetAvg = bnTarget; } else { // NOTE: that's not an average really... bnPastTargetAvg = (bnPastTargetAvg * nCountBlocks + bnTarget) / (nCountBlocks + 1); } if(nCountBlocks != nPastBlocks) { assert(pindex->pprev); // should never fail pindex = pindex->pprev; } } arith_uint256 bnNew(bnPastTargetAvg); int64_t nActualTimespan = pindexLast->GetBlockTime() - pindex->GetBlockTime(); // NOTE: is this accurate? nActualTimespan counts it for (nPastBlocks - 1) blocks only... int64_t nTargetTimespan = nPastBlocks * params.nPowTargetSpacing; if (nActualTimespan < nTargetTimespan/3) nActualTimespan = nTargetTimespan/3; if (nActualTimespan > nTargetTimespan*3) nActualTimespan = nTargetTimespan*3; // Retarget bnNew *= nActualTimespan; bnNew /= nTargetTimespan; if (bnNew > bnPowLimit) { bnNew = bnPowLimit; } return bnNew.GetCompact(); }