From 69aee655e9b065fc05c31a7cb52d663cd670de46 Mon Sep 17 00:00:00 2001 From: Prathamesh Musale Date: Mon, 23 Oct 2023 12:18:08 +0530 Subject: [PATCH] Add a separate ETH client method to get logs in a block range --- packages/ipld-eth-client/src/eth-client.ts | 4 ++- packages/rpc-eth-client/src/eth-client.ts | 39 +++++++++++++--------- packages/util/src/common.ts | 1 - packages/util/src/indexer.ts | 8 ++--- packages/util/src/types.ts | 5 +-- 5 files changed, 32 insertions(+), 25 deletions(-) diff --git a/packages/ipld-eth-client/src/eth-client.ts b/packages/ipld-eth-client/src/eth-client.ts index 33fd0f292..815ce2911 100644 --- a/packages/ipld-eth-client/src/eth-client.ts +++ b/packages/ipld-eth-client/src/eth-client.ts @@ -133,7 +133,6 @@ export class EthClient implements EthClientInterface { }; } - // TODO: Support fetching logs for block range in a single call async getLogs (vars: Vars): Promise { console.time(`time:eth-client#getLogs-${JSON.stringify(vars)}`); const result = await this._getCachedOrFetch('getLogs', vars); @@ -145,6 +144,9 @@ export class EthClient implements EthClientInterface { return { logs: getLogs }; } + // TODO: Implement + // async getLogsForBlockRange(): Promise {} + async _getCachedOrFetch (queryName: keyof typeof ethQueries, vars: Vars): Promise { const keyObj = { queryName, diff --git a/packages/rpc-eth-client/src/eth-client.ts b/packages/rpc-eth-client/src/eth-client.ts index b50a9de8f..dc9400139 100644 --- a/packages/rpc-eth-client/src/eth-client.ts +++ b/packages/rpc-eth-client/src/eth-client.ts @@ -232,31 +232,39 @@ export class EthClient implements EthClientInterface { }; } - // TODO: Implement return type async getLogs (vars: { blockHash: string, blockNumber: string, addresses?: string[] - } | { + }): Promise { + const blockNumber = Number(vars.blockNumber); + + console.time(`time:eth-client#getLogs-${JSON.stringify(vars)}`); + const result = await this._getLogs({ fromBlock: blockNumber, toBlock: blockNumber, addresses: vars.addresses }); + console.timeEnd(`time:eth-client#getLogs-${JSON.stringify(vars)}`); + + return result; + } + + async getLogsForBlockRange (vars: { fromBlock?: number, toBlock?: number, addresses?: string[] }): Promise { - console.time(`time:eth-client#getLogs-${JSON.stringify(vars)}`); + console.time(`time:eth-client#getLogsForBlockRange-${JSON.stringify(vars)}`); + const result = await this._getLogs({ fromBlock: Number(vars.fromBlock), toBlock: Number(vars.toBlock), addresses: vars.addresses }); + console.timeEnd(`time:eth-client#getLogsForBlockRange-${JSON.stringify(vars)}`); - // TODO: Implement a separate method getLogsForBlockRange - // as we may want to make blockNumber an optional param as present in ipld-eth-client - let fromBlock: number | undefined; - let toBlock: number | undefined; - if ('blockNumber' in vars) { - fromBlock = Number(vars.blockNumber); - toBlock = Number(vars.blockNumber); - } else { - fromBlock = Number(vars.fromBlock); - toBlock = Number(vars.toBlock); - } + return result; + } - const { addresses = [] } = vars; + // TODO: Implement return type + async _getLogs (vars: { + fromBlock?: number, + toBlock?: number, + addresses?: string[] + }): Promise { + const { fromBlock, toBlock, addresses = [] } = vars; const result = await this._getCachedOrFetch( 'getLogs', @@ -296,7 +304,6 @@ export class EthClient implements EthClientInterface { acc.set(txReceipt.transactionHash, txReceipt); return acc; }, new Map()); - console.timeEnd(`time:eth-client#getLogs-${JSON.stringify(vars)}`); return { logs: result.map((log) => ({ diff --git a/packages/util/src/common.ts b/packages/util/src/common.ts index 985c5c7ca..8c55fdfea 100644 --- a/packages/util/src/common.ts +++ b/packages/util/src/common.ts @@ -178,7 +178,6 @@ export const _fetchBatchBlocks = async ( // Fetch blocks again if there are missing blocks. while (true) { console.time('time:common#fetchBatchBlocks-getBlocks'); - // TODO: Investigate: fetch txs for the blocks here itself instead of doing it along with logs const blockPromises = blockNumbers.map(async blockNumber => indexer.getBlocks({ blockNumber })); const res = await Promise.all(blockPromises); console.timeEnd('time:common#fetchBatchBlocks-getBlocks'); diff --git a/packages/util/src/indexer.ts b/packages/util/src/indexer.ts index d3c088ad6..997996313 100644 --- a/packages/util/src/indexer.ts +++ b/packages/util/src/indexer.ts @@ -289,9 +289,6 @@ export class Indexer { log(`fetchEventsAndSaveBlocks#fetchEventsForBlocks: fetched for block: ${blockHash} num events: ${blockProgress.numEvents}`); return { blockProgress, events: [] }; - - // TODO: Return events? - // return { blockProgress, events }; }); return Promise.all(blocksWithEventsPromises); @@ -308,19 +305,20 @@ export class Indexer { const fromBlock = blocks[0].blockNumber; const toBlock = blocks[blocks.length - 1].blockNumber; + assert(this._ethClient.getLogsForBlockRange, 'getLogsForBlockRange() not implemented in ethClient'); if (this._serverConfig.filterLogs) { const watchedContracts = this.getWatchedContracts(); const addresses = watchedContracts.map((watchedContract): string => { return watchedContract.address; }); - logsPromise = this._ethClient.getLogs({ + logsPromise = this._ethClient.getLogsForBlockRange({ fromBlock, toBlock, addresses }); } else { - logsPromise = this._ethClient.getLogs({ fromBlock, toBlock }); + logsPromise = this._ethClient.getLogsForBlockRange({ fromBlock, toBlock }); } // Fetch transactions for given blocks diff --git a/packages/util/src/types.ts b/packages/util/src/types.ts index 94af4542d..d359a23de 100644 --- a/packages/util/src/types.ts +++ b/packages/util/src/types.ts @@ -219,11 +219,12 @@ export interface EthClient { blockHash: string, blockNumber: string, addresses?: string[] - } | { + }): Promise; + getLogsForBlockRange?: (vars: { fromBlock?: number, toBlock?: number, addresses?: string[] - }): Promise; + }) => Promise; } export type Clients = {