-
-
Notifications
You must be signed in to change notification settings - Fork 192
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
fix: add SelectedNetworkController
setNetworkClientId useRequestQueuePreference guard
#4388
Conversation
There was a problem hiding this 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', () => { |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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 () => { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
addressed here c30f5d8
) Core PR: MetaMask/core#4388 Patch Branch: [jl/patch-selected-network-controller-12.0.1-setNetworkClientId-guard](https://github.com/MetaMask/core/tree/jl/patch-selected-network-controller-12.0.1-setNetworkClientId-guard)
) Core PR: MetaMask/core#4388 Patch Branch: [jl/patch-selected-network-controller-12.0.1-setNetworkClientId-guard](https://github.com/MetaMask/core/tree/jl/patch-selected-network-controller-12.0.1-setNetworkClientId-guard)
); | ||
expect(controller.state.domains.metamask).toBeUndefined(); | ||
}); | ||
|
||
describe('when the useRequestQueue is false', () => { | ||
describe('when the requesting domain is not metamask', () => { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done here 7d594d9
…kClientIdForDomain-guard
There was a problem hiding this 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
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 |
There was a problem hiding this comment.
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 () => { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 () => { |
There was a problem hiding this comment.
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 () => { |
There was a problem hiding this comment.
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"?
if (!this.#useRequestQueuePreference) { | ||
return; | ||
} |
There was a problem hiding this comment.
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;
}
2afe972
to
e1a71e5
Compare
…kClientIdForDomain-guard
apologies, accidentally comingled some changes intended for another branch |
There was a problem hiding this 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!
…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]>
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 thesetNetworkClientId()
method itselfReferences
Changelog
@metamask/selected-network-controller
setNetworkClientId()
will now result in a noop ifuseRequestQueuePreference
is falseChecklist