Skip to content

Commit

Permalink
Remove old properties from state
Browse files Browse the repository at this point in the history
There are several kinds of errors in Sentry which indicate that there
are properties in controller state we are attempting to persist that do
not have corresponding metadata. This indicates that these properties
were removed at some point from the controller's state but no migration
was added that removed them from the persisted wallet state. In many
cases, at the time of removal, such a migration was not needed because
the controller in question inherited from BaseController v1. However, we
have made a targeted effort over the past few years to migrate all
controllers to BaseController v2, and so it matters now that every
property have corresponding metadata or else are removed from state. We
don't want these errors to show up in Sentry because they create noise,
so this commit removes these properties from state.
  • Loading branch information
mcmire committed Jan 17, 2025
1 parent 582ec93 commit f964268
Show file tree
Hide file tree
Showing 3 changed files with 351 additions and 0 deletions.
271 changes: 271 additions & 0 deletions app/scripts/migrations/139.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,271 @@
import { omit } from 'lodash';
import { migrate, version } from './139';

const oldVersion = 138;

const dataWithAllControllerProperties = {
AppStateController: {},
NetworkController: {},
PreferencesController: {},
};

describe(`migration #${version}`, () => {
it('updates the version metadata', async () => {
const oldStorage = {
meta: { version: oldVersion },
data: {},
};

const newStorage = await migrate(oldStorage);

expect(newStorage.meta).toStrictEqual({ version });
});

it('does nothing if state has no AppStateController property, even if it has other relevant properties', async () => {
const data = omit(dataWithAllControllerProperties, 'AppStateController');
const oldStorage = {
meta: { version: oldVersion },
data,
};

const newStorage = await migrate(oldStorage);

expect(newStorage.data).toStrictEqual(data);
});

it('deletes AppStateController.serviceWorkerLastActiveTime from state', async () => {
const oldStorage = {
meta: { version: oldVersion },
data: {
AppStateController: {
serviceWorkerLastActiveTime: 5,
},
},
};

const newStorage = await migrate(oldStorage);

expect(newStorage.data).toStrictEqual({
AppStateController: {},
});
});

it('does nothing if state has no NetworkController property, even if it has other relevant properties', async () => {
const data = omit(dataWithAllControllerProperties, 'NetworkController');
const oldStorage = {
meta: { version: oldVersion },
data,
};

const newStorage = await migrate(oldStorage);

expect(newStorage.data).toStrictEqual(data);
});

it('deletes NetworkController.networkConfigurations from state', async () => {
const oldStorage = {
meta: { version: oldVersion },
data: {
NetworkController: {
networkConfigurations: {
'AAAA-AAAA-AAAA-AAAA': {
doesnt: 'matter',
},
},
},
},
};

const newStorage = await migrate(oldStorage);

expect(newStorage.data).toStrictEqual({
NetworkController: {},
});
});

it('deletes NetworkController.providerConfig from state', async () => {
const oldStorage = {
meta: { version: oldVersion },
data: {
NetworkController: {
providerConfig: {
doesnt: 'matter',
},
},
},
};

const newStorage = await migrate(oldStorage);

expect(newStorage.data).toStrictEqual({
NetworkController: {},
});
});

it('does nothing if state has no PreferencesController property, even if it has other relevant properties', async () => {
const data = omit(dataWithAllControllerProperties, 'PreferencesController');
const oldStorage = {
meta: { version: oldVersion },
data,
};

const newStorage = await migrate(oldStorage);

expect(newStorage.data).toStrictEqual(data);
});

it('deletes PreferencesController.customNetworkListEnabled from state', async () => {
const oldStorage = {
meta: { version: oldVersion },
data: {
PreferencesController: {
customNetworkListEnabled: true,
},
},
};

const newStorage = await migrate(oldStorage);

expect(newStorage.data).toStrictEqual({
PreferencesController: {},
});
});

it('deletes PreferencesController.disabledRpcMethodPreferences from state', async () => {
const oldStorage = {
meta: { version: oldVersion },
data: {
PreferencesController: {
disabledRpcMethodPreferences: {
doesnt: 'matter',
},
},
},
};

const newStorage = await migrate(oldStorage);

expect(newStorage.data).toStrictEqual({
PreferencesController: {},
});
});

it('deletes PreferencesController.eip1559V2Enabled from state', async () => {
const oldStorage = {
meta: { version: oldVersion },
data: {
PreferencesController: {
eip1559V2Enabled: true,
},
},
};

const newStorage = await migrate(oldStorage);

expect(newStorage.data).toStrictEqual({
PreferencesController: {},
});
});

it('deletes PreferencesController.hasDismissedOpenSeaToBlockaidBanner from state', async () => {
const oldStorage = {
meta: { version: oldVersion },
data: {
PreferencesController: {
hasDismissedOpenSeaToBlockaidBanner: true,
},
},
};

const newStorage = await migrate(oldStorage);

expect(newStorage.data).toStrictEqual({
PreferencesController: {},
});
});

it('deletes PreferencesController.hasMigratedFromOpenSeaToBlockaid from state', async () => {
const oldStorage = {
meta: { version: oldVersion },
data: {
PreferencesController: {
hasMigratedFromOpenSeaToBlockaid: true,
},
},
};

const newStorage = await migrate(oldStorage);

expect(newStorage.data).toStrictEqual({
PreferencesController: {},
});
});

it('deletes PreferencesController.improvedTokenAllowanceEnabled from state', async () => {
const oldStorage = {
meta: { version: oldVersion },
data: {
PreferencesController: {
improvedTokenAllowanceEnabled: true,
},
},
};

const newStorage = await migrate(oldStorage);

expect(newStorage.data).toStrictEqual({
PreferencesController: {},
});
});

it('deletes PreferencesController.infuraBlocked from state', async () => {
const oldStorage = {
meta: { version: oldVersion },
data: {
PreferencesController: {
infuraBlocked: true,
},
},
};

const newStorage = await migrate(oldStorage);

expect(newStorage.data).toStrictEqual({
PreferencesController: {},
});
});

it('deletes PreferencesController.useCollectibleDetection from state', async () => {
const oldStorage = {
meta: { version: oldVersion },
data: {
PreferencesController: {
useCollectibleDetection: true,
},
},
};

const newStorage = await migrate(oldStorage);

expect(newStorage.data).toStrictEqual({
PreferencesController: {},
});
});

it('deletes PreferencesController.useStaticTokenList from state', async () => {
const oldStorage = {
meta: { version: oldVersion },
data: {
PreferencesController: {
useStaticTokenList: true,
},
},
};

const newStorage = await migrate(oldStorage);

expect(newStorage.data).toStrictEqual({
PreferencesController: {},
});
});
});
79 changes: 79 additions & 0 deletions app/scripts/migrations/139.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { hasProperty, isObject } from '@metamask/utils';
import { cloneDeep } from 'lodash';

type VersionedData = {
meta: { version: number };
data: Record<string, unknown>;
};

export const version = 138;

/**
* This migration deletes properties from state which have been removed in
* previous commits.
*
* @param originalVersionedData - Versioned MetaMask extension state, exactly
* what we persist to dist.
* @param originalVersionedData.meta - State metadata.
* @param originalVersionedData.meta.version - The current state version.
* @param originalVersionedData.data - The persisted MetaMask state, keyed by
* controller.
* @returns Updated versioned MetaMask extension state.
*/
export async function migrate(
originalVersionedData: VersionedData,
): Promise<VersionedData> {
const versionedData = cloneDeep(originalVersionedData);
versionedData.meta.version = version;
transformState(versionedData.data);
return versionedData;
}

function transformState(state: Record<string, unknown>) {
if (
hasProperty(state, 'AppStateController') &&
isObject(state.AppStateController)
) {
// Removed in 4ea52511eb7934bf0ce6b9b7d570a525120229ce
delete state.AppStateController.serviceWorkerLastActiveTime;
}

if (
hasProperty(state, 'NetworkController') &&
isObject(state.NetworkController)
) {
// Removed in 555d42b9ead0f4919356ff16e11c663c5e38639e
delete state.NetworkController.networkConfigurations;
// Removed in 800a9d3a177446ff2d05e3e95ec06b3658474207 with a migration, but
// still persists for some people for some reason
delete state.NetworkController.providerConfig;
}

if (
hasProperty(state, 'PreferencesController') &&
isObject(state.PreferencesController)
) {
// Removed in 6c84e9604c7160dd91c685f301f3c8bd128ad3e3
delete state.PreferencesController.customNetworkListEnabled;
// Removed in e6ecd956b054a29481071e4eded2f8cd17d137d2
delete state.PreferencesController.disabledRpcMethodPreferences;
// Removed in 8125473dc53476b6685c5e85918f89bce87e3006
delete state.PreferencesController.eip1559V2Enabled;
// Removed in 699ddccc76302df6130835dc6655077806bf6335
delete state.PreferencesController.hasDismissedOpenSeaToBlockaidBanner;
// I could find references to this in the commit history, but don't know
// where it was removed
delete state.PreferencesController.hasMigratedFromOpenSeaToBlockaid;
// Removed in f988dc1c5ef98ec72212d1f58e736556273b68f7
delete state.PreferencesController.improvedTokenAllowanceEnabled;
// Removed in 315c043785cd5d7a4b0f7e974097ccac18a6b241
delete state.PreferencesController.infuraBlocked;
// Removed in 4f66dc948fee54b8491227414342ab0d373475f1 with a migration, but
// still persists for some people for some reason
delete state.PreferencesController.useCollectibleDetection;
// Removed in eb987a47b51ce410de0047ec883bb4549ce80c85
delete state.PreferencesController.useStaticTokenList;
}

return state;
}
1 change: 1 addition & 0 deletions app/scripts/migrations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ const migrations = [
require('./136'),
require('./137'),
require('./138'),
require('./139'),
];

export default migrations;

0 comments on commit f964268

Please sign in to comment.