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

Replace NetworkController w/ core version #19486

Merged
merged 6 commits into from
Jun 22, 2023
Merged

Conversation

mcmire
Copy link
Contributor

@mcmire mcmire commented Jun 7, 2023

Explanation

This commit fulfills a long-standing desire to get the extension using the same network controller as mobile by removing NetworkController from this repo and replacing it with NetworkController from the @metamask/network-controller package.

The new version of NetworkController is different the old one in a few ways:

  • The new controller inherits from BaseControllerV2, so the state property is used to access the state instead of store.getState(). All references of the latter have been replaced with the former.
  • As the new controller no longer has a store property, it cannot be subscribed to; the controller takes a messenger which can be subscribed to instead. There were various places within MetamaskController where the old way of subscribing has been replaced with the new way. In addition, DetectTokensController has been updated to take a messenger object so that it can listen for NetworkController state changes.
  • The state of the new controller is not updatable from the outside. This affected BackupController, which dumps state from NetworkController (among other controllers), but also loads the same state into NetworkController on import. A method loadBackup has been added to NetworkController to facilitate this use case, and BackupController is now using this method instead of attempting to call update on NetworkController.
  • The new controller does not have a getCurrentChainId method; instead, the chain ID can be read from the provider config in state. This affected MmiController. (MmiController was also updated to read custom networks from the new network controller instead of the preferences controller).
  • The default network that the new controller is set to is always Mainnet (previously it could be either localhost or Goerli in test mode, depending on environment variables). Technically this is true from the core perspective, but I've brought back the old logic so there should be no functional difference on this side of things.

Fixes #17931.

Manual Testing Steps

The new NetworkController should be functionally equivalent to the old one, so there should be no user-facing changes. That said, it would probably worth testing anything that involves the network (which, I know, is basically everything) to ensure that it works as before.

Pre-merge author checklist

  • I've clearly explained:
    • What problem this PR is solving
    • How this problem was solved
    • How reviewers can test my changes
  • Sufficient automated test coverage has been added

Pre-merge reviewer checklist

  • Manual testing (e.g. pull and build branch, run in browser, test code being changed)
  • PR is linked to the appropriate GitHub issue
  • IF this PR fixes a bug in the release milestone, add this PR to the release milestone

If further QA is required (e.g. new feature, complex testing steps, large refactor), add the Extension QA Board label.

In this case, a QA Engineer approval will be be required.

@github-actions
Copy link
Contributor

github-actions bot commented Jun 7, 2023

CLA Signature Action: All authors have signed the CLA. You may need to manually re-run the blocking PR check if it doesn't pass in a few minutes.

@mcmire mcmire force-pushed the upgrade-assets-controllers branch 5 times, most recently from 388c7f0 to 64a1b56 Compare June 8, 2023 21:33
Base automatically changed from upgrade-assets-controllers to develop June 9, 2023 20:48
@mcmire
Copy link
Contributor Author

mcmire commented Jun 9, 2023

Blocked by MetaMask/core#1421

@mcmire mcmire added the DO-NOT-MERGE Pull requests that should not be merged label Jun 9, 2023
@mcmire mcmire force-pushed the replace-network-controller branch 3 times, most recently from 9599182 to cf5c74f Compare June 15, 2023 21:16
@@ -208,8 +218,8 @@ export default class DetectTokensController {
this.interval = DEFAULT_INTERVAL;
}

getChainIdFromNetworkStore(network) {
return network?.store.getState().providerConfig.chainId;
getChainIdFromNetworkStore() {
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 argument for this method was removed because the thing it is trying to access is already accessible as a property.

app/scripts/controllers/mmi-controller.js Show resolved Hide resolved
app/scripts/metamask-controller.actions.test.js Outdated Show resolved Hide resolved
},
getNetwork: () =>
this.networkController.store.getState().networkId ?? 'loading',
onNetworkStateChange: networkControllerMessenger.subscribe.bind(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It seemed that previously we were copying the state but making no other changes to the state. I've replaced this with a direct bind to the stateChange event.

Copy link
Member

Choose a reason for hiding this comment

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

Looks like this was originally done to work around the need to rename provider to providerConfig (see e0ff039), but I must have missed that it was no longer necessary after that was finally renamed in d1cea85#diff-6fbff2cfe97ac01b77296ef2122c7e0a5b3ff6a84b584b4d1a87482f35eea3d6

@mcmire mcmire force-pushed the replace-network-controller branch from cf5c74f to c658295 Compare June 15, 2023 21:23
@socket-security
Copy link

socket-security bot commented Jun 15, 2023

New and updated dependency changes detected. Learn more about Socket for GitHub ↗︎

🚮 Removed packages: @json-rpc-specification/meta-schema@1.0.6, @types/jest-when@3.5.2, jest-when@3.5.2

@mcmire mcmire force-pushed the replace-network-controller branch 3 times, most recently from e176f77 to fc8f266 Compare June 15, 2023 22:19
@socket-security
Copy link

socket-security bot commented Jun 16, 2023

👍 Dependency issues cleared. Learn more about Socket for GitHub ↗︎

This PR previously contained dependency changes with security issues that have been resolved, removed, or ignored.

Next steps

Take a deeper look at the dependency

Take a moment to review the security alert above. Review the linked package source code to understand the potential risk. Ensure the package is not malicious before proceeding. If you're unsure how to proceed, reach out to your security team or ask the Socket team for help at support [AT] socket [DOT] dev.

Remove the package

If you happen to install a dependency that Socket reports as Known Malware you should immediately remove it and select a different dependency. For other alert types, you may may wish to investigate alternative packages or consider if there are other ways to mitigate the specific risk posed by the dependency.

Mark a package as acceptable risk

To ignore an alert, reply with a comment starting with @SocketSecurity ignore followed by a space separated list of package-name@version specifiers. e.g. @SocketSecurity ignore foo@1.0.0 bar@* or ignore all packages with @SocketSecurity ignore-all

@mcmire
Copy link
Contributor Author

mcmire commented Jun 16, 2023

mockttp is a development dependency, so the above warning is acceptable.

@SocketSecurity ignore @httptoolkit/subscriptions-transport-ws@0.9.19

@mcmire mcmire force-pushed the replace-network-controller branch from d0b3bce to d5f9bdf Compare June 18, 2023 23:34
@metamaskbot
Copy link
Collaborator

Builds ready [609712c]
Page Load Metrics (2014 ± 79 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint1472431842914
domContentLoaded16662289201015976
load16662355201416579
domInteractive16662289201015976
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 3269 bytes
  • ui: 899 bytes
  • common: -567 bytes

@codecov
Copy link

codecov bot commented Jun 19, 2023

Codecov Report

Merging #19486 (99a2a9b) into develop (bd12ea7) will decrease coverage by 0.97%.
The diff coverage is 65.00%.

@@             Coverage Diff             @@
##           develop   #19486      +/-   ##
===========================================
- Coverage    70.80%   69.83%   -0.97%     
===========================================
  Files          988      979       -9     
  Lines        38375    36905    -1470     
  Branches     10046     9909     -137     
===========================================
- Hits         27168    25769    -1399     
+ Misses       11207    11136      -71     
Impacted Files Coverage Δ
.mocharc.js 100.00% <ø> (ø)
app/scripts/controllers/mmi-controller.js 20.87% <ø> (-0.17%) ⬇️
app/scripts/metamask-controller.js 65.63% <60.71%> (-0.27%) ⬇️
app/scripts/controllers/detect-tokens.js 92.24% <66.67%> (-0.20%) ⬇️
app/scripts/controllers/backup.js 70.59% <100.00%> (ø)
app/scripts/controllers/swaps.js 81.27% <100.00%> (ø)

... and 1 file with indirect coverage changes

@mcmire mcmire force-pushed the replace-network-controller branch 3 times, most recently from 812db62 to ff73422 Compare June 22, 2023 00:00
@metamaskbot
Copy link
Collaborator

Builds ready [ff73422]
Page Load Metrics (1512 ± 28 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint106143124115
domContentLoaded1403160415125828
load1403160415125828
domInteractive1403160415125828
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 3309 bytes
  • ui: 615 bytes
  • common: -567 bytes

This commit fulfills a long-standing desire to get the extension using
the same network controller as mobile by removing NetworkController from
this repo and replacing it with NetworkController from the
`@metamask/network-controller` package.

The new version of NetworkController is different the old one in a few
ways:

- The new controller inherits from BaseControllerV2, so the `state`
  property is used to access the state instead of `store.getState()`.
  All references of the latter have been replaced with the former.
- As the new controller no longer has a `store` property, it cannot be
  subscribed to; the controller takes a messenger which can be
  subscribed to instead. There were various places within
  MetamaskController where the old way of subscribing has been replaced
  with the new way. In addition, DetectTokensController has been updated
  to take a messenger object so that it can listen for NetworkController
  state changes.
- The state of the new controller is not updatable from the outside.
  This affected BackupController, which dumps state from
  NetworkController (among other controllers), but also loads the same
  state into NetworkController on import. A method `loadBackup` has been
  added to NetworkController to facilitate this use case, and
  BackupController is now using this method instead of attempting to
  call `update` on NetworkController.
- The new controller does not have a `getCurrentChainId` method;
  instead, the chain ID can be read from the provider config in state.
  This affected MmiController. (MmiController was also updated to read
  custom networks from the new network controller instead of the
  preferences controller).
- The default network that the new controller is set to is always
  Mainnet (previously it could be either localhost or Goerli in test
  mode, depending on environment variables).
@mcmire mcmire force-pushed the replace-network-controller branch from ff73422 to 8766ef1 Compare June 22, 2023 04:41
@metamaskbot
Copy link
Collaborator

Builds ready [8766ef1]
Page Load Metrics (1780 ± 75 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint1062011422211
domContentLoaded15141993175014168
load15372092178015775
domInteractive15141993175014168
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 3249 bytes
  • ui: 567 bytes
  • common: -567 bytes

@mcmire mcmire changed the title WIP - Replace NetworkController w/ core version Replace NetworkController w/ core version Jun 22, 2023
@mcmire mcmire marked this pull request as ready for review June 22, 2023 15:48
@mcmire mcmire requested review from a team as code owners June 22, 2023 15:48
@mcmire mcmire requested a review from digiwand June 22, 2023 15:48
@@ -31,7 +31,7 @@ export default class BackupController {
}

if (network) {
this.networkController.store.updateState(network);
Copy link
Contributor Author

@mcmire mcmire Jun 22, 2023

Choose a reason for hiding this comment

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

Because the core NetworkController inherits from BaseControllerV2, it doesn't allow consumers to update its state directly like this. I added a loadBackup method to NetworkController to achieve the same thing as this code here: MetaMask/core#1421

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!

@mcmire mcmire removed the DO-NOT-MERGE Pull requests that should not be merged label Jun 22, 2023
@metamaskbot
Copy link
Collaborator

Builds ready [99a2a9b]
Page Load Metrics (1533 ± 66 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint1091811312010
domContentLoaded13401911153213866
load13401911153313866
domInteractive13401911153213866
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 3429 bytes
  • ui: 567 bytes
  • common: -567 bytes

Copy link
Contributor

@brad-decker brad-decker left a comment

Choose a reason for hiding this comment

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

Code LGTM and works great locally!

@mcmire mcmire merged commit 89cec53 into develop Jun 22, 2023
@mcmire mcmire deleted the replace-network-controller branch June 22, 2023 18:46
@github-actions github-actions bot locked and limited conversation to collaborators Jun 22, 2023
@metamaskbot metamaskbot added the release-10.34.0 Issue or pull request that will be included in release 10.34.0 label Jun 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
release-10.34.0 Issue or pull request that will be included in release 10.34.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Replace the extension network controller with the shared network controller
4 participants