Skip to content

Commit

Permalink
Add a separate ETH client method to get logs in a block range
Browse files Browse the repository at this point in the history
  • Loading branch information
prathamesh0 committed Oct 23, 2023
1 parent 97a4b06 commit 69aee65
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 25 deletions.
4 changes: 3 additions & 1 deletion packages/ipld-eth-client/src/eth-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any> {
console.time(`time:eth-client#getLogs-${JSON.stringify(vars)}`);
const result = await this._getCachedOrFetch('getLogs', vars);
Expand All @@ -145,6 +144,9 @@ export class EthClient implements EthClientInterface {
return { logs: getLogs };
}

// TODO: Implement
// async getLogsForBlockRange(): Promise<any> {}

async _getCachedOrFetch (queryName: keyof typeof ethQueries, vars: Vars): Promise<any> {
const keyObj = {
queryName,
Expand Down
39 changes: 23 additions & 16 deletions packages/rpc-eth-client/src/eth-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,31 +232,39 @@ export class EthClient implements EthClientInterface {
};
}

// TODO: Implement return type
async getLogs (vars: {
blockHash: string,
blockNumber: string,
addresses?: string[]
} | {
}): Promise<any> {
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<any> {
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<any> {
const { fromBlock, toBlock, addresses = [] } = vars;

const result = await this._getCachedOrFetch(
'getLogs',
Expand Down Expand Up @@ -296,7 +304,6 @@ export class EthClient implements EthClientInterface {
acc.set(txReceipt.transactionHash, txReceipt);
return acc;
}, new Map<string, providers.TransactionReceipt>());
console.timeEnd(`time:eth-client#getLogs-${JSON.stringify(vars)}`);

return {
logs: result.map((log) => ({
Expand Down
1 change: 0 additions & 1 deletion packages/util/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
8 changes: 3 additions & 5 deletions packages/util/src/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions packages/util/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,12 @@ export interface EthClient {
blockHash: string,
blockNumber: string,
addresses?: string[]
} | {
}): Promise<any>;
getLogsForBlockRange?: (vars: {
fromBlock?: number,
toBlock?: number,
addresses?: string[]
}): Promise<any>;
}) => Promise<any>;
}

export type Clients = {
Expand Down

0 comments on commit 69aee65

Please sign in to comment.