Skip to content

Commit ea2d245

Browse files
committed
Fix old events from being emitted at the beginning of a filter (ethers-io#3069, ethers-io#3094).
1 parent c004ae5 commit ea2d245

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

packages/providers/src.ts/base-provider.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,18 +1069,26 @@ export class BaseProvider extends Provider implements EnsProvider {
10691069
if (!event._inflight) {
10701070
event._inflight = true;
10711071

1072-
// Filter from the last known event; due to load-balancing
1072+
// This is the first filter for this event, so we want to
1073+
// restrict events to events that happened no earlier than now
1074+
if (event._lastBlockNumber === -2) {
1075+
event._lastBlockNumber = blockNumber - 1;
1076+
}
1077+
1078+
// Filter from the last *known* event; due to load-balancing
10731079
// and some nodes returning updated block numbers before
10741080
// indexing events, a logs result with 0 entries cannot be
10751081
// trusted and we must retry a range which includes it again
10761082
const filter = event.filter;
10771083
filter.fromBlock = event._lastBlockNumber + 1;
10781084
filter.toBlock = blockNumber;
10791085

1080-
// Prevent fitler ranges from growing too wild
1081-
if (filter.toBlock - this._maxFilterBlockRange > filter.fromBlock) {
1082-
filter.fromBlock = filter.toBlock - this._maxFilterBlockRange;
1083-
}
1086+
// Prevent fitler ranges from growing too wild, since it is quite
1087+
// likely there just haven't been any events to move the lastBlockNumber.
1088+
const minFromBlock = filter.toBlock - this._maxFilterBlockRange;
1089+
if (minFromBlock > filter.fromBlock) { filter.fromBlock = minFromBlock; }
1090+
1091+
if (filter.fromBlock < 0) { filter.fromBlock = 0; }
10841092

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

0 commit comments

Comments
 (0)