From f57ea8d3eb6ec8fa864ff291a8f84f92560549d9 Mon Sep 17 00:00:00 2001 From: Prathamesh Musale Date: Fri, 13 Sep 2024 11:57:57 +0530 Subject: [PATCH] Update block tag parsing --- packages/cli/src/server.ts | 8 ++++---- packages/util/src/eth-rpc-handlers.ts | 21 ++++++++++++--------- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/packages/cli/src/server.ts b/packages/cli/src/server.ts index 995c052a..98c64f9a 100644 --- a/packages/cli/src/server.ts +++ b/packages/cli/src/server.ts @@ -32,7 +32,8 @@ import { readParty, UpstreamConfig, fillBlocks, - createGQLLogger + createGQLLogger, + createEthRPCHandlers } from '@cerc-io/util'; import { TypeSource } from '@graphql-tools/utils'; import type { @@ -276,8 +277,7 @@ export class ServerCmd { gqlLogger: winston.Logger ) => Promise, typeDefs: TypeSource, - paymentsManager?: PaymentsManager, - createEthRPCHandlers?: (indexer: IndexerInterface, ethProvider: JsonRpcProvider) => Promise + paymentsManager?: PaymentsManager ): Promise<{ app: Application, server: ApolloServer @@ -319,7 +319,7 @@ export class ServerCmd { const gqlLogger = createGQLLogger(config.server.gql.logDir); const resolvers = await createResolvers(indexer, eventWatcher, gqlLogger); - const ethRPCHandlers = createEthRPCHandlers ? await createEthRPCHandlers(indexer, ethProvider) : {}; + const ethRPCHandlers = await createEthRPCHandlers(indexer, ethProvider); // Create an Express app const app: Application = express(); diff --git a/packages/util/src/eth-rpc-handlers.ts b/packages/util/src/eth-rpc-handlers.ts index 8bd0589f..406a6a20 100644 --- a/packages/util/src/eth-rpc-handlers.ts +++ b/packages/util/src/eth-rpc-handlers.ts @@ -16,6 +16,9 @@ const ERROR_CONTRACT_NOT_RECOGNIZED = 'Contract not recognized'; const ERROR_CONTRACT_METHOD_NOT_FOUND = 'Contract method not found'; const ERROR_METHOD_NOT_IMPLEMENTED = 'Method not implemented'; const ERROR_INVALID_BLOCK_TAG = 'Invalid block tag'; +const ERROR_BLOCK_NOT_FOUND = 'Block not found'; + +const DEFAULT_BLOCK_TAG = 'latest'; class ErrorWithCode extends Error { code: number; @@ -47,7 +50,8 @@ export const createEthRPCHandlers = async ( throw new ErrorWithCode(CODE_INVALID_PARAMS, ERROR_CONTRACT_INSUFFICIENT_PARAMS); } - const { to, data, blockTag } = args[0]; + const { to, data } = args[0]; + const blockTag = args.length > 1 ? args[1] : DEFAULT_BLOCK_TAG; const blockHash = await parseBlockTag(indexer, ethProvider, blockTag); @@ -101,7 +105,7 @@ export const createEthRPCHandlers = async ( }; }; -const parseBlockTag = async (indexer: IndexerInterface, ethProvider: JsonRpcProvider, blockTag: any): Promise => { +const parseBlockTag = async (indexer: IndexerInterface, ethProvider: JsonRpcProvider, blockTag: string): Promise => { if (utils.isHexString(blockTag)) { // Return value if hex string is of block hash length if (utils.hexDataLength(blockTag) === 32) { @@ -110,21 +114,20 @@ const parseBlockTag = async (indexer: IndexerInterface, ethProvider: JsonRpcProv // Treat hex value as a block number const block = await ethProvider.getBlock(blockTag); + if (block === null) { + throw new ErrorWithCode(CODE_INVALID_PARAMS, ERROR_BLOCK_NOT_FOUND); + } + return block.hash; } - // TODO: Handle pending, safe and finalized - if (['earliest', 'latest', 'pending', 'safe', 'finalized', null, undefined].includes(blockTag)) { + if (blockTag === DEFAULT_BLOCK_TAG) { const syncStatus = await indexer.getSyncStatus(); if (!syncStatus) { throw new ErrorWithCode(CODE_INTERNAL_ERROR, 'SyncStatus not found'); } - if (blockTag === 'earliest') { - return syncStatus.initialIndexedBlockHash; - } - - return syncStatus.latestIndexedBlockHash; + return syncStatus.latestProcessedBlockHash; } throw new ErrorWithCode(CODE_INVALID_PARAMS, ERROR_INVALID_BLOCK_TAG);