Index new hashrates once every 24 hours
This commit is contained in:
@@ -261,6 +261,10 @@ class DatabaseMigration {
|
||||
}
|
||||
}
|
||||
|
||||
if (version < 7) {
|
||||
queries.push(`INSERT INTO state(name, number, string) VALUES ('last_hashrates_indexing', 0, NULL)`);
|
||||
}
|
||||
|
||||
return queries;
|
||||
}
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ class Mining {
|
||||
/**
|
||||
* Generate high level overview of the pool ranks and general stats
|
||||
*/
|
||||
public async $getPoolsStats(interval: string | null) : Promise<object> {
|
||||
public async $getPoolsStats(interval: string | null): Promise<object> {
|
||||
const poolsStatistics = {};
|
||||
|
||||
const poolsInfo: PoolInfo[] = await PoolsRepository.$getPoolsInfo(interval);
|
||||
@@ -30,8 +30,8 @@ class Mining {
|
||||
link: poolInfo.link,
|
||||
blockCount: poolInfo.blockCount,
|
||||
rank: rank++,
|
||||
emptyBlocks: 0,
|
||||
}
|
||||
emptyBlocks: 0
|
||||
};
|
||||
for (let i = 0; i < emptyBlocks.length; ++i) {
|
||||
if (emptyBlocks[i].poolId === poolInfo.poolId) {
|
||||
poolStat.emptyBlocks++;
|
||||
@@ -84,32 +84,41 @@ class Mining {
|
||||
return {
|
||||
adjustments: difficultyAdjustments,
|
||||
oldestIndexedBlockTimestamp: oldestBlock.getTime(),
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the historical hashrates and oldest indexed block timestamp
|
||||
*/
|
||||
public async $getHistoricalHashrates(interval: string | null): Promise<object> {
|
||||
public async $getHistoricalHashrates(interval: string | null): Promise<object> {
|
||||
const hashrates = await HashratesRepository.$get(interval);
|
||||
const oldestBlock = new Date(await BlocksRepository.$oldestBlockTimestamp());
|
||||
|
||||
return {
|
||||
hashrates: hashrates,
|
||||
oldestIndexedBlockTimestamp: oldestBlock.getTime(),
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Generate daily hashrate data
|
||||
*/
|
||||
public async $generateNetworkHashrateHistory() : Promise<void> {
|
||||
public async $generateNetworkHashrateHistory(): Promise<void> {
|
||||
// We only run this once a day
|
||||
const latestTimestamp = await HashratesRepository.$getLatestRunTimestamp();
|
||||
const now = new Date().getTime() / 1000;
|
||||
if (now - latestTimestamp < 86400) {
|
||||
return;
|
||||
}
|
||||
|
||||
logger.info(`Indexing hashrates`);
|
||||
|
||||
if (this.hashrateIndexingStarted) {
|
||||
return;
|
||||
}
|
||||
this.hashrateIndexingStarted = true;
|
||||
|
||||
const totalIndexed = await BlocksRepository.$blockCount(null, null);
|
||||
const oldestIndexedBlockHeight = await BlocksRepository.$getOldestIndexedBlockHeight();
|
||||
const indexedTimestamp = (await HashratesRepository.$get(null)).map(hashrate => hashrate.timestamp);
|
||||
|
||||
const genesisTimestamp = 1231006505; // bitcoin-cli getblock 000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f
|
||||
@@ -128,16 +137,18 @@ class Mining {
|
||||
null, fromTimestamp, toTimestamp
|
||||
);
|
||||
|
||||
let lastBlockHashrate = 0;
|
||||
if (blockStats.blockCount > 0) {
|
||||
lastBlockHashrate = await bitcoinClient.getNetworkHashPs(blockStats.blockCount,
|
||||
blockStats.lastBlockHeight);
|
||||
if (blockStats.blockCount === 0) { // We are done indexing, no blocks left
|
||||
break;
|
||||
}
|
||||
|
||||
if (toTimestamp % 864000 === 0) {
|
||||
const progress = Math.round((totalIndexed - blockStats.lastBlockHeight) / totalIndexed * 100);
|
||||
let lastBlockHashrate = 0;
|
||||
lastBlockHashrate = await bitcoinClient.getNetworkHashPs(blockStats.blockCount,
|
||||
blockStats.lastBlockHeight);
|
||||
|
||||
if (toTimestamp % 864000 === 0) { // Log every 10 days during initial indexing
|
||||
const formattedDate = new Date(fromTimestamp * 1000).toUTCString();
|
||||
logger.debug(`Counting blocks and hashrate for ${formattedDate}. Progress: ${progress}%`);
|
||||
const blocksLeft = blockStats.lastBlockHeight - oldestIndexedBlockHeight;
|
||||
logger.debug(`Counting blocks and hashrate for ${formattedDate}. ${blocksLeft} blocks left`);
|
||||
}
|
||||
|
||||
await HashratesRepository.$saveDailyStat({
|
||||
@@ -149,6 +160,9 @@ class Mining {
|
||||
toTimestamp -= 86400;
|
||||
}
|
||||
|
||||
await HashratesRepository.$setLatestRunTimestamp();
|
||||
this.hashrateIndexingStarted = false;
|
||||
|
||||
logger.info(`Hashrates indexing completed`);
|
||||
}
|
||||
|
||||
|
||||
@@ -281,6 +281,13 @@ class BlocksRepository {
|
||||
|
||||
return rows;
|
||||
}
|
||||
|
||||
public async $getOldestIndexedBlockHeight(): Promise<number> {
|
||||
const connection = await DB.pool.getConnection();
|
||||
const [rows]: any[] = await connection.query(`SELECT MIN(height) as minHeight FROM blocks`);
|
||||
connection.release();
|
||||
return rows[0].minHeight;
|
||||
}
|
||||
}
|
||||
|
||||
export default new BlocksRepository();
|
||||
|
||||
@@ -43,11 +43,28 @@ class HashratesRepository {
|
||||
query += ` WHERE hashrate_timestamp BETWEEN DATE_SUB(NOW(), INTERVAL ${interval}) AND NOW()`;
|
||||
}
|
||||
|
||||
query += ` ORDER by hashrate_timestamp DESC`;
|
||||
|
||||
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'`;
|
||||
await connection.query<any>(query, [Math.round(new Date().getTime() / 1000)]);
|
||||
connection.release();
|
||||
}
|
||||
|
||||
public async $getLatestRunTimestamp(): Promise<number> {
|
||||
const connection = await DB.pool.getConnection();
|
||||
const query = `SELECT number FROM state WHERE name = 'last_hashrates_indexing'`;
|
||||
const [rows] = await connection.query<any>(query);
|
||||
connection.release();
|
||||
return rows[0]['number'];
|
||||
}
|
||||
}
|
||||
|
||||
export default new HashratesRepository();
|
||||
|
||||
Reference in New Issue
Block a user