2022-03-22 04:34:29 +01:00
|
|
|
import { PoolInfo, PoolStats, RewardStats } from '../mempool.interfaces';
|
2022-03-08 16:55:49 +01:00
|
|
|
import BlocksRepository from '../repositories/BlocksRepository';
|
2022-01-18 09:37:04 +01:00
|
|
|
import PoolsRepository from '../repositories/PoolsRepository';
|
2022-02-19 12:45:02 +01:00
|
|
|
import HashratesRepository from '../repositories/HashratesRepository';
|
2022-01-18 09:37:04 +01:00
|
|
|
import bitcoinClient from './bitcoin/bitcoin-client';
|
2022-02-19 12:45:02 +01:00
|
|
|
import logger from '../logger';
|
2022-04-08 18:07:13 +02:00
|
|
|
import { Common } from './common';
|
2022-05-10 14:52:37 +02:00
|
|
|
import loadingIndicators from './loading-indicators';
|
2022-05-18 15:01:24 +02:00
|
|
|
import { escape } from 'mysql2';
|
2022-01-06 11:59:33 +01:00
|
|
|
|
|
|
|
class Mining {
|
|
|
|
constructor() {
|
|
|
|
}
|
|
|
|
|
2022-04-08 18:07:13 +02:00
|
|
|
/**
|
2022-04-14 08:37:03 +02:00
|
|
|
* Get historical block total fee
|
2022-04-08 18:07:13 +02:00
|
|
|
*/
|
|
|
|
public async $getHistoricalBlockFees(interval: string | null = null): Promise<any> {
|
2022-04-11 13:57:13 +02:00
|
|
|
return await BlocksRepository.$getHistoricalBlockFees(
|
|
|
|
this.getTimeRange(interval),
|
|
|
|
Common.getSqlInterval(interval)
|
|
|
|
);
|
|
|
|
}
|
2022-04-08 18:07:13 +02:00
|
|
|
|
2022-04-11 13:57:13 +02:00
|
|
|
/**
|
|
|
|
* Get historical block rewards
|
|
|
|
*/
|
|
|
|
public async $getHistoricalBlockRewards(interval: string | null = null): Promise<any> {
|
|
|
|
return await BlocksRepository.$getHistoricalBlockRewards(
|
|
|
|
this.getTimeRange(interval),
|
|
|
|
Common.getSqlInterval(interval)
|
|
|
|
);
|
2022-04-08 18:07:13 +02:00
|
|
|
}
|
|
|
|
|
2022-04-14 08:37:03 +02:00
|
|
|
/**
|
|
|
|
* Get historical block fee rates percentiles
|
|
|
|
*/
|
|
|
|
public async $getHistoricalBlockFeeRates(interval: string | null = null): Promise<any> {
|
|
|
|
return await BlocksRepository.$getHistoricalBlockFeeRates(
|
|
|
|
this.getTimeRange(interval),
|
|
|
|
Common.getSqlInterval(interval)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-05-03 08:44:01 +02:00
|
|
|
/**
|
|
|
|
* Get historical block sizes
|
|
|
|
*/
|
|
|
|
public async $getHistoricalBlockSizes(interval: string | null = null): Promise<any> {
|
|
|
|
return await BlocksRepository.$getHistoricalBlockSizes(
|
|
|
|
this.getTimeRange(interval),
|
|
|
|
Common.getSqlInterval(interval)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get historical block weights
|
|
|
|
*/
|
|
|
|
public async $getHistoricalBlockWeights(interval: string | null = null): Promise<any> {
|
|
|
|
return await BlocksRepository.$getHistoricalBlockWeights(
|
|
|
|
this.getTimeRange(interval),
|
|
|
|
Common.getSqlInterval(interval)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-01-06 11:59:33 +01:00
|
|
|
/**
|
|
|
|
* Generate high level overview of the pool ranks and general stats
|
|
|
|
*/
|
2022-02-21 04:22:20 +01:00
|
|
|
public async $getPoolsStats(interval: string | null): Promise<object> {
|
2022-01-18 09:37:04 +01:00
|
|
|
const poolsStatistics = {};
|
2022-01-06 11:59:33 +01:00
|
|
|
|
2022-02-09 11:41:05 +01:00
|
|
|
const poolsInfo: PoolInfo[] = await PoolsRepository.$getPoolsInfo(interval);
|
2022-03-08 16:55:49 +01:00
|
|
|
const emptyBlocks: any[] = await BlocksRepository.$countEmptyBlocks(null, interval);
|
2022-01-06 11:59:33 +01:00
|
|
|
|
2022-01-18 09:37:04 +01:00
|
|
|
const poolsStats: PoolStats[] = [];
|
2022-01-06 11:59:33 +01:00
|
|
|
let rank = 1;
|
|
|
|
|
|
|
|
poolsInfo.forEach((poolInfo: PoolInfo) => {
|
2022-03-08 16:55:49 +01:00
|
|
|
const emptyBlocksCount = emptyBlocks.filter((emptyCount) => emptyCount.poolId === poolInfo.poolId);
|
2022-01-18 09:37:04 +01:00
|
|
|
const poolStat: PoolStats = {
|
2022-01-06 11:59:33 +01:00
|
|
|
poolId: poolInfo.poolId, // mysql row id
|
|
|
|
name: poolInfo.name,
|
|
|
|
link: poolInfo.link,
|
|
|
|
blockCount: poolInfo.blockCount,
|
|
|
|
rank: rank++,
|
2022-03-25 06:22:22 +01:00
|
|
|
emptyBlocks: emptyBlocksCount.length > 0 ? emptyBlocksCount[0]['count'] : 0,
|
|
|
|
slug: poolInfo.slug,
|
2022-02-21 04:22:20 +01:00
|
|
|
};
|
2022-01-06 11:59:33 +01:00
|
|
|
poolsStats.push(poolStat);
|
2022-01-18 09:37:04 +01:00
|
|
|
});
|
2022-01-06 11:59:33 +01:00
|
|
|
|
2022-01-25 10:33:46 +01:00
|
|
|
poolsStatistics['pools'] = poolsStats;
|
2022-02-22 07:50:14 +01:00
|
|
|
poolsStatistics['oldestIndexedBlockTimestamp'] = await BlocksRepository.$oldestBlockTimestamp();
|
2022-01-25 10:33:46 +01:00
|
|
|
|
2022-02-09 11:41:05 +01:00
|
|
|
const blockCount: number = await BlocksRepository.$blockCount(null, interval);
|
2022-01-18 09:37:04 +01:00
|
|
|
poolsStatistics['blockCount'] = blockCount;
|
2022-01-25 10:33:46 +01:00
|
|
|
|
2022-04-04 17:36:00 +02:00
|
|
|
const totalBlock24h: number = await BlocksRepository.$blockCount(null, '24h');
|
|
|
|
const lastBlockHashrate = await bitcoinClient.getNetworkHashPs(totalBlock24h);
|
2022-01-18 09:37:04 +01:00
|
|
|
poolsStatistics['lastEstimatedHashrate'] = lastBlockHashrate;
|
2022-01-06 11:59:33 +01:00
|
|
|
|
|
|
|
return poolsStatistics;
|
|
|
|
}
|
2022-02-08 10:28:53 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all mining pool stats for a pool
|
|
|
|
*/
|
2022-03-25 06:22:22 +01:00
|
|
|
public async $getPoolStat(slug: string): Promise<object> {
|
|
|
|
const pool = await PoolsRepository.$getPool(slug);
|
2022-02-08 10:28:53 +01:00
|
|
|
if (!pool) {
|
2022-05-18 15:01:24 +02:00
|
|
|
throw new Error('This mining pool does not exist ' + escape(slug));
|
2022-02-08 10:28:53 +01:00
|
|
|
}
|
|
|
|
|
2022-03-25 06:22:22 +01:00
|
|
|
const blockCount: number = await BlocksRepository.$blockCount(pool.id);
|
2022-04-04 17:36:00 +02:00
|
|
|
const totalBlock: number = await BlocksRepository.$blockCount(null, null);
|
|
|
|
|
|
|
|
const blockCount24h: number = await BlocksRepository.$blockCount(pool.id, '24h');
|
|
|
|
const totalBlock24h: number = await BlocksRepository.$blockCount(null, '24h');
|
|
|
|
|
|
|
|
const blockCount1w: number = await BlocksRepository.$blockCount(pool.id, '1w');
|
|
|
|
const totalBlock1w: number = await BlocksRepository.$blockCount(null, '1w');
|
|
|
|
|
|
|
|
const currentEstimatedkHashrate = await bitcoinClient.getNetworkHashPs(totalBlock24h);
|
2022-02-08 10:28:53 +01:00
|
|
|
|
|
|
|
return {
|
|
|
|
pool: pool,
|
2022-04-04 17:36:00 +02:00
|
|
|
blockCount: {
|
|
|
|
'all': blockCount,
|
|
|
|
'24h': blockCount24h,
|
|
|
|
'1w': blockCount1w,
|
|
|
|
},
|
|
|
|
blockShare: {
|
|
|
|
'all': blockCount / totalBlock,
|
|
|
|
'24h': blockCount24h / totalBlock24h,
|
|
|
|
'1w': blockCount1w / totalBlock1w,
|
|
|
|
},
|
|
|
|
estimatedHashrate: currentEstimatedkHashrate * (blockCount24h / totalBlock24h),
|
|
|
|
reportedHashrate: null,
|
2022-02-08 10:28:53 +01:00
|
|
|
};
|
|
|
|
}
|
2022-02-17 01:41:05 +01:00
|
|
|
|
2022-03-22 04:34:29 +01:00
|
|
|
/**
|
|
|
|
* Get miner reward stats
|
|
|
|
*/
|
|
|
|
public async $getRewardStats(blockCount: number): Promise<RewardStats> {
|
|
|
|
return await BlocksRepository.$getBlockStats(blockCount);
|
|
|
|
}
|
|
|
|
|
2022-02-17 01:41:05 +01:00
|
|
|
/**
|
2022-03-06 12:47:16 +01:00
|
|
|
* [INDEXING] Generate weekly mining pool hashrate history
|
2022-02-19 12:45:02 +01:00
|
|
|
*/
|
2022-03-06 12:32:16 +01:00
|
|
|
public async $generatePoolHashrateHistory(): Promise<void> {
|
2022-03-12 14:47:33 +01:00
|
|
|
const now = new Date();
|
2022-04-02 16:44:07 +02:00
|
|
|
|
|
|
|
try {
|
2022-04-30 10:54:49 +02:00
|
|
|
const lastestRunDate = await HashratesRepository.$getLatestRun('last_weekly_hashrates_indexing');
|
2022-04-02 16:44:07 +02:00
|
|
|
|
2022-04-30 10:54:49 +02:00
|
|
|
// Run only if:
|
|
|
|
// * lastestRunDate is set to 0 (node backend restart, reorg)
|
|
|
|
// * we started a new week (around Monday midnight)
|
|
|
|
const runIndexing = lastestRunDate === 0 || now.getUTCDay() === 1 && lastestRunDate !== now.getUTCDate();
|
|
|
|
if (!runIndexing) {
|
2022-04-02 16:44:07 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
throw e;
|
2022-02-21 04:22:20 +01:00
|
|
|
}
|
|
|
|
|
2022-03-06 12:32:16 +01:00
|
|
|
try {
|
|
|
|
const indexedTimestamp = await HashratesRepository.$getWeeklyHashrateTimestamps();
|
|
|
|
const hashrates: any[] = [];
|
2022-03-22 01:20:16 +01:00
|
|
|
const genesisTimestamp = 1231006505000; // bitcoin-cli getblock 000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f
|
2022-03-10 11:48:21 +01:00
|
|
|
|
|
|
|
const lastMonday = new Date(now.setDate(now.getDate() - (now.getDay() + 6) % 7));
|
|
|
|
const lastMondayMidnight = this.getDateMidnight(lastMonday);
|
2022-03-22 01:20:16 +01:00
|
|
|
let toTimestamp = lastMondayMidnight.getTime();
|
2022-03-06 12:32:16 +01:00
|
|
|
|
2022-03-06 12:36:50 +01:00
|
|
|
const totalWeekIndexed = (await BlocksRepository.$blockCount(null, null)) / 1008;
|
|
|
|
let indexedThisRun = 0;
|
|
|
|
let totalIndexed = 0;
|
2022-04-13 09:29:52 +02:00
|
|
|
let newlyIndexed = 0;
|
2022-05-10 14:52:37 +02:00
|
|
|
const startedAt = new Date().getTime() / 1000;
|
|
|
|
let timer = new Date().getTime() / 1000;
|
|
|
|
|
2022-05-19 16:40:38 +02:00
|
|
|
logger.debug(`Indexing weekly mining pool hashrate`);
|
2022-05-10 14:52:37 +02:00
|
|
|
loadingIndicators.setProgress('weekly-hashrate-indexing', 0);
|
2022-03-06 12:36:50 +01:00
|
|
|
|
2022-03-06 12:32:16 +01:00
|
|
|
while (toTimestamp > genesisTimestamp) {
|
2022-03-21 13:19:12 +01:00
|
|
|
const fromTimestamp = toTimestamp - 604800000;
|
2022-03-06 12:32:16 +01:00
|
|
|
|
|
|
|
// Skip already indexed weeks
|
2022-03-21 13:19:12 +01:00
|
|
|
if (indexedTimestamp.includes(toTimestamp / 1000)) {
|
|
|
|
toTimestamp -= 604800000;
|
2022-03-06 12:36:50 +01:00
|
|
|
++totalIndexed;
|
2022-03-06 12:32:16 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-03-21 12:32:57 +01:00
|
|
|
// Check if we have blocks for the previous week (which mean that the week
|
|
|
|
// we are currently indexing has complete data)
|
|
|
|
const blockStatsPreviousWeek: any = await BlocksRepository.$blockCountBetweenTimestamp(
|
2022-03-22 01:20:16 +01:00
|
|
|
null, (fromTimestamp - 604800000) / 1000, (toTimestamp - 604800000) / 1000);
|
2022-03-21 12:32:57 +01:00
|
|
|
if (blockStatsPreviousWeek.blockCount === 0) { // We are done indexing
|
2022-03-06 12:32:16 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-03-21 12:32:57 +01:00
|
|
|
const blockStats: any = await BlocksRepository.$blockCountBetweenTimestamp(
|
2022-03-21 13:19:12 +01:00
|
|
|
null, fromTimestamp / 1000, toTimestamp / 1000);
|
2022-03-06 12:32:16 +01:00
|
|
|
const lastBlockHashrate = await bitcoinClient.getNetworkHashPs(blockStats.blockCount,
|
|
|
|
blockStats.lastBlockHeight);
|
|
|
|
|
2022-03-21 13:19:12 +01:00
|
|
|
let pools = await PoolsRepository.$getPoolsInfoBetween(fromTimestamp / 1000, toTimestamp / 1000);
|
2022-03-06 12:32:16 +01:00
|
|
|
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({
|
2022-03-21 13:19:12 +01:00
|
|
|
hashrateTimestamp: toTimestamp / 1000,
|
2022-03-06 12:32:16 +01:00
|
|
|
avgHashrate: pool['hashrate'],
|
|
|
|
poolId: pool.poolId,
|
|
|
|
share: pool['share'],
|
|
|
|
type: 'weekly',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-04-13 09:29:52 +02:00
|
|
|
newlyIndexed += hashrates.length;
|
2022-03-06 12:32:16 +01:00
|
|
|
await HashratesRepository.$saveHashrates(hashrates);
|
|
|
|
hashrates.length = 0;
|
|
|
|
|
2022-05-10 14:52:37 +02:00
|
|
|
const elapsedSeconds = Math.max(1, Math.round((new Date().getTime() / 1000) - timer));
|
2022-03-10 11:48:21 +01:00
|
|
|
if (elapsedSeconds > 1) {
|
2022-05-10 14:52:37 +02:00
|
|
|
const runningFor = Math.max(1, Math.round((new Date().getTime() / 1000) - startedAt));
|
|
|
|
const weeksPerSeconds = Math.max(1, Math.round(indexedThisRun / elapsedSeconds));
|
|
|
|
const progress = Math.round(totalIndexed / totalWeekIndexed * 10000) / 100;
|
|
|
|
const timeLeft = Math.round((totalWeekIndexed - totalIndexed) / weeksPerSeconds);
|
2022-03-21 13:19:12 +01:00
|
|
|
const formattedDate = new Date(fromTimestamp).toUTCString();
|
2022-05-10 14:52:37 +02:00
|
|
|
logger.debug(`Getting weekly pool hashrate for ${formattedDate} | ~${weeksPerSeconds.toFixed(2)} weeks/sec | total: ~${totalIndexed}/${Math.round(totalWeekIndexed)} (${progress}%) | elapsed: ${runningFor} seconds | left: ~${timeLeft} seconds`);
|
|
|
|
timer = new Date().getTime() / 1000;
|
2022-03-06 12:36:50 +01:00
|
|
|
indexedThisRun = 0;
|
2022-05-10 14:52:37 +02:00
|
|
|
loadingIndicators.setProgress('weekly-hashrate-indexing', progress, false);
|
2022-03-06 12:36:50 +01:00
|
|
|
}
|
|
|
|
|
2022-03-21 13:19:12 +01:00
|
|
|
toTimestamp -= 604800000;
|
2022-03-06 12:36:50 +01:00
|
|
|
++indexedThisRun;
|
|
|
|
++totalIndexed;
|
2022-03-06 12:32:16 +01:00
|
|
|
}
|
2022-04-30 10:54:49 +02:00
|
|
|
await HashratesRepository.$setLatestRun('last_weekly_hashrates_indexing', new Date().getUTCDate());
|
2022-04-13 09:29:52 +02:00
|
|
|
if (newlyIndexed > 0) {
|
|
|
|
logger.info(`Indexed ${newlyIndexed} pools weekly hashrate`);
|
|
|
|
}
|
2022-05-10 14:52:37 +02:00
|
|
|
loadingIndicators.setProgress('weekly-hashrate-indexing', 100);
|
2022-03-06 12:32:16 +01:00
|
|
|
} catch (e) {
|
2022-05-10 14:52:37 +02:00
|
|
|
loadingIndicators.setProgress('weekly-hashrate-indexing', 100);
|
2022-03-06 12:32:16 +01:00
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-03-06 12:47:16 +01:00
|
|
|
* [INDEXING] Generate daily hashrate data
|
2022-03-06 12:32:16 +01:00
|
|
|
*/
|
|
|
|
public async $generateNetworkHashrateHistory(): Promise<void> {
|
2022-04-02 16:44:07 +02:00
|
|
|
try {
|
2022-04-30 10:54:49 +02:00
|
|
|
// We only run this once a day around midnight
|
|
|
|
const latestRunDate = await HashratesRepository.$getLatestRun('last_hashrates_indexing');
|
|
|
|
const now = new Date().getUTCDate();
|
|
|
|
if (now === latestRunDate) {
|
2022-04-02 16:44:07 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
throw e;
|
2022-03-06 12:32:16 +01:00
|
|
|
}
|
|
|
|
|
2022-03-05 16:23:01 +01:00
|
|
|
try {
|
|
|
|
const indexedTimestamp = (await HashratesRepository.$getNetworkDailyHashrate(null)).map(hashrate => hashrate.timestamp);
|
2022-03-22 01:20:16 +01:00
|
|
|
const genesisTimestamp = 1231006505000; // bitcoin-cli getblock 000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f
|
2022-03-06 12:32:16 +01:00
|
|
|
const lastMidnight = this.getDateMidnight(new Date());
|
2022-03-21 13:19:12 +01:00
|
|
|
let toTimestamp = Math.round(lastMidnight.getTime());
|
2022-03-06 12:36:50 +01:00
|
|
|
const hashrates: any[] = [];
|
|
|
|
|
|
|
|
const totalDayIndexed = (await BlocksRepository.$blockCount(null, null)) / 144;
|
2022-03-05 16:23:01 +01:00
|
|
|
let indexedThisRun = 0;
|
|
|
|
let totalIndexed = 0;
|
2022-04-13 09:29:52 +02:00
|
|
|
let newlyIndexed = 0;
|
2022-05-10 14:52:37 +02:00
|
|
|
const startedAt = new Date().getTime() / 1000;
|
|
|
|
let timer = new Date().getTime() / 1000;
|
|
|
|
|
2022-05-19 16:40:38 +02:00
|
|
|
logger.debug(`Indexing daily network hashrate`);
|
2022-05-10 14:52:37 +02:00
|
|
|
loadingIndicators.setProgress('daily-hashrate-indexing', 0);
|
2022-03-05 16:23:01 +01:00
|
|
|
|
|
|
|
while (toTimestamp > genesisTimestamp) {
|
2022-03-21 13:19:12 +01:00
|
|
|
const fromTimestamp = toTimestamp - 86400000;
|
2022-03-06 12:32:16 +01:00
|
|
|
|
|
|
|
// Skip already indexed weeks
|
2022-03-21 13:19:12 +01:00
|
|
|
if (indexedTimestamp.includes(toTimestamp / 1000)) {
|
|
|
|
toTimestamp -= 86400000;
|
2022-03-05 16:23:01 +01:00
|
|
|
++totalIndexed;
|
|
|
|
continue;
|
|
|
|
}
|
2022-02-19 12:45:02 +01:00
|
|
|
|
2022-03-21 12:32:57 +01:00
|
|
|
// Check if we have blocks for the previous day (which mean that the day
|
|
|
|
// we are currently indexing has complete data)
|
|
|
|
const blockStatsPreviousDay: any = await BlocksRepository.$blockCountBetweenTimestamp(
|
2022-03-22 01:20:16 +01:00
|
|
|
null, (fromTimestamp - 86400000) / 1000, (toTimestamp - 86400000) / 1000);
|
2022-03-21 12:32:57 +01:00
|
|
|
if (blockStatsPreviousDay.blockCount === 0) { // We are done indexing
|
2022-03-05 16:23:01 +01:00
|
|
|
break;
|
|
|
|
}
|
2022-02-21 09:34:07 +01:00
|
|
|
|
2022-03-21 12:32:57 +01:00
|
|
|
const blockStats: any = await BlocksRepository.$blockCountBetweenTimestamp(
|
2022-03-21 13:19:12 +01:00
|
|
|
null, fromTimestamp / 1000, toTimestamp / 1000);
|
2022-03-06 12:32:16 +01:00
|
|
|
const lastBlockHashrate = await bitcoinClient.getNetworkHashPs(blockStats.blockCount,
|
2022-03-05 16:23:01 +01:00
|
|
|
blockStats.lastBlockHeight);
|
|
|
|
|
|
|
|
hashrates.push({
|
2022-03-21 13:19:12 +01:00
|
|
|
hashrateTimestamp: toTimestamp / 1000,
|
2022-03-05 16:23:01 +01:00
|
|
|
avgHashrate: lastBlockHashrate,
|
2022-03-12 14:47:33 +01:00
|
|
|
poolId: 0,
|
2022-03-05 16:23:01 +01:00
|
|
|
share: 1,
|
|
|
|
type: 'daily',
|
2022-02-24 08:55:18 +01:00
|
|
|
});
|
2022-03-05 16:23:01 +01:00
|
|
|
|
|
|
|
if (hashrates.length > 10) {
|
2022-04-13 09:29:52 +02:00
|
|
|
newlyIndexed += hashrates.length;
|
2022-03-05 16:23:01 +01:00
|
|
|
await HashratesRepository.$saveHashrates(hashrates);
|
|
|
|
hashrates.length = 0;
|
2022-02-24 08:55:18 +01:00
|
|
|
}
|
2022-02-19 12:45:02 +01:00
|
|
|
|
2022-05-10 14:52:37 +02:00
|
|
|
const elapsedSeconds = Math.max(1, Math.round((new Date().getTime() / 1000) - timer));
|
2022-03-10 11:48:21 +01:00
|
|
|
if (elapsedSeconds > 1) {
|
2022-05-10 14:52:37 +02:00
|
|
|
const runningFor = Math.max(1, Math.round((new Date().getTime() / 1000) - startedAt));
|
|
|
|
const daysPerSeconds = Math.max(1, Math.round(indexedThisRun / elapsedSeconds));
|
|
|
|
const progress = Math.round(totalIndexed / totalDayIndexed * 10000) / 100;
|
|
|
|
const timeLeft = Math.round((totalDayIndexed - totalIndexed) / daysPerSeconds);
|
2022-03-21 13:19:12 +01:00
|
|
|
const formattedDate = new Date(fromTimestamp).toUTCString();
|
2022-05-10 14:52:37 +02:00
|
|
|
logger.debug(`Getting network daily hashrate for ${formattedDate} | ~${daysPerSeconds.toFixed(2)} days/sec | total: ~${totalIndexed}/${Math.round(totalDayIndexed)} (${progress}%) | elapsed: ${runningFor} seconds | left: ~${timeLeft} seconds`);
|
|
|
|
timer = new Date().getTime() / 1000;
|
2022-03-05 16:23:01 +01:00
|
|
|
indexedThisRun = 0;
|
2022-05-10 14:52:37 +02:00
|
|
|
loadingIndicators.setProgress('daily-hashrate-indexing', progress);
|
2022-03-05 16:23:01 +01:00
|
|
|
}
|
2022-02-19 12:45:02 +01:00
|
|
|
|
2022-03-21 13:19:12 +01:00
|
|
|
toTimestamp -= 86400000;
|
2022-03-05 16:23:01 +01:00
|
|
|
++indexedThisRun;
|
|
|
|
++totalIndexed;
|
2022-02-21 09:34:07 +01:00
|
|
|
}
|
|
|
|
|
2022-03-05 16:23:01 +01:00
|
|
|
// Add genesis block manually
|
|
|
|
if (toTimestamp <= genesisTimestamp && !indexedTimestamp.includes(genesisTimestamp)) {
|
|
|
|
hashrates.push({
|
|
|
|
hashrateTimestamp: genesisTimestamp,
|
|
|
|
avgHashrate: await bitcoinClient.getNetworkHashPs(1, 1),
|
|
|
|
poolId: null,
|
|
|
|
type: 'daily',
|
|
|
|
});
|
2022-02-24 08:55:18 +01:00
|
|
|
}
|
|
|
|
|
2022-04-13 09:29:52 +02:00
|
|
|
newlyIndexed += hashrates.length;
|
2022-03-06 12:32:16 +01:00
|
|
|
await HashratesRepository.$saveHashrates(hashrates);
|
2022-02-22 14:53:47 +01:00
|
|
|
|
2022-04-30 10:54:49 +02:00
|
|
|
await HashratesRepository.$setLatestRun('last_hashrates_indexing', new Date().getUTCDate());
|
2022-04-13 09:29:52 +02:00
|
|
|
if (newlyIndexed > 0) {
|
|
|
|
logger.info(`Indexed ${newlyIndexed} day of network hashrate`);
|
|
|
|
}
|
2022-05-10 14:52:37 +02:00
|
|
|
loadingIndicators.setProgress('daily-hashrate-indexing', 100);
|
2022-03-05 16:23:01 +01:00
|
|
|
} catch (e) {
|
2022-05-10 14:52:37 +02:00
|
|
|
loadingIndicators.setProgress('daily-hashrate-indexing', 100);
|
2022-03-05 16:23:01 +01:00
|
|
|
throw e;
|
2022-02-22 14:53:47 +01:00
|
|
|
}
|
2022-02-19 12:45:02 +01:00
|
|
|
}
|
2022-03-06 12:32:16 +01:00
|
|
|
|
|
|
|
private getDateMidnight(date: Date): Date {
|
|
|
|
date.setUTCHours(0);
|
|
|
|
date.setUTCMinutes(0);
|
|
|
|
date.setUTCSeconds(0);
|
|
|
|
date.setUTCMilliseconds(0);
|
|
|
|
|
|
|
|
return date;
|
|
|
|
}
|
2022-04-11 13:57:13 +02:00
|
|
|
|
|
|
|
private getTimeRange(interval: string | null): number {
|
|
|
|
switch (interval) {
|
|
|
|
case '3y': return 43200; // 12h
|
|
|
|
case '2y': return 28800; // 8h
|
|
|
|
case '1y': return 28800; // 8h
|
|
|
|
case '6m': return 10800; // 3h
|
|
|
|
case '3m': return 7200; // 2h
|
|
|
|
case '1m': return 1800; // 30min
|
|
|
|
case '1w': return 300; // 5min
|
|
|
|
case '3d': return 1;
|
|
|
|
case '24h': return 1;
|
|
|
|
default: return 86400; // 24h
|
|
|
|
}
|
|
|
|
}
|
2022-01-06 11:59:33 +01:00
|
|
|
}
|
|
|
|
|
2022-01-25 10:33:46 +01:00
|
|
|
export default new Mining();
|