lwma diff algo

This commit is contained in:
Kks
2024-04-07 13:17:25 -04:00
parent f2fc262362
commit d31261dd65
4 changed files with 90 additions and 23 deletions

View File

@@ -14,6 +14,14 @@ unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHead
{
assert(pindexLast != nullptr);
unsigned int nProofOfWorkLimit = UintToArith256(params.powLimit).GetCompact();
unsigned int nProofOfWorkLimitV2 = UintToArith256(params.powLimitV2).GetCompact();
// reset difficulty for new diff algorithm's average + Segwit/CSV activation
if (pindexLast->nHeight >= 28927)
return nProofOfWorkLimitV2;
if (pindexLast->nHeight >= 28930)
return LwmaCalculateNextWorkRequired(pindexLast, params);
// Only change once per difficulty adjustment interval
if ((pindexLast->nHeight+1) % params.DifficultyAdjustmentInterval() != 0)
@@ -46,6 +54,57 @@ unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHead
return CalculateNextWorkRequired(pindexLast, pindexFirst->GetBlockTime(), params);
}
unsigned int LwmaCalculateNextWorkRequired(const CBlockIndex* pindexLast, const Consensus::Params& params)
{
const int64_t T = params.nPowTargetSpacingV2;
const int64_t N = 240;
const int64_t k = N * (N + 1) * T / 2; // For T=120, 240, 600 use approx N=100, 75, 50
const int64_t height = pindexLast->nHeight;
const arith_uint256 powLimit = UintToArith256(params.powLimit);
if (height < N) { return powLimit.GetCompact(); }
arith_uint256 sumTarget, nextTarget;
int64_t thisTimestamp, previousTimestamp;
int64_t t = 0, j = 0;
// Uncomment next 2 lines to use LWMA-3 jump rule.
//arith_uint256 previousTarget = 0;
//int64_t sumLast3Solvetimes = 0;
const CBlockIndex* blockPreviousTimestamp = pindexLast->GetAncestor(height - N);
previousTimestamp = blockPreviousTimestamp->GetBlockTime();
// Loop through N most recent blocks.
for (int64_t i = height - N + 1; i <= height; i++) {
const CBlockIndex* block = pindexLast->GetAncestor(i);
thisTimestamp = (block->GetBlockTime() > previousTimestamp) ?
block->GetBlockTime() : previousTimestamp + 1;
int64_t solvetime = std::min(6 * T, thisTimestamp - previousTimestamp);
previousTimestamp = thisTimestamp;
j++;
t += solvetime * j; // Weighted solvetime sum.
arith_uint256 target;
target.SetCompact(block->nBits);
sumTarget += target / (k * N);
// Uncomment next 2 lines to use LWMA-3.
// if (i > height - 3) { sumLast3Solvetimes += solvetime; }
// if (i == height) { previousTarget = target.SetCompact(block->nBits); }
}
nextTarget = t * sumTarget;
// Uncomment the following to use LWMA-3.
// This is a "memory-less" jump in difficulty approximately 2x normal
// if (sumLast3Solvetimes < (8 * T) / 10) { nextTarget = (previousTarget*100)/(100+(N*26)/200); }
if (nextTarget > powLimit) { nextTarget = powLimit; }
return nextTarget.GetCompact();
}
unsigned int CalculateNextWorkRequired(const CBlockIndex* pindexLast, int64_t nFirstBlockTime, const Consensus::Params& params)
{
if (params.fPowNoRetargeting)