From e4721e8574ebbf5890f3580556a684fc3c989ae9 Mon Sep 17 00:00:00 2001 From: nymkappa Date: Mon, 21 Feb 2022 14:36:48 +0900 Subject: [PATCH] Improve hashrate indexing logs --- backend/src/api/mining.ts | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/backend/src/api/mining.ts b/backend/src/api/mining.ts index 792e93f45..db73117ce 100644 --- a/backend/src/api/mining.ts +++ b/backend/src/api/mining.ts @@ -118,18 +118,21 @@ class Mining { } this.hashrateIndexingStarted = true; - const oldestIndexedBlockHeight = await BlocksRepository.$getOldestIndexedBlockHeight(); + const totalDayIndexed = (await BlocksRepository.$blockCount(null, null)) / 144; const indexedTimestamp = (await HashratesRepository.$get(null)).map(hashrate => hashrate.timestamp); - + let startedAt = new Date().getTime() / 1000; const genesisTimestamp = 1231006505; // bitcoin-cli getblock 000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f const lastMidnight = new Date(); lastMidnight.setUTCHours(0); lastMidnight.setUTCMinutes(0); lastMidnight.setUTCSeconds(0); lastMidnight.setUTCMilliseconds(0); let toTimestamp = Math.round(lastMidnight.getTime() / 1000); + let indexedThisRun = 0; + let totalIndexed = 0; while (toTimestamp > genesisTimestamp) { const fromTimestamp = toTimestamp - 86400; if (indexedTimestamp.includes(fromTimestamp)) { toTimestamp -= 86400; + ++totalIndexed; continue; } @@ -145,10 +148,14 @@ class Mining { lastBlockHashrate = await bitcoinClient.getNetworkHashPs(blockStats.blockCount, blockStats.lastBlockHeight); - if (toTimestamp % 864000 === 0) { // Log every 10 days during initial indexing + const elapsedSeconds = Math.max(1, Math.round((new Date().getTime() / 1000) - startedAt)); + if (elapsedSeconds > 1) { + const daysPerSeconds = Math.max(1, Math.round(indexedThisRun / elapsedSeconds)); const formattedDate = new Date(fromTimestamp * 1000).toUTCString(); - const blocksLeft = blockStats.lastBlockHeight - oldestIndexedBlockHeight; - logger.debug(`Counting blocks and hashrate for ${formattedDate}. ${blocksLeft} blocks left`); + const daysLeft = Math.round(totalDayIndexed - totalIndexed); + logger.debug(`Getting hashrate for ${formattedDate} | ~${daysPerSeconds} days/sec | ~${daysLeft} days left to index`); + startedAt = new Date().getTime() / 1000; + indexedThisRun = 0; } await HashratesRepository.$saveDailyStat({ @@ -158,6 +165,8 @@ class Mining { }); toTimestamp -= 86400; + ++indexedThisRun; + ++totalIndexed; } await HashratesRepository.$setLatestRunTimestamp();