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

refactor: Refactor state classes to prepare for state corruption backup mitigation #29745

Merged
merged 48 commits into from
Jan 30, 2025

Conversation

danjm
Copy link
Contributor

@danjm danjm commented Jan 16, 2025

Description

This PR replaces #24016

This PR does the following:

Refactors local-store.js and network-store.js to ExtensionStore.ts and ReadOnlyNetworkStore.ts. These two classes are extensions of the new BaseStore class.

The new BaseStore class: "is an abstract class designed to be extended by other classes that implement the abstract methods set and get. This class provides the foundation for different storage implementations, enabling them to adhere to a consistent interface for retrieving and setting application state." In future PRs, new classes that use the indexeddb api, and the localStorage api in the offscreen document, will be added that also extend the BaseStore class.

The BaseStore class has a simple interface with just a get and set method. Accordingly, ExtensionSotre and ReadOnlyNetworkStore are significantly simplified compared to their predecessors. The functionality for managing data (e.g. handling error states, handling metadata, etc) that used to be in local-store.js and network-store.js is now in a new PersistanceManager class.

The new PersistanceManager class: "The PersistanceManager class serves as a high-level manager for handling storage-related operations using a local storage system. It provides methods to read and write state, manage metadata, and handle errors or corruption in the underlying storage system."

This is a step towards providing a fail safe for state corruption. The new classes will be utilized for this additional functionality.

Open in GitHub Codespaces

Related issues

Fixes:

Manual testing steps

No functional changes should have occurred. Manual regression cases to test include:

  • make some changes such as a adding a network or changing settings. Close the browser. Re-open the browser and open metamask. The data should be unchanged.
  • Install an old version. Make some changes. Upgrade to the build generated from this branch. Verify that the state of the wallet, and its data, is as expected.

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.

@danjm danjm changed the title Refactor state classes to prepare for state corruption backup mitigation refactor: Refactor state classes to prepare for state corruption backup mitigation Jan 20, 2025
@danjm danjm marked this pull request as ready for review January 20, 2025 16:00
@metamaskbot
Copy link
Collaborator

Builds ready [c5528f3]
Page Load Metrics (1995 ± 141 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint26526021794542260
domContentLoaded164627141966281135
load166227781995293141
domInteractive2410839209
backgroundConnect1069302110
firstReactRender15412273
getState93172136189
initialActions01000
loadScripts126121851532239115
setupStore615931
uiStartup187333022310363174
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 136 Bytes (0.00%)
  • ui: 0 Bytes (0.00%)
  • common: 3.06 KiB (0.04%)

brad-decker and others added 4 commits January 20, 2025 16:45
Capture sentry error if there is an empty/corrupt vault, and handle more possible empty/corrupt vault types

Ensure log.debug call in case fetch in init in ReadOnlyNetworkStore has an error without a message

Ensure that  in ReadOnlyNetworkStore waits for initializing to complete

Lint fix

Ensure that first time state is returned if state has no data

Update app/scripts/lib/Stores/ExtensionStore.ts

Co-authored-by: Danica Shen <[email protected]>

Update test description

Use const for DEFAULT_INITIAL_STATE in ExtensionStore.test.ts

Improve naming and usage of versionedData variable in background.js

lint fix

Capture exception with sentry in both error cases ExtensionStore.get

Lint fix

Add app/scripts/lib/Stores/ReadOnlyNetworkStore.test.ts

Delete files that are no longer used

Use async/await for get/set in ExtensionStore.ts
@danjm danjm force-pushed the re-refactor-state-classes branch from c5528f3 to 3401019 Compare January 20, 2025 20:15
app/scripts/lib/Stores/BaseStore.ts Outdated Show resolved Hide resolved
app/scripts/background.js Outdated Show resolved Hide resolved
app/scripts/background.js Show resolved Hide resolved
app/scripts/background.js Outdated Show resolved Hide resolved
@metamaskbot
Copy link
Collaborator

Builds ready [bf23d04]
Page Load Metrics (1787 ± 79 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint15462163179117182
domContentLoaded15302097176416077
load15472120178716479
domInteractive267840178
backgroundConnect66021157
firstReactRender1674322010
getState45219189
initialActions00000
loadScripts11091620130213163
setupStore657212010
uiStartup175826722059222106
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 142 Bytes (0.00%)
  • ui: -462 Bytes (-0.01%)
  • common: 3.06 KiB (0.03%)

app/scripts/lib/Stores/BaseStore.ts Outdated Show resolved Hide resolved
app/scripts/lib/Stores/BaseStore.ts Outdated Show resolved Hide resolved
app/scripts/lib/Stores/BaseStore.ts Outdated Show resolved Hide resolved
app/scripts/lib/Stores/BaseStore.ts Outdated Show resolved Hide resolved
app/scripts/lib/Stores/PersistanceManager.ts Outdated Show resolved Hide resolved
app/scripts/lib/Stores/PersistanceManager.ts Outdated Show resolved Hide resolved
app/scripts/lib/Stores/PersistanceManager.ts Outdated Show resolved Hide resolved
app/scripts/lib/Stores/PersistanceManager.ts Outdated Show resolved Hide resolved
app/scripts/lib/Stores/ExtensionStore.ts Outdated Show resolved Hide resolved
app/scripts/lib/Stores/ExtensionStore.ts Outdated Show resolved Hide resolved
app/scripts/lib/Stores/ReadOnlyNetworkStore.ts Outdated Show resolved Hide resolved
app/scripts/lib/Stores/ReadOnlyNetworkStore.ts Outdated Show resolved Hide resolved
@metamaskbot
Copy link
Collaborator

Builds ready [7296ee9]
Page Load Metrics (1820 ± 97 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint155422981823209100
domContentLoaded15462205179419996
load15552229182020397
domInteractive22146492814
backgroundConnect88723189
firstReactRender1596382512
getState560212010
initialActions01000
loadScripts10781735134618086
setupStore660222010
uiStartup181725252089225108
Bundle size diffs [🚀 Bundle size reduced!]
  • background: 44.81 KiB (0.76%)
  • ui: 1.5 KiB (0.02%)
  • common: -246.55 KiB (-2.72%)

});

const localStore = inTest ? new ReadOnlyNetworkStore() : new ExtensionStore();
const persistanceManager = new PersistanceManager({ localStore });
Copy link
Contributor

Choose a reason for hiding this comment

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

Typo:

Suggested change
const persistanceManager = new PersistanceManager({ localStore });
const persistenceManager = new PersistenceManager({ localStore });

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed in acd53a0

@DDDDDanica
Copy link
Contributor

✅ Tested locally with

  • Switch network or changing some basic setting. Close the browser. Re-open the browser and open metamask. The data didn't change.

@metamaskbot
Copy link
Collaborator

Builds ready [ee61d60]
Page Load Metrics (1707 ± 71 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint23519741562422203
domContentLoaded15192007169114670
load15262019170714771
domInteractive25106452311
backgroundConnect66217147
firstReactRender1699443014
getState572232311
initialActions01000
loadScripts10501483122512861
setupStore68313199
uiStartup169325112046253122
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 177 Bytes (0.00%)
  • ui: 0 Bytes (0.00%)
  • common: 2.04 KiB (0.02%)

@metamaskbot
Copy link
Collaborator

Builds ready [0b72887]
Page Load Metrics (1789 ± 93 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint15022180178819393
domContentLoaded14602170175319292
load14882182178919493
domInteractive256239126
backgroundConnect107333189
firstReactRender1584352110
getState55919188
initialActions01000
loadScripts10851655130915876
setupStore56015178
uiStartup177125102057217104
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 177 Bytes (0.00%)
  • ui: 0 Bytes (0.00%)
  • common: 2.04 KiB (0.02%)

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!

@metamaskbot
Copy link
Collaborator

Builds ready [1aa223e]
Page Load Metrics (1898 ± 88 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint15922242189818187
domContentLoaded15482221186918388
load15532246189818488
domInteractive26107492612
backgroundConnect699302311
firstReactRender16100392613
getState5105252612
initialActions01000
loadScripts11401648138515474
setupStore682222211
uiStartup180627712180243117
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 177 Bytes (0.00%)
  • ui: 0 Bytes (0.00%)
  • common: 2.04 KiB (0.02%)

@DDDDDanica
Copy link
Contributor

LGTM !

@danjm danjm added this pull request to the merge queue Jan 30, 2025
Merged via the queue into main with commit 8534e11 Jan 30, 2025
74 checks passed
@danjm danjm deleted the re-refactor-state-classes branch January 30, 2025 15:28
@github-actions github-actions bot locked and limited conversation to collaborators Jan 30, 2025
@metamaskbot metamaskbot added the release-12.12.0 Issue or pull request that will be included in release 12.12.0 label Jan 30, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
release-12.12.0 Issue or pull request that will be included in release 12.12.0 team-extension-platform
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

5 participants