Skip to content

Commit

Permalink
Stall block polling bootstrap when the network is down (ethers-io#3924).
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo authored and Woodpile37 committed Jan 14, 2024
1 parent f01b723 commit aa602ed
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions src.ts/providers/subscriber-polling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,32 @@ export class PollingBlockSubscriber implements Subscriber {
set pollingInterval(value: number) { this.#interval = value; }

async #poll(): Promise<void> {
const blockNumber = await this.#provider.getBlockNumber();
if (this.#blockNumber === -2) {
this.#blockNumber = blockNumber;
return;
}
try {
const blockNumber = await this.#provider.getBlockNumber();

// Bootstrap poll to setup our initial block number
if (this.#blockNumber === -2) {
this.#blockNumber = blockNumber;
return;
}

// @TODO: Put a cap on the maximum number of events per loop?
// @TODO: Put a cap on the maximum number of events per loop?

if (blockNumber !== this.#blockNumber) {
for (let b = this.#blockNumber + 1; b <= blockNumber; b++) {
// We have been stopped
if (this.#poller == null) { return; }
if (blockNumber !== this.#blockNumber) {
for (let b = this.#blockNumber + 1; b <= blockNumber; b++) {
// We have been stopped
if (this.#poller == null) { return; }

await this.#provider.emit("block", b);
await this.#provider.emit("block", b);
}

this.#blockNumber = blockNumber;
}

this.#blockNumber = blockNumber;
} catch (error) {
// @TODO: Minor bump, add an "error" event to let subscribers
// know things went awry.
//console.log(error);
}

// We have been stopped
Expand Down

0 comments on commit aa602ed

Please sign in to comment.