feat(account-tree-controller): persist accountTree#8437
Conversation
|
@metamaskbot publish-preview |
|
Preview builds have been published. Learn how to use preview builds in other projects. Expand for full list of packages and versions. |
| for (const accountId of group.accounts) { | ||
| // The `AccountsController` also persists its accounts, so we | ||
| // can fetch them too! | ||
| const account = this.messenger.call( |
There was a problem hiding this comment.
We can reduce this to one call by getting all the accounts
There was a problem hiding this comment.
I initially wanted to use :getAccounts, but didn't want to introduce a breaking change for this
However, I see we still have the old listMultichainAccounts, so we can probably use that + make a Map from it for fast indexing!
Good idea, let me change this!
There was a problem hiding this comment.
@montelaidev beat me to it. I would prefer the one getAccounts call as well. I don't think it makes sense to rebuild essentially what is the same map to avoid a breaking change
There was a problem hiding this comment.
something like this:
const accounts = this.messenger.call(
'AccountsController:getAccounts',
group.accounts,
);
group.accounts.forEach((accountId, idx) => {
const account = accounts[idx];
let sortOrder = MAX_SORT_ORDER;
if (account) {
sortOrder = ACCOUNT_TYPE_TO_SORT_ORDER[account.type];
}
this.#accountIdToContext.set(accountId, {
walletId,
groupId,
sortOrder,
});
});There was a problem hiding this comment.
I went for :listMultichainAccounts since adding :getAccounts would involve a breaking change for this controller and I'd like to keep it non-breaking for now to avoid the bubbling effect with this change 😅
Though, with :listMultichainAccounts + a Map we can get something pretty similar (I went for that).
There was a problem hiding this comment.
We can always change that later, but my goal for now is to unblock the assets team, we can introduce :getAccounts later for sure 👍
Explanation
We used to not persist the account-tree before and we were re-constructing it "fresh" upon
init. However, some other controllers are dependent on the tree and have to wait for it to be built before consuming it.Since the tree should never really change between lock/unlock, that's ok to have a on-disk copy of it to speedup consumers.
We will still try to re-construct it during
initthough (in case rules have changed or that new accounts have appeared that went unnoticed).References
N/A
Checklist
Note
Medium Risk
Persists and rehydrates core controller state, which can affect startup/lock-unlock behavior and introduce stale/mismatched tree data if persisted state diverges from current accounts.
Overview
AccountTreeControllernow persistsaccountTree(state metadatapersist: true) instead of always recomputing it oninit.The controller constructor now pre-initializes internal reverse lookup maps from the persisted tree via
#initTreeContext, allowing methods likegetAccountContext,getAccountGroupObject, andgetAccountsFromSelectedAccountGroupto work beforeinit()runs.Tests and snapshots are updated to cover pre-populated persisted state behavior and the new persisted-state surface, and the changelog documents the new persistence behavior.
Reviewed by Cursor Bugbot for commit 65dedea. Bugbot is set up for automated code reviews on this repo. Configure here.