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

Ensure that all networkConfiguration object in networkController state have an id #18513

Merged
merged 14 commits into from
Apr 11, 2023

Conversation

danjm
Copy link
Contributor

@danjm danjm commented Apr 8, 2023

Fixes #18509
Fixes #18453

Explanation

We partially fixed the symptom of 18509 and 18453 with #18483, but that fix was incomplete.

This bug will be experienced by anyone who added a network while on v10.27.0 or earlier, and are now on v10.28.1 or v10.28.2 and trying to confirm a dapp prompted switch to that network. This bug will not affect users who first added the network, via a dapp wallet_addEthereumChain request, while on v10.28.1 . This bug also won't affect users who added the network via the MetaMask UI in v10.28.1

The problem is that migration 82 did not add an id property to each network configuration object, but we assume that id property exists in the switch-ethereum-chain.js handler. If a user adds a network on version v10.27.0 or earlier, and then updates to v10.28.x, migration 82 will create an object within NetworkController.networkConfigurations keyed by a random id, but that id will not be added to that object itself. Later, when we attempt to switch to the network using that objects id property, it fails. It does not fail if the network is added while on v10.28.x, because the upsertNetworkConfiguration method, which is called when adding a network on v10.28.x, adds an id property to each network.

This PR corrects the problem with a migration that ensures that the each key within networkConfigurations is used to set the id property of the respective object.

Before

This video shows what would happen when upgrading from v10.27.0 -> v10.28.2 and then trying to switch to a previously added network:

switchnetworkfail-v10282.mp4

After

This shows switch network working after an upgrade from v10.27.0 to the branch of this PR:

switchnetwork-fix.mp4

Manual Testing Steps

Do as is done in the above "After" video:

  1. Build v10.27.0, from d8591d4, install and onboard
  2. Go to https://syncswap.xyz/, connect and add the suggested network
  3. In MetaMask, select Ethereum Mainnet, so that the zkSync network is not selected
  4. git checkout this branch, build and then reload the metamask installation that was previously built from v10.27.0
  5. login to MetaMask and then go to https://syncswap.xyz/
  6. click "Connect Wallet" and confirm the switch network popup
  7. open MetaMask, you should now have the zksync network selected

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.

@danjm danjm requested a review from a team as a code owner April 8, 2023 09:48
@github-actions
Copy link
Contributor

github-actions bot commented Apr 8, 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.

@danjm
Copy link
Contributor Author

danjm commented Apr 8, 2023

This will require #18512 (review) to get the build passing.

@metamaskbot
Copy link
Collaborator

Builds ready [fb3f650]
Page Load Metrics (1591 ± 52 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint94135107105
domContentLoaded1409176115569144
load14091812159110852
domInteractive1408176115569144
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 948 bytes
  • ui: 0 bytes
  • common: 0 bytes

@metamaskbot
Copy link
Collaborator

Builds ready [5ec4489]
Page Load Metrics (1639 ± 39 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint941801112110
domContentLoaded1486173615936230
load1518184116398139
domInteractive1486173615936230
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 948 bytes
  • ui: 0 bytes
  • common: 0 bytes

@metamaskbot
Copy link
Collaborator

Builds ready [7af5ddc]
Page Load Metrics (1603 ± 75 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint843431175426
domContentLoaded14131955158315876
load14141955160315775
domInteractive14131955158315876
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 948 bytes
  • ui: 0 bytes
  • common: 0 bytes

@metamaskbot
Copy link
Collaborator

Builds ready [43d690a]
Page Load Metrics (1953 ± 120 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint993811586933
domContentLoaded160524791932258124
load160724791953251120
domInteractive160524791932258124
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 948 bytes
  • ui: 0 bytes
  • common: 0 bytes

@adonesky1
Copy link
Contributor

It probably does make sense to fix the state here but - just a thought - another lighter weight way to fix this (I think) would be to modify the findNetworkConfigurationBy method on MetamaskController like this:

   findNetworkConfigurationBy(rpcInfo) {
     const { networkConfigurations } = this.networkController.store.getState();
-    const networkConfiguration = Object.values(networkConfigurations).find(
-      (configuration) => {
-        return Object.keys(rpcInfo).some((key) => {
-          return configuration[key] === rpcInfo[key];
-        });
-      },
-    );
+    const [id, networkConfiguration] = Object.entries(
+      networkConfigurations,
+    ).find((configuration) => {
+      return Object.keys(rpcInfo).some((key) => {
+        return configuration[key] === rpcInfo[key];
+      });
+    });
 
-    return networkConfiguration || null;
+    return { ...networkConfiguration, id } || null;
   }

legobeat
legobeat previously approved these changes Apr 11, 2023
Copy link
Contributor

@legobeat legobeat left a comment

Choose a reason for hiding this comment

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

LGTM

DDDDDanica
DDDDDanica previously approved these changes Apr 11, 2023
@metamaskbot
Copy link
Collaborator

Builds ready [e751c04]
Page Load Metrics (1640 ± 107 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint911751152713
domContentLoaded144322241620222107
load144322241640224107
domInteractive144322241620222107
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 948 bytes
  • ui: 0 bytes
  • common: 0 bytes

Co-authored-by: Mark Stacey <markjstacey@gmail.com>
@danjm danjm dismissed stale reviews from legobeat and DDDDDanica via 2a8c2e7 April 11, 2023 13:23
adonesky1
adonesky1 previously approved these changes Apr 11, 2023
Copy link
Contributor

@adonesky1 adonesky1 left a comment

Choose a reason for hiding this comment

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

LGTM!

@metamaskbot
Copy link
Collaborator

Builds ready [83545e3]
Page Load Metrics (1713 ± 77 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint881791222613
domContentLoaded14492016170016278
load14492016171316177
domInteractive14492016170016278
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 974 bytes
  • ui: 0 bytes
  • common: 0 bytes

adonesky1
adonesky1 previously approved these changes Apr 11, 2023
@metamaskbot
Copy link
Collaborator

Builds ready [affa960]
Page Load Metrics (1610 ± 64 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint102164119168
domContentLoaded13711856160112661
load13711878161013364
domInteractive13711856160112661
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 1016 bytes
  • ui: 0 bytes
  • common: 0 bytes

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!

@danjm danjm merged commit b3c4790 into develop Apr 11, 2023
@danjm danjm deleted the fix-wallet_switchEthereumNetwork-2 branch April 11, 2023 19:16
@github-actions github-actions bot locked and limited conversation to collaborators Apr 11, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
6 participants