Hashrates indexing waits for blocks indexing - Batch hashrates I/O ops
This commit is contained in:
		
							parent
							
								
									537e50c682
								
							
						
					
					
						commit
						649ad2e859
					
				@ -20,6 +20,7 @@ class Blocks {
 | 
				
			|||||||
  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;
 | 
					  private blockIndexingStarted = false;
 | 
				
			||||||
 | 
					  public blockIndexingCompleted = false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  constructor() { }
 | 
					  constructor() { }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -240,6 +241,8 @@ class Blocks {
 | 
				
			|||||||
      logger.err('An error occured in $generateBlockDatabase(). Skipping block indexing. ' + e);
 | 
					      logger.err('An error occured in $generateBlockDatabase(). Skipping block indexing. ' + e);
 | 
				
			||||||
      console.log(e);
 | 
					      console.log(e);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    this.blockIndexingCompleted = true;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  public async $updateBlocks() {
 | 
					  public async $updateBlocks() {
 | 
				
			||||||
 | 
				
			|||||||
@ -4,6 +4,7 @@ import PoolsRepository from '../repositories/PoolsRepository';
 | 
				
			|||||||
import HashratesRepository from '../repositories/HashratesRepository';
 | 
					import HashratesRepository from '../repositories/HashratesRepository';
 | 
				
			||||||
import bitcoinClient from './bitcoin/bitcoin-client';
 | 
					import bitcoinClient from './bitcoin/bitcoin-client';
 | 
				
			||||||
import logger from '../logger';
 | 
					import logger from '../logger';
 | 
				
			||||||
 | 
					import blocks from './blocks';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class Mining {
 | 
					class Mining {
 | 
				
			||||||
  hashrateIndexingStarted = false;
 | 
					  hashrateIndexingStarted = false;
 | 
				
			||||||
@ -111,7 +112,7 @@ class Mining {
 | 
				
			|||||||
      return;
 | 
					      return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (this.hashrateIndexingStarted) {
 | 
					    if (!blocks.blockIndexingCompleted || this.hashrateIndexingStarted) {
 | 
				
			||||||
      return;
 | 
					      return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    this.hashrateIndexingStarted = true;
 | 
					    this.hashrateIndexingStarted = true;
 | 
				
			||||||
@ -128,6 +129,8 @@ class Mining {
 | 
				
			|||||||
    let indexedThisRun = 0;
 | 
					    let indexedThisRun = 0;
 | 
				
			||||||
    let totalIndexed = 0;
 | 
					    let totalIndexed = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    const hashrates: any[] = [];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    while (toTimestamp > genesisTimestamp) {
 | 
					    while (toTimestamp > genesisTimestamp) {
 | 
				
			||||||
      const fromTimestamp = toTimestamp - 86400;
 | 
					      const fromTimestamp = toTimestamp - 86400;
 | 
				
			||||||
      if (indexedTimestamp.includes(fromTimestamp)) {
 | 
					      if (indexedTimestamp.includes(fromTimestamp)) {
 | 
				
			||||||
@ -137,9 +140,7 @@ class Mining {
 | 
				
			|||||||
      }
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      const blockStats: any = await BlocksRepository.$blockCountBetweenTimestamp(
 | 
					      const blockStats: any = await BlocksRepository.$blockCountBetweenTimestamp(
 | 
				
			||||||
        null, fromTimestamp, toTimestamp
 | 
					        null, fromTimestamp, toTimestamp);
 | 
				
			||||||
      );
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      if (blockStats.blockCount === 0) { // We are done indexing, no blocks left
 | 
					      if (blockStats.blockCount === 0) { // We are done indexing, no blocks left
 | 
				
			||||||
        break;
 | 
					        break;
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
@ -158,23 +159,28 @@ class Mining {
 | 
				
			|||||||
        indexedThisRun = 0;
 | 
					        indexedThisRun = 0;
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      await HashratesRepository.$saveDailyStat({
 | 
					      hashrates.push({
 | 
				
			||||||
        hashrateTimestamp: fromTimestamp,
 | 
					        hashrateTimestamp: fromTimestamp,
 | 
				
			||||||
        avgHashrate: lastBlockHashrate,
 | 
					        avgHashrate: lastBlockHashrate,
 | 
				
			||||||
        poolId: null,
 | 
					        poolId: null,
 | 
				
			||||||
      });
 | 
					      });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      if (hashrates.length > 100) {
 | 
				
			||||||
 | 
					        await HashratesRepository.$saveHashrates(hashrates);
 | 
				
			||||||
 | 
					        hashrates.length = 0;
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      toTimestamp -= 86400;
 | 
					      toTimestamp -= 86400;
 | 
				
			||||||
      ++indexedThisRun;
 | 
					      ++indexedThisRun;
 | 
				
			||||||
      ++totalIndexed;
 | 
					      ++totalIndexed;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    await HashratesRepository.$saveHashrates(hashrates);
 | 
				
			||||||
    await HashratesRepository.$setLatestRunTimestamp();
 | 
					    await HashratesRepository.$setLatestRunTimestamp();
 | 
				
			||||||
    this.hashrateIndexingStarted = false;
 | 
					    this.hashrateIndexingStarted = false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    logger.info(`Hashrates indexing completed`);
 | 
					    logger.info(`Hashrates indexing completed`);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export default new Mining();
 | 
					export default new Mining();
 | 
				
			||||||
 | 
				
			|||||||
@ -164,12 +164,12 @@ class Server {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  async runIndexingWhenReady() {
 | 
					  runIndexingWhenReady() {
 | 
				
			||||||
    if (!Common.indexingEnabled() || mempool.hasPriority()) {
 | 
					    if (!Common.indexingEnabled() || mempool.hasPriority()) {
 | 
				
			||||||
      return;
 | 
					      return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    await blocks.$generateBlockDatabase();
 | 
					    blocks.$generateBlockDatabase();
 | 
				
			||||||
    await mining.$generateNetworkHashrateHistory();
 | 
					    mining.$generateNetworkHashrateHistory();
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  setUpWebsocketHandling() {
 | 
					  setUpWebsocketHandling() {
 | 
				
			||||||
 | 
				
			|||||||
@ -6,21 +6,19 @@ class HashratesRepository {
 | 
				
			|||||||
  /**
 | 
					  /**
 | 
				
			||||||
   * Save indexed block data in the database
 | 
					   * Save indexed block data in the database
 | 
				
			||||||
   */
 | 
					   */
 | 
				
			||||||
  public async $saveDailyStat(dailyStat: any) {
 | 
					  public async $saveHashrates(hashrates: any) {
 | 
				
			||||||
 | 
					    let query = `INSERT INTO
 | 
				
			||||||
 | 
					      hashrates(hashrate_timestamp, avg_hashrate, pool_id) VALUES`;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    for (const hashrate of hashrates) {
 | 
				
			||||||
 | 
					      query += ` (FROM_UNIXTIME(${hashrate.hashrateTimestamp}), ${hashrate.avgHashrate}, ${hashrate.poolId}),`;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    query = query.slice(0, -1);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const connection = await DB.pool.getConnection();
 | 
					    const connection = await DB.pool.getConnection();
 | 
				
			||||||
 | 
					 | 
				
			||||||
    try {
 | 
					    try {
 | 
				
			||||||
      const query = `INSERT INTO
 | 
					 | 
				
			||||||
      hashrates(hashrate_timestamp, avg_hashrate, pool_id)
 | 
					 | 
				
			||||||
      VALUE (FROM_UNIXTIME(?), ?, ?)`;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      const params: any[] = [
 | 
					 | 
				
			||||||
        dailyStat.hashrateTimestamp, dailyStat.avgHashrate,
 | 
					 | 
				
			||||||
        dailyStat.poolId
 | 
					 | 
				
			||||||
      ];
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      // logger.debug(query);
 | 
					      // logger.debug(query);
 | 
				
			||||||
      await connection.query(query, params);
 | 
					      await connection.query(query);
 | 
				
			||||||
    } catch (e: any) {
 | 
					    } catch (e: any) {
 | 
				
			||||||
      logger.err('$saveHashrateInDatabase() error' + (e instanceof Error ? e.message : e));
 | 
					      logger.err('$saveHashrateInDatabase() error' + (e instanceof Error ? e.message : e));
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user