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

Request Queue Controller #1806

Merged
merged 57 commits into from
Oct 23, 2023
Merged

Request Queue Controller #1806

merged 57 commits into from
Oct 23, 2023

Conversation

BelfordZ
Copy link
Contributor

@BelfordZ BelfordZ commented Oct 11, 2023

Explanation

What is the current state of things and why does it need to change?

Currently, dapps must keep track of what metamasks selected network is, and switch the network appropriately. We recently added a selectedNetworkController that keeps track of what dapp has requested what network. This PR adds some middleware that will automatically prompt the user to switch metamasks network if the saved network for the dapp doesnt match. It also adds a queue, which will cause requests which require confirmations to be processed in a first-in-first-out manner.

The queued request controller provides this api mechanism and serves as a place to interate on the queueing implementation.

The selected network controller is updated to provide a proxy provider - that is, a provider that will be swapped out based on the dapps network selections.

References

needs these
Parent issue: https://github.com/MetaMask/MetaMask-planning/issues/1316

Changelog

@metamask/queued-request-controller

  • ADDED: controller & middleware

@metamask/selected-network-controller

  • ADDED: A method to get a proxy provider for a given origin/domain.
  • CHANGED: No longer update selectedNetworkClientId when the networkcontroller provider changes.

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
Copy link
Contributor Author

@metamaskbot publish-preview

@socket-security
Copy link

socket-security bot commented Oct 11, 2023

New dependencies detected. Learn more about Socket for GitHub ↗︎

Packages Version New capabilities Transitives Size Publisher
@metamask/selected-network-controller 3.0.0 None +0 28.6 kB metamaskbot

@github-actions
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": "2.0.2-preview.26b130a",
  "@metamask-previews/address-book-controller": "3.1.4-preview.26b130a",
  "@metamask-previews/announcement-controller": "4.0.2-preview.26b130a",
  "@metamask-previews/approval-controller": "4.0.0-preview.26b130a",
  "@metamask-previews/assets-controllers": "15.0.0-preview.26b130a",
  "@metamask-previews/base-controller": "3.2.3-preview.26b130a",
  "@metamask-previews/composable-controller": "3.0.2-preview.26b130a",
  "@metamask-previews/controller-utils": "5.0.2-preview.26b130a",
  "@metamask-previews/ens-controller": "5.0.2-preview.26b130a",
  "@metamask-previews/gas-fee-controller": "8.0.0-preview.26b130a",
  "@metamask-previews/keyring-controller": "8.0.2-preview.26b130a",
  "@metamask-previews/logging-controller": "1.0.3-preview.26b130a",
  "@metamask-previews/message-manager": "7.3.5-preview.26b130a",
  "@metamask-previews/name-controller": "3.0.1-preview.26b130a",
  "@metamask-previews/network-controller": "14.0.0-preview.26b130a",
  "@metamask-previews/notification-controller": "3.1.3-preview.26b130a",
  "@metamask-previews/permission-controller": "5.0.0-preview.26b130a",
  "@metamask-previews/phishing-controller": "7.0.0-preview.26b130a",
  "@metamask-previews/polling-controller": "0.1.0-preview.26b130a",
  "@metamask-previews/preferences-controller": "4.4.2-preview.26b130a",
  "@metamask-previews/queued-request-controller": "1.0.0-preview.26b130a",
  "@metamask-previews/rate-limit-controller": "3.0.2-preview.26b130a",
  "@metamask-previews/selected-network-controller": "2.0.1-preview.26b130a",
  "@metamask-previews/signature-controller": "6.1.2-preview.26b130a",
  "@metamask-previews/transaction-controller": "14.0.0-preview.26b130a"
}

@BelfordZ BelfordZ mentioned this pull request Oct 11, 2023
3 tasks
@BelfordZ BelfordZ changed the title Queue2 Request Queue Controller Oct 11, 2023
@BelfordZ
Copy link
Contributor Author

@metamaskbot publish-preview

@github-actions
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": "2.0.2-preview.885fefd",
  "@metamask-previews/address-book-controller": "3.1.4-preview.885fefd",
  "@metamask-previews/announcement-controller": "4.0.2-preview.885fefd",
  "@metamask-previews/approval-controller": "4.0.0-preview.885fefd",
  "@metamask-previews/assets-controllers": "15.0.0-preview.885fefd",
  "@metamask-previews/base-controller": "3.2.3-preview.885fefd",
  "@metamask-previews/composable-controller": "3.0.2-preview.885fefd",
  "@metamask-previews/controller-utils": "5.0.2-preview.885fefd",
  "@metamask-previews/ens-controller": "5.0.2-preview.885fefd",
  "@metamask-previews/gas-fee-controller": "8.0.0-preview.885fefd",
  "@metamask-previews/keyring-controller": "8.0.2-preview.885fefd",
  "@metamask-previews/logging-controller": "1.0.3-preview.885fefd",
  "@metamask-previews/message-manager": "7.3.5-preview.885fefd",
  "@metamask-previews/name-controller": "3.0.1-preview.885fefd",
  "@metamask-previews/network-controller": "14.0.0-preview.885fefd",
  "@metamask-previews/notification-controller": "3.1.3-preview.885fefd",
  "@metamask-previews/permission-controller": "5.0.0-preview.885fefd",
  "@metamask-previews/phishing-controller": "7.0.0-preview.885fefd",
  "@metamask-previews/polling-controller": "0.1.0-preview.885fefd",
  "@metamask-previews/preferences-controller": "4.4.2-preview.885fefd",
  "@metamask-previews/queued-request-controller": "1.0.0-preview.885fefd",
  "@metamask-previews/rate-limit-controller": "3.0.2-preview.885fefd",
  "@metamask-previews/selected-network-controller": "2.0.1-preview.885fefd",
  "@metamask-previews/signature-controller": "6.1.2-preview.885fefd",
  "@metamask-previews/transaction-controller": "14.0.0-preview.885fefd"
}

@socket-security
Copy link

socket-security bot commented Oct 13, 2023

👍 Dependency issues cleared. Learn more about Socket for GitHub ↗︎

This PR previously contained dependency changes with security issues that have been resolved, removed, or ignored.

@BelfordZ BelfordZ force-pushed the queue2 branch 2 times, most recently from 6d59afb to 4e2d674 Compare October 17, 2023 20:39
@BelfordZ
Copy link
Contributor Author

@metamaskbot publish-preview

@BelfordZ
Copy link
Contributor Author

@metamaskbot publish-preview

jiexi
jiexi previously approved these changes Oct 23, 2023
@BelfordZ BelfordZ dismissed stale reviews from jiexi and adonesky1 via 4a82100 October 23, 2023 18:13
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.

One question/suggestion, but I'm good on this otherwise.

packages/queued-request-controller/package.json Outdated Show resolved Hide resolved
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.

Looks good!

@BelfordZ
Copy link
Contributor Author

@metamaskbot publish-preview

@MajorLift
Copy link
Contributor

MajorLift commented Oct 23, 2023

Here are the updates for consolidating messenger and AllowedActions typing: 9bd3dae.
Passes all test cases.

@mcmire
Copy link
Contributor

mcmire commented Oct 23, 2023

@MajorLift What do you think if we included those changes in a new PR? I know there are some other refactors mentioned here that we could possibly include there or in another PR as well.

@MajorLift
Copy link
Contributor

Yes the typing fixes could definitely be done in a separate PR.👍

@BelfordZ BelfordZ merged commit 345f6cb into main Oct 23, 2023
115 checks passed
@BelfordZ BelfordZ deleted the queue2 branch October 23, 2023 19:59
MajorLift added a commit that referenced this pull request Nov 29, 2023
…wareMessenger` pattern, fix `any` (#1970)

## Explanation

### Apply controller-messenger pattern
- Defines `QueuedRequestMiddlewareMessenger`,
`SelectedNetworkMiddlewareMessenger` types to represent external
controller messengers in the middleware chain.
  - Reserved for internal use and not exported at package level.
- Defines separate allow lists for
`QueuedRequest{Controller,Middleware}Messenger` and
`SelectedNetwork{Controller,Middleware}Messenger`.

### Type fixes
- Defines and exports `QueuedRequestMiddlewareJsonRpcRequest`,
`SelectedNetworkMiddlewareJsonRpcRequest` types.
- Fix `any` usage in `createSelectedNetworkMiddleware`

### Tests
- Fixes `any`, `as any` usage in `QueuedRequestController`,
`QueuedRequestMiddleware` tests.
- Moves util functions for building test messengers into individual test
files.
- For context:
#1970 (comment)

## References

- Follow-up from #1806
- Builds on #2051
- Closes #2038

## Changelog

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

- **ADDED**: Add `QueuedRequestMiddlewareJsonRpcRequest` typee
- **BREAKING:** `QueuedRequestControllerMessenger` can no longer be
defined with any allowed actions or events.
- **CHANGED**: Move `@metamask/approval-controller` from devDeps to
deps.

### `@metamask/selected-network-controller`

- **ADDED**: Add `SelectedNetworkMiddlewareJsonRpcRequest` type
- **BREAKING:** Rename `SelectedNetworkControllerAction` to
`SelectedNetworkControllerActions` and `SelectedNetworkControllerEvent`
to `SelectedNetworkControllerEvents` for consistency with corresponding
type exports from other controllers.
- **BREAKING:** `createSelectedNetworkMiddleware` return type is
constrained to satisfy `JsonRpcMiddleware<JsonRpcParams, Json>`, and the
`req` parameter needs to satisfy
`SelectedNetworkMiddlewareJsonRpcRequest`.

## Checklist

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

---------

Co-authored-by: Elliot Winkler <[email protected]>
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.

5 participants