Simplify difficulty averaging code

This commit is contained in:
Jack Grigg 2016-09-06 23:03:36 +12:00
parent 5fbb839f53
commit 7b173bd80f
1 changed files with 6 additions and 8 deletions

View File

@ -26,15 +26,11 @@ unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHead
// Find the first block in the averaging interval // Find the first block in the averaging interval
const CBlockIndex* pindexFirst = pindexLast; const CBlockIndex* pindexFirst = pindexLast;
arith_uint256 bnAvg; arith_uint256 bnTot {0};
arith_uint256 bnTmp;
for (int i = 0; pindexFirst && i < params.nPowAveragingWindow; i++) { for (int i = 0; pindexFirst && i < params.nPowAveragingWindow; i++) {
if (i == 0) { arith_uint256 bnTmp;
bnAvg.SetCompact(pindexFirst->nBits); bnTmp.SetCompact(pindexFirst->nBits);
} else { bnTot += bnTmp;
bnTmp.SetCompact(pindexFirst->nBits);
bnAvg = ((bnAvg * i) + bnTmp) / (i + 1);
}
pindexFirst = pindexFirst->pprev; pindexFirst = pindexFirst->pprev;
} }
@ -42,6 +38,8 @@ unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHead
if (pindexFirst == NULL) if (pindexFirst == NULL)
return nProofOfWorkLimit; return nProofOfWorkLimit;
arith_uint256 bnAvg {bnTot / params.nPowAveragingWindow};
return CalculateNextWorkRequired(bnAvg, pindexLast->GetMedianTimePast(), pindexFirst->GetMedianTimePast(), params); return CalculateNextWorkRequired(bnAvg, pindexLast->GetMedianTimePast(), pindexFirst->GetMedianTimePast(), params);
} }