From 037d6a75eae5abf9cdbdc0835badf46db81898ad Mon Sep 17 00:00:00 2001 From: softsimon Date: Fri, 23 Jul 2021 14:35:04 +0300 Subject: [PATCH] Adding previous difficulty retarget to the difficulty adjustment api. (#652) refs #640 --- backend/src/api/blocks.ts | 13 +++++++++++++ backend/src/api/websocket-handler.ts | 2 ++ backend/src/routes.ts | 15 ++++++++------- .../src/app/interfaces/websocket.interface.ts | 1 + 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/backend/src/api/blocks.ts b/backend/src/api/blocks.ts index 25f92ff5c..fbfcc2828 100644 --- a/backend/src/api/blocks.ts +++ b/backend/src/api/blocks.ts @@ -11,7 +11,9 @@ class Blocks { private static INITIAL_BLOCK_AMOUNT = 8; private blocks: BlockExtended[] = []; private currentBlockHeight = 0; + private currentDifficulty = 0; private lastDifficultyAdjustmentTime = 0; + private previousDifficultyRetarget = 0; private newBlockCallbacks: ((block: BlockExtended, txIds: string[], transactions: TransactionExtended[]) => void)[] = []; constructor() { } @@ -47,6 +49,11 @@ class Blocks { const blockHash = await bitcoinApi.$getBlockHash(blockHeightTip - heightDiff); const block = await bitcoinApi.$getBlock(blockHash); this.lastDifficultyAdjustmentTime = block.timestamp; + this.currentDifficulty = block.difficulty; + + const previousPeriodBlockHash = await bitcoinApi.$getBlockHash(blockHeightTip - heightDiff - 2016); + const previousPeriodBlock = await bitcoinApi.$getBlock(previousPeriodBlockHash); + this.previousDifficultyRetarget = (block.difficulty - previousPeriodBlock.difficulty) / previousPeriodBlock.difficulty * 100; } while (this.currentBlockHeight < blockHeightTip) { @@ -101,7 +108,9 @@ class Blocks { blockExtended.feeRange = transactions.length > 1 ? Common.getFeesInRange(transactions, 8) : [0, 0]; if (block.height % 2016 === 0) { + this.previousDifficultyRetarget = (block.difficulty - this.currentDifficulty) / this.currentDifficulty * 100; this.lastDifficultyAdjustmentTime = block.timestamp; + this.currentDifficulty = block.difficulty; } this.blocks.push(blockExtended); @@ -122,6 +131,10 @@ class Blocks { return this.lastDifficultyAdjustmentTime; } + public getPreviousDifficultyRetarget(): number { + return this.previousDifficultyRetarget; + } + public getCurrentBlockHeight(): number { return this.currentBlockHeight; } diff --git a/backend/src/api/websocket-handler.ts b/backend/src/api/websocket-handler.ts index 135411a3d..eb2b90ccc 100644 --- a/backend/src/api/websocket-handler.ts +++ b/backend/src/api/websocket-handler.ts @@ -172,6 +172,7 @@ class WebsocketHandler { 'mempoolInfo': memPool.getMempoolInfo(), 'vBytesPerSecond': memPool.getVBytesPerSecond(), 'lastDifficultyAdjustment': blocks.getLastDifficultyAdjustmentTime(), + 'previousRetarget': blocks.getPreviousDifficultyRetarget(), 'blocks': _blocks, 'conversions': fiatConversion.getConversionRates(), 'mempool-blocks': mempoolBlocks.getMempoolBlocks(), @@ -384,6 +385,7 @@ class WebsocketHandler { 'block': block, 'mempoolInfo': memPool.getMempoolInfo(), 'lastDifficultyAdjustment': blocks.getLastDifficultyAdjustmentTime(), + 'previousRetarget': blocks.getPreviousDifficultyRetarget(), }; if (mBlocks && client['want-mempool-blocks']) { diff --git a/backend/src/routes.ts b/backend/src/routes.ts index 3e7e487bb..bc42a9868 100644 --- a/backend/src/routes.ts +++ b/backend/src/routes.ts @@ -680,12 +680,12 @@ class Routes { public getDifficultyChange(req: Request, res: Response) { try { const now = new Date().getTime() / 1000; - const DATime=blocks.getLastDifficultyAdjustmentTime(); + const DATime = blocks.getLastDifficultyAdjustmentTime(); + const previousRetarget = blocks.getPreviousDifficultyRetarget(); const diff = now - DATime; - const blockHeight=blocks.getCurrentBlockHeight(); + const blockHeight = blocks.getCurrentBlockHeight(); const blocksInEpoch = blockHeight % 2016; - const estimatedBlocks = Math.round(diff / 60 / 10); - const difficultyChange = (600 / (diff / blocksInEpoch ) - 1) * 100; + const difficultyChange = (600 / (diff / blocksInEpoch) - 1) * 100; const timeAvgDiff = difficultyChange * 0.1; @@ -699,9 +699,9 @@ class Routes { const remainingBlocks = 2016 - blocksInEpoch; const timeAvgSeconds = timeAvgMins * 60; const remainingTime = remainingBlocks * timeAvgSeconds; - const estimatedRetargetDate=(remainingTime + now); - const totalTime=estimatedRetargetDate-DATime; - const progressPercent=100-((remainingTime*100)/totalTime); + const estimatedRetargetDate = (remainingTime + now); + const totalTime = estimatedRetargetDate-DATime; + const progressPercent = 100 - ((remainingTime * 100) / totalTime); const result={ progressPercent, @@ -709,6 +709,7 @@ class Routes { estimatedRetargetDate, remainingBlocks, remainingTime, + previousRetarget, } res.json(result); diff --git a/frontend/src/app/interfaces/websocket.interface.ts b/frontend/src/app/interfaces/websocket.interface.ts index 2138ad8dc..e4e31b942 100644 --- a/frontend/src/app/interfaces/websocket.interface.ts +++ b/frontend/src/app/interfaces/websocket.interface.ts @@ -10,6 +10,7 @@ export interface WebsocketResponse { mempoolInfo?: MempoolInfo; vBytesPerSecond?: number; lastDifficultyAdjustment?: number; + previousRetarget?: number; action?: string; data?: string[]; tx?: Transaction;