Skip to content

perf!: Add accountIdByAddress to AccountsController state#7893

Open
hmalik88 wants to merge 22 commits intomainfrom
hm/mul-1449
Open

perf!: Add accountIdByAddress to AccountsController state#7893
hmalik88 wants to merge 22 commits intomainfrom
hm/mul-1449

Conversation

@hmalik88
Copy link
Contributor

@hmalik88 hmalik88 commented Feb 10, 2026

Explanation

Currently when doing an account lookup by address we're doing an O(n) operation in the clients, this has been improved to O(1) by adding an address -> account id mapping in state. The getAccountByAddress function now also takes advantage of this new piece of state to improve its lookup time.

Note: This will require a migration in the clients.

References

N/A

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 communicated my changes to consumers by updating changelogs for packages I've changed
  • I've introduced breaking changes in this PR and have prepared draft pull requests for clients and consumer packages to resolve them

Note

Medium Risk
Breaking state-shape change for consumers and derived index must stay in sync with account mutations; mistakes could cause incorrect account resolution by address.

Overview
Adds a new AccountsController state field accountIdByAddress (address �� accountId) to speed up address-based account lookups.

getAccountByAddress now resolves via this mapping rather than scanning accounts, and the mapping is (re)built whenever accounts are constructed/updated (constructor, updateAccounts, keyring add/remove handling, and loadBackup). The change is marked BREAKING in the accounts-controller changelog, and downstream tests were updated to include the new state shape (including in other packages that mock AccountsController state).

Written by Cursor Bugbot for commit 27e39dd. This will update automatically on new commits. Configure here.

@hmalik88 hmalik88 requested a review from a team as a code owner February 10, 2026 16:13
@hmalik88 hmalik88 requested a review from a team as a code owner February 10, 2026 16:14
@hmalik88 hmalik88 changed the title perf: Add accountIdByAddress to AccountsController state perf!: Add accountIdByAddress to AccountsController state Feb 10, 2026
Comment on lines 442 to 444
const accountId =
this.state.internalAccounts.accountIdByAddress[address.toLowerCase()];
return accountId ? this.getAccount(accountId) : undefined;
Copy link
Contributor

Choose a reason for hiding this comment

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

We have this problem since quite some time now, but we should not lower-case addresses 😬 While EVM addresses are case-insensitive, non-EVM (at least Solana) are case-sensitives.

"Sometimes" (yes, we have this problem elsewhere 🥹) we're using the account.type to check if it's an EVM account type, and we either use checksummed addresses or lower-cased addresses.

Since we only have the address here, I wonder if we should check for the 0x prefix and lower-case only in this case?

Ideally we should just decide what is the format we want to use everywhere and we just stick to this, but we probably have some legacy code depending on non-sanitized addresses sometimes so...

Any opinion?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just decided to continue to lowercase and left a comment around it as we discussed!


### Added

- **BREAKING:** Add `accountIdByAddress` mapping to state and update `getAccountByAddress` function ([#7893](https://github.com/MetaMask/core/pull/7893))
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this breaking?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It classifies as breaking since we have to write a migration in the clients no? getAccountByAddress calls would break.

@hmalik88 hmalik88 requested a review from a team as a code owner February 10, 2026 19:37
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants