From d66bc57165a11dd8a82f41f08eab4f5594130c19 Mon Sep 17 00:00:00 2001 From: nymkappa Date: Mon, 24 Jan 2022 19:57:54 +0900 Subject: [PATCH] Move block indexing start logic in blocks.ts --- backend/src/api/blocks.ts | 20 +++++++++++++++----- backend/src/index.ts | 12 +----------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/backend/src/api/blocks.ts b/backend/src/api/blocks.ts index 49c080896..5613abfa1 100644 --- a/backend/src/api/blocks.ts +++ b/backend/src/api/blocks.ts @@ -10,7 +10,6 @@ import bitcoinClient from './bitcoin/bitcoin-client'; import { IEsploraApi } from './bitcoin/esplora-api.interface'; import poolsRepository from '../repositories/PoolsRepository'; import blocksRepository from '../repositories/BlocksRepository'; -import BitcoinApi from './bitcoin/bitcoin-api'; class Blocks { private blocks: BlockExtended[] = []; @@ -19,6 +18,7 @@ class Blocks { private lastDifficultyAdjustmentTime = 0; private previousDifficultyRetarget = 0; private newBlockCallbacks: ((block: BlockExtended, txIds: string[], transactions: TransactionExtended[]) => void)[] = []; + private blockIndexingStarted = false; constructor() { } @@ -146,14 +146,24 @@ class Blocks { * Index all blocks metadata for the mining dashboard */ public async $generateBlockDatabase() { - if (['mainnet', 'testnet', 'signet'].includes(config.MEMPOOL.NETWORK) === false || - config.MEMPOOL.INDEXING_BLOCKS_AMOUNT <= 0) { + if (['mainnet', 'testnet', 'signet'].includes(config.MEMPOOL.NETWORK) === false || // Bitcoin only + config.MEMPOOL.INDEXING_BLOCKS_AMOUNT <= 0 || // Indexing must be enabled + this.blockIndexingStarted === true || // Indexing must not already be in progress + !memPool.isInSync() // We sync the mempool first + ) { return; } + const blockchainInfo = await bitcoinClient.getBlockchainInfo(); + if (blockchainInfo.blocks !== blockchainInfo.headers) { + return; + } + + this.blockIndexingStarted = true; + try { - let currentBlockHeight = await bitcoinClient.getBlockCount(); - const lastBlockToIndex = currentBlockHeight - config.MEMPOOL.INDEXING_BLOCKS_AMOUNT + 1; + let currentBlockHeight = blockchainInfo.blocks; + const lastBlockToIndex = Math.max(0, currentBlockHeight - config.MEMPOOL.INDEXING_BLOCKS_AMOUNT + 1); logger.info(`Indexing blocks from #${currentBlockHeight} to #${lastBlockToIndex}`); diff --git a/backend/src/index.ts b/backend/src/index.ts index 84be16fbc..7b4c8ae08 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -26,14 +26,12 @@ import poolsParser from './api/pools-parser'; import syncAssets from './sync-assets'; import icons from './api/liquid/icons'; import { Common } from './api/common'; -import bitcoinClient from './api/bitcoin/bitcoin-client'; class Server { private wss: WebSocket.Server | undefined; private server: http.Server | undefined; private app: Express; private currentBackendRetryInterval = 5; - private blockIndexingStarted = false; constructor() { this.app = express(); @@ -139,15 +137,7 @@ class Server { } await blocks.$updateBlocks(); await memPool.$updateMempool(); - - const blockchainInfo = await bitcoinClient.getBlockchainInfo(); - if (this.blockIndexingStarted === false - && memPool.isInSync() - && blockchainInfo.blocks === blockchainInfo.headers - ) { - blocks.$generateBlockDatabase(); - this.blockIndexingStarted = true; - } + blocks.$generateBlockDatabase(); setTimeout(this.runMainUpdateLoop.bind(this), config.MEMPOOL.POLL_RATE_MS); this.currentBackendRetryInterval = 5;