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

Update QueuedRequestController to support Multichain API #4718

Merged
merged 24 commits into from
Sep 27, 2024

Conversation

adonesky1
Copy link
Contributor

@adonesky1 adonesky1 commented Sep 19, 2024

Explanation

The QueuedRequestController previously batched and processed requests based solely on their origin. This approach doesn't account for scenarios where a dapp interacts with multiple networks simultaneously from the same origin as will be the case on the multichain API.

This PR updates the QueuedRequestController to batch and process requests based on both origin and networkClientId. This ensures that:

  • Requests from the same origin but targeting different networks are processed in separate batches.
  • Network switches occur appropriately between batches when the networkClientId changes.
  • Each request is processed on the correct network.

Key changes:

  • Batching Logic: Modified the batching mechanism to consider both origin and networkClientId, for the legacy API this shouldn't change anything, but it allows us to cleanly batch for the multichain API until we remove queueing altogther.
  • Request Validation: Added validation to throw an error if a request doesn't include a networkClientId.
  • Dependency Removal: Removed reliance on SelectedNetworkController for determining networkClientId. Now NetworkClientId is expected on every request. Which should always be the case if the middleware ordering is correct on both APIs.
  • Test Enhancements: Added and updated tests to cover new scenarios involving multiple origins and networkClientIds.

Draft PR/Branch off caip-multichain feature branch with preview build: MetaMask/metamask-extension#27408

Videos demonstrating functionality w/ multichain API:

2 requests same network different dapps:

Simple-cross-dapp-same-network-batching.mov

2 requests different network same dapp

single-dapp-different-networks.mov

2 requests same network same dapp

single-dapp-same-network-batching.mov

More complex flows:

https://drive.google.com/file/d/1PvCmmCQbxXqglsFLUlyT_7P_znukLUxI/view?usp=drive_link

https://drive.google.com/file/d/18R8zEPz_zsfhsw-DOkRFwkYRI1URynis/view?usp=sharing

Queueing working same as before on legacy API

Branch/PR with preview builds on develop used to test

Screen.Recording.2024-09-26.at.10.37.44.AM.mov

References

  • N/A

Changelog

@metamask/queued-request-controller

  • CHANGED: Batch processing now considers both origin and networkClientId, ensuring requests targeting different networks are processed separately.
  • REMOVED: Dependency on SelectedNetworkController; the controller no longer uses SelectedNetworkController:getNetworkClientIdForDomain.
  • CHANGED: Incoming requests to enqueueRequest now must include a networkClientId; an error is thrown if it's missing. This was previously a required part of the type but since consumers like the extension do not have extensive typescript coverage this isn't definitively enforced.

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
  • I've prepared draft pull requests for clients and consumer packages to resolve any breaking changes

@adonesky1
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": "18.1.1-preview-09a2ffd5",
  "@metamask-previews/address-book-controller": "6.0.0-preview-09a2ffd5",
  "@metamask-previews/announcement-controller": "7.0.0-preview-09a2ffd5",
  "@metamask-previews/approval-controller": "7.0.3-preview-09a2ffd5",
  "@metamask-previews/assets-controllers": "38.0.0-preview-09a2ffd5",
  "@metamask-previews/base-controller": "7.0.0-preview-09a2ffd5",
  "@metamask-previews/build-utils": "3.0.0-preview-09a2ffd5",
  "@metamask-previews/chain-controller": "0.1.1-preview-09a2ffd5",
  "@metamask-previews/composable-controller": "9.0.0-preview-09a2ffd5",
  "@metamask-previews/controller-utils": "11.2.0-preview-09a2ffd5",
  "@metamask-previews/ens-controller": "14.0.0-preview-09a2ffd5",
  "@metamask-previews/eth-json-rpc-provider": "4.1.3-preview-09a2ffd5",
  "@metamask-previews/gas-fee-controller": "20.0.0-preview-09a2ffd5",
  "@metamask-previews/json-rpc-engine": "9.0.2-preview-09a2ffd5",
  "@metamask-previews/json-rpc-middleware-stream": "8.0.2-preview-09a2ffd5",
  "@metamask-previews/keyring-controller": "17.2.0-preview-09a2ffd5",
  "@metamask-previews/logging-controller": "6.0.0-preview-09a2ffd5",
  "@metamask-previews/message-manager": "10.1.0-preview-09a2ffd5",
  "@metamask-previews/name-controller": "8.0.0-preview-09a2ffd5",
  "@metamask-previews/network-controller": "21.0.0-preview-09a2ffd5",
  "@metamask-previews/notification-controller": "6.0.0-preview-09a2ffd5",
  "@metamask-previews/notification-services-controller": "0.3.0-preview-09a2ffd5",
  "@metamask-previews/permission-controller": "11.0.1-preview-09a2ffd5",
  "@metamask-previews/permission-log-controller": "3.0.0-preview-09a2ffd5",
  "@metamask-previews/phishing-controller": "12.0.1-preview-09a2ffd5",
  "@metamask-previews/polling-controller": "10.0.0-preview-09a2ffd5",
  "@metamask-previews/preferences-controller": "13.0.2-preview-09a2ffd5",
  "@metamask-previews/profile-sync-controller": "0.3.0-preview-09a2ffd5",
  "@metamask-previews/queued-request-controller": "5.0.0-preview-09a2ffd5",
  "@metamask-previews/rate-limit-controller": "6.0.0-preview-09a2ffd5",
  "@metamask-previews/selected-network-controller": "18.0.0-preview-09a2ffd5",
  "@metamask-previews/signature-controller": "19.0.0-preview-09a2ffd5",
  "@metamask-previews/transaction-controller": "36.0.0-preview-09a2ffd5",
  "@metamask-previews/user-operation-controller": "15.0.0-preview-09a2ffd5"
}

@adonesky1 adonesky1 marked this pull request as ready for review September 25, 2024 22:24
@adonesky1 adonesky1 requested a review from a team as a code owner September 25, 2024 22:24
@adonesky1 adonesky1 changed the title Ad/update queued request controller barad dur Update QueuedRequestController to support Multichain API Sep 25, 2024
@@ -162,6 +165,8 @@ export class QueuedRequestController extends BaseController<
*/
#showApprovalRequest: () => void;

#networkClientIdOfCurrentBatch?: NetworkClientId;
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it okay if we move this next to originOfCurrentBatch and add a small little comment description?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done here: 2603f25

Copy link
Contributor

@jiexi jiexi left a comment

Choose a reason for hiding this comment

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

🥹

/**
* The networkClientId of the queuedRequest.
*/
networkClientId: NetworkClientId;
Copy link
Contributor

Choose a reason for hiding this comment

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

this is a good catch that it will be on the request already and don't need the SelectedNetworkController call in here.

@adonesky1
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": "18.2.2-preview-c55c3a39",
  "@metamask-previews/address-book-controller": "6.0.1-preview-c55c3a39",
  "@metamask-previews/announcement-controller": "7.0.1-preview-c55c3a39",
  "@metamask-previews/approval-controller": "7.1.0-preview-c55c3a39",
  "@metamask-previews/assets-controllers": "38.1.0-preview-c55c3a39",
  "@metamask-previews/base-controller": "7.0.1-preview-c55c3a39",
  "@metamask-previews/build-utils": "3.0.1-preview-c55c3a39",
  "@metamask-previews/chain-controller": "0.1.3-preview-c55c3a39",
  "@metamask-previews/composable-controller": "9.0.1-preview-c55c3a39",
  "@metamask-previews/controller-utils": "11.3.0-preview-c55c3a39",
  "@metamask-previews/ens-controller": "14.0.1-preview-c55c3a39",
  "@metamask-previews/eth-json-rpc-provider": "4.1.4-preview-c55c3a39",
  "@metamask-previews/gas-fee-controller": "20.0.1-preview-c55c3a39",
  "@metamask-previews/json-rpc-engine": "9.0.3-preview-c55c3a39",
  "@metamask-previews/json-rpc-middleware-stream": "8.0.3-preview-c55c3a39",
  "@metamask-previews/keyring-controller": "17.2.2-preview-c55c3a39",
  "@metamask-previews/logging-controller": "6.0.1-preview-c55c3a39",
  "@metamask-previews/message-manager": "10.1.1-preview-c55c3a39",
  "@metamask-previews/name-controller": "8.0.1-preview-c55c3a39",
  "@metamask-previews/network-controller": "21.0.1-preview-c55c3a39",
  "@metamask-previews/notification-controller": "7.0.0-preview-c55c3a39",
  "@metamask-previews/notification-services-controller": "0.8.1-preview-c55c3a39",
  "@metamask-previews/permission-controller": "11.0.2-preview-c55c3a39",
  "@metamask-previews/permission-log-controller": "3.0.1-preview-c55c3a39",
  "@metamask-previews/phishing-controller": "12.0.3-preview-c55c3a39",
  "@metamask-previews/polling-controller": "10.0.1-preview-c55c3a39",
  "@metamask-previews/preferences-controller": "13.0.3-preview-c55c3a39",
  "@metamask-previews/profile-sync-controller": "0.9.3-preview-c55c3a39",
  "@metamask-previews/queued-request-controller": "5.0.1-preview-c55c3a39",
  "@metamask-previews/rate-limit-controller": "6.0.1-preview-c55c3a39",
  "@metamask-previews/selected-network-controller": "18.0.1-preview-c55c3a39",
  "@metamask-previews/signature-controller": "19.1.0-preview-c55c3a39",
  "@metamask-previews/transaction-controller": "37.1.0-preview-c55c3a39",
  "@metamask-previews/user-operation-controller": "15.0.1-preview-c55c3a39"
}

@adonesky1 adonesky1 merged commit 54ddebf into main Sep 27, 2024
116 checks passed
@adonesky1 adonesky1 deleted the ad/update-queued-request-controller-barad-dur branch September 27, 2024 16:12
@adonesky1 adonesky1 mentioned this pull request Sep 27, 2024
adonesky1 added a commit that referenced this pull request Sep 30, 2024
## Explanation

This is a RC for v210.0.0. See changelog for more details
- @metamask/queued-request-controller@5.1.0

# [5.1.0]

### Changed

- Batch processing now considers both origin and `networkClientId`,
ensuring requests targeting different networks are processed separately.
([#4718](#4718))
- Incoming requests to `enqueueRequest` now must include a
`networkClientId`; an error is thrown if it's missing. This was
previously a required part of the type but since consumers like the
extension do not have extensive typescript coverage this wasn't
definitively enforced.
([#4718](#4718))
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