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 currentNetwork selector when current network config is missing #26327

Merged
merged 3 commits into from
Aug 2, 2024

Conversation

Gudahtt
Copy link
Member

@Gudahtt Gudahtt commented Aug 2, 2024

Description

The providerConfig state is not guaranteed to match a built-in or custom network. It should in the vast majority of cases, but there are some edge cases where that would not hold true. For example:

  • If the wallet crashed partway through removing the current network
  • If the user has had the same network selected since before we introduced the networkConfigurations state

The currentNetwork selector has been updated to handle this case by returning a configuration object constructed from the providerConfig state directly.

Open in GitHub Codespaces

Related issues

Fixes #26320

Manual testing steps

  • Create a dev build and proceed through onboarding
  • Run this command in the background console to set providerConfig to a custom network that isn't present in the user's tracked network configurations:
    chrome.storage.local.get(
      null,
      (state) => {
        state.data.NetworkController.providerConfig = {
          rpcUrl: 'https://mainnet.optimism.io',
          chainId: '0xa',
          ticker: 'ETH',
          type: 'rpc',
        };
        chrome.storage.local.set(state, () => chrome.runtime.reload());
      }
    );
    
  • See that the UI can be opened and does not crash.

Screenshots/Recordings

N/A

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.

Gudahtt added 3 commits August 2, 2024 14:49
…sing

The `providerConfig` state is not guaranteed to match a built-in or custom
network. It should in the vast majority of cases, but there are some edge
cases where that would not hold true. For example:
* If the wallet crashed partway through removing the current network
* If the user has had the same network selected since before we
introduced the `networkConfigurations` state

The `currentNetwork` selector has been updated to handle this case by
returning a configuration object constructed from the `providerConfig`
state directly.

Fixes #26320
@Gudahtt Gudahtt force-pushed the fix-current-network-undefined branch from ec6b27f to c38a3f5 Compare August 2, 2024 17:19
Copy link

sonarqubecloud bot commented Aug 2, 2024

Copy link

codecov bot commented Aug 2, 2024

Codecov Report

Attention: Patch coverage is 83.33333% with 1 line in your changes missing coverage. Please review.

Project coverage is 70.04%. Comparing base (80f538e) to head (c38a3f5).
Report is 1 commits behind head on develop.

Files Patch % Lines
...nts/app/assets/nfts/nft-details/nft-full-image.tsx 0.00% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##           develop   #26327   +/-   ##
========================================
  Coverage    70.04%   70.04%           
========================================
  Files         1411     1411           
  Lines        50029    50034    +5     
  Branches     13815    13820    +5     
========================================
+ Hits         35042    35046    +4     
- Misses       14987    14988    +1     

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

@Gudahtt Gudahtt marked this pull request as ready for review August 2, 2024 17:42
@Gudahtt Gudahtt requested review from a team as code owners August 2, 2024 17:42
</div>
<span
class="mm-box mm-text mm-text--body-sm mm-text--ellipsis mm-box--color-text-default"
data-testid="network-display"
/>
>
Test initial state
Copy link
Member Author

Choose a reason for hiding this comment

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

The test fixtures used here were of the type affected by this change: providerConfig was not a built-in or custom network. Probably not intentional, but I didn't dig in further. This snapshot change is not wrong or totally unexpected at least.

*
* @param {Record<string, unknown>[]} allNetworks - All network configurations.
* @param {Record<string, unknown>} providerConfig - The configuration for the current network's provider.
* @returns {{
Copy link
Member Author

@Gudahtt Gudahtt Aug 2, 2024

Choose a reason for hiding this comment

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

I added a JSDoc block so that tsc could help spot any instances where this change was breaking.

Specifically the only difference in output before and after is that the id property is now optional, whereas before it was guaranteed to exist on the returned object in some cases (if an object was returned at all). But luckily nothing was relying on that.

return allNetworks.find(filter);
return (
allNetworks.find(filter) ?? {
chainId: providerConfig.chainId,
Copy link
Member Author

@Gudahtt Gudahtt Aug 2, 2024

Choose a reason for hiding this comment

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

The providerConfig.type property is being excluded here because that's not an expected network configuration property.

id is excluded as well, since if it is set here it must be invalid (the filter above didn't find anything matching it).

Copy link
Contributor

Choose a reason for hiding this comment

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

Have we removed instances like the one causing this error expecting an id?

Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like it's still in this file down on line 1912?

Copy link
Member Author

@Gudahtt Gudahtt Aug 2, 2024

Choose a reason for hiding this comment

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

To clarify, the id property is still expected to be present in cases where providerConfig matches a valid custom network configuration. It would cause problems if this selector was used where an id must always present, but the line you linked works fine if id is undefined. If the current domain has a selected network configuration set, it'll find that it doesn't match undefined and it will switch, which is correct.

Copy link
Contributor

Choose a reason for hiding this comment

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

@metamaskbot
Copy link
Collaborator

Builds ready [c38a3f5]
Page Load Metrics (287 ± 301 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint743131225929
domContentLoaded10173313718
load462169287627301
domInteractive10173313718
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 0 Bytes (0.00%)
  • ui: 9 Bytes (0.00%)
  • common: 93 Bytes (0.00%)

@Gudahtt Gudahtt merged commit e5651cf into develop Aug 2, 2024
75 checks passed
@Gudahtt Gudahtt deleted the fix-current-network-undefined branch August 2, 2024 19:19
@github-actions github-actions bot locked and limited conversation to collaborators Aug 2, 2024
@metamaskbot metamaskbot added the release-12.4.0 Issue or pull request that will be included in release 12.4.0 label Aug 2, 2024
@gauthierpetetin gauthierpetetin added release-12.3.0 Issue or pull request that will be included in release 12.3.0 and removed release-12.4.0 Issue or pull request that will be included in release 12.4.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.3.0 Issue or pull request that will be included in release 12.3.0 team-wallet-framework
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

[Sentry] TypeError: Cannot read properties of undefined (reading 'id')
6 participants