Skip to content
Merged
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
4 changes: 4 additions & 0 deletions packages/transaction-controller/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- Prevent race condition causing excessive incoming transaction polling ([#6913](https://github.com/MetaMask/core/pull/6913))

## [60.9.0]

### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,31 @@ describe('IncomingTransactionHelper', () => {

expect(jest.getTimerCount()).toBe(0);
});

it('does not queue additional updates if first is still running', async () => {
const remoteTransactionSource = createRemoteTransactionSourceMock([]);

const helper = new IncomingTransactionHelper({
...CONTROLLER_ARGS_MOCK,
remoteTransactionSource,
});

helper.start();
helper.stop();

helper.start();
helper.stop();

helper.start();

await flushPromises();

expect(jest.getTimerCount()).toBe(1);

expect(remoteTransactionSource.fetchTransactions).toHaveBeenCalledTimes(
1,
);
});
});

describe('stop', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ export class IncomingTransactionHelper {

#isRunning: boolean;

#isUpdating: boolean;

readonly #messenger: TransactionControllerMessenger;

readonly #remoteTransactionSource: RemoteTransactionSource;
Expand Down Expand Up @@ -88,6 +90,7 @@ export class IncomingTransactionHelper {
this.#includeTokenTransfers = includeTokenTransfers;
this.#isEnabled = isEnabled ?? (() => true);
this.#isRunning = false;
this.#isUpdating = false;
this.#messenger = messenger;
this.#remoteTransactionSource = remoteTransactionSource;
this.#trimTransactions = trimTransactions;
Expand All @@ -109,6 +112,10 @@ export class IncomingTransactionHelper {

this.#isRunning = true;

if (this.#isUpdating) {
return;
}

this.#onInterval().catch((error) => {
log('Initial polling failed', error);
});
Expand All @@ -129,13 +136,21 @@ export class IncomingTransactionHelper {
}

async #onInterval() {
this.#isUpdating = true;

try {
await this.update({ isInterval: true });
} catch (error) {
console.error('Error while checking incoming transactions', error);
}

this.#isUpdating = false;

if (this.#isRunning) {
if (this.#timeoutId) {
clearTimeout(this.#timeoutId as number);
}

this.#timeoutId = setTimeout(
// eslint-disable-next-line @typescript-eslint/no-misused-promises
() => this.#onInterval(),
Expand Down
Loading