Move block indexing start logic in blocks.ts
This commit is contained in:
		
							parent
							
								
									73019b485f
								
							
						
					
					
						commit
						d66bc57165
					
				@ -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}`);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -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;
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      setTimeout(this.runMainUpdateLoop.bind(this), config.MEMPOOL.POLL_RATE_MS);
 | 
			
		||||
      this.currentBackendRetryInterval = 5;
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user