diff --git a/backend/src/api/mining.ts b/backend/src/api/mining.ts index e7876bd60..9c3689011 100644 --- a/backend/src/api/mining.ts +++ b/backend/src/api/mining.ts @@ -161,7 +161,18 @@ class Mining { ++totalIndexed; } - await HashratesRepository.$saveHashrates(hashrates); + // Add genesis block manually + if (!indexedTimestamp.includes(genesisTimestamp)) { + hashrates.push({ + hashrateTimestamp: genesisTimestamp, + avgHashrate: await bitcoinClient.getNetworkHashPs(1, 1), + poolId: null + }); + } + + if (hashrates.length > 0) { + await HashratesRepository.$saveHashrates(hashrates); + } await HashratesRepository.$setLatestRunTimestamp(); this.hashrateIndexingStarted = false; diff --git a/backend/src/repositories/BlocksRepository.ts b/backend/src/repositories/BlocksRepository.ts index e8bfb4a62..a2b0b2f95 100644 --- a/backend/src/repositories/BlocksRepository.ts +++ b/backend/src/repositories/BlocksRepository.ts @@ -265,15 +265,37 @@ class BlocksRepository { const connection = await DB.pool.getConnection(); - let query = `SELECT MIN(UNIX_TIMESTAMP(blockTimestamp)) as timestamp, difficulty, height - FROM blocks`; + // :D ... Yeah don't ask me about this one https://stackoverflow.com/a/40303162 + // Basically, using temporary user defined fields, we are able to extract all + // difficulty adjustments from the blocks tables. + // This allow use to avoid indexing it in another table. + let query = ` + SELECT + * + FROM + ( + SELECT + UNIX_TIMESTAMP(blockTimestamp) as timestamp, difficulty, height, + IF(@prevStatus = YT.difficulty, @rn := @rn + 1, + IF(@prevStatus := YT.difficulty, @rn := 1, @rn := 1) + ) AS rn + FROM blocks YT + CROSS JOIN + ( + SELECT @prevStatus := -1, @rn := 1 + ) AS var + `; if (interval) { query += ` WHERE blockTimestamp BETWEEN DATE_SUB(NOW(), INTERVAL ${interval}) AND NOW()`; } - query += ` GROUP BY difficulty - ORDER BY blockTimestamp`; + query += ` + ORDER BY YT.height + ) AS t + WHERE t.rn = 1 + ORDER BY t.height + `; const [rows]: any[] = await connection.query(query); 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 ba1e207a0..f6995056e 100644 --- a/frontend/src/app/components/hashrate-chart/hashrate-chart.component.ts +++ b/frontend/src/app/components/hashrate-chart/hashrate-chart.component.ts @@ -60,7 +60,7 @@ export class HashrateChartComponent implements OnInit { tap((data: any) => { // We generate duplicated data point so the tooltip works nicely const diffFixed = []; - let diffIndex = 0; + let diffIndex = 1; let hashIndex = 0; while (hashIndex < data.hashrates.length) { if (diffIndex >= data.difficulty.length) { @@ -74,7 +74,9 @@ export class HashrateChartComponent implements OnInit { break; } - while (data.hashrates[hashIndex].timestamp < data.difficulty[diffIndex].timestamp) { + while (hashIndex < data.hashrates.length && diffIndex < data.difficulty.length && + data.hashrates[hashIndex].timestamp <= data.difficulty[diffIndex].timestamp + ) { diffFixed.push({ timestamp: data.hashrates[hashIndex].timestamp, difficulty: data.difficulty[diffIndex - 1].difficulty @@ -133,7 +135,7 @@ export class HashrateChartComponent implements OnInit { grid: { right: this.right, left: this.left, - bottom: 30, + bottom: this.widget ? 30 : 60, }, tooltip: { trigger: 'axis', @@ -164,7 +166,7 @@ export class HashrateChartComponent implements OnInit { return ` ${data[0].axisValueLabel}
${data[0].marker} ${data[0].seriesName}: ${formatNumber(hashrate, this.locale, '1.0-0')} ${hashratePowerOfTen.unit}H/s
- ${data[1].marker} ${data[1].seriesName}: ${formatNumber(difficulty, this.locale, '1.0-0')} ${difficultyPowerOfTen.unit} + ${data[1].marker} ${data[1].seriesName}: ${formatNumber(difficulty, this.locale, '1.2-2')} ${difficultyPowerOfTen.unit} `; }.bind(this) },