diff --git a/backend/src/api/blocks.ts b/backend/src/api/blocks.ts index 1c5774640..a6b181c1e 100644 --- a/backend/src/api/blocks.ts +++ b/backend/src/api/blocks.ts @@ -2,13 +2,13 @@ import config from '../config'; import bitcoinApi from './bitcoin/bitcoin-api-factory'; import logger from '../logger'; import memPool from './mempool'; -import { BlockExtended, TransactionExtended, TransactionMinerInfo } from '../mempool.interfaces'; +import { BlockExtended, TransactionExtended } from '../mempool.interfaces'; import { Common } from './common'; import diskCache from './disk-cache'; import transactionUtils from './transaction-utils'; class Blocks { - private static KEEP_BLOCK_AMOUNT = 8; + private static INITIAL_BLOCK_AMOUNT = 8; private blocks: BlockExtended[] = []; private currentBlockHeight = 0; private lastDifficultyAdjustmentTime = 0; @@ -32,14 +32,14 @@ class Blocks { const blockHeightTip = await bitcoinApi.$getBlockHeightTip(); if (this.blocks.length === 0) { - this.currentBlockHeight = blockHeightTip - Blocks.KEEP_BLOCK_AMOUNT; + this.currentBlockHeight = blockHeightTip - Blocks.INITIAL_BLOCK_AMOUNT; } else { this.currentBlockHeight = this.blocks[this.blocks.length - 1].height; } - if (blockHeightTip - this.currentBlockHeight > Blocks.KEEP_BLOCK_AMOUNT * 2) { - logger.info(`${blockHeightTip - this.currentBlockHeight} blocks since tip. Fast forwarding to the ${Blocks.KEEP_BLOCK_AMOUNT} recent blocks`); - this.currentBlockHeight = blockHeightTip - Blocks.KEEP_BLOCK_AMOUNT; + if (blockHeightTip - this.currentBlockHeight > Blocks.INITIAL_BLOCK_AMOUNT * 2) { + logger.info(`${blockHeightTip - this.currentBlockHeight} blocks since tip. Fast forwarding to the ${Blocks.INITIAL_BLOCK_AMOUNT} recent blocks`); + this.currentBlockHeight = blockHeightTip - Blocks.INITIAL_BLOCK_AMOUNT; } if (!this.lastDifficultyAdjustmentTime) { @@ -109,8 +109,8 @@ class Blocks { } this.blocks.push(blockExtended); - if (this.blocks.length > Blocks.KEEP_BLOCK_AMOUNT) { - this.blocks = this.blocks.slice(-Blocks.KEEP_BLOCK_AMOUNT); + if (this.blocks.length > Blocks.INITIAL_BLOCK_AMOUNT * 4) { + this.blocks = this.blocks.slice(-Blocks.INITIAL_BLOCK_AMOUNT * 4); } if (this.newBlockCallbacks.length) { diff --git a/backend/src/api/websocket-handler.ts b/backend/src/api/websocket-handler.ts index e696cad51..898cca125 100644 --- a/backend/src/api/websocket-handler.ts +++ b/backend/src/api/websocket-handler.ts @@ -77,7 +77,7 @@ class WebsocketHandler { } if (parsedMessage.action === 'init') { - const _blocks = blocks.getBlocks(); + const _blocks = blocks.getBlocks().slice(-8); if (!_blocks) { return; } @@ -119,7 +119,7 @@ class WebsocketHandler { getInitData(_blocks?: BlockExtended[]) { if (!_blocks) { - _blocks = blocks.getBlocks(); + _blocks = blocks.getBlocks().slice(-8); } return { 'mempoolInfo': memPool.getMempoolInfo(), diff --git a/backend/src/routes.ts b/backend/src/routes.ts index 97aa56769..8dfeffa04 100644 --- a/backend/src/routes.ts +++ b/backend/src/routes.ts @@ -554,12 +554,10 @@ class Routes { public async getBlocks(req: Request, res: Response) { try { const returnBlocks: IEsploraApi.Block[] = []; - const latestBlockHeight = blocks.getCurrentBlockHeight(); - const fromHeight = parseInt(req.params.height, 10) || latestBlockHeight; - const localBlocks = blocks.getBlocks(); + const fromHeight = parseInt(req.params.height, 10) || blocks.getCurrentBlockHeight(); - // See if block hight exist in local cache to skip the hash lookup - const blockByHeight = localBlocks.find((b) => b.height === fromHeight); + // Check if block height exist in local cache to skip the hash lookup + const blockByHeight = blocks.getBlocks().find((b) => b.height === fromHeight); let startFromHash: string | null = null; if (blockByHeight) { startFromHash = blockByHeight.id; @@ -569,7 +567,7 @@ class Routes { let nextHash = startFromHash; for (let i = 0; i < 10; i++) { - const localBlock = localBlocks.find((b) => b.id === nextHash); + const localBlock = blocks.getBlocks().find((b) => b.id === nextHash); if (localBlock) { returnBlocks.push(localBlock); nextHash = localBlock.previousblockhash;