Merge pull request #746 from MiguelMedeiros/bugfix-difficulty-adjustment-calc

Bugfix: difficulty adjustment calculation.
This commit is contained in:
softsimon 2021-08-26 03:27:59 +03:00 committed by GitHub
commit c0f2fa3042
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 54 deletions

View File

@ -693,37 +693,50 @@ class Routes {
public getDifficultyChange(req: Request, res: Response) { public getDifficultyChange(req: Request, res: Response) {
try { try {
const now = new Date().getTime() / 1000;
const DATime = blocks.getLastDifficultyAdjustmentTime(); const DATime = blocks.getLastDifficultyAdjustmentTime();
const previousRetarget = blocks.getPreviousDifficultyRetarget(); const previousRetarget = blocks.getPreviousDifficultyRetarget();
const diff = now - DATime;
const blockHeight = blocks.getCurrentBlockHeight(); const blockHeight = blocks.getCurrentBlockHeight();
const now = new Date().getTime() / 1000;
const diff = now - DATime;
const blocksInEpoch = blockHeight % 2016; const blocksInEpoch = blockHeight % 2016;
const difficultyChange = (600 / (diff / blocksInEpoch) - 1) * 100; const progressPercent = (blocksInEpoch >= 0) ? blocksInEpoch / 2016 * 100 : 100;
const remainingBlocks = 2016 - blocksInEpoch;
const nextRetargetHeight = blockHeight + remainingBlocks;
let difficultyChange = 0;
if (blocksInEpoch > 0) {
difficultyChange = (600 / (diff / blocksInEpoch ) - 1) * 100;
}
if (difficultyChange > 300) {
difficultyChange = 300;
}
if (difficultyChange < -75) {
difficultyChange = -75;
}
const timeAvgDiff = difficultyChange * 0.1; const timeAvgDiff = difficultyChange * 0.1;
let timeAvgMins = 10; let timeAvgMins = 10;
if (timeAvgDiff > 0 ){ if (timeAvgDiff > 0) {
timeAvgMins -= Math.abs(timeAvgDiff); timeAvgMins -= Math.abs(timeAvgDiff);
} else { } else {
timeAvgMins += Math.abs(timeAvgDiff); timeAvgMins += Math.abs(timeAvgDiff);
} }
const remainingBlocks = 2016 - blocksInEpoch; const timeAvg = timeAvgMins * 60;
const timeAvgSeconds = timeAvgMins * 60; const remainingTime = remainingBlocks * timeAvg;
const remainingTime = remainingBlocks * timeAvgSeconds; const estimatedRetargetDate = remainingTime + now;
const estimatedRetargetDate = (remainingTime + now);
const totalTime = estimatedRetargetDate-DATime; const result = {
const progressPercent = 100 - ((remainingTime * 100) / totalTime);
const result={
progressPercent, progressPercent,
difficultyChange, difficultyChange,
estimatedRetargetDate, estimatedRetargetDate,
remainingBlocks, remainingBlocks,
remainingTime, remainingTime,
previousRetarget, previousRetarget,
nextRetargetHeight,
timeAvg,
} }
res.json(result); res.json(result);

View File

@ -116,7 +116,7 @@ export class DashboardComponent implements OnInit {
}; };
}) })
); );
this.difficultyEpoch$ = timer(0, 1000) this.difficultyEpoch$ = timer(0, 1000)
.pipe( .pipe(
switchMap(() => combineLatest([ switchMap(() => combineLatest([
@ -128,72 +128,65 @@ 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){
if (previousRetarget >= 0) { if (previousRetarget >= 0) {
colorPreviousAdjustments = '#3bcc49'; colorPreviousAdjustments = '#3bcc49';
} }
if (previousRetarget === 0) { if (previousRetarget === 0) {
colorPreviousAdjustments = '#ffffff66'; colorPreviousAdjustments = '#ffffff66';
} }
}else{ } else {
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,
}; };
}) })
); );