diff --git a/backend/src/api/database-migration.ts b/backend/src/api/database-migration.ts index f4801deb6..6216e7f2b 100644 --- a/backend/src/api/database-migration.ts +++ b/backend/src/api/database-migration.ts @@ -7,7 +7,7 @@ import cpfpRepository from '../repositories/CpfpRepository'; import { RowDataPacket } from 'mysql2'; class DatabaseMigration { - private static currentVersion = 57; + private static currentVersion = 58; private queryTimeout = 3600_000; private statisticsAddedIndexed = false; private uniqueLogs: string[] = []; @@ -505,6 +505,11 @@ class DatabaseMigration { await this.$executeQuery(`ALTER TABLE nodes MODIFY updated_at datetime NULL`); await this.updateToSchemaVersion(57); } + + if (databaseSchemaVersion < 58) { + // We only run some migration queries for this version + await this.updateToSchemaVersion(58); + } } /** @@ -632,6 +637,11 @@ class DatabaseMigration { queries.push(`INSERT INTO state(name, number, string) VALUES ('last_weekly_hashrates_indexing', 0, NULL)`); } + if (version < 55) { + queries.push(`DELETE FROM state WHERE name = 'last_hashrates_indexing'`); + queries.push(`DELETE FROM state WHERE name = 'last_weekly_hashrates_indexing'`); + } + return queries; } diff --git a/backend/src/api/mining/mining.ts b/backend/src/api/mining/mining.ts index f33a68dcb..347752f99 100644 --- a/backend/src/api/mining/mining.ts +++ b/backend/src/api/mining/mining.ts @@ -13,10 +13,9 @@ import BlocksAuditsRepository from '../../repositories/BlocksAuditsRepository'; import PricesRepository from '../../repositories/PricesRepository'; class Mining { - blocksPriceIndexingRunning = false; - - constructor() { - } + private blocksPriceIndexingRunning = false; + public lastHashrateIndexingDate: number | null = null; + public lastWeeklyHashrateIndexingDate: number | null = null; /** * Get historical block predictions match rate @@ -176,13 +175,14 @@ class Mining { */ public async $generatePoolHashrateHistory(): Promise { const now = new Date(); - const lastestRunDate = await HashratesRepository.$getLatestRun('last_weekly_hashrates_indexing'); // Run only if: - // * lastestRunDate is set to 0 (node backend restart, reorg) + // * this.lastWeeklyHashrateIndexingDate is set to null (node backend restart, reorg) // * we started a new week (around Monday midnight) - const runIndexing = lastestRunDate === 0 || now.getUTCDay() === 1 && lastestRunDate !== now.getUTCDate(); + const runIndexing = this.lastWeeklyHashrateIndexingDate === null || + now.getUTCDay() === 1 && this.lastWeeklyHashrateIndexingDate !== now.getUTCDate(); if (!runIndexing) { + logger.debug(`Pool hashrate history indexing is up to date, nothing to do`, logger.tags.mining); return; } @@ -264,7 +264,7 @@ class Mining { ++indexedThisRun; ++totalIndexed; } - await HashratesRepository.$setLatestRun('last_weekly_hashrates_indexing', new Date().getUTCDate()); + this.lastWeeklyHashrateIndexingDate = new Date().getUTCDate(); if (newlyIndexed > 0) { logger.notice(`Weekly mining pools hashrates indexing completed: indexed ${newlyIndexed}`, logger.tags.mining); } else { @@ -283,9 +283,9 @@ class Mining { */ public async $generateNetworkHashrateHistory(): Promise { // We only run this once a day around midnight - const latestRunDate = await HashratesRepository.$getLatestRun('last_hashrates_indexing'); - const now = new Date().getUTCDate(); - if (now === latestRunDate) { + const today = new Date().getUTCDate(); + if (today === this.lastHashrateIndexingDate) { + logger.debug(`Network hashrate history indexing is up to date, nothing to do`, logger.tags.mining); return; } @@ -369,7 +369,7 @@ class Mining { newlyIndexed += hashrates.length; await HashratesRepository.$saveHashrates(hashrates); - await HashratesRepository.$setLatestRun('last_hashrates_indexing', new Date().getUTCDate()); + this.lastHashrateIndexingDate = new Date().getUTCDate(); if (newlyIndexed > 0) { logger.notice(`Daily network hashrate indexing completed: indexed ${newlyIndexed} days`, logger.tags.mining); } else { diff --git a/backend/src/index.ts b/backend/src/index.ts index 6f259a2bd..05c2ffa83 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -87,9 +87,6 @@ class Server { await databaseMigration.$blocksReindexingTruncate(); } await databaseMigration.$initializeOrMigrateDatabase(); - if (Common.indexingEnabled()) { - await indexer.$resetHashratesIndexingState(); - } } catch (e) { throw new Error(e instanceof Error ? e.message : 'Error'); } diff --git a/backend/src/indexer.ts b/backend/src/indexer.ts index 41c8024e0..1665e443f 100644 --- a/backend/src/indexer.ts +++ b/backend/src/indexer.ts @@ -3,7 +3,6 @@ import blocks from './api/blocks'; import mempool from './api/mempool'; import mining from './api/mining/mining'; import logger from './logger'; -import HashratesRepository from './repositories/HashratesRepository'; import bitcoinClient from './api/bitcoin/bitcoin-client'; import priceUpdater from './tasks/price-updater'; import PricesRepository from './repositories/PricesRepository'; @@ -131,7 +130,6 @@ class Indexer { this.runSingleTask('blocksPrices'); await mining.$indexDifficultyAdjustments(); - await this.$resetHashratesIndexingState(); // TODO - Remove this as it's not efficient await mining.$generateNetworkHashrateHistory(); await mining.$generatePoolHashrateHistory(); await blocks.$generateBlocksSummariesDatabase(); @@ -150,16 +148,6 @@ class Indexer { logger.debug(`Indexing completed. Next run planned at ${new Date(new Date().getTime() + runEvery).toUTCString()}`); setTimeout(() => this.reindex(), runEvery); } - - async $resetHashratesIndexingState(): Promise { - try { - await HashratesRepository.$setLatestRun('last_hashrates_indexing', 0); - await HashratesRepository.$setLatestRun('last_weekly_hashrates_indexing', 0); - } catch (e) { - logger.err(`Cannot reset hashrate indexing timestamps. Reason: ` + (e instanceof Error ? e.message : e)); - throw e; - } - } } export default new Indexer(); diff --git a/backend/src/repositories/HashratesRepository.ts b/backend/src/repositories/HashratesRepository.ts index e5a193477..c380e87d9 100644 --- a/backend/src/repositories/HashratesRepository.ts +++ b/backend/src/repositories/HashratesRepository.ts @@ -1,5 +1,6 @@ import { escape } from 'mysql2'; import { Common } from '../api/common'; +import mining from '../api/mining/mining'; import DB from '../database'; import logger from '../logger'; import PoolsRepository from './PoolsRepository'; @@ -177,20 +178,6 @@ class HashratesRepository { } } - /** - * Set latest run timestamp - */ - public async $setLatestRun(key: string, val: number) { - const query = `UPDATE state SET number = ? WHERE name = ?`; - - try { - await DB.query(query, [val, key]); - } catch (e) { - logger.err(`Cannot set last indexing run for ${key}. Reason: ` + (e instanceof Error ? e.message : e)); - throw e; - } - } - /** * Get latest run timestamp */ @@ -222,8 +209,8 @@ class HashratesRepository { await DB.query(`DELETE FROM hashrates WHERE hashrate_timestamp = ?`, [row.timestamp]); } // Re-run the hashrate indexing to fill up missing data - await this.$setLatestRun('last_hashrates_indexing', 0); - await this.$setLatestRun('last_weekly_hashrates_indexing', 0); + mining.lastHashrateIndexingDate = null; + mining.lastWeeklyHashrateIndexingDate = null; } catch (e) { logger.err('Cannot delete latest hashrates data points. Reason: ' + (e instanceof Error ? e.message : e)); } @@ -238,8 +225,8 @@ class HashratesRepository { try { await DB.query(`DELETE FROM hashrates WHERE hashrate_timestamp >= FROM_UNIXTIME(?)`, [timestamp]); // Re-run the hashrate indexing to fill up missing data - await this.$setLatestRun('last_hashrates_indexing', 0); - await this.$setLatestRun('last_weekly_hashrates_indexing', 0); + mining.lastHashrateIndexingDate = null; + mining.lastWeeklyHashrateIndexingDate = null; } catch (e) { logger.err('Cannot delete latest hashrates data points. Reason: ' + (e instanceof Error ? e.message : e)); }