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
const CBlockIndex* pindexFirst = pindexLast;
arith_uint256 bnAvg;
arith_uint256 bnTmp;
arith_uint256 bnTot {0};
for (int i = 0; pindexFirst && i < params.nPowAveragingWindow; i++) {
if (i == 0) {
bnAvg.SetCompact(pindexFirst->nBits);
} else {
bnTmp.SetCompact(pindexFirst->nBits);
bnAvg = ((bnAvg * i) + bnTmp) / (i + 1);
}
arith_uint256 bnTmp;
bnTmp.SetCompact(pindexFirst->nBits);
bnTot += bnTmp;
pindexFirst = pindexFirst->pprev;
}
@ -42,6 +38,8 @@ unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHead
if (pindexFirst == NULL)
return nProofOfWorkLimit;
arith_uint256 bnAvg {bnTot / params.nPowAveragingWindow};
return CalculateNextWorkRequired(bnAvg, pindexLast->GetMedianTimePast(), pindexFirst->GetMedianTimePast(), params);
}