From a214c5ca203a745242a36a0e377140ea973e2c1c Mon Sep 17 00:00:00 2001 From: nymkappa Date: Tue, 22 Feb 2022 23:32:14 +0900 Subject: [PATCH 1/8] Disable difficulty adjustment table for now until loadMore is implemented --- backend/src/api/mining.ts | 2 +- .../components/hashrate-chart/hashrate-chart.component.html | 4 ++-- .../app/components/hashrate-chart/hashrate-chart.component.ts | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/backend/src/api/mining.ts b/backend/src/api/mining.ts index 9c3689011..edd782e0c 100644 --- a/backend/src/api/mining.ts +++ b/backend/src/api/mining.ts @@ -162,7 +162,7 @@ class Mining { } // Add genesis block manually - if (!indexedTimestamp.includes(genesisTimestamp)) { + if (toTimestamp <= genesisTimestamp && !indexedTimestamp.includes(genesisTimestamp)) { hashrates.push({ hashrateTimestamp: genesisTimestamp, avgHashrate: await bitcoinClient.getNetworkHashPs(1, 1), diff --git a/frontend/src/app/components/hashrate-chart/hashrate-chart.component.html b/frontend/src/app/components/hashrate-chart/hashrate-chart.component.html index 745fe491f..f4b8b8b2a 100644 --- a/frontend/src/app/components/hashrate-chart/hashrate-chart.component.html +++ b/frontend/src/app/components/hashrate-chart/hashrate-chart.component.html @@ -31,7 +31,7 @@
-
+
diff --git a/frontend/src/app/components/hashrate-chart/hashrate-chart.component.ts b/frontend/src/app/components/hashrate-chart/hashrate-chart.component.ts index f6995056e..961e4786e 100644 --- a/frontend/src/app/components/hashrate-chart/hashrate-chart.component.ts +++ b/frontend/src/app/components/hashrate-chart/hashrate-chart.component.ts @@ -172,7 +172,7 @@ export class HashrateChartComponent implements OnInit { }, xAxis: { type: 'time', - splitNumber: this.isMobile() ? 5 : 10, + splitNumber: (this.isMobile() || this.widget) ? 5 : 10, }, legend: { data: [ From 78fa3e33cdcf1b7ffff71810d12678c9d50389a5 Mon Sep 17 00:00:00 2001 From: nymkappa Date: Thu, 24 Feb 2022 16:55:18 +0900 Subject: [PATCH 2/8] Create stacked pools historical hashrates to see dominance over time --- backend/src/api/database-migration.ts | 10 +- backend/src/api/mining.ts | 57 ++++-- backend/src/index.ts | 5 +- backend/src/repositories/BlocksRepository.ts | 6 +- .../src/repositories/HashratesRepository.ts | 49 ++++- backend/src/repositories/PoolsRepository.ts | 16 ++ backend/src/routes.ts | 18 +- frontend/src/app/app-routing.module.ts | 25 +-- frontend/src/app/app.constants.ts | 18 +- frontend/src/app/app.module.ts | 2 + .../hashrate-chart.component.html | 2 +- .../hashrate-chart-pools.component.html | 58 ++++++ .../hashrate-chart-pools.component.scss | 50 +++++ .../hashrate-chart-pools.component.ts | 186 ++++++++++++++++++ .../mining-dashboard.component.html | 14 ++ frontend/src/app/services/api.service.ts | 7 + 16 files changed, 485 insertions(+), 38 deletions(-) create mode 100644 frontend/src/app/components/hashrates-chart-pools/hashrate-chart-pools.component.html create mode 100644 frontend/src/app/components/hashrates-chart-pools/hashrate-chart-pools.component.scss create mode 100644 frontend/src/app/components/hashrates-chart-pools/hashrate-chart-pools.component.ts diff --git a/backend/src/api/database-migration.ts b/backend/src/api/database-migration.ts index 179c56110..931c9ec6d 100644 --- a/backend/src/api/database-migration.ts +++ b/backend/src/api/database-migration.ts @@ -6,7 +6,7 @@ import logger from '../logger'; const sleep = (ms: number) => new Promise(res => setTimeout(res, ms)); class DatabaseMigration { - private static currentVersion = 7; + private static currentVersion = 8; private queryTimeout = 120000; private statisticsAddedIndexed = false; @@ -122,6 +122,14 @@ class DatabaseMigration { await this.$executeQuery(connection, this.getCreateDailyStatsTableQuery(), await this.$checkIfTableExists('hashrates')); } + if (databaseSchemaVersion < 8 && isBitcoin === true) { + await this.$executeQuery(connection, 'TRUNCATE hashrates;'); + await this.$executeQuery(connection, 'ALTER TABLE `hashrates` DROP INDEX `PRIMARY`'); + await this.$executeQuery(connection, 'ALTER TABLE `hashrates` ADD `id` int NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST'); + await this.$executeQuery(connection, 'ALTER TABLE `hashrates` ADD `share` float NOT NULL DEFAULT "0"'); + await this.$executeQuery(connection, 'ALTER TABLE `hashrates` ADD `type` enum("daily", "weekly") DEFAULT "daily"'); + } + connection.release(); } catch (e) { connection.release(); diff --git a/backend/src/api/mining.ts b/backend/src/api/mining.ts index edd782e0c..c0970bf24 100644 --- a/backend/src/api/mining.ts +++ b/backend/src/api/mining.ts @@ -83,8 +83,15 @@ class Mining { /** * Return the historical hashrates and oldest indexed block timestamp */ - public async $getHistoricalHashrates(interval: string | null): Promise { - return await HashratesRepository.$get(interval); + public async $getNetworkHistoricalHashrates(interval: string | null): Promise { + return await HashratesRepository.$getNetworkDailyHashrate(interval); + } + + /** + * Return the historical hashrates and oldest indexed block timestamp for one or all pools + */ + public async $getPoolsHistoricalHashrates(interval: string | null, poolId: number): Promise { + return await HashratesRepository.$getPoolsWeeklyHashrate(interval); } /** @@ -106,7 +113,7 @@ class Mining { logger.info(`Indexing hashrates`); const totalDayIndexed = (await BlocksRepository.$blockCount(null, null)) / 144; - const indexedTimestamp = (await HashratesRepository.$get(null)).map(hashrate => hashrate.timestamp); + const indexedTimestamp = (await HashratesRepository.$getNetworkDailyHashrate(null)).map(hashrate => hashrate.timestamp); let startedAt = new Date().getTime() / 1000; const genesisTimestamp = 1231006505; // bitcoin-cli getblock 000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f const lastMidnight = new Date(); @@ -135,27 +142,50 @@ class Mining { lastBlockHashrate = await bitcoinClient.getNetworkHashPs(blockStats.blockCount, blockStats.lastBlockHeight); - const elapsedSeconds = Math.max(1, Math.round((new Date().getTime() / 1000) - startedAt)); - if (elapsedSeconds > 10) { - const daysPerSeconds = Math.max(1, Math.round(indexedThisRun / elapsedSeconds)); - const formattedDate = new Date(fromTimestamp * 1000).toUTCString(); - 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; + if (totalIndexed % 7 === 0 && !indexedTimestamp.includes(fromTimestamp + 1)) { // Save weekly pools hashrate + logger.debug("Indexing weekly hashrates for mining pools"); + let pools = await PoolsRepository.$getPoolsInfoBetween(fromTimestamp - 604800, fromTimestamp); + const totalBlocks = pools.reduce((acc, pool) => acc + pool.blockCount, 0); + pools = pools.map((pool: any) => { + pool.hashrate = (pool.blockCount / totalBlocks) * lastBlockHashrate; + pool.share = (pool.blockCount / totalBlocks); + return pool; + }); + + for (const pool of pools) { + hashrates.push({ + hashrateTimestamp: fromTimestamp + 1, + avgHashrate: pool['hashrate'], + poolId: pool.poolId, + share: pool['share'], + type: 'weekly', + }); + } } hashrates.push({ hashrateTimestamp: fromTimestamp, avgHashrate: lastBlockHashrate, poolId: null, + share: 1, + type: 'daily', }); - if (hashrates.length > 100) { + if (hashrates.length > 10) { await HashratesRepository.$saveHashrates(hashrates); hashrates.length = 0; } + const elapsedSeconds = Math.max(1, Math.round((new Date().getTime() / 1000) - startedAt)); + if (elapsedSeconds > 5) { + const daysPerSeconds = (indexedThisRun / elapsedSeconds).toFixed(2); + const formattedDate = new Date(fromTimestamp * 1000).toUTCString(); + 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; + } + toTimestamp -= 86400; ++indexedThisRun; ++totalIndexed; @@ -166,7 +196,8 @@ class Mining { hashrates.push({ hashrateTimestamp: genesisTimestamp, avgHashrate: await bitcoinClient.getNetworkHashPs(1, 1), - poolId: null + poolId: null, + type: 'daily', }); } diff --git a/backend/src/index.ts b/backend/src/index.ts index 65d453859..5702ee032 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -297,8 +297,11 @@ class Server { .get(config.MEMPOOL.API_URL_PREFIX + 'mining/pool/:poolId/:interval', routes.$getPool) .get(config.MEMPOOL.API_URL_PREFIX + 'mining/difficulty', routes.$getHistoricalDifficulty) .get(config.MEMPOOL.API_URL_PREFIX + 'mining/difficulty/:interval', routes.$getHistoricalDifficulty) + .get(config.MEMPOOL.API_URL_PREFIX + 'mining/hashrate/pools', routes.$getPoolsHistoricalHashrate) + .get(config.MEMPOOL.API_URL_PREFIX + 'mining/hashrate/pools/:interval', routes.$getPoolsHistoricalHashrate) .get(config.MEMPOOL.API_URL_PREFIX + 'mining/hashrate', routes.$getHistoricalHashrate) - .get(config.MEMPOOL.API_URL_PREFIX + 'mining/hashrate/:interval', routes.$getHistoricalHashrate); + .get(config.MEMPOOL.API_URL_PREFIX + 'mining/hashrate/:interval', routes.$getHistoricalHashrate) + ; } if (config.BISQ.ENABLED) { diff --git a/backend/src/repositories/BlocksRepository.ts b/backend/src/repositories/BlocksRepository.ts index a2b0b2f95..d91777880 100644 --- a/backend/src/repositories/BlocksRepository.ts +++ b/backend/src/repositories/BlocksRepository.ts @@ -173,7 +173,7 @@ class BlocksRepository { } else { query += ` WHERE`; } - query += ` UNIX_TIMESTAMP(blockTimestamp) BETWEEN '${from}' AND '${to}'`; + query += ` blockTimestamp BETWEEN FROM_UNIXTIME('${from}') AND FROM_UNIXTIME('${to}')`; // logger.debug(query); const connection = await DB.pool.getConnection(); @@ -300,6 +300,10 @@ class BlocksRepository { const [rows]: any[] = await connection.query(query); connection.release(); + for (let row of rows) { + delete row['rn']; + } + return rows; } diff --git a/backend/src/repositories/HashratesRepository.ts b/backend/src/repositories/HashratesRepository.ts index fd4340d4e..d11c444d5 100644 --- a/backend/src/repositories/HashratesRepository.ts +++ b/backend/src/repositories/HashratesRepository.ts @@ -1,6 +1,7 @@ import { Common } from '../api/common'; import { DB } from '../database'; import logger from '../logger'; +import PoolsRepository from './PoolsRepository'; class HashratesRepository { /** @@ -8,10 +9,10 @@ class HashratesRepository { */ public async $saveHashrates(hashrates: any) { let query = `INSERT INTO - hashrates(hashrate_timestamp, avg_hashrate, pool_id) VALUES`; + hashrates(hashrate_timestamp, avg_hashrate, pool_id, share, type) VALUES`; for (const hashrate of hashrates) { - query += ` (FROM_UNIXTIME(${hashrate.hashrateTimestamp}), ${hashrate.avgHashrate}, ${hashrate.poolId}),`; + query += ` (FROM_UNIXTIME(${hashrate.hashrateTimestamp}), ${hashrate.avgHashrate}, ${hashrate.poolId}, ${hashrate.share}, "${hashrate.type}"),`; } query = query.slice(0, -1); @@ -26,10 +27,7 @@ class HashratesRepository { connection.release(); } - /** - * Returns an array of all timestamp we've already indexed - */ - public async $get(interval: string | null): Promise { + public async $getNetworkDailyHashrate(interval: string | null): Promise { interval = Common.getSqlInterval(interval); const connection = await DB.pool.getConnection(); @@ -38,7 +36,12 @@ class HashratesRepository { FROM hashrates`; if (interval) { - query += ` WHERE hashrate_timestamp BETWEEN DATE_SUB(NOW(), INTERVAL ${interval}) AND NOW()`; + query += ` WHERE hashrate_timestamp BETWEEN DATE_SUB(NOW(), INTERVAL ${interval}) AND NOW() + AND hashrates.type = 'daily' + AND pool_id IS NULL`; + } else { + query += ` WHERE hashrates.type = 'daily' + AND pool_id IS NULL`; } query += ` ORDER by hashrate_timestamp`; @@ -49,6 +52,38 @@ class HashratesRepository { return rows; } + /** + * Returns the current biggest pool hashrate history + */ + public async $getPoolsWeeklyHashrate(interval: string | null): Promise { + interval = Common.getSqlInterval(interval); + + const connection = await DB.pool.getConnection(); + const topPoolsId = (await PoolsRepository.$getPoolsInfo('1w')).map((pool) => pool.poolId); + + let query = `SELECT UNIX_TIMESTAMP(hashrate_timestamp) as timestamp, avg_hashrate as avgHashrate, share, pools.name as poolName + FROM hashrates + JOIN pools on pools.id = pool_id`; + + if (interval) { + query += ` WHERE hashrate_timestamp BETWEEN DATE_SUB(NOW(), INTERVAL ${interval}) AND NOW() + AND hashrates.type = 'weekly' + AND pool_id IN (${topPoolsId})`; + } else { + query += ` WHERE hashrates.type = 'weekly' + AND pool_id IN (${topPoolsId})`; + } + + query += ` ORDER by hashrate_timestamp, FIELD(pool_id, ${topPoolsId})`; + + console.log(query); + + const [rows]: any[] = await connection.query(query); + connection.release(); + + return rows; + } + public async $setLatestRunTimestamp() { const connection = await DB.pool.getConnection(); const query = `UPDATE state SET number = ? WHERE name = 'last_hashrates_indexing'`; diff --git a/backend/src/repositories/PoolsRepository.ts b/backend/src/repositories/PoolsRepository.ts index a7b716da7..ea617322a 100644 --- a/backend/src/repositories/PoolsRepository.ts +++ b/backend/src/repositories/PoolsRepository.ts @@ -49,6 +49,22 @@ class PoolsRepository { return rows; } + /** + * Get basic pool info and block count between two timestamp + */ + public async $getPoolsInfoBetween(from: number, to: number): Promise { + let query = `SELECT COUNT(height) as blockCount, pools.id as poolId, pools.name as poolName + FROM pools + LEFT JOIN blocks on pools.id = blocks.pool_id AND blocks.blockTimestamp BETWEEN FROM_UNIXTIME(?) AND FROM_UNIXTIME(?) + GROUP BY pools.id`; + + const connection = await DB.pool.getConnection(); + const [rows] = await connection.query(query, [from, to]); + connection.release(); + + return rows; + } + /** * Get mining pool statistics for one pool */ diff --git a/backend/src/routes.ts b/backend/src/routes.ts index 7fba2e66b..dea9da2f2 100644 --- a/backend/src/routes.ts +++ b/backend/src/routes.ts @@ -586,9 +586,25 @@ class Routes { } } + public async $getPoolsHistoricalHashrate(req: Request, res: Response) { + try { + const hashrates = await mining.$getPoolsHistoricalHashrates(req.params.interval ?? null, parseInt(req.params.poolId, 10)); + const oldestIndexedBlockTimestamp = await BlocksRepository.$oldestBlockTimestamp(); + res.header('Pragma', 'public'); + res.header('Cache-control', 'public'); + res.setHeader('Expires', new Date(Date.now() + 1000 * 300).toUTCString()); + res.json({ + oldestIndexedBlockTimestamp: oldestIndexedBlockTimestamp, + hashrates: hashrates, + }); + } catch (e) { + res.status(500).send(e instanceof Error ? e.message : e); + } + } + public async $getHistoricalHashrate(req: Request, res: Response) { try { - const hashrates = await mining.$getHistoricalHashrates(req.params.interval ?? null); + const hashrates = await mining.$getNetworkHistoricalHashrates(req.params.interval ?? null); const difficulty = await mining.$getHistoricalDifficulty(req.params.interval ?? null); const oldestIndexedBlockTimestamp = await BlocksRepository.$oldestBlockTimestamp(); res.header('Pragma', 'public'); diff --git a/frontend/src/app/app-routing.module.ts b/frontend/src/app/app-routing.module.ts index bd7d2d516..19285fc8f 100644 --- a/frontend/src/app/app-routing.module.ts +++ b/frontend/src/app/app-routing.module.ts @@ -29,6 +29,7 @@ import { AssetsComponent } from './components/assets/assets.component'; import { PoolComponent } from './components/pool/pool.component'; import { MiningDashboardComponent } from './components/mining-dashboard/mining-dashboard.component'; import { HashrateChartComponent } from './components/hashrate-chart/hashrate-chart.component'; +import { HashrateChartPoolsComponent } from './components/hashrates-chart-pools/hashrate-chart-pools.component'; import { MiningStartComponent } from './components/mining-start/mining-start.component'; let routes: Routes = [ @@ -78,6 +79,10 @@ let routes: Routes = [ path: 'hashrate', component: HashrateChartComponent, }, + { + path: 'hashrate/pools', + component: HashrateChartPoolsComponent, + }, { path: 'pools', component: PoolRankingComponent, @@ -89,10 +94,6 @@ let routes: Routes = [ path: ':poolId', component: PoolComponent, }, - { - path: ':poolId/hashrate', - component: HashrateChartComponent, - }, ] }, ] @@ -193,6 +194,10 @@ let routes: Routes = [ path: 'hashrate', component: HashrateChartComponent, }, + { + path: 'hashrate/pools', + component: HashrateChartPoolsComponent, + }, { path: 'pools', component: PoolRankingComponent, @@ -204,10 +209,6 @@ let routes: Routes = [ path: ':poolId', component: PoolComponent, }, - { - path: ':poolId/hashrate', - component: HashrateChartComponent, - }, ] }, ] @@ -302,6 +303,10 @@ let routes: Routes = [ path: 'hashrate', component: HashrateChartComponent, }, + { + path: 'hashrate/pools', + component: HashrateChartPoolsComponent, + }, { path: 'pools', component: PoolRankingComponent, @@ -313,10 +318,6 @@ let routes: Routes = [ path: ':poolId', component: PoolComponent, }, - { - path: ':poolId/hashrate', - component: HashrateChartComponent, - }, ] }, ] diff --git a/frontend/src/app/app.constants.ts b/frontend/src/app/app.constants.ts index dc89748ad..1dfdce00e 100644 --- a/frontend/src/app/app.constants.ts +++ b/frontend/src/app/app.constants.ts @@ -71,7 +71,23 @@ export const chartColors = [ "#263238", ]; -export const feeLevels = [1, 2, 3, 4, 5, 6, 8, 10, 12, 15, 20, 30, 40, 50, 60, 70, 80, 90, 100, 125, 150, 175, 200, +export const poolsColor = { + 'foundryusa': '#D81B60', + 'antpool': '#8E24AA', + 'f2pool': '#5E35B1', + 'poolin': '#3949AB', + 'binancepool': '#1E88E5', + 'viabtc': '#039BE5', + 'btccom': '#00ACC1', + 'slushpool': '#00897B', + 'sbicrypto': '#43A047', + 'marapool': '#7CB342', + 'luxor': '#C0CA33', + 'unknown': '#FDD835', + 'okkong': '#FFB300', +} + + export const feeLevels = [1, 2, 3, 4, 5, 6, 8, 10, 12, 15, 20, 30, 40, 50, 60, 70, 80, 90, 100, 125, 150, 175, 200, 250, 300, 350, 400, 500, 600, 700, 800, 900, 1000, 1200, 1400, 1600, 1800, 2000]; export interface Language { diff --git a/frontend/src/app/app.module.ts b/frontend/src/app/app.module.ts index c161e24dc..956990f94 100644 --- a/frontend/src/app/app.module.ts +++ b/frontend/src/app/app.module.ts @@ -71,6 +71,7 @@ import { AssetGroupComponent } from './components/assets/asset-group/asset-group import { AssetCirculationComponent } from './components/asset-circulation/asset-circulation.component'; import { MiningDashboardComponent } from './components/mining-dashboard/mining-dashboard.component'; import { HashrateChartComponent } from './components/hashrate-chart/hashrate-chart.component'; +import { HashrateChartPoolsComponent } from './components/hashrates-chart-pools/hashrate-chart-pools.component'; import { MiningStartComponent } from './components/mining-start/mining-start.component'; import { AmountShortenerPipe } from './shared/pipes/amount-shortener.pipe'; @@ -126,6 +127,7 @@ import { AmountShortenerPipe } from './shared/pipes/amount-shortener.pipe'; AssetCirculationComponent, MiningDashboardComponent, HashrateChartComponent, + HashrateChartPoolsComponent, MiningStartComponent, AmountShortenerPipe, ], diff --git a/frontend/src/app/components/hashrate-chart/hashrate-chart.component.html b/frontend/src/app/components/hashrate-chart/hashrate-chart.component.html index f4b8b8b2a..ea5c5a2a7 100644 --- a/frontend/src/app/components/hashrate-chart/hashrate-chart.component.html +++ b/frontend/src/app/components/hashrate-chart/hashrate-chart.component.html @@ -1,6 +1,6 @@
-
+
+ + +
\ No newline at end of file diff --git a/frontend/src/app/services/api.service.ts b/frontend/src/app/services/api.service.ts index cf0ebd414..5548780b1 100644 --- a/frontend/src/app/services/api.service.ts +++ b/frontend/src/app/services/api.service.ts @@ -163,4 +163,11 @@ export class ApiService { (interval !== undefined ? `/${interval}` : '') ); } + + getHistoricalPoolsHashrate$(interval: string | undefined): Observable { + return this.httpClient.get( + this.apiBaseUrl + this.apiBasePath + `/api/v1/mining/hashrate/pools` + + (interval !== undefined ? `/${interval}` : '') + ); + } } From e358a553c17afbc2373bd12ac4c005bb97517ed5 Mon Sep 17 00:00:00 2001 From: nymkappa Date: Thu, 24 Feb 2022 17:28:16 +0900 Subject: [PATCH 3/8] Match pool color between pools pie and pools stack --- frontend/src/app/app.constants.ts | 4 ++-- .../app/components/pool-ranking/pool-ranking.component.ts | 6 ++++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/frontend/src/app/app.constants.ts b/frontend/src/app/app.constants.ts index 1dfdce00e..232578e6b 100644 --- a/frontend/src/app/app.constants.ts +++ b/frontend/src/app/app.constants.ts @@ -78,8 +78,8 @@ export const poolsColor = { 'poolin': '#3949AB', 'binancepool': '#1E88E5', 'viabtc': '#039BE5', - 'btccom': '#00ACC1', - 'slushpool': '#00897B', + 'btccom': '#00897B', + 'slushpool': '#00ACC1', 'sbicrypto': '#43A047', 'marapool': '#7CB342', 'luxor': '#C0CA33', diff --git a/frontend/src/app/components/pool-ranking/pool-ranking.component.ts b/frontend/src/app/components/pool-ranking/pool-ranking.component.ts index a5f2e54a4..a3017c7af 100644 --- a/frontend/src/app/components/pool-ranking/pool-ranking.component.ts +++ b/frontend/src/app/components/pool-ranking/pool-ranking.component.ts @@ -9,7 +9,7 @@ import { SeoService } from 'src/app/services/seo.service'; import { StorageService } from '../..//services/storage.service'; import { MiningService, MiningStats } from '../../services/mining.service'; import { StateService } from '../../services/state.service'; -import { chartColors } from 'src/app/app.constants'; +import { chartColors, poolsColor } from 'src/app/app.constants'; @Component({ selector: 'app-pool-ranking', @@ -120,6 +120,9 @@ export class PoolRankingComponent implements OnInit { return; } data.push({ + itemStyle: { + color: poolsColor[pool.name.replace(/[^a-zA-Z0-9]/g, "").toLowerCase()], + }, value: pool.share, name: pool.name + (this.isMobile() ? `` : ` (${pool.share}%)`), label: { @@ -216,7 +219,6 @@ export class PoolRankingComponent implements OnInit { } } ], - color: chartColors }; } From 54ccfe070e5fc69bb70161e58a6724fa83cc685e Mon Sep 17 00:00:00 2001 From: nymkappa Date: Thu, 24 Feb 2022 17:45:10 +0900 Subject: [PATCH 4/8] Move pool pie chart at the bottom of the mining dashboard --- .../mining-dashboard.component.html | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/frontend/src/app/components/mining-dashboard/mining-dashboard.component.html b/frontend/src/app/components/mining-dashboard/mining-dashboard.component.html index f18359a49..0b89eb954 100644 --- a/frontend/src/app/components/mining-dashboard/mining-dashboard.component.html +++ b/frontend/src/app/components/mining-dashboard/mining-dashboard.component.html @@ -2,16 +2,16 @@ - +
From ec40231f930aeaf103a9c6818fae13adb194630b Mon Sep 17 00:00:00 2001 From: nymkappa Date: Thu, 24 Feb 2022 20:20:18 +0900 Subject: [PATCH 5/8] warn on re-index - fix hash indexing state issue - cleanup ui mining --- backend/src/api/database-migration.ts | 5 +++- backend/src/index.ts | 10 +++++-- .../src/repositories/HashratesRepository.ts | 5 ++-- .../hashrate-chart.component.ts | 5 +++- .../hashrate-chart-pools.component.html | 24 ----------------- .../hashrate-chart-pools.component.scss | 6 ++--- .../hashrate-chart-pools.component.ts | 26 +++++++++---------- .../mining-dashboard.component.html | 2 +- .../mining-dashboard.component.scss | 2 +- .../mining-dashboard.component.ts | 5 +++- .../pool-ranking/pool-ranking.component.ts | 8 +++--- 11 files changed, 45 insertions(+), 53 deletions(-) diff --git a/backend/src/api/database-migration.ts b/backend/src/api/database-migration.ts index 931c9ec6d..49e9ef9c4 100644 --- a/backend/src/api/database-migration.ts +++ b/backend/src/api/database-migration.ts @@ -92,11 +92,13 @@ class DatabaseMigration { await this.$executeQuery(connection, this.getCreateBlocksTableQuery(), await this.$checkIfTableExists('blocks')); } if (databaseSchemaVersion < 5 && isBitcoin === true) { + logger.warn(`'blocks' table has been truncated. Re-indexing from scratch.'`); await this.$executeQuery(connection, 'TRUNCATE blocks;'); // Need to re-index await this.$executeQuery(connection, 'ALTER TABLE blocks ADD `reward` double unsigned NOT NULL DEFAULT "0"'); } if (databaseSchemaVersion < 6 && isBitcoin === true) { + logger.warn(`'blocks' table has been truncated. Re-indexing from scratch.'`); await this.$executeQuery(connection, 'TRUNCATE blocks;'); // Need to re-index // Cleanup original blocks fields type await this.$executeQuery(connection, 'ALTER TABLE blocks MODIFY `height` integer unsigned NOT NULL DEFAULT "0"'); @@ -123,7 +125,8 @@ class DatabaseMigration { } if (databaseSchemaVersion < 8 && isBitcoin === true) { - await this.$executeQuery(connection, 'TRUNCATE hashrates;'); + logger.warn(`'hashrates' table has been truncated. Re-indexing from scratch.'`); + await this.$executeQuery(connection, 'TRUNCATE hashrates;'); // Need to re-index await this.$executeQuery(connection, 'ALTER TABLE `hashrates` DROP INDEX `PRIMARY`'); await this.$executeQuery(connection, 'ALTER TABLE `hashrates` ADD `id` int NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST'); await this.$executeQuery(connection, 'ALTER TABLE `hashrates` ADD `share` float NOT NULL DEFAULT "0"'); diff --git a/backend/src/index.ts b/backend/src/index.ts index 5702ee032..4305d6dac 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -27,6 +27,7 @@ import syncAssets from './sync-assets'; import icons from './api/liquid/icons'; import { Common } from './api/common'; import mining from './api/mining'; +import HashratesRepository from './repositories/HashratesRepository'; class Server { private wss: WebSocket.Server | undefined; @@ -95,6 +96,7 @@ class Server { await Common.sleep(5000); await databaseMigration.$truncateIndexedData(tables); } + await this.$resetHashratesIndexingState(); await databaseMigration.$initializeOrMigrateDatabase(); await poolsParser.migratePoolsJson(); } catch (e) { @@ -145,7 +147,7 @@ class Server { } await blocks.$updateBlocks(); await memPool.$updateMempool(); - this.runIndexingWhenReady(); + this.$runIndexingWhenReady(); setTimeout(this.runMainUpdateLoop.bind(this), config.MEMPOOL.POLL_RATE_MS); this.currentBackendRetryInterval = 5; @@ -164,7 +166,11 @@ class Server { } } - async runIndexingWhenReady() { + async $resetHashratesIndexingState() { + return await HashratesRepository.$setLatestRunTimestamp(0); + } + + async $runIndexingWhenReady() { if (!Common.indexingEnabled() || mempool.hasPriority()) { return; } diff --git a/backend/src/repositories/HashratesRepository.ts b/backend/src/repositories/HashratesRepository.ts index d11c444d5..6d49be811 100644 --- a/backend/src/repositories/HashratesRepository.ts +++ b/backend/src/repositories/HashratesRepository.ts @@ -84,10 +84,11 @@ class HashratesRepository { return rows; } - public async $setLatestRunTimestamp() { + public async $setLatestRunTimestamp(val: any = null) { const connection = await DB.pool.getConnection(); const query = `UPDATE state SET number = ? WHERE name = 'last_hashrates_indexing'`; - await connection.query(query, [Math.round(new Date().getTime() / 1000)]); + + await connection.query(query, (val === null) ? [Math.round(new Date().getTime() / 1000)] : [val]); connection.release(); } diff --git a/frontend/src/app/components/hashrate-chart/hashrate-chart.component.ts b/frontend/src/app/components/hashrate-chart/hashrate-chart.component.ts index 961e4786e..37b4f07ba 100644 --- a/frontend/src/app/components/hashrate-chart/hashrate-chart.component.ts +++ b/frontend/src/app/components/hashrate-chart/hashrate-chart.component.ts @@ -45,12 +45,15 @@ export class HashrateChartComponent implements OnInit { private apiService: ApiService, private formBuilder: FormBuilder, ) { - this.seoService.setTitle($localize`:@@mining.hashrate-difficulty:Hashrate and Difficulty`); this.radioGroupForm = this.formBuilder.group({ dateSpan: '1y' }); this.radioGroupForm.controls.dateSpan.setValue('1y'); } ngOnInit(): void { + if (!this.widget) { + this.seoService.setTitle($localize`:@@mining.hashrate-difficulty:Hashrate and Difficulty`); + } + this.hashrateObservable$ = this.radioGroupForm.get('dateSpan').valueChanges .pipe( startWith('1y'), diff --git a/frontend/src/app/components/hashrates-chart-pools/hashrate-chart-pools.component.html b/frontend/src/app/components/hashrates-chart-pools/hashrate-chart-pools.component.html index ea5c5a2a7..8750caa56 100644 --- a/frontend/src/app/components/hashrates-chart-pools/hashrate-chart-pools.component.html +++ b/frontend/src/app/components/hashrates-chart-pools/hashrate-chart-pools.component.html @@ -30,29 +30,5 @@
- -
diff --git a/frontend/src/app/components/hashrates-chart-pools/hashrate-chart-pools.component.scss b/frontend/src/app/components/hashrates-chart-pools/hashrate-chart-pools.component.scss index 590de28db..316f0fc47 100644 --- a/frontend/src/app/components/hashrates-chart-pools/hashrate-chart-pools.component.scss +++ b/frontend/src/app/components/hashrates-chart-pools/hashrate-chart-pools.component.scss @@ -11,12 +11,12 @@ .full-container { width: 100%; - height: calc(100% - 50px); + height: calc(100% - 100px); @media (max-width: 992px) { - height: calc(100% - 110px); + height: calc(100% - 140px); }; @media (max-width: 576px) { - height: calc(100% - 130px); + height: calc(100% - 180px); }; } diff --git a/frontend/src/app/components/hashrates-chart-pools/hashrate-chart-pools.component.ts b/frontend/src/app/components/hashrates-chart-pools/hashrate-chart-pools.component.ts index 9a9526f00..79ac7e2e1 100644 --- a/frontend/src/app/components/hashrates-chart-pools/hashrate-chart-pools.component.ts +++ b/frontend/src/app/components/hashrates-chart-pools/hashrate-chart-pools.component.ts @@ -1,10 +1,9 @@ -import { Component, Inject, Input, LOCALE_ID, OnInit } from '@angular/core'; +import { ChangeDetectionStrategy, Component, Inject, Input, LOCALE_ID, OnInit } from '@angular/core'; import { EChartsOption } from 'echarts'; import { Observable } from 'rxjs'; import { map, share, startWith, switchMap, tap } from 'rxjs/operators'; import { ApiService } from 'src/app/services/api.service'; import { SeoService } from 'src/app/services/seo.service'; -import { formatNumber } from '@angular/common'; import { FormBuilder, FormGroup } from '@angular/forms'; import { poolsColor } from 'src/app/app.constants'; @@ -15,11 +14,12 @@ import { poolsColor } from 'src/app/app.constants'; styles: [` .loadingGraphs { position: absolute; - top: 38%; + top: 50%; left: calc(50% - 15px); z-index: 100; } `], + changeDetection: ChangeDetectionStrategy.OnPush, }) export class HashrateChartPoolsComponent implements OnInit { @Input() widget: boolean = false; @@ -37,7 +37,6 @@ export class HashrateChartPoolsComponent implements OnInit { hashrateObservable$: Observable; isLoading = true; - formatNumber = formatNumber; constructor( @Inject(LOCALE_ID) public locale: string, @@ -45,19 +44,24 @@ export class HashrateChartPoolsComponent implements OnInit { private apiService: ApiService, private formBuilder: FormBuilder, ) { - this.seoService.setTitle($localize`:@@mining.hashrate-difficulty:Hashrate and Difficulty`); this.radioGroupForm = this.formBuilder.group({ dateSpan: '1y' }); this.radioGroupForm.controls.dateSpan.setValue('1y'); } ngOnInit(): void { + if (!this.widget) { + this.seoService.setTitle($localize`:@@mining.pools-historical-dominance:Pools Historical Dominance`); + } + this.hashrateObservable$ = this.radioGroupForm.get('dateSpan').valueChanges .pipe( startWith('1y'), switchMap((timespan) => { + this.isLoading = true; return this.apiService.getHistoricalPoolsHashrate$(timespan) .pipe( tap((data: any) => { + // Prepare series (group all hashrates data point by pool) const grouped = {}; for (const hashrate of data.hashrates) { if (!grouped.hasOwnProperty(hashrate.poolName)) { @@ -68,7 +72,6 @@ export class HashrateChartPoolsComponent implements OnInit { const series = []; const legends = []; - for (const name in grouped) { series.push({ stack: 'Total', @@ -76,12 +79,8 @@ export class HashrateChartPoolsComponent implements OnInit { showSymbol: false, data: grouped[name].map((val) => [val.timestamp * 1000, (val.share * 100).toFixed(2)]), type: 'line', - lineStyle: { - width: 0, - }, - areaStyle: { - opacity: 1, - }, + lineStyle: { width: 0 }, + areaStyle: { opacity: 1 }, smooth: true, color: poolsColor[name.replace(/[^a-zA-Z0-9]/g, "").toLowerCase()], emphasis: { @@ -115,7 +114,6 @@ export class HashrateChartPoolsComponent implements OnInit { ) / 3600 / 24; return { availableTimespanDay: availableTimespanDay, - hashrates: data }; }), ); @@ -129,7 +127,7 @@ export class HashrateChartPoolsComponent implements OnInit { grid: { right: this.right, left: this.left, - bottom: this.widget ? 30 : 60, + bottom: this.widget ? 30 : 20, top: this.widget ? 10 : 40, }, tooltip: { diff --git a/frontend/src/app/components/mining-dashboard/mining-dashboard.component.html b/frontend/src/app/components/mining-dashboard/mining-dashboard.component.html index 0b89eb954..65672ed08 100644 --- a/frontend/src/app/components/mining-dashboard/mining-dashboard.component.html +++ b/frontend/src/app/components/mining-dashboard/mining-dashboard.component.html @@ -8,7 +8,7 @@
- Pools Hashrate Share (1y) + Mining Pools Dominance (1y)
diff --git a/frontend/src/app/components/mining-dashboard/mining-dashboard.component.scss b/frontend/src/app/components/mining-dashboard/mining-dashboard.component.scss index e575a405b..fca52ca79 100644 --- a/frontend/src/app/components/mining-dashboard/mining-dashboard.component.scss +++ b/frontend/src/app/components/mining-dashboard/mining-dashboard.component.scss @@ -12,7 +12,7 @@ .card { background-color: #1d1f31; - height: 100%; + height: 340px; } .card-title { diff --git a/frontend/src/app/components/mining-dashboard/mining-dashboard.component.ts b/frontend/src/app/components/mining-dashboard/mining-dashboard.component.ts index 3dc139b43..aac546ca1 100644 --- a/frontend/src/app/components/mining-dashboard/mining-dashboard.component.ts +++ b/frontend/src/app/components/mining-dashboard/mining-dashboard.component.ts @@ -1,4 +1,5 @@ import { ChangeDetectionStrategy, Component, OnInit } from '@angular/core'; +import { SeoService } from 'src/app/services/seo.service'; @Component({ selector: 'app-mining-dashboard', @@ -8,7 +9,9 @@ import { ChangeDetectionStrategy, Component, OnInit } from '@angular/core'; }) export class MiningDashboardComponent implements OnInit { - constructor() { } + constructor(private seoService: SeoService) { + this.seoService.setTitle($localize`:@@mining.mining-dashboard:Mining Dashboard`); + } ngOnInit(): void { } diff --git a/frontend/src/app/components/pool-ranking/pool-ranking.component.ts b/frontend/src/app/components/pool-ranking/pool-ranking.component.ts index a3017c7af..c46a85c65 100644 --- a/frontend/src/app/components/pool-ranking/pool-ranking.component.ts +++ b/frontend/src/app/components/pool-ranking/pool-ranking.component.ts @@ -1,4 +1,4 @@ -import { Component, Input, OnInit } from '@angular/core'; +import { ChangeDetectionStrategy, Component, Input, OnInit } from '@angular/core'; import { FormBuilder, FormGroup } from '@angular/forms'; import { Router } from '@angular/router'; import { EChartsOption, PieSeriesOption } from 'echarts'; @@ -18,11 +18,12 @@ import { chartColors, poolsColor } from 'src/app/app.constants'; styles: [` .loadingGraphs { position: absolute; - top: 38%; + top: 50%; left: calc(50% - 15px); z-index: 100; } `], + changeDetection: ChangeDetectionStrategy.OnPush, }) export class PoolRankingComponent implements OnInit { @Input() widget: boolean = false; @@ -49,13 +50,13 @@ export class PoolRankingComponent implements OnInit { private seoService: SeoService, private router: Router, ) { - this.seoService.setTitle($localize`:@@mining.mining-pools:Mining Pools`); } ngOnInit(): void { if (this.widget) { this.poolsWindowPreference = '1w'; } else { + this.seoService.setTitle($localize`:@@mining.mining-pools:Mining Pools`); this.poolsWindowPreference = this.storageService.getValue('poolsWindowPreference') ? this.storageService.getValue('poolsWindowPreference') : '1w'; } this.radioGroupForm = this.formBuilder.group({ dateSpan: this.poolsWindowPreference }); @@ -167,6 +168,7 @@ export class PoolRankingComponent implements OnInit { } this.chartOptions = { + color: chartColors, title: { text: this.widget ? '' : $localize`:@@mining.pool-chart-title:${network}:NETWORK: mining pools share`, left: 'center', From c419b7dd1afb8a46a1a44323b8e4553d9a1c9864 Mon Sep 17 00:00:00 2001 From: nymkappa Date: Fri, 25 Feb 2022 10:21:16 +0900 Subject: [PATCH 6/8] Add new api endpoint to cache warmer --- production/nginx-cache-warmer | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/production/nginx-cache-warmer b/production/nginx-cache-warmer index 8c679980c..2eab09dd1 100755 --- a/production/nginx-cache-warmer +++ b/production/nginx-cache-warmer @@ -27,6 +27,12 @@ do for url in / \ '/api/v1/mining/hashrate/2y' \ '/api/v1/mining/hashrate/3y' \ '/api/v1/mining/hashrate/all' \ + '/api/v1/mining/hashrate/pools/3m' \ + '/api/v1/mining/hashrate/pools/6m' \ + '/api/v1/mining/hashrate/pools/1y' \ + '/api/v1/mining/hashrate/pools/2y' \ + '/api/v1/mining/hashrate/pools/3y' \ + '/api/v1/mining/hashrate/pools/all' \ do curl -s "https://${hostname}${url}" >/dev/null From 88dd956354a247a1647c181c4c11560578827e73 Mon Sep 17 00:00:00 2001 From: nymkappa Date: Fri, 25 Feb 2022 13:06:33 +0900 Subject: [PATCH 7/8] Hide 'dot' when hovering charts --- .../app/components/hashrate-chart/hashrate-chart.component.ts | 2 ++ .../hashrates-chart-pools/hashrate-chart-pools.component.ts | 1 + .../incoming-transactions-graph.component.ts | 1 + 3 files changed, 4 insertions(+) diff --git a/frontend/src/app/components/hashrate-chart/hashrate-chart.component.ts b/frontend/src/app/components/hashrate-chart/hashrate-chart.component.ts index 37b4f07ba..7efb83098 100644 --- a/frontend/src/app/components/hashrate-chart/hashrate-chart.component.ts +++ b/frontend/src/app/components/hashrate-chart/hashrate-chart.component.ts @@ -246,6 +246,7 @@ export class HashrateChartComponent implements OnInit { { name: 'Hashrate', showSymbol: false, + symbol: 'none', data: data.hashrates, type: 'line', lineStyle: { @@ -256,6 +257,7 @@ export class HashrateChartComponent implements OnInit { yAxisIndex: 1, name: 'Difficulty', showSymbol: false, + symbol: 'none', data: data.difficulty, type: 'line', lineStyle: { diff --git a/frontend/src/app/components/hashrates-chart-pools/hashrate-chart-pools.component.ts b/frontend/src/app/components/hashrates-chart-pools/hashrate-chart-pools.component.ts index 79ac7e2e1..3d7935e3d 100644 --- a/frontend/src/app/components/hashrates-chart-pools/hashrate-chart-pools.component.ts +++ b/frontend/src/app/components/hashrates-chart-pools/hashrate-chart-pools.component.ts @@ -77,6 +77,7 @@ export class HashrateChartPoolsComponent implements OnInit { stack: 'Total', name: name, showSymbol: false, + symbol: 'none', data: grouped[name].map((val) => [val.timestamp * 1000, (val.share * 100).toFixed(2)]), type: 'line', lineStyle: { width: 0 }, diff --git a/frontend/src/app/components/incoming-transactions-graph/incoming-transactions-graph.component.ts b/frontend/src/app/components/incoming-transactions-graph/incoming-transactions-graph.component.ts index 157ec0a2f..7a0d9c4e5 100644 --- a/frontend/src/app/components/incoming-transactions-graph/incoming-transactions-graph.component.ts +++ b/frontend/src/app/components/incoming-transactions-graph/incoming-transactions-graph.component.ts @@ -160,6 +160,7 @@ export class IncomingTransactionsGraphComponent implements OnInit, OnChanges { type: 'line', smooth: false, showSymbol: false, + symbol: 'none', lineStyle: { width: 3, }, From 434b60ef8b95be505ebf0238ad47413ddb87fd9f Mon Sep 17 00:00:00 2001 From: nymkappa Date: Fri, 25 Feb 2022 18:17:43 +0900 Subject: [PATCH 8/8] Removed debug console.log --- backend/src/repositories/HashratesRepository.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/backend/src/repositories/HashratesRepository.ts b/backend/src/repositories/HashratesRepository.ts index 6d49be811..212e58327 100644 --- a/backend/src/repositories/HashratesRepository.ts +++ b/backend/src/repositories/HashratesRepository.ts @@ -76,8 +76,6 @@ class HashratesRepository { query += ` ORDER by hashrate_timestamp, FIELD(pool_id, ${topPoolsId})`; - console.log(query); - const [rows]: any[] = await connection.query(query); connection.release();