Use oldest consecutive block timestamp as hashrate indexing limit
This commit is contained in:
		
							parent
							
								
									d8a90cce47
								
							
						
					
					
						commit
						067ee168dd
					
				@ -184,6 +184,8 @@ class Mining {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    try {
 | 
			
		||||
      const oldestConsecutiveBlockTimestamp = 1000 * (await BlocksRepository.$getOldestConsecutiveBlockTimestamp());
 | 
			
		||||
 | 
			
		||||
      const genesisBlock = await bitcoinClient.getBlock(await bitcoinClient.getBlockHash(0));
 | 
			
		||||
      const genesisTimestamp = genesisBlock.time * 1000;
 | 
			
		||||
 | 
			
		||||
@ -204,7 +206,7 @@ class Mining {
 | 
			
		||||
      logger.debug(`Indexing weekly mining pool hashrate`);
 | 
			
		||||
      loadingIndicators.setProgress('weekly-hashrate-indexing', 0);
 | 
			
		||||
 | 
			
		||||
      while (toTimestamp > genesisTimestamp) {
 | 
			
		||||
      while (toTimestamp > genesisTimestamp && toTimestamp > oldestConsecutiveBlockTimestamp) {
 | 
			
		||||
        const fromTimestamp = toTimestamp - 604800000;
 | 
			
		||||
 | 
			
		||||
        // Skip already indexed weeks
 | 
			
		||||
@ -214,14 +216,6 @@ class Mining {
 | 
			
		||||
          continue;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // Check if we have blocks for the previous week (which mean that the week
 | 
			
		||||
        // we are currently indexing has complete data)
 | 
			
		||||
        const blockStatsPreviousWeek: any = await BlocksRepository.$blockCountBetweenTimestamp(
 | 
			
		||||
          null, (fromTimestamp - 604800000) / 1000, (toTimestamp - 604800000) / 1000);
 | 
			
		||||
        if (blockStatsPreviousWeek.blockCount === 0) { // We are done indexing
 | 
			
		||||
          break;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        const blockStats: any = await BlocksRepository.$blockCountBetweenTimestamp(
 | 
			
		||||
          null, fromTimestamp / 1000, toTimestamp / 1000);
 | 
			
		||||
        const lastBlockHashrate = await bitcoinClient.getNetworkHashPs(blockStats.blockCount,
 | 
			
		||||
@ -289,6 +283,8 @@ class Mining {
 | 
			
		||||
      return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    const oldestConsecutiveBlockTimestamp = 1000 * (await BlocksRepository.$getOldestConsecutiveBlockTimestamp());
 | 
			
		||||
 | 
			
		||||
    try {
 | 
			
		||||
      const genesisBlock = await bitcoinClient.getBlock(await bitcoinClient.getBlockHash(0));
 | 
			
		||||
      const genesisTimestamp = genesisBlock.time * 1000;
 | 
			
		||||
@ -307,7 +303,7 @@ class Mining {
 | 
			
		||||
      logger.debug(`Indexing daily network hashrate`);
 | 
			
		||||
      loadingIndicators.setProgress('daily-hashrate-indexing', 0);
 | 
			
		||||
 | 
			
		||||
      while (toTimestamp > genesisTimestamp) {
 | 
			
		||||
      while (toTimestamp > genesisTimestamp && toTimestamp > oldestConsecutiveBlockTimestamp) {
 | 
			
		||||
        const fromTimestamp = toTimestamp - 86400000;
 | 
			
		||||
 | 
			
		||||
        // Skip already indexed days
 | 
			
		||||
@ -317,17 +313,9 @@ class Mining {
 | 
			
		||||
          continue;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // Check if we have blocks for the previous day (which mean that the day
 | 
			
		||||
        // we are currently indexing has complete data)
 | 
			
		||||
        const blockStatsPreviousDay: any = await BlocksRepository.$blockCountBetweenTimestamp(
 | 
			
		||||
          null, (fromTimestamp - 86400000) / 1000, (toTimestamp - 86400000) / 1000);
 | 
			
		||||
        if (blockStatsPreviousDay.blockCount === 0 && config.MEMPOOL.NETWORK === 'mainnet') { // We are done indexing
 | 
			
		||||
          break;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        const blockStats: any = await BlocksRepository.$blockCountBetweenTimestamp(
 | 
			
		||||
          null, fromTimestamp / 1000, toTimestamp / 1000);
 | 
			
		||||
        const lastBlockHashrate = await bitcoinClient.getNetworkHashPs(blockStats.blockCount,
 | 
			
		||||
        const lastBlockHashrate = blockStats.blockCount === 0 ? 0 : await bitcoinClient.getNetworkHashPs(blockStats.blockCount,
 | 
			
		||||
          blockStats.lastBlockHeight);
 | 
			
		||||
 | 
			
		||||
        hashrates.push({
 | 
			
		||||
@ -363,7 +351,7 @@ class Mining {
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      // Add genesis block manually
 | 
			
		||||
      if (!indexedTimestamp.includes(genesisTimestamp / 1000)) {
 | 
			
		||||
      if (config.MEMPOOL.INDEXING_BLOCKS_AMOUNT === -1 && !indexedTimestamp.includes(genesisTimestamp / 1000)) {
 | 
			
		||||
        hashrates.push({
 | 
			
		||||
          hashrateTimestamp: genesisTimestamp / 1000,
 | 
			
		||||
          avgHashrate: await bitcoinClient.getNetworkHashPs(1, 1),
 | 
			
		||||
 | 
			
		||||
@ -610,6 +610,24 @@ class BlocksRepository {
 | 
			
		||||
      throw e;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * Return the oldest block timestamp from a consecutive chain of block from the most recent one
 | 
			
		||||
   */
 | 
			
		||||
  public async $getOldestConsecutiveBlockTimestamp(): Promise<number> {
 | 
			
		||||
    try {
 | 
			
		||||
      const [rows]: any = await DB.query(`SELECT height, UNIX_TIMESTAMP(blockTimestamp) as timestamp FROM blocks ORDER BY height DESC`);
 | 
			
		||||
      for (let i = 0; i < rows.length - 1; ++i) {
 | 
			
		||||
        if (rows[i].height - rows[i + 1].height > 1) {
 | 
			
		||||
          return rows[i].timestamp;
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
      return rows[rows.length - 1].timestamp;
 | 
			
		||||
    } catch (e) {
 | 
			
		||||
      logger.err('Cannot generate block size and weight history. Reason: ' + (e instanceof Error ? e.message : e));
 | 
			
		||||
      throw e;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export default new BlocksRepository();
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user