From e627122239fa668a790795541f280c3858462e75 Mon Sep 17 00:00:00 2001 From: Mononaut Date: Fri, 25 Nov 2022 19:32:50 +0900 Subject: [PATCH] move block audit endpoint from mining to bitcoin routes --- backend/src/api/bitcoin/bitcoin.routes.ts | 15 +++++++++++++-- backend/src/api/blocks.ts | 13 +++++++++++++ .../src/repositories/BlocksAuditsRepository.ts | 6 +----- .../src/app/components/block/block.component.ts | 8 ++------ frontend/src/app/services/api.service.ts | 2 +- 5 files changed, 30 insertions(+), 14 deletions(-) diff --git a/backend/src/api/bitcoin/bitcoin.routes.ts b/backend/src/api/bitcoin/bitcoin.routes.ts index 3740cccd4..cdcc589fd 100644 --- a/backend/src/api/bitcoin/bitcoin.routes.ts +++ b/backend/src/api/bitcoin/bitcoin.routes.ts @@ -89,6 +89,7 @@ class BitcoinRoutes { .get(config.MEMPOOL.API_URL_PREFIX + 'blocks/:height', this.getBlocks.bind(this)) .get(config.MEMPOOL.API_URL_PREFIX + 'block/:hash', this.getBlock) .get(config.MEMPOOL.API_URL_PREFIX + 'block/:hash/summary', this.getStrippedBlockTransactions) + .get(config.MEMPOOL.API_URL_PREFIX + 'block/:hash/audit-summary', this.getBlockAuditSummary) .post(config.MEMPOOL.API_URL_PREFIX + 'psbt/addparents', this.postPsbtCompletion) ; @@ -324,6 +325,16 @@ class BitcoinRoutes { } } + private async getStrippedBlockTransactions(req: Request, res: Response) { + try { + const transactions = await blocks.$getStrippedBlockTransactions(req.params.hash); + res.setHeader('Expires', new Date(Date.now() + 1000 * 3600 * 24 * 30).toUTCString()); + res.json(transactions); + } catch (e) { + res.status(500).send(e instanceof Error ? e.message : e); + } + } + private async getBlock(req: Request, res: Response) { try { const block = await blocks.$getBlock(req.params.hash); @@ -356,9 +367,9 @@ class BitcoinRoutes { } } - private async getStrippedBlockTransactions(req: Request, res: Response) { + private async getBlockAuditSummary(req: Request, res: Response) { try { - const transactions = await blocks.$getStrippedBlockTransactions(req.params.hash); + const transactions = await blocks.$getBlockAuditSummary(req.params.hash); res.setHeader('Expires', new Date(Date.now() + 1000 * 3600 * 24 * 30).toUTCString()); res.json(transactions); } catch (e) { diff --git a/backend/src/api/blocks.ts b/backend/src/api/blocks.ts index 6f036e533..111d0fa1e 100644 --- a/backend/src/api/blocks.ts +++ b/backend/src/api/blocks.ts @@ -651,6 +651,19 @@ class Blocks { return returnBlocks; } + public async $getBlockAuditSummary(hash: string): Promise { + let summary = await BlocksAuditsRepository.$getBlockAudit(hash); + + // fallback to non-audited transaction summary + if (!summary?.transactions?.length) { + const strippedTransactions = await this.$getStrippedBlockTransactions(hash); + summary = { + transactions: strippedTransactions + }; + } + return summary; + } + public getLastDifficultyAdjustmentTime(): number { return this.lastDifficultyAdjustmentTime; } diff --git a/backend/src/repositories/BlocksAuditsRepository.ts b/backend/src/repositories/BlocksAuditsRepository.ts index 81e68b67f..c6156334b 100644 --- a/backend/src/repositories/BlocksAuditsRepository.ts +++ b/backend/src/repositories/BlocksAuditsRepository.ts @@ -70,11 +70,7 @@ class BlocksAuditRepositories { return rows[0]; } } - // fallback to non-audited transaction summary - const strippedTransactions = await blocks.$getStrippedBlockTransactions(hash); - return { - transactions: strippedTransactions - }; + return null; } catch (e: any) { logger.err(`Cannot fetch block audit from db. Reason: ` + (e instanceof Error ? e.message : e)); throw e; diff --git a/frontend/src/app/components/block/block.component.ts b/frontend/src/app/components/block/block.component.ts index 66dad6d4b..916904375 100644 --- a/frontend/src/app/components/block/block.component.ts +++ b/frontend/src/app/components/block/block.component.ts @@ -210,11 +210,7 @@ export class BlockComponent implements OnInit, OnDestroy { setTimeout(() => { this.nextBlockSubscription = this.apiService.getBlock$(block.previousblockhash).subscribe(); this.nextBlockTxListSubscription = this.electrsApiService.getBlockTransactions$(block.previousblockhash).subscribe(); - if (this.indexingAvailable) { - this.apiService.getBlockAudit$(block.previousblockhash); - } else { - this.nextBlockSummarySubscription = this.apiService.getStrippedBlockTransactions$(block.previousblockhash).subscribe(); - } + this.apiService.getBlockAudit$(block.previousblockhash); }, 100); } @@ -318,7 +314,7 @@ export class BlockComponent implements OnInit, OnDestroy { this.numMissing = 0; this.numUnexpected = 0; - if (blockAudit.template) { + if (blockAudit?.template) { for (const tx of blockAudit.template) { inTemplate[tx.txid] = true; } diff --git a/frontend/src/app/services/api.service.ts b/frontend/src/app/services/api.service.ts index dfed35d72..f813959e3 100644 --- a/frontend/src/app/services/api.service.ts +++ b/frontend/src/app/services/api.service.ts @@ -230,7 +230,7 @@ export class ApiService { getBlockAudit$(hash: string) : Observable { return this.httpClient.get( - this.apiBaseUrl + this.apiBasePath + `/api/v1/mining/blocks/audit/` + hash, { observe: 'response' } + this.apiBaseUrl + this.apiBasePath + `/api/v1/block/${hash}/audit-summary`, { observe: 'response' } ); }