Skip to content

Commit

Permalink
ensures that the EMA has all the datapoints it needs to perform the E…
Browse files Browse the repository at this point in the history
…MA caculation
  • Loading branch information
pedro-at-decenomy committed Jul 11, 2024
1 parent 1f5951b commit f3e26ec
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions src/pow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,21 +258,27 @@ unsigned int GetNextWorkRequiredPOSV3(const CBlockIndex* pIndexLast)
const int64_t nN = 20; // EMA period

uint256 bnNew(0);
const CBlockIndex* pIndexReading = pIndexLast;

if(pIndexLast->nHeight > nN) {

const CBlockIndex* pIndexReading = pIndexLast;

for (unsigned int i = 0;
pIndexReading && pIndexReading->nHeight && i < nN;
i++
) {
uint256 currentValue;
currentValue.SetCompact(pIndexReading->nBits);
for (unsigned int i = 0;
pIndexReading && pIndexReading->nHeight && i < nN;
i++
) {
uint256 currentValue;
currentValue.SetCompact(pIndexReading->nBits);

bnNew = ((currentValue * nAlpha) / (nN + 1)) + ((bnNew * (nN + 1 - nAlpha)) / (nN + 1));
bnNew = ((currentValue * nAlpha) / (nN + 1)) + ((bnNew * (nN + 1 - nAlpha)) / (nN + 1));

pIndexReading = pIndexReading->pprev;
}
pIndexReading = pIndexReading->pprev;
}

std::cout << "GetNextWorkRequiredPOSV3 nBits ema: " << GetDifficulty(bnNew.GetCompact()) << std::endl;
std::cout << "GetNextWorkRequiredPOSV3 nBits EMA: " << GetDifficulty(bnNew.GetCompact()) << std::endl;
} else {
bnNew.SetCompact(pIndexLast->nBits);
}

// Retarget the difficulty based on a PID controller based function
int64_t nActualSpacing = nHeight > 1 ? pIndexLast->GetBlockTime() - pIndexLast->pprev->GetBlockTime() : nTargetSpacing;
Expand Down

0 comments on commit f3e26ec

Please sign in to comment.