Hashrates indexing waits for blocks indexing - Batch hashrates I/O ops

This commit is contained in:
nymkappa
2022-02-21 17:34:07 +09:00
parent 9de3c14b3d
commit 150b137f0c
4 changed files with 28 additions and 21 deletions

View File

@@ -20,6 +20,7 @@ class Blocks {
private previousDifficultyRetarget = 0;
private newBlockCallbacks: ((block: BlockExtended, txIds: string[], transactions: TransactionExtended[]) => void)[] = [];
private blockIndexingStarted = false;
public blockIndexingCompleted = false;
constructor() { }
@@ -240,6 +241,8 @@ class Blocks {
logger.err('An error occured in $generateBlockDatabase(). Skipping block indexing. ' + e);
console.log(e);
}
this.blockIndexingCompleted = true;
}
public async $updateBlocks() {

View File

@@ -4,6 +4,7 @@ import PoolsRepository from '../repositories/PoolsRepository';
import HashratesRepository from '../repositories/HashratesRepository';
import bitcoinClient from './bitcoin/bitcoin-client';
import logger from '../logger';
import blocks from './blocks';
class Mining {
hashrateIndexingStarted = false;
@@ -111,7 +112,7 @@ class Mining {
return;
}
if (this.hashrateIndexingStarted) {
if (!blocks.blockIndexingCompleted || this.hashrateIndexingStarted) {
return;
}
this.hashrateIndexingStarted = true;
@@ -128,6 +129,8 @@ class Mining {
let indexedThisRun = 0;
let totalIndexed = 0;
const hashrates: any[] = [];
while (toTimestamp > genesisTimestamp) {
const fromTimestamp = toTimestamp - 86400;
if (indexedTimestamp.includes(fromTimestamp)) {
@@ -137,9 +140,7 @@ class Mining {
}
const blockStats: any = await BlocksRepository.$blockCountBetweenTimestamp(
null, fromTimestamp, toTimestamp
);
null, fromTimestamp, toTimestamp);
if (blockStats.blockCount === 0) { // We are done indexing, no blocks left
break;
}
@@ -158,23 +159,28 @@ class Mining {
indexedThisRun = 0;
}
await HashratesRepository.$saveDailyStat({
hashrates.push({
hashrateTimestamp: fromTimestamp,
avgHashrate: lastBlockHashrate,
poolId: null,
});
if (hashrates.length > 100) {
await HashratesRepository.$saveHashrates(hashrates);
hashrates.length = 0;
}
toTimestamp -= 86400;
++indexedThisRun;
++totalIndexed;
}
await HashratesRepository.$saveHashrates(hashrates);
await HashratesRepository.$setLatestRunTimestamp();
this.hashrateIndexingStarted = false;
logger.info(`Hashrates indexing completed`);
}
}
export default new Mining();