diff --git a/backend/src/api/difficulty-adjustment.ts b/backend/src/api/difficulty-adjustment.ts index c4e2abf31..3e953e4c8 100644 --- a/backend/src/api/difficulty-adjustment.ts +++ b/backend/src/api/difficulty-adjustment.ts @@ -24,12 +24,11 @@ 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 - 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,18 +36,16 @@ export function calcDifficultyAdjustment( const expectedBlocks = diffSeconds / BLOCK_SECONDS_TARGET; let difficultyChange = 0; - let timeAvgSecs = diffSeconds / blocksInEpoch; - // 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; - } + let timeAvgSecs = blocksInEpoch ? diffSeconds / blocksInEpoch : BLOCK_SECONDS_TARGET; + + 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, 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 {