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

Prevent redundant token rate updates #1512

Merged
merged 1 commit into from
Jul 18, 2023

Conversation

Gudahtt
Copy link
Member

@Gudahtt Gudahtt commented Jul 15, 2023

Explanation

The TokenRatesController was updating token rates each time any state change event was recieved, even when the change was totally unrelated.

The controller now ignores irrelevant state changes. Only relevant state changes will trigger token rate updates.

References

Fixes #1466

Changelog

@metamask/assets-controllers

  • Fixed: Prevent the TokenRateController from making redundant token rate updates

Checklist

  • I've updated the test suite for new or updated code as appropriate
  • I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate
  • I've highlighted breaking changes using the "BREAKING" category above as appropriate

@Gudahtt

This comment was marked as resolved.

@Gudahtt Gudahtt force-pushed the fix-another-token-rate-event-ordering-bug branch from 0a78d8d to 1cc946f Compare July 17, 2023 15:02
Base automatically changed from fix-another-token-rate-event-ordering-bug to main July 17, 2023 17:11
The `TokenRatesController` was updating token rates each time any state
change event was recieved, even when the change was totally unrelated.

The controller now ignores irrelevant state changes. Only relevant
state changes will trigger token rate updates.

Fixes #1466
@Gudahtt Gudahtt force-pushed the prevent-redundant-token-rate-lookups branch from 82ad946 to a346aa6 Compare July 17, 2023 17:11
@Gudahtt Gudahtt marked this pull request as ready for review July 17, 2023 17:12
@Gudahtt Gudahtt requested a review from a team as a code owner July 17, 2023 17:12
Copy link
Contributor

@mcmire mcmire left a comment

Choose a reason for hiding this comment

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

Changes make sense, just had a couple of suggestions.

@@ -295,7 +295,7 @@ describe('TokenRatesController', () => {
expect(mock).not.toThrow();
});

it('should update exchange rates when tokens change while polling is active', async () => {
it('should update exchange rates when tokens change', async () => {
Copy link
Contributor

@mcmire mcmire Jul 17, 2023

Choose a reason for hiding this comment

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

It looks like we have a test for when allTokens changes, but what do you think about adding another one for when allDetectedTokens changes?

Copy link
Member Author

Choose a reason for hiding this comment

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

We already have a test like this, though it's at the end of the test suite (the order doesn't make a great deal of sense; I think it's last because it was worked on last)

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah I see now.

In that case should we have a test that's the same as that test but verifies nothing happens when polling is disabled?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch. Since this is unrelated to the changes in this PR, I will address it in a follow-up PR to reorganize these tests and add few missing test cases

Copy link
Member Author

Choose a reason for hiding this comment

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

Added in #1522

Copy link
Contributor

Choose a reason for hiding this comment

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

This file has a test "should clear contractExchangeRates state when network is changed", which covers the case in which onNetworkStateChange is called with a different chain ID, but it seems like we might be missing a test for when this occurs but polling is inactive. Does it make sense to add this test?

Copy link
Member Author

Choose a reason for hiding this comment

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

That sounds like "should not update exchange rates when ticker changes while polling is inactive"

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, I guess that doesn't cover the "clearing" behavior, true. I'll look at adding a test for that

Copy link
Contributor

@mcmire mcmire Jul 17, 2023

Choose a reason for hiding this comment

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

The test "should not update exchange rates when ticker changes while polling is inactive" verifies that nothing happens when the ticker changes but polling is inactive, but I'm talking about when the chain ID changes but polling is inactive. Does that test already exist?

Copy link
Member Author

Choose a reason for hiding this comment

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

The tests with "ticker" in the description actually test when the ticker and chain ID change. We don't have any tests for when they change independently.

Copy link
Member Author

Choose a reason for hiding this comment

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

I can add tests for independent chain ID and ticker changes in a follow-up PR, along with the additional "clearing" test case

Copy link
Member Author

Choose a reason for hiding this comment

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

Added in #1522

@Gudahtt Gudahtt merged commit 27236f4 into main Jul 18, 2023
@Gudahtt Gudahtt deleted the prevent-redundant-token-rate-lookups branch July 18, 2023 00:54
Gudahtt added a commit that referenced this pull request Jul 18, 2023
Missing test cases found in #1512 have been added.
Gudahtt added a commit that referenced this pull request Jul 18, 2023
Missing test cases found in #1512 have been added.
Gudahtt added a commit that referenced this pull request Jul 18, 2023
Missing test cases found in #1512 have been added.
Gudahtt added a commit that referenced this pull request Jul 18, 2023
Missing test cases found in #1512 have been added.
Gudahtt added a commit that referenced this pull request Jul 18, 2023
Missing test cases found in #1512 have been added.
Gudahtt added a commit that referenced this pull request Jul 18, 2023
Missing test cases found in #1512 have been added.
MajorLift pushed a commit that referenced this pull request Oct 11, 2023
The `TokenRatesController` was updating token rates each time any state
change event was recieved, even when the change was totally unrelated.

The controller now ignores irrelevant state changes. Only relevant
state changes will trigger token rate updates.

Fixes #1466
MajorLift pushed a commit that referenced this pull request Oct 11, 2023
Missing test cases found in #1512 have been added.
MajorLift pushed a commit that referenced this pull request Oct 11, 2023
The `TokenRatesController` was updating token rates each time any state
change event was recieved, even when the change was totally unrelated.

The controller now ignores irrelevant state changes. Only relevant
state changes will trigger token rate updates.

Fixes #1466
MajorLift pushed a commit that referenced this pull request Oct 11, 2023
Missing test cases found in #1512 have been added.
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.

Prevent redundant currency rate lookups
3 participants