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: Reuse main logic for migration52 #12126

Merged
merged 2 commits into from
Oct 31, 2024
Merged
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
38 changes: 30 additions & 8 deletions app/store/migrations/052.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@ import { hasProperty, isObject } from '@metamask/utils';
import { ensureValidState } from './util';
import { AccountsControllerState } from '@metamask/accounts-controller';

/**
* Validates AccountsController state
*
* @param state - AccountsController state
* @returns - Boolean indicating if state is valid
*/
function isAccountsControllerState(
state: unknown,
migrationNumber: number,
): state is AccountsControllerState {
if (!isObject(state)) {
captureException(
new Error(
`FATAL ERROR: Migration 52: Invalid AccountsController state error: AccountsController state is not an object, type: '${typeof state}'`,
`FATAL ERROR: Migration ${migrationNumber}: Invalid AccountsController state error: AccountsController state is not an object, type: '${typeof state}'`,
),
);
return false;
Expand All @@ -18,7 +25,7 @@ function isAccountsControllerState(
if (!hasProperty(state, 'internalAccounts')) {
captureException(
new Error(
'FATAL ERROR: Migration 52: Invalid AccountsController state error: missing internalAccounts',
`FATAL ERROR: Migration ${migrationNumber}: Invalid AccountsController state error: missing internalAccounts`,
),
);
return false;
Expand All @@ -27,7 +34,7 @@ function isAccountsControllerState(
if (!isObject(state.internalAccounts)) {
captureException(
new Error(
`FATAL ERROR: Migration 52: Invalid AccountsController state error: internalAccounts is not an object, type: '${typeof state.internalAccounts}'`,
`FATAL ERROR: Migration ${migrationNumber}: Invalid AccountsController state error: internalAccounts is not an object, type: '${typeof state.internalAccounts}'`,
),
);
return false;
Expand All @@ -36,7 +43,7 @@ function isAccountsControllerState(
if (!hasProperty(state.internalAccounts, 'accounts')) {
captureException(
new Error(
'FATAL ERROR: Migration 52: Invalid AccountsController state error: missing internalAccounts.accounts',
`FATAL ERROR: Migration ${migrationNumber}: Invalid AccountsController state error: missing internalAccounts.accounts`,
),
);
return false;
Expand All @@ -45,7 +52,7 @@ function isAccountsControllerState(
if (!isObject(state.internalAccounts.accounts)) {
captureException(
new Error(
`FATAL ERROR: Migration 52: Invalid AccountsController state error: internalAccounts.accounts is not an object, type: '${typeof state
`FATAL ERROR: Migration ${migrationNumber}: Invalid AccountsController state error: internalAccounts.accounts is not an object, type: '${typeof state
.internalAccounts.accounts}'`,
),
);
Expand All @@ -55,21 +62,29 @@ function isAccountsControllerState(
return true;
}

export default function migrate(state: unknown) {
if (!ensureValidState(state, 52)) {
/**
* Migration for ensuring that selectedAccount on the AccountsController is defined
*
* @param state - Persisted data store from redux persist
* @param migrationNumber - Migration version
* @returns - Migrated data store
*/
export const migration52 = (state: unknown, migrationNumber: number) => {
if (!ensureValidState(state, migrationNumber)) {
return state;
}

const accountsControllerState =
state.engine.backgroundState.AccountsController;

if (!isAccountsControllerState(accountsControllerState)) {
if (!isAccountsControllerState(accountsControllerState, migrationNumber)) {
return state;
}

const { accounts, selectedAccount } =
accountsControllerState.internalAccounts;

// Set selectedAccount by default if selectedAccount is undefined or account no longer exists for the selectedAccount
if (
Object.values(accounts).length > 0 &&
(!selectedAccount || (selectedAccount && !accounts[selectedAccount]))
Expand All @@ -79,4 +94,11 @@ export default function migrate(state: unknown) {
}

return state;
};

/**
* Migration for ensuring that selectedAccount on the AccountsController is defined
*/
export default function migrate(state: unknown) {
return migration52(state, 52);
}
197 changes: 0 additions & 197 deletions app/store/migrations/057.test.ts

This file was deleted.

84 changes: 5 additions & 79 deletions app/store/migrations/057.ts
Original file line number Diff line number Diff line change
@@ -1,85 +1,11 @@
import { captureException } from '@sentry/react-native';
import { hasProperty, isObject } from '@metamask/utils';
import { ensureValidState } from './util';
import { AccountsControllerState } from '@metamask/accounts-controller';

function isAccountsControllerState(
state: unknown,
): state is AccountsControllerState {
if (!isObject(state)) {
captureException(
new Error(
`FATAL ERROR: Migration 57: Invalid AccountsController state error: AccountsController state is not an object, type: '${typeof state}'`,
),
);
return false;
}

if (!hasProperty(state, 'internalAccounts')) {
captureException(
new Error(
'FATAL ERROR: Migration 57: Invalid AccountsController state error: missing internalAccounts',
),
);
return false;
}

if (!isObject(state.internalAccounts)) {
captureException(
new Error(
`FATAL ERROR: Migration 57: Invalid AccountsController state error: internalAccounts is not an object, type: '${typeof state.internalAccounts}'`,
),
);
return false;
}

if (!hasProperty(state.internalAccounts, 'accounts')) {
captureException(
new Error(
'FATAL ERROR: Migration 57: Invalid AccountsController state error: missing internalAccounts.accounts',
),
);
return false;
}

if (!isObject(state.internalAccounts.accounts)) {
captureException(
new Error(
`FATAL ERROR: Migration 57: Invalid AccountsController state error: internalAccounts.accounts is not an object, type: '${typeof state
.internalAccounts.accounts}'`,
),
);
return false;
}

return true;
}
import { migration52 } from './052';

/**
* Migration for ensuring that selectedAccount on the AccountsController is defined
* Re-uses logic from migration 52
* We have to re-run 52 as 57 because we fixed it in dfcd8c87a19a8d38553aa6a9742b1e7683be9e93
* and users who already had 52 ran would not have the fix
*/
export default function migrate(state: unknown) {
if (!ensureValidState(state, 57)) {
return state;
}

const accountsControllerState =
state.engine.backgroundState.AccountsController;

if (!isAccountsControllerState(accountsControllerState)) {
return state;
}

const { accounts, selectedAccount } =
accountsControllerState.internalAccounts;

if (
Object.values(accounts).length > 0 &&
(!selectedAccount || (selectedAccount && !accounts[selectedAccount]))
) {
accountsControllerState.internalAccounts.selectedAccount =
Object.values(accounts)[0].id;
}

return state;
return migration52(state, 57);
}
Loading