Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: internal listeners infinite retry loop #284

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Fixed
- Avoid risk of infinte retry loops when fetching new blocks ([#284](https://github.com/MetaMask/eth-block-tracker/pull/284))

## [11.0.2]
### Fixed
Expand Down
34 changes: 26 additions & 8 deletions src/PollingBlockTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ const log = createModuleLogger(projectLogger, 'polling-block-tracker');
const createRandomId = getCreateRandomId();
const sec = 1000;

const calculateSum = (accumulator: number, currentValue: number) =>
accumulator + currentValue;
const blockTrackerEvents: (string | symbol)[] = ['sync', 'latest'];

export interface PollingBlockTrackerOptions {
Expand Down Expand Up @@ -54,6 +52,10 @@ export class PollingBlockTracker

private readonly _setSkipCacheFlag: boolean;

readonly #internalEventListeners: ((
value: string | PromiseLike<string>,
) => void)[] = [];

constructor(opts: PollingBlockTrackerOptions = {}) {
// parse + validate args
if (!opts.provider) {
Expand Down Expand Up @@ -106,9 +108,17 @@ export class PollingBlockTracker
return this._currentBlock;
}
// wait for a new latest block
const latestBlock: string = await new Promise((resolve) =>
this.once('latest', resolve),
);
const latestBlock: string = await new Promise((resolve) => {
const listener = (value: string | PromiseLike<string>) => {
this.#internalEventListeners.splice(
this.#internalEventListeners.indexOf(listener),
1,
);
resolve(value);
};
this.#internalEventListeners.push(listener);
this.once('latest', listener);
});
// return newly set current block
return latestBlock;
}
Expand Down Expand Up @@ -179,9 +189,17 @@ export class PollingBlockTracker
}

private _getBlockTrackerEventCount(): number {
return blockTrackerEvents
.map((eventName) => this.listenerCount(eventName))
.reduce(calculateSum);
return (
blockTrackerEvents
.map((eventName) => this.listeners(eventName))
.flat()
// internal listeners are not included in the count
.filter((listener) =>
this.#internalEventListeners.every(
(internalListener) => !Object.is(internalListener, listener),
),
mikesposito marked this conversation as resolved.
Show resolved Hide resolved
).length
);
}

private _shouldUseNewBlock(newBlock: string) {
Expand Down
34 changes: 26 additions & 8 deletions src/SubscribeBlockTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ const createRandomId = getCreateRandomId();

const sec = 1000;

const calculateSum = (accumulator: number, currentValue: number) =>
accumulator + currentValue;
const blockTrackerEvents: (string | symbol)[] = ['sync', 'latest'];

export interface SubscribeBlockTrackerOptions {
Expand Down Expand Up @@ -43,6 +41,10 @@ export class SubscribeBlockTracker

private _subscriptionId: string | null;

readonly #internalEventListeners: ((
value: string | PromiseLike<string>,
) => void)[] = [];

constructor(opts: SubscribeBlockTrackerOptions = {}) {
// parse + validate args
if (!opts.provider) {
Expand Down Expand Up @@ -91,9 +93,17 @@ export class SubscribeBlockTracker
return this._currentBlock;
}
// wait for a new latest block
const latestBlock: string = await new Promise((resolve) =>
this.once('latest', resolve),
);
const latestBlock: string = await new Promise((resolve) => {
const listener = (value: string | PromiseLike<string>) => {
this.#internalEventListeners.splice(
this.#internalEventListeners.indexOf(listener),
1,
);
resolve(value);
};
this.#internalEventListeners.push(listener);
this.once('latest', listener);
});
// return newly set current block
return latestBlock;
}
Expand Down Expand Up @@ -162,9 +172,17 @@ export class SubscribeBlockTracker
}

private _getBlockTrackerEventCount(): number {
return blockTrackerEvents
.map((eventName) => this.listenerCount(eventName))
.reduce(calculateSum);
return (
blockTrackerEvents
.map((eventName) => this.listeners(eventName))
.flat()
// internal listeners are not included in the count
.filter((listener) =>
this.#internalEventListeners.every(
(internalListener) => !Object.is(internalListener, listener),
),
).length
);
}

private _shouldUseNewBlock(newBlock: string) {
Expand Down
Loading