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

feat: NetworkController changes to support Network Syncing #4939

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

Conversation

Prithpal-Sooriya
Copy link
Contributor

@Prithpal-Sooriya Prithpal-Sooriya commented Nov 15, 2024

Explanation

A set of changes to support network syncing. This is a big scary change, so will have a 1:1 with assets team and pair review this.

I'll also record a loom vid for a quick code walkthrough.

References

N/A

Changelog

@metamask/network-controller

  • ADDED: logic to use added network networkClientId if provided instead of uuidV4()
  • ADDED: new method dangerouslySetNetworkConfiguration to cautiously overwrite a given network configuration. Used for network syncing, where we need to override local state with the synced remote state.

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
  • I've prepared draft pull requests for clients and consumer packages to resolve any breaking changes

this is to support network syncing, where we add a new network to a synced device
@Prithpal-Sooriya Prithpal-Sooriya changed the title feat: use original networkClientId if provided when adding a network feat: NetworkController changes to support Network Syncing Nov 15, 2024
@Prithpal-Sooriya Prithpal-Sooriya marked this pull request as draft November 15, 2024 15:21
@Prithpal-Sooriya Prithpal-Sooriya force-pushed the feat/add-network-controller-changes-to-support-network-syncing branch from 417566b to 6e65961 Compare November 19, 2024 12:16
…rkConfiguration

this is used for network syncing to override local state with the remove state.
* This will subsequently update the network client registry; state.networksMetadata, and state.selectedNetworkClientId
* @param networkConfiguration - the network configuration to override
*/
async dangerouslySetNetworkConfiguration(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, I understand that this is a lot of logic here. I've ensured that we correctly update controller state; as well as internal data structures used inside the controller.

Fundamentally we need this method as we are unable to sync/override networks due to the random (uuidV4()) networkClientIds assigned to different devices (hence we were not able to use the existing updatedNetwork method).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The name for this is a bit of a throwback to Reacts dangerouslySetInnerHTML. I want to signify to developers that we should not use this method, and we need to be very cautious when using this method.

Copy link
Contributor

Choose a reason for hiding this comment

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

@Prithpal-Sooriya Should this method be private then? Is this one used programmatically in the extension via this.networkController.dangerouslySetNetworkConfiguration or only called through the messaging system?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will need to investigate, can private methods be used in the messaging system.

Copy link
Contributor Author

@Prithpal-Sooriya Prithpal-Sooriya Nov 20, 2024

Choose a reason for hiding this comment

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

Updated this method to private as it is only accessible through the messaging system - good callout!

@@ -11410,6 +11436,205 @@ describe('NetworkController', () => {
});
});

describe('dangerouslySetNetworkConfiguration', () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This test file is large. Understandably since the network controller is responsible for MANY things and logic.

I've tried to keep these tests isolated and lean.

@Prithpal-Sooriya Prithpal-Sooriya marked this pull request as ready for review November 19, 2024 12:22
…-controller-changes-to-support-network-syncing
@mcmire
Copy link
Contributor

mcmire commented Nov 19, 2024

Hello! I have some concerns here and would definitely love to understand the use case for the new method you're adding. As you've mentioned elsewhere, there is a lot going on already in the network controller and I just want to make sure that we absolutely need a different way of updating a network configuration.

@Prithpal-Sooriya
Copy link
Contributor Author

Prithpal-Sooriya commented Nov 19, 2024

@mcmire yep I agree, this is a very big change. As mentioned in the PR description, I'm planning on having a 1:1 with the assets team and also include a code walkthrough.

Happy to include you and discuss in DMs the context for this change.

@mcmire
Copy link
Contributor

mcmire commented Nov 19, 2024

@Prithpal-Sooriya That would be great, thanks!

mathieuartu
mathieuartu previously approved these changes Nov 20, 2024
Copy link
Contributor

@mathieuartu mathieuartu left a comment

Choose a reason for hiding this comment

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

Amazing work!

* This will subsequently update the network client registry; state.networksMetadata, and state.selectedNetworkClientId
* @param networkConfiguration - the network configuration to override
*/
async dangerouslySetNetworkConfiguration(
Copy link
Contributor

Choose a reason for hiding this comment

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

@Prithpal-Sooriya Should this method be private then? Is this one used programmatically in the extension via this.networkController.dangerouslySetNetworkConfiguration or only called through the messaging system?

This is now only accessible through the messaging systems
…-controller-changes-to-support-network-syncing
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