Commit 73602c14 by Nullfunction

WIP: LWMA algo

parent 5dcffbdd
......@@ -1353,6 +1353,59 @@ unsigned int GetNextTargetRequired_V1(const CBlockIndex* pindexLast, bool fProof
return bnNew.GetCompact();
}
unsigned int LwmaCalculateNextWorkRequired(const CBlockIndex* pindexLast, bool fProofOfStake, int algo)
{
// Special difficulty rule for testnet:
// If the new block's timestamp is more than 2 * 10 minutes
// then allow mining of a min-difficulty block.
// if (false &&
// pblock->GetBlockTime() > pindexLast->GetBlockTime() + 60 * 10) {
// return UintToArith256(params.PowLimit(true)).GetCompact();
// }
const int N = 45; // Avg Window
const int k = 13632; //Adjusted Weight
const int height = pindexLast->nHeight + 1;
assert(height > N);
uint256 sum_target;
int t = 0, j = 0;
// Loop through N most recent blocks.
for (int i = height - N; i < height; i++) {
const CBlockIndex* block = pindexLast->GetAncestor(i);
const CBlockIndex* block_Prev = block->GetAncestor(i - 1);
//TODO: Search for last block with algo if (GetAlgo(block->nVersion) == algo)
int64_t solvetime = block->GetBlockTime() - block_Prev->GetBlockTime();
j++;
t += solvetime * j; // Weighted solvetime sum.
// TODO: Get safety feature -6/5 from pseudo code
// Target sum divided by a factor, (k N^2).
// The factor is a part of the final equation. However we divide sum_target here to avoid
// potential overflow.
uint256 target;
target.SetCompact(block->nBits);
sum_target += target / (k * N * N);
}
// Keep t reasonable in case strange solvetimes occurred.
if (t < N * k / 3) {
t = N * k / 3;
}
const uint256 pow_limit = bnProofOfWorkLimit[algo].getuint256();
uint256 next_target = t * sum_target;
if (next_target > pow_limit) {
next_target = pow_limit;
}
return next_target.GetCompact();
}
unsigned int GetNextTargetRequired(const CBlockIndex* pindexLast, bool fProofOfStake, int algo)
{
if (pindexLast->nHeight < 200){//first 200 blocks with default retarget
......
......@@ -1439,6 +1439,24 @@ public:
return pindex->GetMedianTimePast();
}
CBlockIndex* GetAncestor(int height)
{
if (height > nHeight || height < 0)
return nullptr;
CBlockIndex* pindexWalk = this;
int heightWalk = nHeight;
while (heightWalk > height) {
int heightSkip = GetSkipHeight(heightWalk);
int heightSkipPrev = GetSkipHeight(heightWalk - 1);
assert(pindexWalk->pprev);
pindexWalk = pindexWalk->pprev;
heightWalk--;
}
return pindexWalk;
}
/**
* Returns true if there are nRequired or more blocks of minVersion or above
* in the last nToCheck blocks, starting at pstart and going backwards.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment