From 4ff2aad94aeb7edef637cb76cf54f29254db91a3 Mon Sep 17 00:00:00 2001 From: nymkappa <1612910616@pm.me> Date: Fri, 29 Nov 2024 17:55:06 +0100 Subject: [PATCH] [blocks] return list of block hash filtered by definition hash --- backend/src/api/bitcoin/bitcoin.routes.ts | 28 +++++++++++++++++++---- backend/src/api/blocks.ts | 27 ++++++++++++++++++---- 2 files changed, 46 insertions(+), 9 deletions(-) diff --git a/backend/src/api/bitcoin/bitcoin.routes.ts b/backend/src/api/bitcoin/bitcoin.routes.ts index c95d89d0f..eb5cca6f8 100644 --- a/backend/src/api/bitcoin/bitcoin.routes.ts +++ b/backend/src/api/bitcoin/bitcoin.routes.ts @@ -47,13 +47,15 @@ class BitcoinRoutes { .get(config.MEMPOOL.API_URL_PREFIX + 'block/:hash/audit-summary', this.getBlockAuditSummary) .get(config.MEMPOOL.API_URL_PREFIX + 'block/:hash/tx/:txid/audit', this.$getBlockTxAuditSummary) .get(config.MEMPOOL.API_URL_PREFIX + 'blocks/tip/height', this.getBlockTipHeight) - .get(config.MEMPOOL.API_URL_PREFIX + 'blocks/definition/list', this.getBlockDefinitionHashes) - .get(config.MEMPOOL.API_URL_PREFIX + 'blocks/definition/current', this.getCurrentBlockDefinitionHash) .post(config.MEMPOOL.API_URL_PREFIX + 'psbt/addparents', this.postPsbtCompletion) .get(config.MEMPOOL.API_URL_PREFIX + 'blocks-bulk/:from', this.getBlocksByBulk.bind(this)) .get(config.MEMPOOL.API_URL_PREFIX + 'blocks-bulk/:from/:to', this.getBlocksByBulk.bind(this)) // Temporarily add txs/package endpoint for all backends until esplora supports it .post(config.MEMPOOL.API_URL_PREFIX + 'txs/package', this.$submitPackage) + // Internal routes + .get(config.MEMPOOL.API_URL_PREFIX + 'internal/blocks/definition/list', this.getBlockDefinitionHashes) + .get(config.MEMPOOL.API_URL_PREFIX + 'internal/blocks/definition/current', this.getCurrentBlockDefinitionHash) + .get(config.MEMPOOL.API_URL_PREFIX + 'internal/blocks/:definitionHash', this.getBlocksByDefinitionHash) ; if (config.MEMPOOL.BACKEND !== 'esplora') { @@ -660,7 +662,7 @@ class BitcoinRoutes { } } - private async getBlockDefinitionHashes(req: Request, res: Response) { + private async getBlockDefinitionHashes(req: Request, res: Response): Promise { try { const result = await blocks.$getBlockDefinitionHashes(); if (!result) { @@ -674,7 +676,7 @@ class BitcoinRoutes { } } - private async getCurrentBlockDefinitionHash(req: Request, res: Response) { + private async getCurrentBlockDefinitionHash(req: Request, res: Response): Promise { try { const currentSha = await poolsUpdater.getShaFromDb(); if (!currentSha) { @@ -688,6 +690,24 @@ class BitcoinRoutes { } } + private async getBlocksByDefinitionHash(req: Request, res: Response): Promise { + try { + if (typeof(req.params.definitionHash) !== 'string') { + res.status(400).send('Parameter "hash" must be a valid string'); + return; + } + const blocksHash = await blocks.$getBlocksByDefinitionHash(req.params.definitionHash as string); + if (!blocksHash) { + handleError(req, res, 503, `Service Temporarily Unavailable`); + return; + } + res.setHeader('content-type', 'application/json'); + res.send(blocksHash); + } catch (e) { + handleError(req, res, 500, e instanceof Error ? e.message : e); + } + } + private getBlockTipHeight(req: Request, res: Response) { try { const result = blocks.getCurrentBlockHeight(); diff --git a/backend/src/api/blocks.ts b/backend/src/api/blocks.ts index 2b468a8f8..102601594 100644 --- a/backend/src/api/blocks.ts +++ b/backend/src/api/blocks.ts @@ -1463,17 +1463,34 @@ class Blocks { } } - public async $getBlockDefinitionHashes(): Promise { + public async $getBlockDefinitionHashes(): Promise { try { const [rows]: any = await database.query(`SELECT DISTINCT(definition_hash) FROM blocks`); - if (rows && rows.length) { + if (rows && Array.isArray(rows)) { return rows.map(r => r.definition_hash); + } else { + logger.debug(`Unable to retreive list of blocks.definition_hash from db (no result)`); + return null; } } catch (e) { - // we just return an empty array + logger.debug(`Unable to retreive list of blocks.definition_hash from db (exception: ${e})`); + return null; + } + } + + public async $getBlocksByDefinitionHash(definitionHash: string): Promise { + try { + const [rows]: any = await database.query(`SELECT hash FROM blocks WHERE definition_hash = ?`, [definitionHash]); + if (rows && Array.isArray(rows)) { + return rows.map(r => r.hash); + } else { + logger.debug(`Unable to retreive list of blocks for definition hash ${definitionHash} from db (no result)`); + return null; + } + } catch (e) { + logger.debug(`Unable to retreive list of blocks for definition hash ${definitionHash} from db (exception: ${e})`); + return null; } - logger.debug(`Unable to retreive list of blocks.definition_hash from db`); - return []; } }