Closed
Conversation
bergeron
commented
Sep 19, 2023
Comment on lines
+220
to
+230
| if (newCurrentCurrency !== undefined) { | ||
| this.update((state) => { | ||
| state.pendingCurrentCurrency = newCurrentCurrency; | ||
| }); | ||
| } | ||
|
|
||
| if (newNativeCurrency !== undefined) { | ||
| this.update((state) => { | ||
| state.pendingNativeCurrency = newNativeCurrency; | ||
| }); | ||
| } |
Contributor
Author
There was a problem hiding this comment.
The pending state may look less required now that the values are available as function arguments. But the pending state may be needed to fix a related bug.
When switching networks, MM extension temporarily uses the symbol + conversion rate from the old network until the calls return for the new network. (More visible on slow internet):
Screen.Recording.2023-09-18.at.8.25.51.AM.mov
Balances are usually available before conversion rates, so I think clients should be consulting the pending state to know the correct native symbol, and not show fiat values until rates are available for the new network.
Contributor
Author
|
This may get superseded by this multichain refactor: #1805 |
13 tasks
jiexi
added a commit
to MetaMask/metamask-extension
that referenced
this pull request
Nov 14, 2023
## **Description** Bumps `@metamask/assets-controllers` to `v18.0.0` which includes breaking changes for the CurrencyRateController. Notably the structure of the state has changed significantly. Part of making the state change so drastic (as opposed to making the refactor towards Multichain be as small/incremental as possible), is that the usage of `pendingNativeCurrency`, `pendingCurrentCurrency`, and how the controller handles state change in general causes unexpected behavior with concurrent updates. The update to CurrencyRateController also solves this issue. ## **Related issues** Fixes: MetaMask/MetaMask-planning#1025 Related: MetaMask/core#1687 ## **Manual testing steps** Nothing should change for the end user. ## **Screenshots/Recordings** <!-- If applicable, add screenshots and/or recordings to visualize the before and after of your change. --> ### **Before** <!-- [screenshots/recordings] --> ### **After** <!-- [screenshots/recordings] --> ## **Pre-merge author checklist** - [x] I’ve followed [MetaMask Coding Standards](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/CODING_GUIDELINES.md). - [x] I've clearly explained what problem this PR is solving and how it is solved. - [x] I've linked related issues - [ ] I've included manual testing steps - [ ] I've included screenshots/recordings if applicable - [ ] I’ve included tests if applicable - [ ] I’ve documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [x] I’ve applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/LABELING_GUIDELINES.md)). - [ ] I’ve properly set the pull request status: - [ ] In case it's not yet "ready for review", I've set it to "draft". - [ ] In case it's "ready for review", I've changed it from "draft" to "non-draft". ## **Pre-merge reviewer checklist** - [ ] I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed). - [ ] I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots. --------- Co-authored-by: MetaMask Bot <metamaskbot@users.noreply.github.com> Co-authored-by: Alex Donesky <adonesky@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Explanation
There is a race condition when the
CurrencyRateController'ssetNativeCurrencyorsetCurrentCurrencyare called concurrently. The user is impacted when switching networks, then switching again before the first switch has finished its currency update. It leaves MM extension stuck with the native token and exchange rate for the wrong network. See video (easier to reproduce if you throttle Chrome to 3G network speed):Screen.Recording.2023-09-19.at.9.16.33.AM.mov
There is already a mutex guarding this operation. But when the first update finishes, it sets
pendingNativeCurrencyandpendingCurrentCurrencyto null. So when the second update gets its turn at the mutex, it has lost the pending state it was waiting to switch to. So the controller never switches to the new network.My fix is to expand the scope of the mutex to also include the setting of the pending state.
This should be a non-breaking change. New arguments are added to
updateExchangeRate, but it keeps the same behavior as before when no arguments are provided.A unit test is added that reproduces the bug on the old code, and passes on the code.
References
User reports we suspect are related:
Changelog
@metamask/assets-controllersCurrencyRateControllerwhen currencies are updated concurrentlyCurrencyRateController.updateExchangeRateadds 2 arguments:newCurrentCurrencyandnewNativeCurrencyChecklist