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

selected network controller: redirect domains to default endpoint #4679

Merged
merged 3 commits into from
Sep 11, 2024

Conversation

bergeron
Copy link
Contributor

@bergeron bergeron commented Sep 10, 2024

Explanation

Changes how the SelectedNetworkController responds to state updates from the network controller. Now when the default RPC endpoint changes for a network, it will redirect domains that were referencing a network client id on that network to the new default endpoint for that network. Instead of redirecting them to the globally selected network, as happened before.

Accompanies MetaMask/metamask-extension#26433

References

Changelog

@metamask/selected-network-controller

  • CHANGED: When the default RPC endpoint changes for a network, domains that were referencing a network client id on that network are redirected to the new default RPC endpoint.

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

@bergeron bergeron marked this pull request as ready for review September 10, 2024 18:09
@bergeron bergeron requested review from a team as code owners September 10, 2024 18:09
@bergeron bergeron requested a review from jiexi September 10, 2024 18:09
Comment on lines +215 to +225
Object.entries(this.state.domains).forEach(
([domain, networkClientIdForDomain]) => {
const chainIdForDomain =
networkClientIdToChainId[networkClientIdForDomain];

if (patch.op === 'remove' && !chainIdForDomain) {
// If the network was removed, fall back to the globally selected network
this.setNetworkClientIdForDomain(
domain,
selectedNetworkClientId,
);
Copy link
Contributor

Choose a reason for hiding this comment

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

Wouldn't this set the chainId for the domain to the globally selected network if we just removed the rpcUrl that it was connected to? I would assume in this case we'd want to replace it with the new "default" rpcUrl for that chainId? And, if that network was removed entirely (not just the rpcUrl) we'd set it to the globally selected network?

Copy link
Contributor

Choose a reason for hiding this comment

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

oh does the remove op only apply if the entire chainId is removed?

Copy link
Contributor Author

@bergeron bergeron Sep 10, 2024

Choose a reason for hiding this comment

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

Yes, the case you described of removing an RPC url falls into the replace operation.
Deleting the whole network falls into the remove operation.

@bergeron
Copy link
Contributor Author

bergeron commented Sep 10, 2024

I also updated the migration in MetaMask/metamask-extension#26433 to point domains to their default endpoint so its correct initially.

Comment on lines +199 to +202
const patch = patches.find(
({ op, path }) =>
(op === 'replace' || op === 'remove') &&
path[0] === 'networkConfigurationsByChainId',
Copy link
Contributor

Choose a reason for hiding this comment

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

Thoughts on using filter vs find? Not that it does currently, but patches could include multiple state changes

Copy link
Contributor Author

@bergeron bergeron Sep 10, 2024

Choose a reason for hiding this comment

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

This whole technique won't work if there are multiple changes. It relies on the fact that the network controller only exposes an APIs to update or remove a single network at a time.

If multiple networks were updated in 1 update, you wouldn't be able to correlate the missing network client ids to a particular chain id. Since there's only 1, we can do it with path[1].

This is relevant for the case where you simultaneously delete the default endpoint and replace it with a new one in 1 update

Copy link
Contributor Author

Choose a reason for hiding this comment

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

unless I missed some overload of subscribe that gives you enough information to do so

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!

@bergeron bergeron merged commit 6212abe into main Sep 11, 2024
116 checks passed
@bergeron bergeron deleted the brian/temp-selected-network-redirect-to-default2 branch September 11, 2024 03:19
bergeron added a commit to MetaMask/metamask-extension that referenced this pull request Sep 11, 2024
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