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(accounts-controller): re-use current internal state in updateAccounts #5161

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

ccharly
Copy link
Contributor

@ccharly ccharly commented Jan 16, 2025

Explanation

The AccountsController sometimes re-build its state by querying all accounts from every keyrings. This makes sure it's in-sync with the KeyringController's state (mirroring).

However, the way this was done was preventing clients from doing migrations to the AccountsController's state (mainly on the fields owned by him, like the InternalAccount's fields).

The previous logic was re-creating all internal accounts with a new copy of all "keyring accounts", thus overriding changes done to the state.AccountsController.internalAccounts object.

This issue was found while adding the new required scopes field with a migration, see:

Detailed explanations (for the extension):

  • Client runs a migration to inject scopes to each accounts (see here)
  • Client then unlocks the extension
  • updateAccounts fetches accounts from each keyrings:
    • "normal" accounts were "wrongly" getting their scopes from this logic here
    • Snap accounts were not getting any scopes since we were using the previous Snap keyring state (the migration only affects the AccountsController state) which did not have the scopes field, no scopes was added when re-creating the internal state here.

This logic was overwriting the migration state update, resulting in a non-consistent state where we had scopes for all "normal" accounts, but an undefined scopes for ALL Snap accounts.

The fix

To prevent this from happening, we are now re-using the current internal state if the account already exists, thus, keeping its current "representation" (in our case, this representation got migrated and now has a new scopes for all existing accounts).

However, if the account does not exist on the current internal state, we are then just adding it to the state "as-is".

References

Changelog

@metamask/accounts-controller

  • FIXED: Re-use internal state in updateAccounts
    • The internal state was "refreshed" using keyrings state without re-using the current staet, thus preventing any migrations to the controller state.

Checklist

  • I've updated the test suite for new or updated code as appropriate
  • I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate
  • I've highlighted breaking changes using the "BREAKING" category above as appropriate
  • I've prepared draft pull requests for clients and consumer packages to resolve any breaking changes

@ccharly
Copy link
Contributor Author

ccharly commented Jan 16, 2025

@metamaskbot publish-preview

Copy link
Contributor

Preview builds have been published. See these instructions for more information about preview builds.

Expand for full list of packages and versions.
{
  "@metamask-previews/accounts-controller": "21.0.0-preview-bb5ab3f",
  "@metamask-previews/address-book-controller": "6.0.2-preview-bb5ab3f",
  "@metamask-previews/announcement-controller": "7.0.2-preview-bb5ab3f",
  "@metamask-previews/approval-controller": "7.1.2-preview-bb5ab3f",
  "@metamask-previews/assets-controllers": "46.0.0-preview-bb5ab3f",
  "@metamask-previews/base-controller": "7.1.1-preview-bb5ab3f",
  "@metamask-previews/build-utils": "3.0.2-preview-bb5ab3f",
  "@metamask-previews/composable-controller": "10.0.0-preview-bb5ab3f",
  "@metamask-previews/controller-utils": "11.4.5-preview-bb5ab3f",
  "@metamask-previews/ens-controller": "15.0.1-preview-bb5ab3f",
  "@metamask-previews/eth-json-rpc-provider": "4.1.7-preview-bb5ab3f",
  "@metamask-previews/gas-fee-controller": "22.0.2-preview-bb5ab3f",
  "@metamask-previews/json-rpc-engine": "10.0.2-preview-bb5ab3f",
  "@metamask-previews/json-rpc-middleware-stream": "8.0.6-preview-bb5ab3f",
  "@metamask-previews/keyring-controller": "19.0.3-preview-bb5ab3f",
  "@metamask-previews/logging-controller": "6.0.3-preview-bb5ab3f",
  "@metamask-previews/message-manager": "11.0.3-preview-bb5ab3f",
  "@metamask-previews/multichain": "2.0.0-preview-bb5ab3f",
  "@metamask-previews/name-controller": "8.0.2-preview-bb5ab3f",
  "@metamask-previews/network-controller": "22.1.1-preview-bb5ab3f",
  "@metamask-previews/notification-services-controller": "0.16.0-preview-bb5ab3f",
  "@metamask-previews/permission-controller": "11.0.5-preview-bb5ab3f",
  "@metamask-previews/permission-log-controller": "3.0.2-preview-bb5ab3f",
  "@metamask-previews/phishing-controller": "12.3.1-preview-bb5ab3f",
  "@metamask-previews/polling-controller": "12.0.2-preview-bb5ab3f",
  "@metamask-previews/preferences-controller": "15.0.1-preview-bb5ab3f",
  "@metamask-previews/profile-sync-controller": "4.1.0-preview-bb5ab3f",
  "@metamask-previews/queued-request-controller": "8.0.2-preview-bb5ab3f",
  "@metamask-previews/rate-limit-controller": "6.0.2-preview-bb5ab3f",
  "@metamask-previews/remote-feature-flag-controller": "1.3.0-preview-bb5ab3f",
  "@metamask-previews/selected-network-controller": "20.0.2-preview-bb5ab3f",
  "@metamask-previews/signature-controller": "23.2.0-preview-bb5ab3f",
  "@metamask-previews/transaction-controller": "43.0.0-preview-bb5ab3f",
  "@metamask-previews/user-operation-controller": "22.0.0-preview-bb5ab3f"
}

@ccharly ccharly changed the title fix(accounts-controller): re-use internal state in updateAccounts state update fix(accounts-controller): re-use internal state in updateAccounts state update Jan 16, 2025
@ccharly ccharly changed the title fix(accounts-controller): re-use internal state in updateAccounts state update fix(accounts-controller): re-use current internal state in updateAccounts Jan 16, 2025
@ccharly ccharly force-pushed the fix/do-not-overwrite-internal-accounts-from-state branch from 49b28e1 to d2b96e9 Compare January 16, 2025 11:19
@ccharly ccharly marked this pull request as ready for review January 16, 2025 11:20
@ccharly ccharly requested a review from a team as a code owner January 16, 2025 11:20
@ccharly
Copy link
Contributor Author

ccharly commented Jan 16, 2025

@metamaskbot publish-preview

Copy link
Contributor

Preview builds have been published. See these instructions for more information about preview builds.

Expand for full list of packages and versions.
{
  "@metamask-previews/accounts-controller": "21.0.0-preview-d2b96e95",
  "@metamask-previews/address-book-controller": "6.0.2-preview-d2b96e95",
  "@metamask-previews/announcement-controller": "7.0.2-preview-d2b96e95",
  "@metamask-previews/approval-controller": "7.1.2-preview-d2b96e95",
  "@metamask-previews/assets-controllers": "46.0.0-preview-d2b96e95",
  "@metamask-previews/base-controller": "7.1.1-preview-d2b96e95",
  "@metamask-previews/build-utils": "3.0.2-preview-d2b96e95",
  "@metamask-previews/composable-controller": "10.0.0-preview-d2b96e95",
  "@metamask-previews/controller-utils": "11.4.5-preview-d2b96e95",
  "@metamask-previews/ens-controller": "15.0.1-preview-d2b96e95",
  "@metamask-previews/eth-json-rpc-provider": "4.1.7-preview-d2b96e95",
  "@metamask-previews/gas-fee-controller": "22.0.2-preview-d2b96e95",
  "@metamask-previews/json-rpc-engine": "10.0.2-preview-d2b96e95",
  "@metamask-previews/json-rpc-middleware-stream": "8.0.6-preview-d2b96e95",
  "@metamask-previews/keyring-controller": "19.0.3-preview-d2b96e95",
  "@metamask-previews/logging-controller": "6.0.3-preview-d2b96e95",
  "@metamask-previews/message-manager": "11.0.3-preview-d2b96e95",
  "@metamask-previews/multichain": "2.0.0-preview-d2b96e95",
  "@metamask-previews/name-controller": "8.0.2-preview-d2b96e95",
  "@metamask-previews/network-controller": "22.1.1-preview-d2b96e95",
  "@metamask-previews/notification-services-controller": "0.16.0-preview-d2b96e95",
  "@metamask-previews/permission-controller": "11.0.5-preview-d2b96e95",
  "@metamask-previews/permission-log-controller": "3.0.2-preview-d2b96e95",
  "@metamask-previews/phishing-controller": "12.3.1-preview-d2b96e95",
  "@metamask-previews/polling-controller": "12.0.2-preview-d2b96e95",
  "@metamask-previews/preferences-controller": "15.0.1-preview-d2b96e95",
  "@metamask-previews/profile-sync-controller": "4.1.0-preview-d2b96e95",
  "@metamask-previews/queued-request-controller": "8.0.2-preview-d2b96e95",
  "@metamask-previews/rate-limit-controller": "6.0.2-preview-d2b96e95",
  "@metamask-previews/remote-feature-flag-controller": "1.3.0-preview-d2b96e95",
  "@metamask-previews/selected-network-controller": "20.0.2-preview-d2b96e95",
  "@metamask-previews/signature-controller": "23.2.0-preview-d2b96e95",
  "@metamask-previews/transaction-controller": "43.0.0-preview-d2b96e95",
  "@metamask-previews/user-operation-controller": "22.0.0-preview-d2b96e95"
}

@ccharly ccharly force-pushed the fix/do-not-overwrite-internal-accounts-from-state branch from d2b96e9 to 5b0a5fe Compare January 16, 2025 11:50
@ccharly
Copy link
Contributor Author

ccharly commented Jan 16, 2025

@metamaskbot publish-preview

Copy link
Contributor

Preview builds have been published. See these instructions for more information about preview builds.

Expand for full list of packages and versions.
{
  "@metamask-previews/accounts-controller": "21.0.0-preview-5b0a5fea",
  "@metamask-previews/address-book-controller": "6.0.2-preview-5b0a5fea",
  "@metamask-previews/announcement-controller": "7.0.2-preview-5b0a5fea",
  "@metamask-previews/approval-controller": "7.1.2-preview-5b0a5fea",
  "@metamask-previews/assets-controllers": "46.0.0-preview-5b0a5fea",
  "@metamask-previews/base-controller": "7.1.1-preview-5b0a5fea",
  "@metamask-previews/build-utils": "3.0.2-preview-5b0a5fea",
  "@metamask-previews/composable-controller": "10.0.0-preview-5b0a5fea",
  "@metamask-previews/controller-utils": "11.4.5-preview-5b0a5fea",
  "@metamask-previews/ens-controller": "15.0.1-preview-5b0a5fea",
  "@metamask-previews/eth-json-rpc-provider": "4.1.7-preview-5b0a5fea",
  "@metamask-previews/gas-fee-controller": "22.0.2-preview-5b0a5fea",
  "@metamask-previews/json-rpc-engine": "10.0.2-preview-5b0a5fea",
  "@metamask-previews/json-rpc-middleware-stream": "8.0.6-preview-5b0a5fea",
  "@metamask-previews/keyring-controller": "19.0.3-preview-5b0a5fea",
  "@metamask-previews/logging-controller": "6.0.3-preview-5b0a5fea",
  "@metamask-previews/message-manager": "11.0.3-preview-5b0a5fea",
  "@metamask-previews/multichain": "2.0.0-preview-5b0a5fea",
  "@metamask-previews/name-controller": "8.0.2-preview-5b0a5fea",
  "@metamask-previews/network-controller": "22.1.1-preview-5b0a5fea",
  "@metamask-previews/notification-services-controller": "0.16.0-preview-5b0a5fea",
  "@metamask-previews/permission-controller": "11.0.5-preview-5b0a5fea",
  "@metamask-previews/permission-log-controller": "3.0.2-preview-5b0a5fea",
  "@metamask-previews/phishing-controller": "12.3.1-preview-5b0a5fea",
  "@metamask-previews/polling-controller": "12.0.2-preview-5b0a5fea",
  "@metamask-previews/preferences-controller": "15.0.1-preview-5b0a5fea",
  "@metamask-previews/profile-sync-controller": "4.1.0-preview-5b0a5fea",
  "@metamask-previews/queued-request-controller": "8.0.2-preview-5b0a5fea",
  "@metamask-previews/rate-limit-controller": "6.0.2-preview-5b0a5fea",
  "@metamask-previews/remote-feature-flag-controller": "1.3.0-preview-5b0a5fea",
  "@metamask-previews/selected-network-controller": "20.0.2-preview-5b0a5fea",
  "@metamask-previews/signature-controller": "23.2.0-preview-5b0a5fea",
  "@metamask-previews/transaction-controller": "43.0.0-preview-5b0a5fea",
  "@metamask-previews/user-operation-controller": "22.0.0-preview-5b0a5fea"
}

@ccharly ccharly marked this pull request as draft January 16, 2025 22:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants