Move block indexing start logic in blocks.ts

This commit is contained in:
nymkappa 2022-01-24 19:57:54 +09:00
parent 73019b485f
commit d66bc57165
No known key found for this signature in database
GPG Key ID: E155910B16E8BD04
2 changed files with 16 additions and 16 deletions

View File

@ -10,7 +10,6 @@ import bitcoinClient from './bitcoin/bitcoin-client';
import { IEsploraApi } from './bitcoin/esplora-api.interface'; import { IEsploraApi } from './bitcoin/esplora-api.interface';
import poolsRepository from '../repositories/PoolsRepository'; import poolsRepository from '../repositories/PoolsRepository';
import blocksRepository from '../repositories/BlocksRepository'; import blocksRepository from '../repositories/BlocksRepository';
import BitcoinApi from './bitcoin/bitcoin-api';
class Blocks { class Blocks {
private blocks: BlockExtended[] = []; private blocks: BlockExtended[] = [];
@ -19,6 +18,7 @@ class Blocks {
private lastDifficultyAdjustmentTime = 0; private lastDifficultyAdjustmentTime = 0;
private previousDifficultyRetarget = 0; private previousDifficultyRetarget = 0;
private newBlockCallbacks: ((block: BlockExtended, txIds: string[], transactions: TransactionExtended[]) => void)[] = []; private newBlockCallbacks: ((block: BlockExtended, txIds: string[], transactions: TransactionExtended[]) => void)[] = [];
private blockIndexingStarted = false;
constructor() { } constructor() { }
@ -146,14 +146,24 @@ class Blocks {
* Index all blocks metadata for the mining dashboard * Index all blocks metadata for the mining dashboard
*/ */
public async $generateBlockDatabase() { public async $generateBlockDatabase() {
if (['mainnet', 'testnet', 'signet'].includes(config.MEMPOOL.NETWORK) === false || if (['mainnet', 'testnet', 'signet'].includes(config.MEMPOOL.NETWORK) === false || // Bitcoin only
config.MEMPOOL.INDEXING_BLOCKS_AMOUNT <= 0) { 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; return;
} }
const blockchainInfo = await bitcoinClient.getBlockchainInfo();
if (blockchainInfo.blocks !== blockchainInfo.headers) {
return;
}
this.blockIndexingStarted = true;
try { try {
let currentBlockHeight = await bitcoinClient.getBlockCount(); let currentBlockHeight = blockchainInfo.blocks;
const lastBlockToIndex = currentBlockHeight - config.MEMPOOL.INDEXING_BLOCKS_AMOUNT + 1; const lastBlockToIndex = Math.max(0, currentBlockHeight - config.MEMPOOL.INDEXING_BLOCKS_AMOUNT + 1);
logger.info(`Indexing blocks from #${currentBlockHeight} to #${lastBlockToIndex}`); logger.info(`Indexing blocks from #${currentBlockHeight} to #${lastBlockToIndex}`);

View File

@ -26,14 +26,12 @@ import poolsParser from './api/pools-parser';
import syncAssets from './sync-assets'; import syncAssets from './sync-assets';
import icons from './api/liquid/icons'; import icons from './api/liquid/icons';
import { Common } from './api/common'; import { Common } from './api/common';
import bitcoinClient from './api/bitcoin/bitcoin-client';
class Server { class Server {
private wss: WebSocket.Server | undefined; private wss: WebSocket.Server | undefined;
private server: http.Server | undefined; private server: http.Server | undefined;
private app: Express; private app: Express;
private currentBackendRetryInterval = 5; private currentBackendRetryInterval = 5;
private blockIndexingStarted = false;
constructor() { constructor() {
this.app = express(); this.app = express();
@ -139,15 +137,7 @@ class Server {
} }
await blocks.$updateBlocks(); await blocks.$updateBlocks();
await memPool.$updateMempool(); await memPool.$updateMempool();
blocks.$generateBlockDatabase();
const blockchainInfo = await bitcoinClient.getBlockchainInfo();
if (this.blockIndexingStarted === false
&& memPool.isInSync()
&& blockchainInfo.blocks === blockchainInfo.headers
) {
blocks.$generateBlockDatabase();
this.blockIndexingStarted = true;
}
setTimeout(this.runMainUpdateLoop.bind(this), config.MEMPOOL.POLL_RATE_MS); setTimeout(this.runMainUpdateLoop.bind(this), config.MEMPOOL.POLL_RATE_MS);
this.currentBackendRetryInterval = 5; this.currentBackendRetryInterval = 5;