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

Zb/queue flush event #4139

Merged
merged 2 commits into from
Apr 11, 2024
Merged

Zb/queue flush event #4139

merged 2 commits into from
Apr 11, 2024

Conversation

BelfordZ
Copy link
Contributor

@BelfordZ BelfordZ commented Apr 8, 2024

Explanation

This PR is a follow up to this - where instead of calling the flush method directly after successfully handling a request, we instead subscribe to SelectedNetworkController state changes and flush the queue for the domain in question when the network changes.

One potential issue that may arise is relating to a possible race condition. The question is when will the subscriptions fire - will they happen before processing the next request starts? It's hard to say for certain.

References

Changelog

@metamask/request-queue-controller

  • ADDED: A method for flushing or clearing the queue of items for a particular domain
  • FIXED: calling wallet_switchEthereumChain will clear all queued transactions from the domain, across all queued 'batches'

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

@BelfordZ BelfordZ marked this pull request as ready for review April 8, 2024 21:51
@BelfordZ BelfordZ requested a review from a team as a code owner April 8, 2024 21:51
@BelfordZ
Copy link
Contributor Author

@metamaskbot publish-preview

Copy link
Contributor

Preview builds have been published. See these instructions for more information about preview builds.

Expand for full list of packages and versions.
{
  "@metamask-previews/accounts-controller": "12.0.1-preview-f04be62f",
  "@metamask-previews/address-book-controller": "4.0.1-preview-f04be62f",
  "@metamask-previews/announcement-controller": "6.1.0-preview-f04be62f",
  "@metamask-previews/approval-controller": "6.0.1-preview-f04be62f",
  "@metamask-previews/assets-controllers": "27.2.0-preview-f04be62f",
  "@metamask-previews/base-controller": "5.0.1-preview-f04be62f",
  "@metamask-previews/build-utils": "2.0.1-preview-f04be62f",
  "@metamask-previews/composable-controller": "6.0.1-preview-f04be62f",
  "@metamask-previews/controller-utils": "9.0.2-preview-f04be62f",
  "@metamask-previews/ens-controller": "10.0.1-preview-f04be62f",
  "@metamask-previews/eth-json-rpc-provider": "3.0.1-preview-f04be62f",
  "@metamask-previews/gas-fee-controller": "15.0.0-preview-f04be62f",
  "@metamask-previews/json-rpc-engine": "8.0.1-preview-f04be62f",
  "@metamask-previews/json-rpc-middleware-stream": "7.0.1-preview-f04be62f",
  "@metamask-previews/keyring-controller": "14.0.1-preview-f04be62f",
  "@metamask-previews/logging-controller": "3.0.1-preview-f04be62f",
  "@metamask-previews/message-manager": "8.0.1-preview-f04be62f",
  "@metamask-previews/name-controller": "6.0.1-preview-f04be62f",
  "@metamask-previews/network-controller": "18.1.0-preview-f04be62f",
  "@metamask-previews/notification-controller": "5.0.1-preview-f04be62f",
  "@metamask-previews/permission-controller": "9.0.2-preview-f04be62f",
  "@metamask-previews/permission-log-controller": "2.0.1-preview-f04be62f",
  "@metamask-previews/phishing-controller": "9.0.1-preview-f04be62f",
  "@metamask-previews/polling-controller": "6.0.1-preview-f04be62f",
  "@metamask-previews/preferences-controller": "9.0.1-preview-f04be62f",
  "@metamask-previews/queued-request-controller": "0.7.0-preview-f04be62f",
  "@metamask-previews/rate-limit-controller": "5.0.1-preview-f04be62f",
  "@metamask-previews/selected-network-controller": "11.0.0-preview-f04be62f",
  "@metamask-previews/signature-controller": "14.0.1-preview-f04be62f",
  "@metamask-previews/transaction-controller": "27.0.0-preview-f04be62f",
  "@metamask-previews/user-operation-controller": "7.0.0-preview-f04be62f"
}

SelectedNetworkControllerEventTypes.stateChange,
(_, patch) => {
patch.forEach(({ op, path }) => {
if (
Copy link
Member

@Gudahtt Gudahtt Apr 11, 2024

Choose a reason for hiding this comment

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

Nit: Hmm, I can see why you chose you use the patch here. I don't love seeing patches used for this because this code ends up being a bit fragile; different patch operations than expected would break this. This would also break depending on how domains were updated, e.g. if we did domains = { 'foo.com': 'networkClientId', ...domains } instead of domains['foo.com'] = 'networkClientId', it might result in a domains replacement patch rather than a domains['foo.com'] replacement patch. Some tricky assumptions here.

The obvious alternative approach would be to cache the entire domains state here and spot the difference, but that's a bit inelegant and inefficient compared to the patch approach.

Maybe we could add an event to the SelectedNetworkController that specifically tells us when a network is switched? That would make this much simpler.

Copy link
Member

@Gudahtt Gudahtt left a comment

Choose a reason for hiding this comment

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

LGTM! I left one suggestion on how the trigger could be improved, but I think what we have now works.

One other non-blocking question: should we clear the queue when an origin gets connected or disconnected as well? 🤔 I hadn't thought of that before.

@BelfordZ BelfordZ merged commit 48a661e into main Apr 11, 2024
139 checks passed
@BelfordZ BelfordZ deleted the zb/queue-flush-event branch April 11, 2024 15:48
adonesky1 pushed a commit that referenced this pull request Apr 16, 2024
## Explanation

<!--
Thanks for your contribution! Take a moment to answer these questions so
that reviewers have the information they need to properly understand
your changes:

* What is the current state of things and why does it need to change?
* What is the solution your changes offer and how does it work?
* Are there any changes whose purpose might not obvious to those
unfamiliar with the domain?
* If your primary goal was to update one package but you found you had
to update another one along the way, why did you do so?
* If you had to upgrade a dependency, why did you do so?
-->

## References

<!--
Are there any issues that this pull request is tied to? Are there other
links that reviewers should consult to understand these changes better?

For example:

* Fixes #12345
* Related to #67890
-->

## Changelog

<!--
If you're making any consumer-facing changes, list those changes here as
if you were updating a changelog, using the template below as a guide.

(CATEGORY is one of BREAKING, ADDED, CHANGED, DEPRECATED, REMOVED, or
FIXED. For security-related issues, follow the Security Advisory
process.)

Please take care to name the exact pieces of the API you've added or
changed (e.g. types, interfaces, functions, or methods).

If there are any breaking changes, make sure to offer a solution for
consumers to follow once they upgrade to the changes.

Finally, if you're only making changes to development scripts or tests,
you may replace the template below with "None".
-->

### `@metamask/queued-request-controller`

- **BREAKING**: The `QueuedRequestMiddleware` constructor now requires
the `methodsWithConfirmation` param which should be a list of methods
that can trigger confirmations
([#4066](#4066))
- **BREAKING**: The `QueuedRequestController` constructor now requires
the `methodsRequiringNetworkSwitch` param which should be a list of
methods that need the globally selected network to switched to the dapp
selected network before being processed
([#4066](#4066))
- **BREAKING**: Clear pending confirmations (non-queued requests) after
processing revokePermissions.
([#4165](#4165))
- **BREAKING**: Flush the RequestQueue after processing
revokePermissions.
([#4139](#4139))


## 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

---------

Co-authored-by: jiexi <jiexiluan@gmail.com>
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.

4 participants