Skip to content

Commit

Permalink
Fix old events from being emitted at the beginning of a filter (#3069,
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed Jul 20, 2022
1 parent c004ae5 commit ea2d245
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions packages/providers/src.ts/base-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1069,18 +1069,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 ea2d245

Please sign in to comment.