From 3608fa6f199cc3d396469c86a8a8d5b7e91ed7b4 Mon Sep 17 00:00:00 2001 From: Antoni Spaanderman <56turtle56@gmail.com> Date: Wed, 19 Jan 2022 16:58:56 +0100 Subject: [PATCH] load blocks with height under INITIAL_BLOCKS_AMOUNT --- backend/src/api/blocks.ts | 14 ++++++++------ backend/src/config.ts | 2 +- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/backend/src/api/blocks.ts b/backend/src/api/blocks.ts index 1043c344f..3d50e1684 100644 --- a/backend/src/api/blocks.ts +++ b/backend/src/api/blocks.ts @@ -34,7 +34,7 @@ class Blocks { const blockHeightTip = await bitcoinApi.$getBlockHeightTip(); if (this.blocks.length === 0) { - this.currentBlockHeight = blockHeightTip - config.MEMPOOL.INITIAL_BLOCKS_AMOUNT; + this.currentBlockHeight = Math.max(blockHeightTip - config.MEMPOOL.INITIAL_BLOCKS_AMOUNT, -1); } else { this.currentBlockHeight = this.blocks[this.blocks.length - 1].height; } @@ -53,17 +53,19 @@ class Blocks { 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; - logger.debug(`Initial difficulty adjustment data set.`); + if (blockHeightTip - heightDiff - 2016 >= 0) { + const previousPeriodBlockHash = await bitcoinApi.$getBlockHash(blockHeightTip - heightDiff - 2016); + const previousPeriodBlock = await bitcoinApi.$getBlock(previousPeriodBlockHash); + this.previousDifficultyRetarget = (block.difficulty - previousPeriodBlock.difficulty) / previousPeriodBlock.difficulty * 100; + logger.debug(`Initial difficulty adjustment data set.`); + } } else { logger.debug(`Blockchain headers (${blockchainInfo.headers}) and blocks (${blockchainInfo.blocks}) not in sync. Waiting...`); } } while (this.currentBlockHeight < blockHeightTip) { - if (this.currentBlockHeight === 0) { + if (this.currentBlockHeight < blockHeightTip - config.MEMPOOL.INITIAL_BLOCKS_AMOUNT) { this.currentBlockHeight = blockHeightTip; } else { this.currentBlockHeight++; diff --git a/backend/src/config.ts b/backend/src/config.ts index 4c2888834..13ad224a1 100644 --- a/backend/src/config.ts +++ b/backend/src/config.ts @@ -2,7 +2,7 @@ const configFile = require('../mempool-config.json'); interface IConfig { MEMPOOL: { - NETWORK: 'mainnet' | 'testnet' | 'signet' | 'liquid' | 'liquidtestnet'; + NETWORK: 'mainnet' | 'testnet' | 'signet' | 'regtest' | 'liquid' | 'liquidtestnet'; BACKEND: 'esplora' | 'electrum' | 'none'; HTTP_PORT: number; SPAWN_CLUSTER_PROCS: number;