Don't assume two difficulty with the same value is impossible

This commit is contained in:
nymkappa 2022-02-22 22:53:47 +09:00
parent 8aa1fe48dc
commit 807ef2288a
No known key found for this signature in database
GPG Key ID: E155910B16E8BD04
3 changed files with 44 additions and 9 deletions

View File

@ -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;

View File

@ -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();

View File

@ -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 `
<b style="color: white; margin-left: 18px">${data[0].axisValueLabel}</b><br>
<span>${data[0].marker} ${data[0].seriesName}: ${formatNumber(hashrate, this.locale, '1.0-0')} ${hashratePowerOfTen.unit}H/s</span><br>
<span>${data[1].marker} ${data[1].seriesName}: ${formatNumber(difficulty, this.locale, '1.0-0')} ${difficultyPowerOfTen.unit}</span>
<span>${data[1].marker} ${data[1].seriesName}: ${formatNumber(difficulty, this.locale, '1.2-2')} ${difficultyPowerOfTen.unit}</span>
`;
}.bind(this)
},