@@ -281,40 +281,56 @@ unsigned int AverageTargetASERT(const CBlockIndex* pindexLast, const CBlockHeade
281281 static arith_uint256 refBlockTargetCache GUARDED_BY (cs_target_cache);
282282 static int nTargetCacheHeight GUARDED_BY (cs_target_cache) = -2 ;
283283 static int nTargetCacheAlgo GUARDED_BY (cs_target_cache) = CBlockHeader::AlgoType::ALGO_COUNT;
284+ static uint256 nTargetCacheHeightHash GUARDED_BY (cs_target_cache);
284285
285- if (nASERTBlockTargetsToAverage > 0 && nHeight >= nASERTStartHeight + nASERTBlockTargetsToAverage && nHeightDiff >= nASERTBlockTargetsToAverage) {
286- if (!fUseCache || nTargetCacheHeight != static_cast <int >(nHeightDiff / nASERTBlockTargetsToAverage) || nTargetCacheAlgo != algo || refBlockTargetCache == arith_uint256 () || fAlgoMissing ) {
287- const uint32_t nBlocksToSkip = nHeightDiff % nASERTBlockTargetsToAverage;
288- const CBlockIndex* pindex = pindexPrev;
289- // LogPrintf("nBlocksToSkip = %u\n", nBlocksToSkip);
286+ const uint32_t nBlocksToSkip = nHeightDiff % nASERTBlockTargetsToAverage;
287+ const CBlockIndex* pindex = pindexPrev;
288+ // LogPrintf("nBlocksToSkip = %u\n", nBlocksToSkip);
290289
291- for ( unsigned int i = 0 ; i < nBlocksToSkip; i++) {
292- pindex = fAlgoMissing ? GetLastBlockIndex (pindex-> pprev , fProofOfStake ) : GetLastBlockIndexForAlgo (pindex-> pprev , algo);
293- }
294- // LogPrintf("begin pindex->nHeight = %i\n", pindex->nHeight);
290+ // Get last block index in averaging window
291+ for ( unsigned int i = 0 ; i < nBlocksToSkip; i++) {
292+ pindex = fAlgoMissing ? GetLastBlockIndex (pindex-> pprev , fProofOfStake ) : GetLastBlockIndexForAlgo (pindex-> pprev , algo);
293+ }
295294
295+ if (pindex && nASERTBlockTargetsToAverage > 0 && nHeight >= nASERTStartHeight + nASERTBlockTargetsToAverage && nHeightDiff >= nASERTBlockTargetsToAverage) {
296+ const CBlockIndex* pindexWindowEnd = pindex;
297+ // LogPrintf("pindexWindowEnd->nHeight = %u, pindexWindowEnd->GetBlockHash() = %s\n", pindexWindowEnd->nHeight, pindexWindowEnd->GetBlockHash().ToString().c_str());
298+
299+ if (!fUseCache || nTargetCacheHeight != static_cast <int >(pindexWindowEnd->nHeight ) || nTargetCacheAlgo != algo || nTargetCacheHeightHash != pindexWindowEnd->GetBlockHash () ||
300+ refBlockTargetCache == arith_uint256 () || fAlgoMissing ) {
296301 // unsigned int nBlocksAveraged = 0;
302+ // bool fAvgGenesis = false;
297303 for (int i = 0 ; i < static_cast <int >(nASERTBlockTargetsToAverage); i++) {
298- if (pindex->nBits != (nProofOfWorkLimit - 1 ) || !params.fPowAllowMinDifficultyBlocks ) { // Don't add min difficulty targets to the average
304+ // Don't add min difficulty targets to the average
305+ if (pindex->nBits != (nProofOfWorkLimit - 1 ) || !params.fPowAllowMinDifficultyBlocks ) {
299306 arith_uint256 bnTarget = arith_uint256 ().SetCompactBase256 (pindex->nBits );
300307 refBlockTarget += bnTarget / nASERTBlockTargetsToAverage;
301- // nBlocksAveraged++ ;
302- // if (pindex->GetBlockHash() == params.hashGenesisBlock)
308+ // ++nBlocksAveraged ;
309+ // if (pindex->GetBlockHash() == params.hashGenesisBlock) {
303310 // LogPrintf("Averaging genesis block target\n");
304- } else
305- i--; // Average one more block to make up for the one we skipped
311+ // fAvgGenesis = true;
312+ // }
313+ } else {
314+ // Average one more block to make up for the one we skipped
315+ --i;
316+ // LogPrintf("Skipping min difficulty block %s at nHeight %i\n", pindex->GetBlockHash().ToString().c_str(), pindex->nHeight);
317+ }
318+
306319 pindex = fAlgoMissing ? GetLastBlockIndex (pindex->pprev , fProofOfStake ) : GetLastBlockIndexForAlgo (pindex->pprev , algo);
307- if (!pindex)
320+ if (!pindex) {
321+ // If we break here due to reaching the genesis, then it will count as averaging zeroes for the number of blocks skipped which lowers the target/increases difficulty
308322 break ;
323+ }
309324 }
310325 // LogPrintf("nBlocksAveraged = %u\n", nBlocksAveraged);
311- // assert(nBlocksAveraged == nASERTBlockTargetsToAverage);
326+ // assert(fAvgGenesis || nBlocksAveraged == nASERTBlockTargetsToAverage);
312327 // if (pindex)
313- // LogPrintf("end pindex ->nHeight = %i\n", pindex->nHeight);
328+ // LogPrintf("pindexWindowStart ->nHeight = %i\n", pindex->nHeight);
314329 if (fUseCache ) {
315330 refBlockTargetCache = refBlockTarget;
316- nTargetCacheHeight = nHeightDiff / nASERTBlockTargetsToAverage ;
331+ nTargetCacheHeight = pindexWindowEnd-> nHeight ;
317332 nTargetCacheAlgo = algo;
333+ nTargetCacheHeightHash = pindexWindowEnd->GetBlockHash ();
318334 // LogPrintf("Set average target cache at nHeight = %u with algo = %i\n", nHeight, algo);
319335 }
320336 } else {
@@ -323,10 +339,11 @@ unsigned int AverageTargetASERT(const CBlockIndex* pindexLast, const CBlockHeade
323339 }
324340 } else {
325341 if (fUseCache && !fAlgoMissing ) {
326- if (nTargetCacheHeight != -1 || nTargetCacheAlgo != algo || refBlockTargetCache == arith_uint256 ()) {
342+ if (nTargetCacheHeight != -1 || nTargetCacheAlgo != algo || nTargetCacheHeightHash != uint256 () || refBlockTargetCache == arith_uint256 ()) {
327343 refBlockTargetCache = arith_uint256 ().SetCompactBase256 (pindexReferenceBlock->nBits );
328344 nTargetCacheHeight = -1 ;
329345 nTargetCacheAlgo = algo;
346+ nTargetCacheHeightHash = uint256 ();
330347 // LogPrintf("Set ref target cache at nHeight = %u with algo = %i\n", nHeight, algo);
331348 }
332349 refBlockTarget = refBlockTargetCache;
@@ -393,14 +410,16 @@ unsigned int AverageTargetASERT(const CBlockIndex* pindexLast, const CBlockHeade
393410 // arith_uint512 bnNew512(arith_uint512(bnNew) * arith_uint512(numerator) / arith_uint512(denominator));
394411 bnNew = bnNew512.trim256 ();
395412
396- // LogPrintf("numerator = %s\n", numerator.ToString().c_str());
397- // LogPrintf("denominator = %s\n", denominator.ToString().c_str());
413+ // LogPrintf("numerator = %s\n", numerator.trim256(). ToString().c_str());
414+ // LogPrintf("denominator = %s\n", denominator.trim256(). ToString().c_str());
398415 // LogPrintf("numerator = %lu\n", numerator);
399416 // LogPrintf("denominator = %lu\n", denominator);
400417 // LogPrintf("10000 * 2^(%li/%u) = %s\n", dividend, divisor, arith_uint512((10000 * arith_uint512(numerator)) / arith_uint512(denominator)).trim256().ToString().c_str());
401418 if (bnNew512 > arith_uint512 (bnPowLimit) || bnNew == arith_uint256 ())
402419 return nProofOfWorkLimit;
403420
421+ // LogPrintf("bnNew.GetCompactBase256() = %08x\n", bnNew.GetCompactBase256());
422+ // LogPrintf("bnNew.GetCompactRoundedBase256() = %08x\n", bnNew.GetCompactRoundedBase256());
404423 return bnNew.GetCompactRoundedBase256 ();
405424}
406425
0 commit comments