Skip to content

Commit

Permalink
Filter blocks without contract events when eth_getLogs filter is unset
Browse files Browse the repository at this point in the history
  • Loading branch information
nikugogoi committed Jul 3, 2024
1 parent b2fe3ac commit 0330e24
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
6 changes: 5 additions & 1 deletion packages/codegen/src/templates/indexer-template.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ export class Indexer implements IndexerInterface {
};
}

const { block: { number } } = await this._ethClient.getBlockByHash(blockHash);
const { block: { number } } = await this.getBlockByHash(blockHash);
const blockNumber = ethers.BigNumber.from(number).toNumber();

log('{{query.name}}: db miss, fetching from upstream server');
Expand Down Expand Up @@ -679,6 +679,10 @@ export class Indexer implements IndexerInterface {
return this._baseIndexer.getBlocks(blockFilter);
}

async getBlockByHash (blockHash?: string): Promise<{ block: any }> {
return this._baseIndexer.getBlockByHash(blockHash);
}

async updateSyncStatusIndexedBlock (blockHash: string, blockNumber: number, force = false): Promise<SyncStatus> {
return this._baseIndexer.updateSyncStatusIndexedBlock(blockHash, blockNumber, force);
}
Expand Down
25 changes: 21 additions & 4 deletions packages/util/src/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -469,10 +469,27 @@ export class Indexer {
topics
});

const blockLogsMap = this._reduceLogsToBlockLogsMap(logs);
// Create unique list of tx required
let blockLogsMap = this._reduceLogsToBlockLogsMap(logs);

// Filter blocks which have no events from watched contracts
blockLogsMap = Array.from(blockLogsMap.entries())
.filter(([, logs]) => {
return logs.some(log => {
const contractAddress = ethers.utils.getAddress(log.account.address);
return this.isContractAddressWatched(contractAddress)?.length;
});
})
.reduce((acc, [blockHash, logs]) => {
acc.set(blockHash, logs);
return acc;
}, new Map());

// Create unique list of txs required
const txHashes = Array.from([
...new Set<string>(logs.map((log: any) => log.transaction.hash))
...new Set<string>(
Array.from(blockLogsMap.values())
.flat()
.map((log: any) => log.transaction.hash))
]);

// Fetch blocks with transactions for the logs returned
Expand Down Expand Up @@ -543,7 +560,7 @@ export class Indexer {
return blocksWithDbEvents;
}

_reduceLogsToBlockLogsMap (logs: any[]): Map<string, any> {
_reduceLogsToBlockLogsMap (logs: any[]): Map<string, any[]> {
return logs.reduce((acc: Map<string, any>, log: any) => {
const { blockHash: logBlockHash } = log;
assert(typeof logBlockHash === 'string');
Expand Down
6 changes: 3 additions & 3 deletions packages/util/src/job-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -728,9 +728,9 @@ export class JobRunner {

this._blockAndEventsMap.delete(block.blockHash);

// Check if new contract was added and filterLogsByAddresses is set to true
if (isNewContractWatched && this._indexer.upstreamConfig.ethServer.filterLogsByAddresses) {
// Check if historical processing is running and that current block is being processed was trigerred by historical processing
// Check if new contract was added
if (isNewContractWatched) {
// Check if historical processing is running and that current block being processed was trigerred by historical processing
if (this._historicalProcessingCompletedUpto && this._historicalProcessingCompletedUpto > block.blockNumber) {
const nextBlockNumberToProcess = block.blockNumber + 1;

Expand Down

0 comments on commit 0330e24

Please sign in to comment.