diff --git a/backend/src/api/blocks.ts b/backend/src/api/blocks.ts index b9b7b3aff..526d4889e 100644 --- a/backend/src/api/blocks.ts +++ b/backend/src/api/blocks.ts @@ -426,25 +426,32 @@ class Blocks { return returnBlocks; } + if (currentHeight === 0 && Common.indexingEnabled()) { + currentHeight = await blocksRepository.$mostRecentBlockHeight(); + } + // Check if block height exist in local cache to skip the hash lookup const blockByHeight = this.getBlocks().find((b) => b.height === currentHeight); let startFromHash: string | null = null; if (blockByHeight) { startFromHash = blockByHeight.id; - } else { + } else if (!Common.indexingEnabled()) { startFromHash = await bitcoinApi.$getBlockHash(currentHeight); } let nextHash = startFromHash; for (let i = 0; i < limit && currentHeight >= 0; i++) { 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); - } else if (!block) { + returnBlocks.push(block); + } else if (nextHash != null) { block = prepareBlock(await bitcoinApi.$getBlock(nextHash)); + nextHash = block.previousblockhash; + returnBlocks.push(block); } - returnBlocks.push(block); - nextHash = block.previousblockhash; currentHeight--; } diff --git a/backend/src/api/mining.ts b/backend/src/api/mining.ts index 7e7008351..be786db42 100644 --- a/backend/src/api/mining.ts +++ b/backend/src/api/mining.ts @@ -94,8 +94,13 @@ class Mining { poolsStatistics['blockCount'] = blockCount; 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; } @@ -118,7 +123,12 @@ class Mining { const blockCount1w: number = await BlocksRepository.$blockCount(pool.id, '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 { pool: pool, @@ -132,7 +142,7 @@ class Mining { '24h': blockCount24h / totalBlock24h, '1w': blockCount1w / totalBlock1w, }, - estimatedHashrate: currentEstimatedkHashrate * (blockCount24h / totalBlock24h), + estimatedHashrate: currentEstimatedHashrate * (blockCount24h / totalBlock24h), reportedHashrate: null, }; } diff --git a/backend/src/repositories/BlocksRepository.ts b/backend/src/repositories/BlocksRepository.ts index dcd5b48d1..4fd5511af 100644 --- a/backend/src/repositories/BlocksRepository.ts +++ b/backend/src/repositories/BlocksRepository.ts @@ -121,6 +121,19 @@ class BlocksRepository { } } + /** + * Return most recent block height + */ + public async $mostRecentBlockHeight(): Promise { + 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 */ diff --git a/backend/src/routes.ts b/backend/src/routes.ts index ef8dd47e5..91c41faa6 100644 --- a/backend/src/routes.ts +++ b/backend/src/routes.ts @@ -619,6 +619,14 @@ class Routes { } 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 { const hashrates = await HashratesRepository.$getNetworkDailyHashrate(req.params.interval); const difficulty = await BlocksRepository.$getBlocksDifficulty(req.params.interval); @@ -630,8 +638,8 @@ class Routes { res.json({ hashrates: hashrates, difficulty: difficulty, - currentHashrate: await bitcoinClient.getNetworkHashPs(), - currentDifficulty: await bitcoinClient.getDifficulty(), + currentHashrate: currentHashrate, + currentDifficulty: currentDifficulty, }); } catch (e) { res.status(500).send(e instanceof Error ? e.message : e);