Refactor difficulty adjustment calculation.

This commit is contained in:
Miguel Medeiros 2021-08-25 17:18:27 -03:00
parent bd1a37b8ef
commit c7db81c97c

View File

@ -128,24 +128,40 @@ export class DashboardComponent implements OnInit {
const now = new Date().getTime() / 1000; const now = new Date().getTime() / 1000;
const diff = now - DATime; const diff = now - DATime;
const blocksInEpoch = block.height % 2016; const blocksInEpoch = block.height % 2016;
const estimatedBlocks = Math.round(diff / 60 / 10); const progress = (blocksInEpoch >= 0) ? (blocksInEpoch / 2016 * 100).toFixed(2) : `100`;
let difficultyChange = 0; const remainingBlocks = 2016 - blocksInEpoch;
const newDifficultyHeight = block.height + remainingBlocks;
let change = 0;
if (blocksInEpoch > 0) { if (blocksInEpoch > 0) {
difficultyChange = (600 / (diff / blocksInEpoch ) - 1) * 100; change = (600 / (diff / blocksInEpoch ) - 1) * 100;
}
if (change > 300) {
change = 300;
}
if (change < -75) {
change = -75;
} }
let base = 0; const timeAvgDiff = change * 0.1;
if (blocksInEpoch >= estimatedBlocks) { let timeAvgMins = 10;
base = estimatedBlocks / 2016 * 100; if (timeAvgDiff > 0) {
timeAvgMins -= Math.abs(timeAvgDiff);
} else { } else {
base = blocksInEpoch / 2016 * 100; timeAvgMins += Math.abs(timeAvgDiff);
} }
let colorAdjustments = '#dc3545'; const timeAvg = timeAvgMins.toFixed(0);
if (difficultyChange >= 0) { const remainingTime = (remainingBlocks * timeAvgMins * 60 * 1000) + (now * 1000);
let colorAdjustments = '#ffffff66';
if (change > 0) {
colorAdjustments = '#3bcc49'; colorAdjustments = '#3bcc49';
} }
if (change < 0) {
colorAdjustments = '#dc3545';
}
let colorPreviousAdjustments = '#dc3545'; let colorPreviousAdjustments = '#dc3545';
if (previousRetarget){ if (previousRetarget){
@ -159,41 +175,18 @@ export class DashboardComponent implements OnInit {
colorPreviousAdjustments = '#ffffff66'; colorPreviousAdjustments = '#ffffff66';
} }
const timeAvgDiff = difficultyChange * 0.1;
let timeAvgMins = 10;
if(timeAvgDiff > timeAvgDiff){
if (timeAvgDiff > 0){
timeAvgMins -= Math.abs(timeAvgDiff);
} else {
timeAvgMins += Math.abs(timeAvgDiff);
}
}
const remainingBlocks = 2016 - blocksInEpoch;
const nowMilliseconds = now * 1000;
const timeAvgMilliseconds = timeAvgMins * 60 * 1000;
const remainingBlocsMilliseconds = remainingBlocks * timeAvgMilliseconds;
if(difficultyChange > 300) {
difficultyChange = 300;
}
if(difficultyChange < -75){
difficultyChange = -75;
}
return { return {
base: base + '%', base: `${progress}%`,
change: difficultyChange, change,
progress: base.toFixed(2), progress,
remainingBlocks, remainingBlocks,
timeAvg: timeAvgMins.toFixed(0), timeAvg,
colorAdjustments, colorAdjustments,
colorPreviousAdjustments, colorPreviousAdjustments,
blocksInEpoch, blocksInEpoch,
newDifficultyHeight: block.height + remainingBlocks, newDifficultyHeight,
remainingTime: remainingBlocsMilliseconds + nowMilliseconds, remainingTime,
previousRetarget: previousRetarget ? previousRetarget : 0 previousRetarget,
}; };
}) })
); );