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 bug that allowed for multiple fetch requests instantiate from getGasFeeEstimatesAndStartPolling #586

Merged
merged 3 commits into from
Sep 16, 2021

Conversation

danjm
Copy link
Contributor

@danjm danjm commented Sep 14, 2021

There was a bug in getGasFeeEstimatesAndStartPolling that could result in multiple fetch requests to the gas api being made, despite the pollTokens related logic meant to prevent this.

If pollTokens is empty, it immediately fetches the gas estimates, but doesn't add the poll token until after the fetch has finished.
So every single call will result in a new network request until the first one finishes. Then it will behave correctly and ensure only a single poll is active.

This PR fixes this by ensuring that the pollTokens are set before the asyncronous call. Tests are added that fail without this change and succeed with it.

@danjm danjm requested a review from a team as a code owner September 14, 2021 20:01
@@ -74,6 +76,9 @@ describe('GasFeeController', () => {
estimatedBaseFee: '28',
})
.persist();
mockGasFeeRequest.on('request', () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was the best I could come up with for tracking number of requests. I look through nock documentation fairly extensively, tried a few other things, but this meets my exact need. Open to suggested alternative if there is a simpler way based on the nock api.

Copy link
Member

Choose a reason for hiding this comment

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

I looked this up and it seems event is the way to go, however (nit) I'd change the event handler to a jest mock and use expect(handler).toHaveBeenCalledTimes instead of a counter

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the suggestion @wachunei, I made this change in the latest commit 4f8ae4b

expect(result2).toStrictEqual(pollToken);
});

it('should fetch new estimates if the poll tokens are cleared, and then should not make additional new fetches', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm a bit confused on this one... I see it that it shouldn't make additional fetches on getGasFeeEstimatesAndStartPolling calls (until pollTokens are cleared again), but won't it make additional fetches on the polling interval?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I guess this comment can be clarified. It means to say that subsequent calls to getGasFeeEstimatesAndStartPolling should not trigger new fetches

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@adonesky1 updated the comment in the latest commit

adonesky1
adonesky1 previously approved these changes Sep 15, 2021
Copy link
Contributor

@adonesky1 adonesky1 left a comment

Choose a reason for hiding this comment

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

One small question but otherwise code LGTM! (didn't test)

adonesky1
adonesky1 previously approved these changes Sep 16, 2021
Copy link
Contributor

@adonesky1 adonesky1 left a comment

Choose a reason for hiding this comment

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

LGTM!

@danjm danjm merged commit ef8fcdd into main Sep 16, 2021
@danjm danjm deleted the safer-gas-controller-polling branch September 16, 2021 17:49
@adonesky1 adonesky1 mentioned this pull request Sep 16, 2021
MajorLift pushed a commit that referenced this pull request Oct 11, 2023
…GasFeeEstimatesAndStartPolling (#586)

* Ensure that the gas fee controller does not make new network requests while awaiting for a previous getGasFeeEstimatesAndStartPolling call

* Use a jest function for handling events from nock calls in GasFeeController.test.ts

* Improve unit test description'
MajorLift pushed a commit that referenced this pull request Oct 11, 2023
…GasFeeEstimatesAndStartPolling (#586)

* Ensure that the gas fee controller does not make new network requests while awaiting for a previous getGasFeeEstimatesAndStartPolling call

* Use a jest function for handling events from nock calls in GasFeeController.test.ts

* Improve unit test description'
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.

3 participants