Fix old events from being emitted at the beginning of a filter (#3069, #3094).

This commit is contained in:
Richard Moore 2022-07-20 16:36:30 -04:00
parent c004ae50f3
commit ea2d2453a5

View File

@ -1069,7 +1069,13 @@ export class BaseProvider extends Provider implements EnsProvider {
if (!event._inflight) { if (!event._inflight) {
event._inflight = true; 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 // and some nodes returning updated block numbers before
// indexing events, a logs result with 0 entries cannot be // indexing events, a logs result with 0 entries cannot be
// trusted and we must retry a range which includes it again // trusted and we must retry a range which includes it again
@ -1077,10 +1083,12 @@ export class BaseProvider extends Provider implements EnsProvider {
filter.fromBlock = event._lastBlockNumber + 1; filter.fromBlock = event._lastBlockNumber + 1;
filter.toBlock = blockNumber; filter.toBlock = blockNumber;
// Prevent fitler ranges from growing too wild // Prevent fitler ranges from growing too wild, since it is quite
if (filter.toBlock - this._maxFilterBlockRange > filter.fromBlock) { // likely there just haven't been any events to move the lastBlockNumber.
filter.fromBlock = filter.toBlock - this._maxFilterBlockRange; 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) => { const runner = this.getLogs(filter).then((logs) => {
// Allow the next getLogs // Allow the next getLogs