From 067ee168dd7d9fe4500fb731f908733d88e69117 Mon Sep 17 00:00:00 2001 From: nymkappa Date: Sat, 9 Jul 2022 12:05:36 +0200 Subject: [PATCH] Use oldest consecutive block timestamp as hashrate indexing limit --- backend/src/api/mining.ts | 28 ++++++-------------- backend/src/repositories/BlocksRepository.ts | 18 +++++++++++++ 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/backend/src/api/mining.ts b/backend/src/api/mining.ts index de59e9b58..7f142922b 100644 --- a/backend/src/api/mining.ts +++ b/backend/src/api/mining.ts @@ -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), diff --git a/backend/src/repositories/BlocksRepository.ts b/backend/src/repositories/BlocksRepository.ts index c6b14ff51..c3f824d61 100644 --- a/backend/src/repositories/BlocksRepository.ts +++ b/backend/src/repositories/BlocksRepository.ts @@ -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 { + 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();