Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions packages/account-tree-controller/src/AccountTreeController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1308,6 +1308,12 @@ export class AccountTreeController extends BaseController<
throw new Error('No accounts found in group');
}

const gutoSetGroupStart = Date.now();
console.log(
'[GUTO PERFORMANCE LOG] AccountTreeController.#setSelectedAccountGroup START',
);

const gutoUpdateStart = Date.now();
this.update((state) => {
// Update our selected account group first.
state.selectedAccountGroup = groupId;
Expand All @@ -1329,25 +1335,48 @@ export class AccountTreeController extends BaseController<
].metadata.lastSelected = now;
}
});
console.log(
`[GUTO PERFORMANCE LOG] AccountTreeController state.update (sync) took ${
Date.now() - gutoUpdateStart
}ms`,
);

log(`Selected group is now: [${this.state.selectedAccountGroup}]`);

const gutoPublishStart = Date.now();
this.messenger.publish(
`${controllerName}:selectedAccountGroupChange`,
groupId,
previousSelectedAccountGroup,
);
console.log(
`[GUTO PERFORMANCE LOG] AccountTreeController publish selectedAccountGroupChange (sync subscriber fan-out) took ${
Date.now() - gutoPublishStart
}ms`,
);

if (forwardSelectedAccount) {
// Update AccountsController - this will trigger selectedAccountChange event,
// but our handler is idempotent so it won't cause infinite loop
const gutoSetAccountStart = Date.now();
this.messenger.call(
'AccountsController:setSelectedAccount',
accountToSelect,
);
console.log(
`[GUTO PERFORMANCE LOG] AccountTreeController AccountsController:setSelectedAccount (sync selectedAccountChange fan-out) took ${
Date.now() - gutoSetAccountStart
}ms`,
);

log(`Selected account is now: ${accountToSelect}`);
}

console.log(
`[GUTO PERFORMANCE LOG] AccountTreeController.#setSelectedAccountGroup END (total, sync) took ${
Date.now() - gutoSetGroupStart
}ms`,
);
}

/**
Expand Down
23 changes: 23 additions & 0 deletions packages/assets-controller/src/AssetsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3333,6 +3333,10 @@ export class AssetsController extends BaseController<
// ============================================================================

async #handleAccountGroupChanged(): Promise<void> {
const gutoStart = Date.now();
console.log(
'[GUTO PERFORMANCE LOG] AssetsController.#handleAccountGroupChanged START',
);
const accounts = this.#getSelectedAccounts();

log('Account group changed', {
Expand All @@ -3342,13 +3346,27 @@ export class AssetsController extends BaseController<

this.#lastKnownAccountIds = new Set(accounts.map((a) => a.id));

const gutoMutexStart = Date.now();
const releaseLock = await this.#accountRefreshMutex.acquire();
console.log(
`[GUTO PERFORMANCE LOG] AssetsController accountRefreshMutex wait took ${
Date.now() - gutoMutexStart
}ms`,
);
try {
if (accounts.length > 0) {
const gutoGetAssetsStart = Date.now();
await this.getAssets(accounts, {
chainIds: [...this.#enabledChains],
forceUpdate: true,
});
console.log(
`[GUTO PERFORMANCE LOG] AssetsController.getAssets (accounts=${
accounts.length
}, chains=${this.#enabledChains.size}) took ${
Date.now() - gutoGetAssetsStart
}ms`,
);
}

// Subscribe after fetch so WS notifications can recover state
Expand All @@ -3359,6 +3377,11 @@ export class AssetsController extends BaseController<
} finally {
releaseLock();
}
console.log(
`[GUTO PERFORMANCE LOG] AssetsController.#handleAccountGroupChanged END (total) took ${
Date.now() - gutoStart
}ms`,
);
}

async #handleEnabledNetworksChanged(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,22 @@ export class DeFiPositionsController extends StaticIntervalPollingController()<
this.messenger.subscribe(
'AccountTreeController:selectedAccountGroupChange',
async () => {
const gutoStart = Date.now();
console.log(
'[GUTO PERFORMANCE LOG] DeFiPositionsController selectedAccountGroupChange handler START',
);
const selectedAddress = this.#getSelectedEvmAdress();

if (!selectedAddress) {
return;
}

await this.#updateAccountPositions(selectedAddress);
console.log(
`[GUTO PERFORMANCE LOG] DeFiPositionsController.#updateAccountPositions resolved in ${
Date.now() - gutoStart
}ms`,
);
},
);

Expand Down
34 changes: 31 additions & 3 deletions packages/earn-controller/src/EarnController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -415,13 +415,41 @@ export class EarnController extends BaseController<
this.messenger.subscribe(
'AccountTreeController:selectedAccountGroupChange',
() => {
const gutoStart = Date.now();
console.log(
'[GUTO PERFORMANCE LOG] EarnController selectedAccountGroupChange handler START',
);
const address = this.#getSelectedEvmAccountAddress();

// TODO: temp solution, this will refresh lending eligibility also
// we could have a more general check, as what is happening is a compliance address check
this.refreshEarnEligibility({ address }).catch(console.error);
this.refreshPooledStakes({ address }).catch(console.error);
this.refreshLendingPositions({ address }).catch(console.error);
this.refreshEarnEligibility({ address })
.then(() =>
console.log(
`[GUTO PERFORMANCE LOG] EarnController.refreshEarnEligibility resolved in ${
Date.now() - gutoStart
}ms`,
),
)
.catch(console.error);
this.refreshPooledStakes({ address })
.then(() =>
console.log(
`[GUTO PERFORMANCE LOG] EarnController.refreshPooledStakes resolved in ${
Date.now() - gutoStart
}ms`,
),
)
.catch(console.error);
this.refreshLendingPositions({ address })
.then(() =>
console.log(
`[GUTO PERFORMANCE LOG] EarnController.refreshLendingPositions resolved in ${
Date.now() - gutoStart
}ms`,
),
)
.catch(console.error);
},
);

Expand Down
23 changes: 20 additions & 3 deletions packages/perps-controller/src/PerpsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3266,6 +3266,10 @@ export class PerpsController extends BaseController<

// Watch for selected account changes and selected account group changes.
const accountChangeHandler = (): void => {
const gutoStart = Date.now();
console.log(
'[GUTO PERFORMANCE LOG] PerpsController accountChangeHandler START',
);
const evmAccount = getSelectedEvmAccountFromMessenger(this.messenger);
const currentAddress = evmAccount?.address ?? null;

Expand All @@ -3292,11 +3296,24 @@ export class PerpsController extends BaseController<
});
// Only preload if the new account is an EVM account
if (currentAddress) {
this.#performUserDataPreload().catch(() => {
/* fire-and-forget */
});
this.#performUserDataPreload()
.then(() =>
console.log(
`[GUTO PERFORMANCE LOG] PerpsController.#performUserDataPreload resolved in ${
Date.now() - gutoStart
}ms`,
),
)
.catch(() => {
/* fire-and-forget */
});
}
}
console.log(
`[GUTO PERFORMANCE LOG] PerpsController accountChangeHandler END (sync) took ${
Date.now() - gutoStart
}ms hasStaleEntries=${hasStaleEntries}`,
);
};
this.messenger.subscribe(
'AccountsController:selectedAccountChange',
Expand Down
23 changes: 18 additions & 5 deletions packages/snap-account-service/src/SnapAccountService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,11 +288,24 @@
* @param groupId - The ID of the newly selected account group.
*/
#handleSelectedAccountGroupChange(groupId: AccountGroupId | ''): void {
// eslint-disable-next-line no-void
void this.#forwardSelectedAccounts(
const gutoStart = Date.now();
console.log(
'[GUTO PERFORMANCE LOG] SnapAccountService.#handleSelectedAccountGroupChange START',
);
this.#forwardSelectedAccounts(
groupId,
this.#getAccountGroup(groupId)?.accounts,
);
)
.then(() =>
console.log(
`[GUTO PERFORMANCE LOG] SnapAccountService.#forwardSelectedAccounts resolved in ${
Date.now() - gutoStart
}ms`,
),
)
.catch(() => {
/* fire-and-forget */
});
}

/**
Expand Down Expand Up @@ -499,7 +512,7 @@
({ keyringV2 }) =>
keyringV2 &&
isSnapKeyring(keyringV2) &&
keyringV2.snapId === snapId,
(keyringV2 as SnapKeyring).snapId === snapId,

Check failure on line 515 in packages/snap-account-service/src/SnapAccountService.ts

View workflow job for this annotation

GitHub Actions / Lint, build, and test / Lint (lint:eslint)

This assertion is unnecessary since it does not change the type of the expression
);

if (!hasKeyring) {
Expand Down Expand Up @@ -536,7 +549,7 @@
action,
{
filter: (keyring): keyring is SnapKeyring =>
isSnapKeyring(keyring) && keyring.snapId === snapId,
isSnapKeyring(keyring) && (keyring as SnapKeyring).snapId === snapId,

Check failure on line 552 in packages/snap-account-service/src/SnapAccountService.ts

View workflow job for this annotation

GitHub Actions / Lint, build, and test / Lint (lint:eslint)

This assertion is unnecessary since it does not change the type of the expression
},
async ({ keyring }) => operation(keyring as SnapKeyring),
) as Result;
Expand Down
Loading