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

fix: Fix SelectedNetworkController state corruption by making NetworkForm update in-place rather than remove and add for rpcUrl changes #26453

Merged
merged 6 commits into from
Aug 16, 2024

Conversation

jiexi
Copy link
Contributor

@jiexi jiexi commented Aug 15, 2024

Description

Currently it is not possible to change the rpcUrl for an existing network configuration without first removing it then re-adding it. This poses an issue on extension when the rpcUrl for the currently selected network is changed via the NetworkForm. The NetworkForm first removes the existing networkClient which causes the SelectedNetworkController to replace any references to the networkClientId being removed with the selectedNetworkClientId, but the problem is that the selectedNetworkClientId is no longer valid at that point and leaves the SelectedNetworkController in a corrupted state.

Really what we want in this case is to allow the network configuration to be updated in place by id, but only if the rpcUrl isn't changing to one that already exists on a different network configuration.

This PR achieves that by getting rid of the removeNetworkConfiguration() call triggered by NetworkForm and patching the NetworkController with changes from MetaMask/core#4614 which allow network configurations to have their rpcUrl updated in place. It also keeps the existing patch that removed a lookupNetwork() call in initializeProvider().

Open in GitHub Codespaces

Related issues

Related: #26309

Manual testing steps

  1. Open extension in expanded view
  2. Visit the Settings -> Networks
  3. Add a network manually and add an rpc provider for Arbitrum One (for your convenience, chainlist.org)
  4. In the extension service worker console note the state via chrome.storage.local.get(console.log) for NetworkController.networkConfigurations
  5. Visit the Network Form for that Network
  6. Change the rpcUrl to a different valid one for Arbitrum
  7. In the extension service worker console check state via chrome.storage.local.get(console.log), the id for the network configuration that was just edited should not have changed in NetworkController.networkConfigurations
  8. Visit a dapp, switch the chain to Arbitrum
await window.ethereum.request({
  "method": "eth_requestAccounts",
});
await window.ethereum.request({
  "method": "wallet_switchEthereumChain",
  "params": [
    {
      "chainId": "0xa4b1"
    }
  ]
});
  1. In the extension service worker console check state via chrome.storage.local.get(console.log), the dapp should have an entry for the network client we added above SelectedNetworkController.domains
  2. Again, Change the rpcUrl to a different valid one for Arbitrum
  3. In the extension service worker console check state via chrome.storage.local.get(console.log), the dapp should still be pointed at the same network client id in SelectedNetworkController.domains which should still exist in NetworkController.networkConfigurations

Screenshots/Recordings

Before

After

Pre-merge author checklist

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.

Copy link
Contributor

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.

@metamaskbot
Copy link
Collaborator

Builds ready [fffe69d]
Page Load Metrics (258 ± 245 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint714731328440
domContentLoaded9101372411
load391951258510245
domInteractive9101372411
Bundle size diffs [🚀 Bundle size reduced!]
  • background: 0 Bytes (0.00%)
  • ui: -22 Bytes (-0.00%)
  • common: -647 Bytes (-0.01%)

@jiexi jiexi changed the title Jl/mme 26309/fix network form upsert fix: Fix SelectedNetworkController state corruption by making NetworkForm update in-place rather than remove and add for rpcUrl changes Aug 15, 2024
@jiexi jiexi marked this pull request as ready for review August 15, 2024 22:23
@jiexi jiexi requested a review from a team as a code owner August 15, 2024 22:23
@metamaskbot
Copy link
Collaborator

Builds ready [7911aef]
Page Load Metrics (540 ± 458 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint801641182612
domContentLoaded106634168
load463175540953458
domInteractive106634168
Bundle size diffs [🚀 Bundle size reduced!]
  • background: 0 Bytes (0.00%)
  • ui: -22 Bytes (-0.00%)
  • common: -360 Bytes (-0.00%)

Copy link

codecov bot commented Aug 15, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 70.12%. Comparing base (54b3237) to head (7911aef).

Additional details and impacted files
@@             Coverage Diff             @@
##           develop   #26453      +/-   ##
===========================================
- Coverage    70.12%   70.12%   -0.01%     
===========================================
  Files         1428     1428              
  Lines        50103    50089      -14     
  Branches     13896    13894       -2     
===========================================
- Hits         35133    35120      -13     
+ Misses       14970    14969       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@@ -1,24 +1,4263 @@
diff --git a/dist/NetworkController.js b/dist/NetworkController.js
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it possible to just release the fixed network-controller package really quickly and then update to it?

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 don't think that'd be realistically possible given the breaking change in 20.0.0 removes providerConfig from state. This is probably why we are still on 19.0.0

https://github.com/MetaMask/core/blob/main/packages/network-controller/CHANGELOG.md#removed

I'm going to guess that you're suggesting this since the patch is not human readable

Copy link
Contributor

Choose a reason for hiding this comment

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

Is it possible to just release the fixed network-controller package really quickly and then update to it?

It is not possible to do this in a timely manner, but we need to get this fix into prod asap

@jiexi
Copy link
Contributor Author

jiexi commented Aug 15, 2024

Do we need to include changes to rerun 120.5 in this PR too?

Copy link
Contributor

@danjm danjm left a comment

Choose a reason for hiding this comment

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

LGTM

@danjm
Copy link
Contributor

danjm commented Aug 15, 2024

Do we need to include changes to rerun 120.5 in this PR too?

yes, that makes sense

Copy link
Contributor

@danjm danjm left a comment

Choose a reason for hiding this comment

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

changing my approval as there will be adding more migration code added here

@DDDDDanica
Copy link
Contributor

DDDDDanica commented Aug 16, 2024

LGTM ! Tested locally and worked as described.

One thing as Nit is we can add an e2e test for editing rpcURL for selected network in the future ticket

Copy link

@jiexi jiexi merged commit 2dc3768 into develop Aug 16, 2024
77 of 78 checks passed
@jiexi jiexi deleted the jl/mme-26309/fix-network-form-upsert branch August 16, 2024 00:39
@github-actions github-actions bot locked and limited conversation to collaborators Aug 16, 2024
@metamaskbot metamaskbot added the release-12.5.0 Issue or pull request that will be included in release 12.5.0 label Aug 16, 2024
@metamaskbot
Copy link
Collaborator

Builds ready [957f59a]
Page Load Metrics (140 ± 151 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint652221014019
domContentLoaded9134272612
load401505140315151
domInteractive9134272612
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 2.44 KiB (0.06%)
  • ui: -22 Bytes (-0.00%)
  • common: -360 Bytes (-0.00%)

@jiexi
Copy link
Contributor Author

jiexi commented Aug 16, 2024

@gauthierpetetin gauthierpetetin added release-12.4.0 Issue or pull request that will be included in release 12.4.0 and removed release-12.5.0 Issue or pull request that will be included in release 12.5.0 labels Sep 11, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
release-12.4.0 Issue or pull request that will be included in release 12.4.0 team-wallet-api-platform
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants