Skip to content

Commit

Permalink
fix eth_getLogs for latest block timing issue (#828)
Browse files Browse the repository at this point in the history
* fix getlogs timing issue

* update

* avoid infinite loop

* update
  • Loading branch information
shunjizhan authored Sep 20, 2023
1 parent 493352d commit 4988871
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
19 changes: 16 additions & 3 deletions packages/eth-providers/src/base-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ import {
runWithRetries,
runWithTiming,
sendTx,
sleep,
sortObjByKey,
subqlReceiptAdapter,
throwNotImplemented,
Expand Down Expand Up @@ -1774,10 +1775,22 @@ export abstract class BaseProvider extends AbstractProvider {
}

const filter = await this._sanitizeRawFilter(rawFilter);
const subqlLogs = await this.subql.getFilteredLogs(filter); // only filtered by blockNumber and address
const filteredLogs = subqlLogs.filter((log) => filterLogByTopics(log, filter.topics));

return filteredLogs.map((log) => this.formatter.filterLog(log));
// make sure subql already indexed all target blocks, up until the latest finalized block
const upperBoundry = await this.finalizedBlockNumber;
const targetBlock = filter.toBlock <= upperBoundry
? filter.toBlock
: upperBoundry;
let lastProcessedHeight = await this.subql.getLastProcessedHeight();
while (lastProcessedHeight < targetBlock) {
await sleep(1000);
lastProcessedHeight = await this.subql.getLastProcessedHeight();
}

const subqlLogs = await this.subql.getFilteredLogs(filter); // only filtered by blockNumber and address
return subqlLogs
.filter(log => filterLogByTopics(log, filter.topics))
.map(log => this.formatter.filterLog(log));
};

getIndexerMetadata = async (): Promise<_Metadata | undefined> => {
Expand Down
5 changes: 5 additions & 0 deletions packages/eth-providers/src/utils/subqlProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ export class SubqlProvider {
return metaData?.genesisHash as string;
};

getLastProcessedHeight = async (): Promise<number> => {
const metaData = await this.getIndexerMetadata();
return metaData?.lastProcessedHeight ?? 0;
};

queryGraphql = (query: string): Promise<Query> =>
request(
this.url,
Expand Down

0 comments on commit 4988871

Please sign in to comment.