From 32d5cd10d0448c9f30d72aa11d58cad5b5317cc3 Mon Sep 17 00:00:00 2001 From: prathamesh0 <42446521+prathamesh0@users.noreply.github.com> Date: Fri, 10 Nov 2023 09:39:58 +0530 Subject: [PATCH] Prioritize block hash over number in singular time travel queries (#462) * Keep return value for singular subgraph entity queries nullable * Prioritize block hash over number in time travel queries * Throw error when block with given hash doesn't exist in db --- packages/codegen/src/schema.ts | 2 +- packages/util/src/database.ts | 10 +++++++++- packages/util/src/graph/database.ts | 10 +++------- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/packages/codegen/src/schema.ts b/packages/codegen/src/schema.ts index 88a6f146..62f3178d 100644 --- a/packages/codegen/src/schema.ts +++ b/packages/codegen/src/schema.ts @@ -193,7 +193,7 @@ export class Schema { const queryObject: { [key: string]: any; } = {}; queryObject[queryName] = { // Get type composer object for return type from the schema composer. - type: this._composer.getAnyTC(subgraphType).NonNull, + type: this._composer.getAnyTC(subgraphType), args: { id: 'ID!', block: BlockHeight diff --git a/packages/util/src/database.ts b/packages/util/src/database.ts index 56cf51fa..c9596f36 100644 --- a/packages/util/src/database.ts +++ b/packages/util/src/database.ts @@ -515,7 +515,15 @@ export class Database { `; // Fetching blockHash for previous entity in frothy region. - const [{ block_hash: blockHash, block_number: blockNumber, id }] = await queryRunner.query(heirerchicalQuery, [data.blockHash, data.id, MAX_REORG_DEPTH]); + const result = await queryRunner.query(heirerchicalQuery, [data.blockHash, data.id, MAX_REORG_DEPTH]); + + // Check if empty array returned + // (occurs when block at given hash doesn't exist in the db) + if (!result?.length) { + throw new Error('no block with that hash found'); + } + + const [{ block_hash: blockHash, block_number: blockNumber, id }] = result; return { blockHash, blockNumber, id }; } diff --git a/packages/util/src/graph/database.ts b/packages/util/src/graph/database.ts index 3a17ba08..36976381 100644 --- a/packages/util/src/graph/database.ts +++ b/packages/util/src/graph/database.ts @@ -222,18 +222,14 @@ export class GraphDatabase { block: CanonicalBlockHeight = {}, selections: ReadonlyArray = [] ): Promise { - let { hash: blockHash, number: blockNumber } = block; + const { hash: blockHash, number: blockNumber } = block; const repo = queryRunner.manager.getRepository(entityType); const whereOptions: any = { id }; - if (blockNumber) { - whereOptions.blockNumber = LessThanOrEqual(blockNumber); - } - if (blockHash) { whereOptions.blockHash = blockHash; - const block = await this._baseDatabase.getBlockProgress(queryRunner.manager.getRepository('block_progress'), blockHash); - blockNumber = block?.blockNumber; + } else if (blockNumber) { + whereOptions.blockNumber = LessThanOrEqual(blockNumber); } const findOptions = {