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

fix: add SelectedNetworkController setNetworkClientId useRequestQueuePreference guard #4388

Conversation

jiexi
Copy link
Contributor

@jiexi jiexi commented Jun 6, 2024

Explanation

Our previous SelectedNetworkController fix that added a guard to permission state changes was not sufficient. This PR properly addresses the underlying issue of setting networkClientId for domains when the useRequestQueuePreference flag is false by moving the guard into the setNetworkClientId() method itself

References

Changelog

@metamask/selected-network-controller

  • FIXED: setNetworkClientId() will now result in a noop if useRequestQueuePreference is false

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

Gudahtt
Gudahtt previously approved these changes Jun 7, 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! Left a couple of suggestions for additional tests though

@@ -225,7 +225,7 @@ describe('SelectedNetworkController', () => {
});
});

describe('It updates domain state when the network controller state changes', () => {
describe('networkController:stateChange and useRequestQueuePreference is true', () => {
Copy link
Member

Choose a reason for hiding this comment

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

Nit: We may want a test for the opposite condition (i.e. when useRequestQueue is false)

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 fd79acf

@@ -742,11 +746,12 @@ describe('SelectedNetworkController', () => {
});

describe('Constructor checks for domains in permissions', () => {
it('should set networkClientId for domains not already in state', async () => {
it('should set networkClientId for domains not already in state when useRequestQueuePreference is true', async () => {
Copy link
Member

Choose a reason for hiding this comment

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

Nit: Likewise here, we may want to test the opposite case

Copy link
Contributor Author

Choose a reason for hiding this comment

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

addressed here c30f5d8

Gudahtt pushed a commit to MetaMask/metamask-extension that referenced this pull request Jun 7, 2024
Gudahtt pushed a commit to MetaMask/metamask-extension that referenced this pull request Jun 7, 2024
);
expect(controller.state.domains.metamask).toBeUndefined();
});

describe('when the useRequestQueue is false', () => {
describe('when the requesting domain is not metamask', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit we can probably get rid of this nested describe block when the requesting domain is not metamask since the test applies whether or not the request domain is MetaMask

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 7d594d9

adonesky1
adonesky1 previously approved these changes Jun 7, 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.

Both of my comments are non blocking

@jiexi jiexi dismissed stale reviews from adonesky1 and Gudahtt via fd79acf June 7, 2024 17:38
@jiexi jiexi requested review from Gudahtt and adonesky1 June 7, 2024 18:07
it('does not update the networkClientId for domains which were previously set to the deleted networkClientId', () => {
const { controller, messenger } = setup({
state: {
// normally there would not be any domains in state if useRequestQueuePreference is false
Copy link
Contributor

@adonesky1 adonesky1 Jun 7, 2024

Choose a reason for hiding this comment

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

Lets remove this whole block now since this state shouldn't be possible?

domains: {
'existingdomain.com': 'initialNetworkId',
describe('when useRequestQueuePreference is false', () => {
it('should set networkClientId for domains not already in state', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

So this seems like another one where there shouldn't be domains in state so maybe we don't need this test? But if we're going to have it the networkClientId for the existing domain should probably change?

Copy link
Contributor

Choose a reason for hiding this comment

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

But it seems confusing to have this test at all.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry I misphrased my initial comment. This implication of this test is actually backwards. It should be should **not** set networkClientId for domains not already in state

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 the only one that I think is kindof egregious enough to block merging

Copy link
Contributor Author

Choose a reason for hiding this comment

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

addressed here d16bc7f

expect(controller.state.domains['existingdomain.com']).toBe(
'initialNetworkId',
);
it('should not modify domains already in state', async () => {
Copy link
Contributor

@adonesky1 adonesky1 Jun 7, 2024

Choose a reason for hiding this comment

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

This seems like one where there shouldn't be domains in state so maybe we don't need this test

});

// Now, 'newdomain.com' should have the selectedNetworkClientId set
expect(controller.state.domains['newdomain.com']).toBe('mainnet');
it('should not modify domains already in state', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

this test seems redundant since the previous one tests this condition too?

Seems like the previous test could be called "should set networkClientId for domains not already in state and not modify domains already in state"?

Comment on lines +310 to +312
if (!this.#useRequestQueuePreference) {
return;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

nit this could be bundled with the snaps check below which returns

   if (snapsPrefixes.some((prefix) => domain.startsWith(prefix)) || !this.#useRequestQueuePreference) {
      return;
    }

@jiexi jiexi closed this Jun 10, 2024
@jiexi jiexi force-pushed the jl/fix-selected-network-controller-setNetworkClientIdForDomain-guard branch from 2afe972 to e1a71e5 Compare June 10, 2024 19:26
@jiexi jiexi reopened this Jun 10, 2024
@jiexi
Copy link
Contributor Author

jiexi commented Jun 10, 2024

apologies, accidentally comingled some changes intended for another branch

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! I'll do some of the clean up I suggested here in follow up!

@jiexi jiexi merged commit 1900a9d into main Jun 10, 2024
113 checks passed
@jiexi jiexi deleted the jl/fix-selected-network-controller-setNetworkClientIdForDomain-guard branch June 10, 2024 21:21
adonesky1 added a commit to MetaMask/metamask-extension that referenced this pull request Jun 12, 2024
…work domain is incorrectly set (#25106)

## **Description**

Pulls [this fix](MetaMask/core#4388) which adds
a guard to the `setNetworkClientIdForDomain` method on the
`SelectedNetworkController` to no longer add domains to domains state
(nor create a selected network proxy for them) unless the
`useRequestQueuePreference` flag is true

Bumping the `@metamask/selected-network-controller` version required
[bumping peer
dependencies](https://github.com/MetaMask/core/blob/main/packages/selected-network-controller/CHANGELOG.md#changed):
 - `@metamask/network-controller` to v19.0.0
    - An existing patch was repointed to this version.
 - `@metamask/permission-controller` to v10.0.0
-[ This required bumping its own
peerDep](https://github.com/MetaMask/core/blob/main/packages/selected-network-controller/CHANGELOG.md#changed)
`@metamask/approval-controller` to v7.0.0

[![Open in GitHub
Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/25106?quickstart=1)

## **Related issues**

See: https://consensys.slack.com/archives/C1L7H42BT/p1717512150509719

## **Manual testing steps**

1. Open settings -> experimental and toggle of the `Select networks for
each site` setting
2. Go to any site
3. Open the console and execute:
```
await window.ethereum.request({
  "method": "eth_chainId",
  "params": []
});
```
4. See that it matches the globally selected network
5. Manually change the network with the network switcher
6. Go back to the site and execute the same `eth_chainId` script
7. See that the result has changed to the chainId you switched to
8. Now execute
```
await window.ethereum.request({
  "method": "wallet_requestPermissions",
  "params": [
    {
      "eth_accounts": {}
    }
  ]
});
```
and connect an account to the site
9. Repeat steps 1 - 7
10. The `eth_chainId` results should still match the globally selected
network

## **Screenshots/Recordings**

<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->

### **Before**

<!-- [screenshots/recordings] -->


https://github.com/MetaMask/metamask-extension/assets/34557516/ff2f68ff-6ec8-495f-96c1-e938973ce59b


### **After**

<!-- [screenshots/recordings] -->


https://github.com/MetaMask/metamask-extension/assets/34557516/6061f4e2-faab-48a5-8c55-3b2a4e3aa9f2


## **Pre-merge author checklist**

- [ ] I’ve followed [MetaMask Coding
Standards](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/CODING_GUIDELINES.md).
- [ ] I've completed the PR template to the best of my ability
- [ ] I’ve included tests if applicable
- [ ] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [ ] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.

## **Pre-merge reviewer checklist**

- [ ] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [ ] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.

---------

Co-authored-by: MetaMask Bot <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: Cannot get the latest value after switching the network
3 participants