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: Add migration to fix NotificationServicesController bug (#12219) #12250

Merged
merged 3 commits into from
Nov 12, 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
34 changes: 11 additions & 23 deletions app/store/migrations/058.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import migrate from './058';
import migrate, { DEFAULT_NOTIFICATION_SERVICES_CONTROLLER } from './058';
import { merge } from 'lodash';
import { captureException } from '@sentry/react-native';
import initialRootState from '../../util/test/initial-root-state';
import initialRootState, { backgroundState } from '../../util/test/initial-root-state';
import mockedEngine from '../../core/__mocks__/MockedEngine';

jest.mock('@sentry/react-native', () => ({
Expand All @@ -13,7 +13,7 @@ jest.mock('../../core/Engine', () => ({
init: () => mockedEngine.init(),
}));

describe('Migration #58 - Update default search engine from DDG to Google', () => {
describe('Migration #58 - Insert NotificationServicesController if missing', () => {
beforeEach(() => {
jest.restoreAllMocks();
jest.resetAllMocks();
Expand Down Expand Up @@ -43,17 +43,6 @@ describe('Migration #58 - Update default search engine from DDG to Google', () =
"FATAL ERROR: Migration 58: Invalid engine backgroundState error: 'object'",
scenario: 'backgroundState is invalid',
},
{
state: merge({}, initialRootState, {
engine: {
backgroundState: {},
},
settings: null,
}),
errorMessage:
"FATAL ERROR: Migration 58: Invalid Settings state error: 'object'",
scenario: 'Settings object is invalid',
},
];

for (const { errorMessage, scenario, state } of invalidStates) {
Expand All @@ -68,23 +57,22 @@ describe('Migration #58 - Update default search engine from DDG to Google', () =
});
}

it('should update the search engine to Google', async () => {
it('should insert default NotificationServicesController if missing', async () => {
const oldState = {
...initialRootState,
engine: {
backgroundState: {},
backgroundState,
},
settings: {
searchEngine: 'DuckDuckGo',
}
};

const expectedState = {
...initialRootState,
engine: {
backgroundState: {},
backgroundState: {
...backgroundState,
NotificationServicesController: DEFAULT_NOTIFICATION_SERVICES_CONTROLLER
},
},
settings: {
searchEngine: 'Google',
}
};

const migratedState = await migrate(oldState);
Expand Down
41 changes: 23 additions & 18 deletions app/store/migrations/058.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
import { captureException } from '@sentry/react-native';
import { isObject } from '@metamask/utils';
import { ensureValidState } from './util';
import { hasProperty, isObject } from '@metamask/utils';

export const DEFAULT_NOTIFICATION_SERVICES_CONTROLLER = {
isCheckingAccountsPresence: false,
isFeatureAnnouncementsEnabled: false,
isFetchingMetamaskNotifications: false,
isMetamaskNotificationsFeatureSeen: false,
isNotificationServicesEnabled: false,
isUpdatingMetamaskNotifications: false,
isUpdatingMetamaskNotificationsAccount: [],
metamaskNotificationsList: [],
metamaskNotificationsReadList: [],
subscriptionAccountsSeen: [],
};

export default function migrate(state: unknown) {
if (!ensureValidState(state, 58)) {
// Increment the migration number as appropriate
return state;
}
if (!ensureValidState(state, 58)) {
return state;
}

if (!isObject(state.settings)) {
captureException(
new Error(
`FATAL ERROR: Migration 58: Invalid Settings state error: '${typeof state.settings}'`,
),
);
if (
!hasProperty(state.engine.backgroundState, 'NotificationServicesController') ||
!isObject(state.engine.backgroundState.NotificationServicesController)
) {
state.engine.backgroundState.NotificationServicesController = DEFAULT_NOTIFICATION_SERVICES_CONTROLLER;
}
return state;
}

state.settings.searchEngine = 'Google';

// Return the modified state
return state;
}
93 changes: 93 additions & 0 deletions app/store/migrations/059.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import migrate from './059';
import { merge } from 'lodash';
import { captureException } from '@sentry/react-native';
import initialRootState from '../../util/test/initial-root-state';
import mockedEngine from '../../core/__mocks__/MockedEngine';

jest.mock('@sentry/react-native', () => ({
captureException: jest.fn(),
}));
const mockedCaptureException = jest.mocked(captureException);

jest.mock('../../core/Engine', () => ({
init: () => mockedEngine.init(),
}));

describe('Migration #59 - Update default search engine from DDG to Google', () => {
beforeEach(() => {
jest.restoreAllMocks();
jest.resetAllMocks();
});

const invalidStates = [
{
state: null,
errorMessage: "FATAL ERROR: Migration 59: Invalid state error: 'object'",
scenario: 'state is invalid',
},
{
state: merge({}, initialRootState, {
engine: null,
}),
errorMessage:
"FATAL ERROR: Migration 59: Invalid engine state error: 'object'",
scenario: 'engine state is invalid',
},
{
state: merge({}, initialRootState, {
engine: {
backgroundState: null,
},
}),
errorMessage:
"FATAL ERROR: Migration 59: Invalid engine backgroundState error: 'object'",
scenario: 'backgroundState is invalid',
},
{
state: merge({}, initialRootState, {
engine: {
backgroundState: {},
},
settings: null,
}),
errorMessage:
"FATAL ERROR: Migration 59: Invalid Settings state error: 'object'",
scenario: 'Settings object is invalid',
},
];

for (const { errorMessage, scenario, state } of invalidStates) {
it(`should capture exception if ${scenario}`, async () => {
const newState = await migrate(state);

expect(newState).toStrictEqual(state);
expect(mockedCaptureException).toHaveBeenCalledWith(expect.any(Error));
expect(mockedCaptureException.mock.calls[0][0].message).toBe(
errorMessage,
);
});
}

it('should update the search engine to Google', async () => {
const oldState = {
engine: {
backgroundState: {},
},
settings: {
searchEngine: 'DuckDuckGo',
}
};

const expectedState = {
engine: {
backgroundState: {},
},
settings: {
searchEngine: 'Google',
}
};

const migratedState = await migrate(oldState);
expect(migratedState).toStrictEqual(expectedState);
});
});
24 changes: 24 additions & 0 deletions app/store/migrations/059.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { captureException } from '@sentry/react-native';
import { isObject } from '@metamask/utils';
import { ensureValidState } from './util';

export default function migrate(state: unknown) {
if (!ensureValidState(state, 59)) {
// Increment the migration number as appropriate
return state;
}

if (!isObject(state.settings)) {
captureException(
new Error(
`FATAL ERROR: Migration 59: Invalid Settings state error: '${typeof state.settings}'`,
),
);
return state;
}

state.settings.searchEngine = 'Google';

// Return the modified state
return state;
}
2 changes: 2 additions & 0 deletions app/store/migrations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import migration55 from './055';
import migration56 from './056';
import migration57 from './057';
import migration58 from './058';
import migration59 from './059';

type MigrationFunction = (state: unknown) => unknown;
type AsyncMigrationFunction = (state: unknown) => Promise<unknown>;
Expand Down Expand Up @@ -130,6 +131,7 @@ export const migrationList: MigrationsList = {
56: migration56,
57: migration57,
58: migration58,
59: migration59,
};

// Enable both synchronous and asynchronous migrations
Expand Down
6 changes: 6 additions & 0 deletions app/util/sentry/tags/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ import { selectPendingApprovals } from '../../../selectors/approvalController';

export function getTraceTags(state: RootState) {
if (!Object.keys(state?.engine?.backgroundState).length) return;
if (!state?.engine?.backgroundState?.AccountsController) return;
if (!state?.engine?.backgroundState?.NftController) return;
if (!state?.engine?.backgroundState?.NotificationServicesController) return;
if (!state?.engine?.backgroundState?.TokensController) return;
if (!state?.engine?.backgroundState?.TransactionController) return;
if (!state?.engine?.backgroundState?.NotificationServicesController) return;
const unlocked = state.user.userLoggedIn;
const accountCount = selectInternalAccounts(state).length;
const nftCount = selectAllNftsFlat(state).length;
Expand Down
Loading