diff --git a/backend/src/api/audit.ts b/backend/src/api/audit.ts index e520cf470..6aafc9ded 100644 --- a/backend/src/api/audit.ts +++ b/backend/src/api/audit.ts @@ -133,45 +133,6 @@ class Audit { score }; } - - public async $getBlockAuditScores(fromHeight?: number, limit: number = 15): Promise { - let currentHeight = fromHeight !== undefined ? fromHeight : await blocksRepository.$mostRecentBlockHeight(); - const returnScores: AuditScore[] = []; - - if (currentHeight < 0) { - return returnScores; - } - - for (let i = 0; i < limit && currentHeight >= 0; i++) { - const block = blocks.getBlocks().find((b) => b.height === currentHeight); - if (block?.extras?.matchRate != null) { - returnScores.push({ - hash: block.id, - matchRate: block.extras.matchRate - }); - } else { - let currentHash; - if (!currentHash && Common.indexingEnabled()) { - const dbBlock = await blocksRepository.$getBlockByHeight(currentHeight); - if (dbBlock && dbBlock['id']) { - currentHash = dbBlock['id']; - } - } - if (!currentHash) { - currentHash = await bitcoinApi.$getBlockHash(currentHeight); - } - if (currentHash) { - const auditScore = await blocksAuditsRepository.$getBlockAuditScore(currentHash); - returnScores.push({ - hash: currentHash, - matchRate: auditScore?.matchRate - }); - } - } - currentHeight--; - } - return returnScores; - } } export default new Audit(); \ No newline at end of file diff --git a/backend/src/api/mining/mining-routes.ts b/backend/src/api/mining/mining-routes.ts index 73d38d841..81c7b5a99 100644 --- a/backend/src/api/mining/mining-routes.ts +++ b/backend/src/api/mining/mining-routes.ts @@ -283,9 +283,12 @@ class MiningRoutes { private async $getBlockAuditScores(req: Request, res: Response) { try { - const height = req.params.height === undefined ? undefined : parseInt(req.params.height, 10); + let height = req.params.height === undefined ? undefined : parseInt(req.params.height, 10); + if (height == null) { + height = await BlocksRepository.$mostRecentBlockHeight(); + } res.setHeader('Expires', new Date(Date.now() + 1000 * 60).toUTCString()); - res.json(await audits.$getBlockAuditScores(height, 15)); + res.json(await BlocksAuditsRepository.$getBlockAuditScores(height, height - 15)); } catch (e) { res.status(500).send(e instanceof Error ? e.message : e); } diff --git a/backend/src/repositories/BlocksAuditsRepository.ts b/backend/src/repositories/BlocksAuditsRepository.ts index 6f914ca0b..831a33e3d 100644 --- a/backend/src/repositories/BlocksAuditsRepository.ts +++ b/backend/src/repositories/BlocksAuditsRepository.ts @@ -93,6 +93,20 @@ class BlocksAuditRepositories { throw e; } } + + public async $getBlockAuditScores(maxHeight: number, minHeight: number): Promise { + try { + const [rows]: any[] = await DB.query( + `SELECT hash, match_rate as matchRate + FROM blocks_audits + WHERE blocks_audits.height BETWEEN ? AND ? + `, [minHeight, maxHeight]); + return rows; + } catch (e: any) { + logger.err(`Cannot fetch block audit from db. Reason: ` + (e instanceof Error ? e.message : e)); + throw e; + } + } } export default new BlocksAuditRepositories(); diff --git a/frontend/src/app/components/blocks-list/blocks-list.component.html b/frontend/src/app/components/blocks-list/blocks-list.component.html index 7cc55a815..628efb51b 100644 --- a/frontend/src/app/components/blocks-list/blocks-list.component.html +++ b/frontend/src/app/components/blocks-list/blocks-list.component.html @@ -52,8 +52,7 @@ [ngStyle]="{'width': (100 - (auditScores[block.id] || 0)) + '%' }">
{{ auditScores[block.id] }}% - - ~ + ~