From 936921781e8ebc503371c08b1a4b0cce057e4db5 Mon Sep 17 00:00:00 2001 From: softsimon Date: Sat, 27 Aug 2022 10:25:24 +0200 Subject: [PATCH] Fix for difficulty adjustment throwing error before in sync --- backend/src/api/bitcoin/bitcoin.routes.ts | 7 ++++++- backend/src/api/difficulty-adjustment.ts | 7 ++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/backend/src/api/bitcoin/bitcoin.routes.ts b/backend/src/api/bitcoin/bitcoin.routes.ts index 66bcb2569..2c3fd9467 100644 --- a/backend/src/api/bitcoin/bitcoin.routes.ts +++ b/backend/src/api/bitcoin/bitcoin.routes.ts @@ -510,7 +510,12 @@ class BitcoinRoutes { private getDifficultyChange(req: Request, res: Response) { try { - res.json(difficultyAdjustment.getDifficultyAdjustment()); + const da = difficultyAdjustment.getDifficultyAdjustment(); + if (da) { + res.json(da); + } else { + res.status(503).send(`Service Temporarily Unavailable`); + } } catch (e) { res.status(500).send(e instanceof Error ? e.message : e); } diff --git a/backend/src/api/difficulty-adjustment.ts b/backend/src/api/difficulty-adjustment.ts index 43c6e463f..a1b6ab70e 100644 --- a/backend/src/api/difficulty-adjustment.ts +++ b/backend/src/api/difficulty-adjustment.ts @@ -81,14 +81,15 @@ export function calcDifficultyAdjustment( } class DifficultyAdjustmentApi { - constructor() { } - - public getDifficultyAdjustment(): IDifficultyAdjustment { + public getDifficultyAdjustment(): IDifficultyAdjustment | null { const DATime = blocks.getLastDifficultyAdjustmentTime(); const previousRetarget = blocks.getPreviousDifficultyRetarget(); const blockHeight = blocks.getCurrentBlockHeight(); const blocksCache = blocks.getBlocks(); const latestBlock = blocksCache[blocksCache.length - 1]; + if (!latestBlock) { + return null; + } const nowSeconds = Math.floor(new Date().getTime() / 1000); return calcDifficultyAdjustment(