Refactor Difficulty API logic

This commit is contained in:
junderw 2022-08-20 13:02:22 +09:00
parent 87c9f881c0
commit 3a4982c5e6
No known key found for this signature in database
GPG key ID: A9273B5AD3E47B45

View file

@ -25,6 +25,7 @@ export function calcDifficultyAdjustment(
const ESTIMATE_LAG_BLOCKS = 146; // For first 7.2% of epoch, don't estimate. const ESTIMATE_LAG_BLOCKS = 146; // For first 7.2% of epoch, don't estimate.
const EPOCH_BLOCK_LENGTH = 2016; // Bitcoin mainnet const EPOCH_BLOCK_LENGTH = 2016; // Bitcoin mainnet
const BLOCK_SECONDS_TARGET = 600; // Bitcoin mainnet const BLOCK_SECONDS_TARGET = 600; // Bitcoin mainnet
const TESTNET_MAX_BLOCK_SECONDS = 1200; // Bitcoin testnet
const diffSeconds = nowSeconds - DATime; const diffSeconds = nowSeconds - DATime;
const blocksInEpoch = (blockHeight >= 0) ? blockHeight % EPOCH_BLOCK_LENGTH : 0; const blocksInEpoch = (blockHeight >= 0) ? blockHeight % EPOCH_BLOCK_LENGTH : 0;
@ -33,12 +34,11 @@ export function calcDifficultyAdjustment(
const nextRetargetHeight = (blockHeight >= 0) ? blockHeight + remainingBlocks : 0; const nextRetargetHeight = (blockHeight >= 0) ? blockHeight + remainingBlocks : 0;
let difficultyChange = 0; let difficultyChange = 0;
let timeAvgMins = 10; let timeAvgSecs = BLOCK_SECONDS_TARGET;
// Only calculate the estimate once we have 7.2% of blocks in current epoch // Only calculate the estimate once we have 7.2% of blocks in current epoch
if (blocksInEpoch >= ESTIMATE_LAG_BLOCKS) { if (blocksInEpoch >= ESTIMATE_LAG_BLOCKS) {
const secondsPerBlock = diffSeconds / blocksInEpoch; timeAvgSecs = diffSeconds / blocksInEpoch;
timeAvgMins = secondsPerBlock / 60; difficultyChange = (BLOCK_SECONDS_TARGET / timeAvgSecs - 1) * 100;
difficultyChange = (BLOCK_SECONDS_TARGET / secondsPerBlock - 1) * 100;
// Max increase is x4 (+300%) // Max increase is x4 (+300%)
if (difficultyChange > 300) { if (difficultyChange > 300) {
difficultyChange = 300; difficultyChange = 300;
@ -53,16 +53,17 @@ export function calcDifficultyAdjustment(
// therefore the time between blocks will always be below 20 minutes (1200s). // therefore the time between blocks will always be below 20 minutes (1200s).
let timeOffset = 0; let timeOffset = 0;
if (network === 'testnet') { if (network === 'testnet') {
if (timeAvgMins > 20) { if (timeAvgSecs > TESTNET_MAX_BLOCK_SECONDS) {
timeAvgMins = 20; timeAvgSecs = TESTNET_MAX_BLOCK_SECONDS;
} }
if (nowSeconds - latestBlockTimestamp + timeAvgMins * 60 > 1200) { const secondsSinceLastBlock = nowSeconds - latestBlockTimestamp;
timeOffset = -Math.min(nowSeconds - latestBlockTimestamp, 1200) * 1000; if (secondsSinceLastBlock + timeAvgSecs > TESTNET_MAX_BLOCK_SECONDS) {
timeOffset = -Math.min(secondsSinceLastBlock, TESTNET_MAX_BLOCK_SECONDS) * 1000;
} }
} }
const timeAvg = Math.floor(timeAvgMins * 60 * 1000); const timeAvg = Math.floor(timeAvgSecs * 1000);
const remainingTime = remainingBlocks * timeAvg; const remainingTime = remainingBlocks * timeAvg;
const estimatedRetargetDate = remainingTime + nowSeconds * 1000; const estimatedRetargetDate = remainingTime + nowSeconds * 1000;