Skip to content

Commit

Permalink
Fetch txs only for blocks having relevant events (#436)
Browse files Browse the repository at this point in the history
* Fix address filter usage in rpc-eth-client getLogs implementation

* Avoid fetching block txs when no relevant logs exist
  • Loading branch information
prathamesh0 authored Oct 25, 2023
1 parent 07887c1 commit 320bf02
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 21 deletions.
2 changes: 1 addition & 1 deletion packages/codegen/src/templates/indexer-template.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ export class Indexer implements IndexerInterface {
}

async fetchEventsAndSaveBlocks (blocks: DeepPartial<BlockProgress>[]): Promise<{ blockProgress: BlockProgress, events: DeepPartial<Event>[] }[]> {
return this._baseIndexer.fetchEventsAndSaveBlocks(blocks, this.parseEventNameAndArgs.bind(this))
return this._baseIndexer.fetchEventsAndSaveBlocks(blocks, this.parseEventNameAndArgs.bind(this));
}

async saveBlockAndFetchEvents (block: DeepPartial<BlockProgress>): Promise<[BlockProgress, DeepPartial<Event>[]]> {
Expand Down
2 changes: 1 addition & 1 deletion packages/rpc-eth-client/src/eth-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ export class EthClient implements EthClientInterface {
let logs = logsByAddress.flat();

// If no addresses provided to filter
if (!logs.length) {
if (!addresses.length) {
logs = await this._provider.getLogs({
fromBlock,
toBlock
Expand Down
48 changes: 29 additions & 19 deletions packages/util/src/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ export class Indexer {
}

// Fetch logs for block range of given blocks
let logsPromise: Promise<any>;
let logs: any;
const fromBlock = blocks[0].blockNumber;
const toBlock = blocks[blocks.length - 1].blockNumber;

Expand All @@ -312,20 +312,43 @@ export class Indexer {
return watchedContract.address;
});

logsPromise = this._ethClient.getLogsForBlockRange({
({ logs } = await this._ethClient.getLogsForBlockRange({
fromBlock,
toBlock,
addresses
});
}));
} else {
logsPromise = this._ethClient.getLogsForBlockRange({ fromBlock, toBlock });
({ logs } = await this._ethClient.getLogsForBlockRange({ fromBlock, toBlock }));
}

// Skip further processing if no relevant logs found in the entire block range
if (!logs.length) {
return new Map();
}

// Sort logs according to blockhash
const logsMap: Map<string, any> = new Map();
logs.forEach((log: any) => {
const { blockHash: logBlockHash } = log;
assert(typeof logBlockHash === 'string');

if (!logsMap.has(logBlockHash)) {
logsMap.set(logBlockHash, []);
}

logsMap.get(logBlockHash).push(log);
});

// Fetch transactions for given blocks
const transactionsMap: Map<string, any> = new Map();
const transactionPromises = blocks.map(async (block) => {
assert(block.blockHash);

// Skip fetching txs if no relevant logs found in this block
if (!logsMap.has(block.blockHash)) {
return;
}

const blockWithTransactions = await this._ethClient.getBlockWithTransactions({ blockHash: block.blockHash, blockNumber: block.blockNumber });
const {
allEthHeaderCids: {
Expand All @@ -342,20 +365,7 @@ export class Indexer {
transactionsMap.set(block.blockHash, transactions);
});

const [{ logs }] = await Promise.all([logsPromise, ...transactionPromises]);

// Sort logs according to blockhash
const logsMap: Map<string, any> = new Map();
logs.forEach((log: any) => {
const { blockHash: logBlockHash } = log;
assert(typeof logBlockHash === 'string');

if (!logsMap.has(logBlockHash)) {
logsMap.set(logBlockHash, []);
}

logsMap.get(logBlockHash).push(log);
});
await Promise.all(transactionPromises);

// Map db ready events according to blockhash
const dbEventsMap: Map<string, DeepPartial<EventInterface>[]> = new Map();
Expand All @@ -364,7 +374,7 @@ export class Indexer {
assert(blockHash);

const logs = logsMap.get(blockHash) || [];
const transactions = transactionsMap.get(blockHash);
const transactions = transactionsMap.get(blockHash) || [];

const dbEvents = this.createDbEventsFromLogsAndTxs(blockHash, logs, transactions, parseEventNameAndArgs);
dbEventsMap.set(blockHash, dbEvents);
Expand Down

0 comments on commit 320bf02

Please sign in to comment.