-
Notifications
You must be signed in to change notification settings - Fork 5k
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 contract exchange rate race condition #10414
Conversation
96a48b2
to
afeb45e
Compare
Builds ready [afeb45e]
Page Load Metrics (606 ± 47 ms)
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These changes all seem like improvements!
I'm still a little fuzzy on the order-of-operations when a network switch happens. It seems like now the exchange rate update is being triggered twice - once as a result of the network change, and a second time as a result of the tokens changing.
this is true, but we need both in the case where tokens change without a network change. |
🤔 Could we only update on a token change than, and assume it will always change when the network switches? It does seem to change every time at the moment. But. That doesn't seem great to have that assumption baked in here when it might not always apply... |
You'd still have a race condition then if the token preference change occurs before the network switch, which doesn't seem likely. |
afeb45e
to
96ad3e2
Compare
removing the additional sync led to a race condition, which is weird but I think I found a way around it. The race condition occurs because we're copying the By making the updateExchangeRates function always ask for the most recent in practice, this failed state doesn't seem to occur. 🎉 Here's my theory:
By removing 3b, it broke this paradigm. However ticker is part of the provider store, so by always getting the value fresh from this store before fetching exchange rates you're guaranteed to be using the value that was set in #2a before tokens are updated. |
Builds ready [96ad3e2]
Page Load Metrics (592 ± 37 ms)
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
This is a good case-study for our future controller messaging improvements.
Fixes: #10189
Token fiat amounts would become out of sync with one another because contract exchange rates are fetched when tokens are added, not when the network switches. As a result, token exchange rates would get updated on network switch as a result of the token list changing -- but the token rates controller's event was happening prior to the currency rate controller's native currency key being updated.
To fix this I simplified the code a bit, breaking apart the dependency of token-rates-controller to currency-rate-controller. The only thing that token rates needed from currency rates was the native currency. Because currency rates controller also has a dependency on the network's native currency I decided to tie token rates controller to the network instead.
I also removed the 'preferences' setter in the controller because it confused me. in no place are we setting preferences on the controller except the constructor.