perf: Add accountIdByAddress to AccountsController state#7893
perf: Add accountIdByAddress to AccountsController state#7893
accountIdByAddress to AccountsController state#7893Conversation
accountIdByAddress to AccountsController stateaccountIdByAddress to AccountsController state
| const accountId = | ||
| this.state.internalAccounts.accountIdByAddress[address.toLowerCase()]; | ||
| return accountId ? this.getAccount(accountId) : undefined; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
It classifies as breaking since we have to write a migration in the clients no? getAccountByAddress calls would break.
There was a problem hiding this comment.
I don't think so since this should be constructed inside the constructor therefore we shouldn't need a migration nor breaking change
|
@metamaskbot publish-preview |
|
Preview builds have been published. See these instructions for more information about preview builds. Expand for full list of packages and versions. |
mathieuartu
left a comment
There was a problem hiding this comment.
Looks good, added one possible readability improvement suggestion and a perf question
accountIdByAddress to AccountsController stateaccountIdByAddress to AccountsController state
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
getAccountByAddressfunction 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
Note
Medium Risk
State shape changes are breaking for consumers and require client migrations; correctness depends on address normalization (currently forced to lowercase), which could be problematic for case-sensitive non-EVM addresses.
Overview
Improves
AccountsControlleraddress lookups by adding a newaccountIdByAddress(lowercased address → account id) map to controller state and switchinggetAccountByAddressfrom linear scan to direct map access.Keeps this map in sync by (re)building it in the constructor,
updateAccounts,loadBackup, and account add/remove flows; it’s marked non-persisted and excluded from state logs. Updates controller typings and adjusts tests/fixtures across dependent packages to include the new state field.Written by Cursor Bugbot for commit b51eceb. This will update automatically on new commits. Configure here.