From 5f60cb821ac55c87f59087329aba168515eb4042 Mon Sep 17 00:00:00 2001 From: Mononaut Date: Sun, 26 Mar 2023 07:27:11 +0900 Subject: [PATCH 1/3] Fix difficulty adjustment start-of-epoch edge cases --- backend/src/api/difficulty-adjustment.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/src/api/difficulty-adjustment.ts b/backend/src/api/difficulty-adjustment.ts index c4e2abf31..0ddf2b540 100644 --- a/backend/src/api/difficulty-adjustment.ts +++ b/backend/src/api/difficulty-adjustment.ts @@ -29,7 +29,7 @@ export function calcDifficultyAdjustment( const BLOCK_SECONDS_TARGET = 600; // Bitcoin mainnet const TESTNET_MAX_BLOCK_SECONDS = 1200; // Bitcoin testnet - const diffSeconds = nowSeconds - DATime; + const diffSeconds = Math.max(0, nowSeconds - DATime); const blocksInEpoch = (blockHeight >= 0) ? blockHeight % EPOCH_BLOCK_LENGTH : 0; const progressPercent = (blockHeight >= 0) ? blocksInEpoch / EPOCH_BLOCK_LENGTH * 100 : 100; const remainingBlocks = EPOCH_BLOCK_LENGTH - blocksInEpoch; @@ -37,7 +37,7 @@ export function calcDifficultyAdjustment( const expectedBlocks = diffSeconds / BLOCK_SECONDS_TARGET; let difficultyChange = 0; - let timeAvgSecs = diffSeconds / blocksInEpoch; + 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; From edfbede704356bf497953d772c3e7158fa1f8af4 Mon Sep 17 00:00:00 2001 From: Mononaut Date: Sun, 26 Mar 2023 09:05:41 +0900 Subject: [PATCH 2/3] Don't send back difficulty adjustment info --- backend/src/api/websocket-handler.ts | 9 ++++++--- backend/src/mempool.interfaces.ts | 2 ++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/backend/src/api/websocket-handler.ts b/backend/src/api/websocket-handler.ts index c89179ce7..7dbd48c46 100644 --- a/backend/src/api/websocket-handler.ts +++ b/backend/src/api/websocket-handler.ts @@ -211,6 +211,7 @@ class WebsocketHandler { if (!_blocks) { _blocks = blocks.getBlocks().slice(-config.MEMPOOL.INITIAL_BLOCKS_AMOUNT); } + const da = difficultyAdjustment.getDifficultyAdjustment(); return { 'mempoolInfo': memPool.getMempoolInfo(), 'vBytesPerSecond': memPool.getVBytesPerSecond(), @@ -220,7 +221,7 @@ class WebsocketHandler { 'transactions': memPool.getLatestTransactions(), 'backendInfo': backendInfo.getBackendInfo(), 'loadingIndicators': loadingIndicators.getLoadingIndicators(), - 'da': difficultyAdjustment.getDifficultyAdjustment(), + 'da': da?.previousTime ? da : undefined, 'fees': feeApi.getRecommendedFee(), ...this.extraInitProperties }; @@ -278,7 +279,9 @@ class WebsocketHandler { response['mempoolInfo'] = mempoolInfo; response['vBytesPerSecond'] = vBytesPerSecond; response['transactions'] = newTransactions.slice(0, 6).map((tx) => Common.stripTransaction(tx)); - response['da'] = da; + if (da?.previousTime) { + response['da'] = da; + } response['fees'] = recommendedFees; } @@ -505,7 +508,7 @@ class WebsocketHandler { const response = { 'block': block, 'mempoolInfo': memPool.getMempoolInfo(), - 'da': da, + 'da': da?.previousTime ? da : undefined, 'fees': fees, }; diff --git a/backend/src/mempool.interfaces.ts b/backend/src/mempool.interfaces.ts index 9961632c3..28c1e21b5 100644 --- a/backend/src/mempool.interfaces.ts +++ b/backend/src/mempool.interfaces.ts @@ -309,9 +309,11 @@ export interface IDifficultyAdjustment { remainingBlocks: number; remainingTime: number; previousRetarget: number; + previousTime: number; nextRetargetHeight: number; timeAvg: number; timeOffset: number; + expectedBlocks: number; } export interface IndexedDifficultyAdjustment { 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 3/3] 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,