Skip to content

Commit

Permalink
Fix old events from being emitted at the beginning of a filter (ether…
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo authored and Woodpile37 committed Jan 14, 2024
1 parent e6e8061 commit 170d791
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions packages/providers/src.ts/base-provider.ts
Expand Up @@ -936,18 +936,26 @@ export class BaseProvider extends Provider implements EnsProvider {
if (!event._inflight) {
event._inflight = true;

// Filter from the last known event; due to load-balancing
// This is the first filter for this event, so we want to
// restrict events to events that happened no earlier than now
if (event._lastBlockNumber === -2) {
event._lastBlockNumber = blockNumber - 1;
}

// Filter from the last *known* event; due to load-balancing
// and some nodes returning updated block numbers before
// indexing events, a logs result with 0 entries cannot be
// trusted and we must retry a range which includes it again
const filter = event.filter;
filter.fromBlock = event._lastBlockNumber + 1;
filter.toBlock = blockNumber;

// Prevent fitler ranges from growing too wild
if (filter.toBlock - this._maxFilterBlockRange > filter.fromBlock) {
filter.fromBlock = filter.toBlock - this._maxFilterBlockRange;
}
// Prevent fitler ranges from growing too wild, since it is quite
// likely there just haven't been any events to move the lastBlockNumber.
const minFromBlock = filter.toBlock - this._maxFilterBlockRange;
if (minFromBlock > filter.fromBlock) { filter.fromBlock = minFromBlock; }

if (filter.fromBlock < 0) { filter.fromBlock = 0; }

const runner = this.getLogs(filter).then((logs) => {
// Allow the next getLogs
Expand Down

0 comments on commit 170d791

Please sign in to comment.