Skip to content

chore: create network enablement controller #6028

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

Open
wants to merge 22 commits into
base: main
Choose a base branch
from

Conversation

salimtb
Copy link
Contributor

@salimtb salimtb commented Jun 24, 2025

Explanation

The NetworkEnablementController is a core implementation of network enablement functionality that was previously only available in the extension. This PR moves and expands this functionality from the extension's NetworkOrder controller to core, enabling network visibility features across both extension and mobile platforms. It also renames it to NetworkEnablementController to more accurately represent what it does. Maybe in the future this can be expanded to include other UX enhancements that don't need to be tightly coupled with the NetworkController

Key motivations for this change:

Network Enablement Feature: This work is part of the broader Network Enablement initiative (#5737). The controller has been expanded to include enabledNetworkMap state, allowing us to:

Track which networks are enabled/disabled
Support the new network enablement feature
Code Quality Improvements: The move to core has provided an opportunity to:

Improve test coverage of the existing network ordering logic
The controller now handles three key aspects:

Network enablement (new functionality. recently introduced on extension)

References

Changelog

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 communicated my changes to consumers by updating changelogs for packages I've changed, highlighting breaking changes as necessary
  • I've prepared draft pull requests for clients and consumer packages to resolve any breaking changes

@salimtb salimtb marked this pull request as ready for review June 24, 2025 16:47
@salimtb salimtb requested a review from a team as a code owner June 24, 2025 16:47
Copy link
Contributor

@mcmire mcmire left a comment

Choose a reason for hiding this comment

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

Looks pretty good, just had some comments. Most are minor but there are a few that would be good to address.

@salimtb salimtb requested a review from a team as a code owner July 22, 2025 11:33
Copy link

socket-security bot commented Jul 22, 2025

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Added@​metamask/​multichain-network-controller@​0.9.0971007497100

View full report

cursor[bot]

This comment was marked as outdated.

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Bug: Nested Update Calls Cause State Inconsistencies

Nested this.update() calls occur when #toggleNetwork calls #ensureNetworkEntry within its this.update() callback, as #ensureNetworkEntry also performs a this.update() operation. This anti-pattern can lead to state inconsistencies, race conditions, and unpredictable behavior in the BaseController's state management. The logic from #ensureNetworkEntry should be inlined into the outer update callback.

packages/network-enablement-controller/src/NetworkEnablementController.ts#L467-L485

this.update((s) => {
// Ensure entry exists first
this.#ensureNetworkEntry(chainId);
if (this.#hasOneEnabled(s, namespace, chainId)) {
return;
}
// If enabling a non-popular network, disable all networks in the same namespace
if (enable && !this.#isPopularNetwork(caipId)) {
Object.keys(s.enabledNetworkMap[namespace]).forEach((key) => {
s.enabledNetworkMap[namespace][key] = false;
});
}
s.enabledNetworkMap[namespace][storageKey] = enable;
});
}

Fix in CursorFix in Web


Was this report helpful? Give feedback by reacting with 👍 or 👎

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.

4 participants