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 issue where mutex is acquired after state is read in addToken resulting in overwritten state with batched addToken/watchAsset calls #1768

Merged
merged 4 commits into from
Oct 4, 2023

Conversation

adonesky1
Copy link
Contributor

Fixes a subtle bug: when multiple wallet_watchAsset calls of ERC20 tokens are batched, only the last of the tokens added was getting added because the calls were not properly single threaded, since the mutex was acquired after reading state in the addToken call.

TokensController

Fixes

  • Fixes bug where batched addToken calls overwrite each other because mutex is acquired after reading state

…ulting in overwritten state with batched addToken/watchAsset calls
@adonesky1 adonesky1 requested a review from a team as a code owner October 3, 2023 21:32
@adonesky1 adonesky1 marked this pull request as draft October 3, 2023 21:44
@adonesky1 adonesky1 marked this pull request as ready for review October 3, 2023 22:39
Comment on lines +1394 to +1395
.mockImplementationOnce(() => requestId)
.mockImplementationOnce(() => '67890');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.mockImplementationOnce(() => requestId)
.mockImplementationOnce(() => '67890');
.mockReturnValueOnce(requestId)
.mockReturnValueOnce('67890');

Comment on lines +1400 to +1401
state.allTokens?.[ChainId.mainnet]?.[interactingAddress].length ===
2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
state.allTokens?.[ChainId.mainnet]?.[interactingAddress].length ===
2
state.tokens.length === 2

maybe? but definitely keep the allTokens checks below

...anotherAsset,
},
]);
generateRandomIdStub.mockRestore();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jest.resetAllMocks() in a beforeEach()?

EDIT: ohh... I see. This pattern is used 4 other times in this file already. Up to you if you want to clean it up or not

@adonesky1 adonesky1 merged commit 7c5030d into main Oct 4, 2023
107 checks passed
@adonesky1 adonesky1 deleted the fix-watchAsset-erc20 branch October 4, 2023 15:18
MajorLift pushed a commit that referenced this pull request Oct 11, 2023
…esulting in overwritten state with batched addToken/watchAsset calls (#1768)

Fixes a subtle bug: when multiple `wallet_watchAsset` calls of ERC20
tokens are batched, only the last of the tokens added was getting added
because the calls were not properly single threaded, since the mutex was
acquired after reading state in the `addToken` call.

## TokensController
### Fixes
- Fixes bug where batched addToken calls overwrite each other because
mutex is acquired after reading state
MajorLift pushed a commit that referenced this pull request Oct 11, 2023
…esulting in overwritten state with batched addToken/watchAsset calls (#1768)

Fixes a subtle bug: when multiple `wallet_watchAsset` calls of ERC20
tokens are batched, only the last of the tokens added was getting added
because the calls were not properly single threaded, since the mutex was
acquired after reading state in the `addToken` call.

## TokensController
### Fixes
- Fixes bug where batched addToken calls overwrite each other because
mutex is acquired after reading state
MajorLift pushed a commit that referenced this pull request Oct 12, 2023
…esulting in overwritten state with batched addToken/watchAsset calls (#1768)

Fixes a subtle bug: when multiple `wallet_watchAsset` calls of ERC20
tokens are batched, only the last of the tokens added was getting added
because the calls were not properly single threaded, since the mutex was
acquired after reading state in the `addToken` call.

## TokensController
### Fixes
- Fixes bug where batched addToken calls overwrite each other because
mutex is acquired after reading state
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants