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 BaseControllerV1 state rehydration #10133

Merged
merged 1 commit into from
Jun 27, 2024

Conversation

Gudahtt
Copy link
Member

@Gudahtt Gudahtt commented Jun 27, 2024

Description

We were failing to properly restore persisted state for some BaseControllerV1 controllers. This resulted in state being lost when the application was restarted. The only confirmed affected controller so far is the NftController.

The context here is that for some older controllers, rather than passing the initial state into the controller directly when it is constructed, we call the update function directly. This has always been a bad practice (controllers should update their own state), and we're forced to fix this for controllers that we update to BaseControllerV2 (because directly updating state like this is no longer possible. But this method of rehydrating controller state was still relied upon in some cases.

This was broken recently in #9570 when a condition was added to fix a type error. The condition was meant to check that the controller had a subscribe function. Unfortunately this hasProperty check only looks at owned properties, not inherited properties, and the subscribe function was inherited from the base class. It has been updated to use the in operator instead, which does look up the entire prototype chain.

Related issues

Fixes #10057

Manual testing steps

See #10057

Screenshots/Recordings

Before

See #10057

After

Screen.Recording.2024-06-26.at.22.06.21.mov

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.

We were failing to properly restore persisted state for some
BaseControllerV1 controllers. This resulted in state being lost when
the application was restarted. The only confirmed affected controller
so far is the `NftController`.

The context here is that for some older controllers, rather than
passing the initial state into the controller directly when it is
constructed, we call the `update` function directly. This has always
been a bad practice (controllers should update their own state), and
we're forced to fix this for controllers that we update to
`BaseControllerV2` (because directly updating state like this is no
longer possible. But this method of rehydrating controller state was
still relied upon in some cases.

This was broken recently in #9570 when a condition was added to fix
a type error. The condition was meant to check that the controller had
a `subscribe` function. Unfortunately this `hasProperty` check only
looks at owned properties, not inherited properties, and the
`subscribe` function was inherited from the base class. It has been
updated to use the `in` operator instead, which does look up the
entire prototype chain.

Fixes #10057
@Gudahtt Gudahtt force-pushed the fix-base-controller-v1-state-rehydration branch from e8ebf6c to 9c5c461 Compare June 27, 2024 00:37
@Gudahtt Gudahtt marked this pull request as ready for review June 27, 2024 00:37
@Gudahtt Gudahtt requested a review from a team as a code owner June 27, 2024 00:37
Copy link

sonarcloud bot commented Jun 27, 2024

@Gudahtt
Copy link
Member Author

Gudahtt commented Jun 27, 2024

I suspect that these controllers were also affected:

  • TokensController
  • AccountTrackerController
  • TransactionController
  • NftDetectionController
  • TokenRatesController
  • SwapsController

@tommasini
Copy link
Contributor

Awesome finding!

@Gudahtt Gudahtt merged commit 0703383 into main Jun 27, 2024
32 checks passed
@Gudahtt Gudahtt deleted the fix-base-controller-v1-state-rehydration branch June 27, 2024 12:13
@github-actions github-actions bot locked and limited conversation to collaborators Jun 27, 2024
@metamaskbot metamaskbot added the release-7.27.0 Issue or pull request that will be included in release 7.27.0 label Jun 27, 2024
@Gudahtt Gudahtt restored the fix-base-controller-v1-state-rehydration branch June 27, 2024 12:19
@Gudahtt Gudahtt added the Run Smoke E2E Triggers smoke e2e on Bitrise label Jun 27, 2024
@MetaMask MetaMask unlocked this conversation Jun 27, 2024
@Gudahtt Gudahtt removed the Run Smoke E2E Triggers smoke e2e on Bitrise label Jun 27, 2024
Gudahtt added a commit that referenced this pull request Jun 27, 2024
This PR cherry-picks #10133

Co-authored-by: Mark Stacey <markjstacey@gmail.com>
@metamaskbot metamaskbot added release-7.26.0 Issue or pull request that will be included in release 7.26.0 and removed release-7.27.0 Issue or pull request that will be included in release 7.27.0 labels Jun 27, 2024
@metamaskbot
Copy link
Collaborator

Missing release label release-7.26.0 on PR. Adding release label release-7.26.0 on PR and removing other release labels(release-7.27.0), as PR was cherry-picked in branch 7.26.0.

Jonathansoufer pushed a commit that referenced this pull request Jun 27, 2024
<!--
Please submit this PR as a draft initially.
Do not mark it as "Ready for review" until the template has been
completely filled out, and PR status checks have passed at least once.
-->

## **Description**

We were failing to properly restore persisted state for some
BaseControllerV1 controllers. This resulted in state being lost when the
application was restarted. The only confirmed affected controller so far
is the `NftController`.

The context here is that for some older controllers, rather than passing
the initial state into the controller directly when it is constructed,
we call the `update` function directly. This has always been a bad
practice (controllers should update their own state), and we're forced
to fix this for controllers that we update to `BaseControllerV2`
(because directly updating state like this is no longer possible. But
this method of rehydrating controller state was still relied upon in
some cases.

This was broken recently in #9570 when a condition was added to fix a
type error. The condition was meant to check that the controller had a
`subscribe` function. Unfortunately this `hasProperty` check only looks
at owned properties, not inherited properties, and the `subscribe`
function was inherited from the base class. It has been updated to use
the `in` operator instead, which does look up the entire prototype
chain.

## **Related issues**

Fixes #10057

## **Manual testing steps**

See #10057

## **Screenshots/Recordings**

### **Before**

See #10057

### **After**


https://github.com/MetaMask/metamask-mobile/assets/2459287/b7cf3e09-086b-4964-8180-e58195969e17

## **Pre-merge author checklist**

- [x] I’ve followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Mobile
Coding
Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/CODING_GUIDELINES.md).
- [x] I've completed the PR template to the best of my ability
- [x] I’ve included tests if applicable
- [x] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [x] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.

## **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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-7.26.0 Issue or pull request that will be included in release 7.26.0 team-mobile-platform team-wallet-framework
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: NFT disappears after killing and relaunching the app
4 participants