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

Refactor: Expose SelectedNetworkController proxies map in the constructor params #4104

Merged
merged 6 commits into from
Mar 26, 2024

Conversation

jiexi
Copy link
Contributor

@jiexi jiexi commented Mar 22, 2024

Explanation

In a preceding PR, the SelectedNetworkController will start to keep proxy instances for all domains seen without a way to remove them when they become stale. This PR exposes the private proxies cache map as a constructor param which will enable callers to implement their own cache invalidation strategies without needing to concern the SelectedNetworkController of the implementation details themselves. This is needed because Mobile and Extension will need to follow different cache invalidation strategies.

This PR should be merged before #4063

References

Fixes: #4062
See: #4063

Changelog

@metamask/selected-network-controller

  • BREAKING: SelectedNetworkController now expects the domainProxyMap param which is a Map of Domain to NetworkProxy
  • ADDED: exports the Domain type

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

adonesky1
adonesky1 previously approved these changes Mar 25, 2024
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 based on the approach you carved out with @Gudahtt while I was away. Thanks for all your hard work here!

Gudahtt
Gudahtt previously approved these changes Mar 26, 2024
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!

@adonesky1 adonesky1 dismissed stale reviews from Gudahtt and themself via af889f7 March 26, 2024 14:20
@adonesky1 adonesky1 merged commit 319bcfc into main Mar 26, 2024
139 checks passed
@adonesky1 adonesky1 deleted the jl/selected-network-controller-domainProxyMap branch March 26, 2024 14:24
adonesky1 added a commit that referenced this pull request Mar 26, 2024
…orkClient from `getProviderAndBlockTracker` when domain not in state (#4063)

## Explanation

The SelectedNetworkController currently constructs a proxy for any
domain that has permissions. Other domains have no associated proxy, so
the getProviderAndBlockTracker method would throw an error.

This is problematic because we grab the network client for an origin a
single time when constructing an RPC pipeline for that origin in the
MetaMask extension. We don't re-create the RPC pipeline when permissions
change. That means that the pipeline is setup with the wrong network
client (see here for more details on this bug:
MetaMask/metamask-extension#23509 )

To resolve this problem, we can instead keep network client proxies for
all origins, not just those with permissions. Origins without
permissions still should not have a selected network client ID state,
but they can have proxies that point at the globally selected network
client. That way, when the origin is granted permissions, we can
redirect this proxy to the selected network client and everything works
smoothly, without needing to reconstruct the RPC pipeline.

This PR should be merged shortly after
#4104

## References
Fixes: #4062
See: #4104

## 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/network-controller`
- **ADDED**: Add `getSelectedNetworkClient()` method that returns the
provider and blockTracker for the currently selected network
- **ADDED**: Add 'NetworkController:getSelectedNetworkClient' action

### `@metamask/selected-network-controller`
- **BREAKING**: `SelectedNetworkController` now requires the
`NetworkController:getSelectedNetworkClient` messenger action
- **CHANGED**: Previously when a domain became no longer permissioned,
it's network client proxy would continue to point at the networkClientId
it was last set to. Now it is set to follow the globally selected
network
- **CHANGED**: `SelectedNetworkController. getProviderAndBlockTracker()`
no longer throws an error if the `useRequestQueue` flag is false
- **CHANGED**: Previously `SelectedNetworkController.
getProviderAndBlockTracker()` threw an error if there was no
networkClientId set for the passed domain. Now it returns a proxy that
follows the globally selected network instead.

## 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: Mark Stacey <markjstacey@gmail.com>
Co-authored-by: Jiexi Luan <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.

[selected-network-controller] Proxy all domains, even those without permissions
3 participants