Merge pull request #1716 from mempool/nymkappa/feature/bitcoind-offline-support

Mining dashboard still runs fine if Bitcoin Core becomes unavailable
This commit is contained in:
wiz 2022-05-31 17:09:08 +09:00 committed by GitHub
commit 768be0cc70
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 49 additions and 11 deletions

View File

@ -426,25 +426,32 @@ class Blocks {
return returnBlocks; return returnBlocks;
} }
if (currentHeight === 0 && Common.indexingEnabled()) {
currentHeight = await blocksRepository.$mostRecentBlockHeight();
}
// Check if block height exist in local cache to skip the hash lookup // Check if block height exist in local cache to skip the hash lookup
const blockByHeight = this.getBlocks().find((b) => b.height === currentHeight); const blockByHeight = this.getBlocks().find((b) => b.height === currentHeight);
let startFromHash: string | null = null; let startFromHash: string | null = null;
if (blockByHeight) { if (blockByHeight) {
startFromHash = blockByHeight.id; startFromHash = blockByHeight.id;
} else { } else if (!Common.indexingEnabled()) {
startFromHash = await bitcoinApi.$getBlockHash(currentHeight); startFromHash = await bitcoinApi.$getBlockHash(currentHeight);
} }
let nextHash = startFromHash; let nextHash = startFromHash;
for (let i = 0; i < limit && currentHeight >= 0; i++) { for (let i = 0; i < limit && currentHeight >= 0; i++) {
let block = this.getBlocks().find((b) => b.height === currentHeight); let block = this.getBlocks().find((b) => b.height === currentHeight);
if (!block && Common.indexingEnabled()) { if (block) {
returnBlocks.push(block);
} else if (Common.indexingEnabled()) {
block = await this.$indexBlock(currentHeight); block = await this.$indexBlock(currentHeight);
} else if (!block) { returnBlocks.push(block);
} else if (nextHash != null) {
block = prepareBlock(await bitcoinApi.$getBlock(nextHash)); block = prepareBlock(await bitcoinApi.$getBlock(nextHash));
nextHash = block.previousblockhash;
returnBlocks.push(block);
} }
returnBlocks.push(block);
nextHash = block.previousblockhash;
currentHeight--; currentHeight--;
} }

View File

@ -94,8 +94,13 @@ class Mining {
poolsStatistics['blockCount'] = blockCount; poolsStatistics['blockCount'] = blockCount;
const totalBlock24h: number = await BlocksRepository.$blockCount(null, '24h'); const totalBlock24h: number = await BlocksRepository.$blockCount(null, '24h');
const lastBlockHashrate = await bitcoinClient.getNetworkHashPs(totalBlock24h);
poolsStatistics['lastEstimatedHashrate'] = lastBlockHashrate; try {
poolsStatistics['lastEstimatedHashrate'] = await bitcoinClient.getNetworkHashPs(totalBlock24h);
} catch (e) {
poolsStatistics['lastEstimatedHashrate'] = 0;
logger.debug('Bitcoin Core is not available, using zeroed value for current hashrate');
}
return poolsStatistics; return poolsStatistics;
} }
@ -118,7 +123,12 @@ class Mining {
const blockCount1w: number = await BlocksRepository.$blockCount(pool.id, '1w'); const blockCount1w: number = await BlocksRepository.$blockCount(pool.id, '1w');
const totalBlock1w: number = await BlocksRepository.$blockCount(null, '1w'); const totalBlock1w: number = await BlocksRepository.$blockCount(null, '1w');
const currentEstimatedkHashrate = await bitcoinClient.getNetworkHashPs(totalBlock24h); let currentEstimatedHashrate = 0;
try {
currentEstimatedHashrate = await bitcoinClient.getNetworkHashPs(totalBlock24h);
} catch (e) {
logger.debug('Bitcoin Core is not available, using zeroed value for current hashrate');
}
return { return {
pool: pool, pool: pool,
@ -132,7 +142,7 @@ class Mining {
'24h': blockCount24h / totalBlock24h, '24h': blockCount24h / totalBlock24h,
'1w': blockCount1w / totalBlock1w, '1w': blockCount1w / totalBlock1w,
}, },
estimatedHashrate: currentEstimatedkHashrate * (blockCount24h / totalBlock24h), estimatedHashrate: currentEstimatedHashrate * (blockCount24h / totalBlock24h),
reportedHashrate: null, reportedHashrate: null,
}; };
} }

View File

@ -121,6 +121,19 @@ class BlocksRepository {
} }
} }
/**
* Return most recent block height
*/
public async $mostRecentBlockHeight(): Promise<number> {
try {
const [row] = await DB.query('SELECT MAX(height) as maxHeight from blocks');
return row[0]['maxHeight'];
} catch (e) {
logger.err(`Cannot count blocks for this pool (using offset). Reason: ` + (e instanceof Error ? e.message : e));
throw e;
}
}
/** /**
* Get blocks count for a period * Get blocks count for a period
*/ */

View File

@ -619,6 +619,14 @@ class Routes {
} }
public async $getHistoricalHashrate(req: Request, res: Response) { public async $getHistoricalHashrate(req: Request, res: Response) {
let currentHashrate = 0, currentDifficulty = 0;
try {
currentHashrate = await bitcoinClient.getNetworkHashPs();
currentDifficulty = await bitcoinClient.getDifficulty();
} catch (e) {
logger.debug('Bitcoin Core is not available, using zeroed value for current hashrate and difficulty');
}
try { try {
const hashrates = await HashratesRepository.$getNetworkDailyHashrate(req.params.interval); const hashrates = await HashratesRepository.$getNetworkDailyHashrate(req.params.interval);
const difficulty = await BlocksRepository.$getBlocksDifficulty(req.params.interval); const difficulty = await BlocksRepository.$getBlocksDifficulty(req.params.interval);
@ -630,8 +638,8 @@ class Routes {
res.json({ res.json({
hashrates: hashrates, hashrates: hashrates,
difficulty: difficulty, difficulty: difficulty,
currentHashrate: await bitcoinClient.getNetworkHashPs(), currentHashrate: currentHashrate,
currentDifficulty: await bitcoinClient.getDifficulty(), currentDifficulty: currentDifficulty,
}); });
} catch (e) { } catch (e) {
res.status(500).send(e instanceof Error ? e.message : e); res.status(500).send(e instanceof Error ? e.message : e);