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

chore(runway): cherry-pick fix: freeze during swap with approval #11209

Merged
merged 3 commits into from
Sep 13, 2024

Conversation

runway-github[bot]
Copy link
Contributor

@runway-github runway-github bot commented Sep 13, 2024

Description

The freeze was ultimately caused by a render loop inside
RootRPCMethodsUI caused by the
TransactionController:unapprovedTransactionAdded listener.

More specifically:

  1. When swaps are detected, the transaction is automatically approved
    via the autoSign function.
  2. This registers a listener to send a completed metric once the
    transaction is confirmed.
  3. This is triggered by adding the transaction ID to a queue via the
    addTransactionMetaIdForListening function.
  4. This function updates React state, but was also using that same state
    as a dependency, meaning the callback was updated on every usage.
  5. This function was a dependency of autoSign which was in turn a
    dependency of onUnapprovedTransaction which was the only dependency
    for a useEffect that re-created the
    TransactionController:unapprovedTransactionAdded listener.
  6. The ControllerMessenger processes listeners using an iterator
    rather than a static list meaning each new listener was automatically
    ran which in turn created another listener and resulted in a very high
    CPU render loop.

The core fix was to update the addTransactionMetaIdForListening to
update the state using the callback override meaning it did not require
the state dependency.

However during testing it was discovered that this swaps completed
metric was not functioning since the swapsTransaction state was being
incorrectly populated with the TransactionController instance due to a
legacy function argument that was not fully removed.

This has been addressed, and all swaps transactions updates have been
abstracted via a new swaps-transactions utility file.

Related issues

Fixes:
#11085

Manual testing steps

Full swaps regression, including:

  • Swaps with approvals.
  • Smart swaps.
  • Metric support.
  • Multiple sequential swaps (with and without approvals).

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.

Co-authored-by: sethkfman 10342624+sethkfman@users.noreply.github.com 059a391

)

## **Description**

The freeze was ultimately caused by a render loop inside
`RootRPCMethodsUI` caused by the
`TransactionController:unapprovedTransactionAdded` listener.

More specifically:

1. When swaps are detected, the transaction is automatically approved
via the `autoSign` function.
2. This registers a listener to send a completed metric once the
transaction is confirmed.
3. This is triggered by adding the transaction ID to a queue via the
`addTransactionMetaIdForListening` function.
4. This function updates React state, but was also using that same state
as a dependency, meaning the callback was updated on every usage.
5. This function was a dependency of `autoSign` which was in turn a
dependency of `onUnapprovedTransaction` which was the only dependency
for a `useEffect` that re-created the
`TransactionController:unapprovedTransactionAdded` listener.
6. The `ControllerMessenger` processes listeners using an iterator
rather than a static list meaning each new listener was automatically
ran which in turn created another listener and resulted in a very high
CPU render loop.

The core fix was to update the `addTransactionMetaIdForListening` to
update the state using the callback override meaning it did not require
the state dependency.

However during testing it was discovered that this swaps completed
metric was not functioning since the `swapsTransaction` state was being
incorrectly populated with the `TransactionController` instance due to a
legacy function argument that was not fully removed.

This has been addressed, and all swaps transactions updates have been
abstracted via a new `swaps-transactions` utility file.

## **Related issues**

Fixes:
[#11085](#11085)

## **Manual testing steps**

Full swaps regression, including:

- Swaps with approvals.
- Smart swaps.
- Metric support.
- Multiple sequential swaps (with and without approvals).

## **Screenshots/Recordings**

### **Before**

### **After**

## **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.

---------

Co-authored-by: sethkfman <10342624+sethkfman@users.noreply.github.com>
@runway-github runway-github bot requested review from a team as code owners September 13, 2024 20:31
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.

Copy link
Contributor

@sethkfman sethkfman left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link

@sethkfman sethkfman merged commit c0ff7f4 into release/7.31.0 Sep 13, 2024
34 of 35 checks passed
@sethkfman sethkfman deleted the runway-cherry-pick-7.31.0-1726259478 branch September 13, 2024 21:26
@github-actions github-actions bot locked and limited conversation to collaborators Sep 13, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants