Skip to content

Conversation

@mikesposito
Copy link
Member

@mikesposito mikesposito commented Aug 26, 2024

Explanation

Currently, SelectedNetworkController subscribes to the NetworkController:stateChange event to make sure that network client ids associated with managed domains stay valid. Though, we are currently checking on the specific immer patch applied to the NetworkController state: this means that based on how the state draft was manipulated on the origin controller, we may or may not capture the change and react to it.

To fix it, now SelectedNetworkController will look at the actual content of the updated state, instead of relying on immer patches. To reduce the amount of event instances received by the controller, a selector is applied to the subscription, filtering out irrelevant instances.

References

Changelog

@metamask/network-controller

  • ADDED: Added getNetworkConfigurations, getAvailableNetworkClientIds and selectAvailableNetworkClientIds selectors
    • These new selectors can be applied to messenger event subscriptions

@metamask/selected-network-controller

No consumer-faced changes

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

@mikesposito mikesposito changed the base branch from main to group-network-configurations-by-chain August 26, 2024 10:27
@mikesposito mikesposito changed the title fix: SelectedNetworkController expected network stateChange patch fix: SelectedNetworkController network stateChange listener Aug 26, 2024
@mikesposito mikesposito force-pushed the fix/selected-network-controller-chainid-networks branch from 290f23a to e0fda92 Compare August 26, 2024 14:10
@bergeron
Copy link
Contributor

bergeron commented Aug 26, 2024

product change looks good, ive tested it via extension. Would be good to merge this to the main PR so we can publish a new preview release.

Edit: I committed it to the main PR for that reason

@mikesposito
Copy link
Member Author

@bergeron sorry about that, I realized just now that this was committed to the target branch already.
I just made an additional refactor here to introduce state selectors, so that SelectedNetworkController only receives the event when it's needed: d6aca05

@socket-security
Copy link

socket-security bot commented Aug 28, 2024

New dependencies detected. Learn more about Socket for GitHub ↗︎

Package New capabilities Transitives Size Publisher
npm/reselect@5.1.1 None 0 646 kB acemarke

View full report↗︎

Comment on lines +195 to +207
(availableNetworkClientIds) => {
// if a network is updated or removed, update the networkClientId for all domains
// that were using it to the selected network client id
const { selectedNetworkClientId } = this.messagingSystem.call(
'NetworkController:getState',
);
Object.entries(this.state.domains).forEach(
([domain, networkClientIdForDomain]) => {
if (!availableNetworkClientIds.includes(networkClientIdForDomain)) {
this.setNetworkClientIdForDomain(domain, selectedNetworkClientId);
}
},
);
Copy link
Member Author

Choose a reason for hiding this comment

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

The usage of immer patches has been removed since it strictly depends on how state changes are made on the controller that originates the event.

We can reduce the number of events received by this listener using a selector

@mikesposito mikesposito marked this pull request as ready for review August 28, 2024 15:22
@mikesposito mikesposito requested review from a team and bergeron August 28, 2024 15:22
@mikesposito mikesposito force-pushed the fix/selected-network-controller-chainid-networks branch from aa8d6f7 to e050d07 Compare September 2, 2024 10:01
@mikesposito mikesposito merged commit 21bb4d4 into group-network-configurations-by-chain Sep 2, 2024
@mikesposito mikesposito deleted the fix/selected-network-controller-chainid-networks branch September 2, 2024 10:25
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