Simplify difficulty averaging code
This commit is contained in:
14
src/pow.cpp
14
src/pow.cpp
@@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user