Create stacked pools historical hashrates to see dominance over time
This commit is contained in:
@@ -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();
|
||||
|
||||
@@ -83,8 +83,15 @@ class Mining {
|
||||
/**
|
||||
* Return the historical hashrates and oldest indexed block timestamp
|
||||
*/
|
||||
public async $getHistoricalHashrates(interval: string | null): Promise<object> {
|
||||
return await HashratesRepository.$get(interval);
|
||||
public async $getNetworkHistoricalHashrates(interval: string | null): Promise<object> {
|
||||
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<object> {
|
||||
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',
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user