feat: Add StorageService for offloading large controller data #7192
+1,829
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Add StorageService for Large Controller Data
Explanation
What is the current state and why does it need to change?
Current state: MetaMask Mobile Engine state is 10.79 MB, with 92% (9.94 MB) concentrated in just 2 controllers:
This data is rarely accessed after initial load but stays in Redux state, causing:
Why change: Controllers need a way to store large, infrequently-accessed data outside of Redux state while maintaining platform portability and testability.
What is the solution and how does it work?
New package:
@metamask/storage-serviceA platform-agnostic service that allows controllers to offload large data from state to persistent storage via messenger actions.
How it works:
StorageService:setItemvia messenger to store large dataStorageService:itemSet:{namespace}) so other controllers can reactStorageService:getItemto load data lazily (only when needed)Storage adapter pattern:
Example controller usage:
Why this architecture?
Platform-agnostic: Service defines
StorageAdapterinterface; clients provide implementation (mobile: FilesystemStorage, extension: IndexedDB, tests: in-memory)Messenger-integrated: Controllers access storage via messenger actions, no direct dependencies
Event-driven: Controllers can subscribe to storage changes without coupling
Testable: InMemoryAdapter provides zero-config testing (no mocking needed)
Proven pattern: Follows ErrorReportingService design (accepts platform-specific function)
Expected impact?
With both controllers optimized:
This PR adds infrastructure - Controllers can now use StorageService. Separate PRs will integrate with SnapController and TokenListController.
References
Checklist