Fix for difficulty adjustment throwing error before in sync

This commit is contained in:
softsimon 2022-08-27 10:25:24 +02:00
parent 69d4ba18d5
commit 936921781e
No known key found for this signature in database
GPG Key ID: 488D7DCFB5A430D7
2 changed files with 10 additions and 4 deletions

View File

@ -510,7 +510,12 @@ class BitcoinRoutes {
private getDifficultyChange(req: Request, res: Response) { private getDifficultyChange(req: Request, res: Response) {
try { try {
res.json(difficultyAdjustment.getDifficultyAdjustment()); const da = difficultyAdjustment.getDifficultyAdjustment();
if (da) {
res.json(da);
} else {
res.status(503).send(`Service Temporarily Unavailable`);
}
} catch (e) { } catch (e) {
res.status(500).send(e instanceof Error ? e.message : e); res.status(500).send(e instanceof Error ? e.message : e);
} }

View File

@ -81,14 +81,15 @@ export function calcDifficultyAdjustment(
} }
class DifficultyAdjustmentApi { class DifficultyAdjustmentApi {
constructor() { } public getDifficultyAdjustment(): IDifficultyAdjustment | null {
public getDifficultyAdjustment(): IDifficultyAdjustment {
const DATime = blocks.getLastDifficultyAdjustmentTime(); const DATime = blocks.getLastDifficultyAdjustmentTime();
const previousRetarget = blocks.getPreviousDifficultyRetarget(); const previousRetarget = blocks.getPreviousDifficultyRetarget();
const blockHeight = blocks.getCurrentBlockHeight(); const blockHeight = blocks.getCurrentBlockHeight();
const blocksCache = blocks.getBlocks(); const blocksCache = blocks.getBlocks();
const latestBlock = blocksCache[blocksCache.length - 1]; const latestBlock = blocksCache[blocksCache.length - 1];
if (!latestBlock) {
return null;
}
const nowSeconds = Math.floor(new Date().getTime() / 1000); const nowSeconds = Math.floor(new Date().getTime() / 1000);
return calcDifficultyAdjustment( return calcDifficultyAdjustment(