From c7cab4c877f11499a0f2d13b5f017a8781b301cc Mon Sep 17 00:00:00 2001 From: nymkappa <1612910616@pm.me> Date: Sun, 26 Mar 2023 17:01:04 +0900 Subject: [PATCH] Remove difficulty adjustment calculation lag in the backend --- backend/src/api/difficulty-adjustment.ts | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/backend/src/api/difficulty-adjustment.ts b/backend/src/api/difficulty-adjustment.ts index 0ddf2b540..3e953e4c8 100644 --- a/backend/src/api/difficulty-adjustment.ts +++ b/backend/src/api/difficulty-adjustment.ts @@ -24,7 +24,6 @@ export function calcDifficultyAdjustment( network: string, latestBlockTimestamp: number, ): DifficultyAdjustment { - const ESTIMATE_LAG_BLOCKS = 146; // For first 7.2% of epoch, don't estimate. const EPOCH_BLOCK_LENGTH = 2016; // Bitcoin mainnet const BLOCK_SECONDS_TARGET = 600; // Bitcoin mainnet const TESTNET_MAX_BLOCK_SECONDS = 1200; // Bitcoin testnet @@ -38,17 +37,15 @@ export function calcDifficultyAdjustment( let difficultyChange = 0; let timeAvgSecs = blocksInEpoch ? diffSeconds / blocksInEpoch : BLOCK_SECONDS_TARGET; - // Only calculate the estimate once we have 7.2% of blocks in current epoch - if (blocksInEpoch >= ESTIMATE_LAG_BLOCKS) { - difficultyChange = (BLOCK_SECONDS_TARGET / timeAvgSecs - 1) * 100; - // Max increase is x4 (+300%) - if (difficultyChange > 300) { - difficultyChange = 300; - } - // Max decrease is /4 (-75%) - if (difficultyChange < -75) { - difficultyChange = -75; - } + + difficultyChange = (BLOCK_SECONDS_TARGET / timeAvgSecs - 1) * 100; + // Max increase is x4 (+300%) + if (difficultyChange > 300) { + difficultyChange = 300; + } + // Max decrease is /4 (-75%) + if (difficultyChange < -75) { + difficultyChange = -75; } // Testnet difficulty is set to 1 after 20 minutes of no blocks,