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 SelectedNetworkController to use chainId #4670

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

jiexi
Copy link
Contributor

@jiexi jiexi commented Sep 9, 2024

Explanation

DRAFT

References

Changelog

@metamask/package-a

  • : Your change here
  • : Your change here

@metamask/package-b

  • : Your change here
  • : Your change here

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

}
}
});
},
);

// TODO also monitor the defaultRpcEndpoint for a chainId changing
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this something that needs to be done? Feels like the only thing that needs to be reacted to is the whole network/chain being deleted.

Copy link
Contributor

@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.

If moving to chain ids is too much effort in the short term, a similar effect can be achieved by redirecting domains to the default endpoint when it changes: #4679

Copy link
Contributor

Choose a reason for hiding this comment

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

Hey @bergeron yeah Jiexi and I strategized around this and decided to pursue both options.
"redirecting domains to the default endpoint when it changes" per your and my conversation at first - in case the work in this PR takes a bit longer to merge.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh but I'm just seeing your PR #4679. We were thinking we could add this logic using the SelectedNetworkController's public API in the extension client in your PR first

Copy link
Contributor

Choose a reason for hiding this comment

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

in the extension

Do you have more precise idea for how this would work?

Here's the 2 reasons I implemented it via #4679 instead of through the extension.

(1) The SelectedNetworkController will need changes either way, since the current NetworkController:stateChange subscriber doesn't do what we want. Deleting a single endpoint currently gets it defaulted to the global network client instead of the network's default. So we'll be editing that subscriber anyway.

(2) This case where you both delete the default endpoint, and replace it with a new default in 1 operation to updateNetwork:

Screen.Recording.2024-09-10.at.8.12.19.AM.mov

The extension doesn't know the new endpoint's network client id until the controller autogenerates it. So domain edits have to occur in response to the state update, and the SelectedNetworkController's seemed most convenient as it has everything to handle both removes and edits in 1 place.

Copy link
Contributor

Choose a reason for hiding this comment

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

(1) The SelectedNetworkController will need changes either way, since the current NetworkController:stateChange subscriber doesn't do what we want. Deleting a single endpoint currently gets it defaulted to the global network client instead of the network's default. So we'll be editing that subscriber anyway.

I suppose this does make it tricky. I was thinking we could subscribe to this event in the MetaMask-Controller and call setNetworkClientDomain using the SelectedNetworkController's public API (similar pattern to what we do in several ways here) But if we're competing with the subscription already in the controller we could have some funky race conditions...

@jiexi
Copy link
Contributor Author

jiexi commented Sep 10, 2024

@metamask-bot publish-preview

@jiexi
Copy link
Contributor Author

jiexi commented Sep 10, 2024

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

chainIdForDomain === patch.path[1]
) {
// If the network was updated, redirect to the network's default endpoint
this.setChainIdForDomain(domain, chainIdForDomain);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We don't have a good way to inspect what rpc endpoint the network client proxy for the domain is currently pointed at, so we repoint the network client proxy for the domain at the default network client for the chainId any time the network configuration for that chainId changes

Comment on lines -289 to +298
const originNetworkClientId = this.messagingSystem.call(
'SelectedNetworkController:getNetworkClientIdForDomain',
const originChainId = this.messagingSystem.call(
'SelectedNetworkController:getChainIdForDomain',
this.#originOfCurrentBatch,
);
const originNetworkClientId = this.messagingSystem.call(
'NetworkController:getDefaultNetworkClientIdForChainId',
originChainId,
);
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this the bit you were saying was awkward? Yeah I see that. Not a big deal though. And we're going to be removing the need for these calls altogether very soon!

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