From 9e4e7cea5da11085f016f34ba621d24d758d7d82 Mon Sep 17 00:00:00 2001 From: boray Date: Fri, 10 Jan 2025 12:39:30 +0300 Subject: [PATCH] fix: schema and sql query --- schema.graphql | 2 +- src/db/sql/events-actions/queries.ts | 24 ++++++++++---------- src/services/blocks-service/block-service.ts | 4 ++-- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/schema.graphql b/schema.graphql index 8feab78..8d6f849 100644 --- a/schema.graphql +++ b/schema.graphql @@ -83,5 +83,5 @@ type ActionOutput { type Query { events(input: EventFilterOptionsInput!): [EventOutput]! actions(input: ActionFilterOptionsInput!): [ActionOutput]! - maxBlockHeightInfo: MaxBlockHeightInfo! + block: MaxBlockHeightInfo! } diff --git a/src/db/sql/events-actions/queries.ts b/src/db/sql/events-actions/queries.ts index 4df195e..dec7814 100644 --- a/src/db/sql/events-actions/queries.ts +++ b/src/db/sql/events-actions/queries.ts @@ -373,19 +373,19 @@ export function getActionsQuery( export function getBlockQuery(db_client: postgres.Sql) { return db_client` - WITH max_height AS ( - SELECT MAX(height) AS max_height +WITH max_heights AS ( + SELECT + chain_status, + MAX(height) AS max_height FROM blocks - ) - SELECT * - FROM blocks - WHERE height = (SELECT max_height FROM max_height) - AND chain_status = 'canonical' - UNION - SELECT * - FROM blocks - WHERE height = (SELECT max_height FROM max_height) - AND chain_status = 'pending' + WHERE chain_status IN ('canonical', 'pending') + GROUP BY chain_status +) +SELECT b.* +FROM blocks b +JOIN max_heights mh + ON b.chain_status = mh.chain_status + AND b.height = mh.max_height; `; } diff --git a/src/services/blocks-service/block-service.ts b/src/services/blocks-service/block-service.ts index dce79fb..5a3adef 100644 --- a/src/services/blocks-service/block-service.ts +++ b/src/services/blocks-service/block-service.ts @@ -34,10 +34,10 @@ class BlockService implements IBlockService { const processingSpan = tracingState.startSpan('block.processing'); const blockData = { canonicalMaxBlockHeight: Number( - rows.filter((row) => row.chain_status === 'canonical')[0] + rows.filter((row) => row.chain_status === 'canonical')[0].height ), pendingMaxBlockHeight: Number( - rows.filter((row) => row.chain_status === 'pending')[0] + rows.filter((row) => row.chain_status === 'pending')[0].height ), }; processingSpan.end();