1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-22 09:57:02 +01:00

Rename migration 83 to 84 and rename migration 84 to 83 (#18655)

Co-authored-by: legobeat <109787230+legobeat@users.noreply.github.com>
This commit is contained in:
Dan J Miller 2023-04-19 11:38:16 -02:30 committed by GitHub
parent 1cc709af41
commit 7bc13e90f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 362 additions and 362 deletions

View File

@ -1,103 +1,254 @@
import { migrate } from './083'; import { v4 } from 'uuid';
import { migrate, version } from './083';
describe('migration #83', () => { jest.mock('uuid', () => {
it('updates the version metadata', async () => { const actual = jest.requireActual('uuid');
const originalVersionedData = buildOriginalVersionedData({
meta: {
version: 9999999,
},
});
const newVersionedData = await migrate(originalVersionedData); return {
...actual,
expect(newVersionedData.meta).toStrictEqual({ v4: jest.fn(),
version: 83, };
});
});
it('does not change the state if the network controller state does not exist', async () => {
const originalVersionedData = buildOriginalVersionedData({
data: {
test: '123',
},
});
const newVersionedData = await migrate(originalVersionedData);
expect(newVersionedData.data).toStrictEqual(originalVersionedData.data);
});
const nonObjects = [undefined, null, 'test', 1, ['test']];
for (const invalidState of nonObjects) {
it(`does not change the state if the network controller state is ${invalidState}`, async () => {
const originalVersionedData = buildOriginalVersionedData({
data: {
NetworkController: invalidState,
},
});
const newVersionedData = await migrate(originalVersionedData);
expect(newVersionedData.data).toStrictEqual(originalVersionedData.data);
});
}
it('does not change the state if the network controller state does not include "network"', async () => {
const originalVersionedData = buildOriginalVersionedData({
data: {
NetworkController: {
test: '123',
},
},
});
const newVersionedData = await migrate(originalVersionedData);
expect(newVersionedData.data).toStrictEqual(originalVersionedData.data);
});
it('replaces "network" in the network controller state with "networkId": null, "networkStatus": "unknown" if it is "loading"', async () => {
const originalVersionedData = buildOriginalVersionedData({
data: {
NetworkController: {
network: 'loading',
},
},
});
const newVersionedData = await migrate(originalVersionedData);
expect(newVersionedData.data).toStrictEqual({
NetworkController: {
networkId: null,
networkStatus: 'unknown',
},
});
});
it('replaces "network" in the network controller state with "networkId": network, "networkStatus": "available" if it is not "loading"', async () => {
const originalVersionedData = buildOriginalVersionedData({
data: {
NetworkController: {
network: '12345',
},
},
});
const newVersionedData = await migrate(originalVersionedData);
expect(newVersionedData.data).toStrictEqual({
NetworkController: {
networkId: '12345',
networkStatus: 'available',
},
});
});
}); });
function buildOriginalVersionedData({ meta = {}, data = {} } = {}) { describe('migration #83', () => {
return { beforeEach(() => {
meta: { version: 999999, ...meta }, v4.mockImplementationOnce(() => 'network-configuration-id-1')
data: { ...data }, .mockImplementationOnce(() => 'network-configuration-id-2')
}; .mockImplementationOnce(() => 'network-configuration-id-3')
} .mockImplementationOnce(() => 'network-configuration-id-4');
});
afterEach(() => {
jest.resetAllMocks();
});
it('should update the version metadata', async () => {
const oldStorage = {
meta: {
version: 82,
},
data: {},
};
const newStorage = await migrate(oldStorage);
expect(newStorage.meta).toStrictEqual({
version,
});
});
it('should use the key of the networkConfigurations object to set the id of each network configuration', async () => {
const oldStorage = {
meta: {
version,
},
data: {
NetworkController: {
networkConfigurations: {
'network-configuration-id-1': {
chainId: '0x539',
nickname: 'Localhost 8545',
rpcPrefs: {},
rpcUrl: 'http://localhost:8545',
ticker: 'ETH',
},
'network-configuration-id-2': {
chainId: '0xa4b1',
nickname: 'Arbitrum One',
rpcPrefs: {
blockExplorerUrl: 'https://explorer.arbitrum.io',
},
rpcUrl:
'https://arbitrum-mainnet.infura.io/v3/373266a93aab4acda48f89d4fe77c748',
ticker: 'ETH',
},
'network-configuration-id-3': {
chainId: '0x4e454152',
nickname: 'Aurora Mainnet',
rpcPrefs: {
blockExplorerUrl: 'https://aurorascan.dev/',
},
rpcUrl:
'https://aurora-mainnet.infura.io/v3/373266a93aab4acda48f89d4fe77c748',
ticker: 'Aurora ETH',
},
'network-configuration-id-4': {
chainId: '0x38',
nickname:
'BNB Smart Chain (previously Binance Smart Chain Mainnet)',
rpcPrefs: {
blockExplorerUrl: 'https://bscscan.com/',
},
rpcUrl: 'https://bsc-dataseed.binance.org/',
ticker: 'BNB',
},
},
},
},
};
const newStorage = await migrate(oldStorage);
const expectedNewStorage = {
meta: {
version,
},
data: {
NetworkController: {
networkConfigurations: {
'network-configuration-id-1': {
chainId: '0x539',
nickname: 'Localhost 8545',
rpcPrefs: {},
rpcUrl: 'http://localhost:8545',
ticker: 'ETH',
id: 'network-configuration-id-1',
},
'network-configuration-id-2': {
chainId: '0xa4b1',
nickname: 'Arbitrum One',
rpcPrefs: {
blockExplorerUrl: 'https://explorer.arbitrum.io',
},
rpcUrl:
'https://arbitrum-mainnet.infura.io/v3/373266a93aab4acda48f89d4fe77c748',
ticker: 'ETH',
id: 'network-configuration-id-2',
},
'network-configuration-id-3': {
chainId: '0x4e454152',
nickname: 'Aurora Mainnet',
rpcPrefs: {
blockExplorerUrl: 'https://aurorascan.dev/',
},
rpcUrl:
'https://aurora-mainnet.infura.io/v3/373266a93aab4acda48f89d4fe77c748',
ticker: 'Aurora ETH',
id: 'network-configuration-id-3',
},
'network-configuration-id-4': {
chainId: '0x38',
nickname:
'BNB Smart Chain (previously Binance Smart Chain Mainnet)',
rpcPrefs: {
blockExplorerUrl: 'https://bscscan.com/',
},
rpcUrl: 'https://bsc-dataseed.binance.org/',
ticker: 'BNB',
id: 'network-configuration-id-4',
},
},
},
},
};
expect(newStorage).toStrictEqual(expectedNewStorage);
});
it('should not modify state if state.NetworkController is undefined', async () => {
const oldStorage = {
meta: {
version,
},
data: {
testProperty: 'testValue',
},
};
const newStorage = await migrate(oldStorage);
const expectedNewStorage = {
meta: {
version,
},
data: {
testProperty: 'testValue',
},
};
expect(newStorage).toStrictEqual(expectedNewStorage);
});
it('should not modify state if state.NetworkController is not an object', async () => {
const oldStorage = {
meta: {
version,
},
data: {
NetworkController: false,
testProperty: 'testValue',
},
};
const newStorage = await migrate(oldStorage);
const expectedNewStorage = {
meta: {
version,
},
data: {
NetworkController: false,
testProperty: 'testValue',
},
};
expect(newStorage).toStrictEqual(expectedNewStorage);
});
it('should not modify state if state.NetworkController.networkConfigurations is undefined', async () => {
const oldStorage = {
meta: {
version,
},
data: {
NetworkController: {
testNetworkControllerProperty: 'testNetworkControllerValue',
networkConfigurations: undefined,
},
testProperty: 'testValue',
},
};
const newStorage = await migrate(oldStorage);
const expectedNewStorage = {
meta: {
version,
},
data: {
NetworkController: {
testNetworkControllerProperty: 'testNetworkControllerValue',
networkConfigurations: undefined,
},
testProperty: 'testValue',
},
};
expect(newStorage).toStrictEqual(expectedNewStorage);
});
it('should not modify state if state.NetworkController.networkConfigurations is an empty object', async () => {
const oldStorage = {
meta: {
version,
},
data: {
NetworkController: {
testNetworkControllerProperty: 'testNetworkControllerValue',
networkConfigurations: {},
},
testProperty: 'testValue',
},
};
const newStorage = await migrate(oldStorage);
const expectedNewStorage = {
meta: {
version,
},
data: {
NetworkController: {
testNetworkControllerProperty: 'testNetworkControllerValue',
networkConfigurations: {},
},
testProperty: 'testValue',
},
};
expect(newStorage).toStrictEqual(expectedNewStorage);
});
});

View File

@ -1,10 +1,11 @@
import { cloneDeep } from 'lodash'; import { cloneDeep } from 'lodash';
import { hasProperty, isObject } from '@metamask/utils'; import { isObject } from '@metamask/utils';
export const version = 83; export const version = 83;
/** /**
* The `network` property in state was replaced with `networkId` and `networkStatus`. * Ensure that each networkConfigurations object in state.NetworkController.networkConfigurations has an
* `id` property which matches the key pointing that object
* *
* @param originalVersionedData - Versioned MetaMask extension state, exactly what we persist to dist. * @param originalVersionedData - Versioned MetaMask extension state, exactly what we persist to dist.
* @param originalVersionedData.meta - State metadata. * @param originalVersionedData.meta - State metadata.
@ -23,25 +24,35 @@ export async function migrate(originalVersionedData: {
} }
function transformState(state: Record<string, unknown>) { function transformState(state: Record<string, unknown>) {
if ( if (!isObject(state.NetworkController)) {
!hasProperty(state, 'NetworkController') || return state;
!isObject(state.NetworkController) || }
!hasProperty(state.NetworkController, 'network') const { NetworkController } = state;
) {
if (!isObject(NetworkController.networkConfigurations)) {
return state; return state;
} }
const NetworkController = { ...state.NetworkController }; const { networkConfigurations } = NetworkController;
if (NetworkController.network === 'loading') { const newNetworkConfigurations: Record<string, Record<string, unknown>> = {};
NetworkController.networkId = null;
NetworkController.networkStatus = 'unknown'; for (const networkConfigurationId of Object.keys(networkConfigurations)) {
} else { const networkConfiguration = networkConfigurations[networkConfigurationId];
NetworkController.networkId = NetworkController.network; if (!isObject(networkConfiguration)) {
NetworkController.networkStatus = 'available'; return state;
}
newNetworkConfigurations[networkConfigurationId] = {
...networkConfiguration,
id: networkConfigurationId,
};
} }
delete NetworkController.network; return {
...state,
return { ...state, NetworkController }; NetworkController: {
...NetworkController,
networkConfigurations: newNetworkConfigurations,
},
};
} }

View File

@ -1,254 +1,103 @@
import { v4 } from 'uuid'; import { migrate } from './084';
import { migrate, version } from './084';
jest.mock('uuid', () => {
const actual = jest.requireActual('uuid');
return {
...actual,
v4: jest.fn(),
};
});
describe('migration #84', () => { describe('migration #84', () => {
beforeEach(() => { it('updates the version metadata', async () => {
v4.mockImplementationOnce(() => 'network-configuration-id-1') const originalVersionedData = buildOriginalVersionedData({
.mockImplementationOnce(() => 'network-configuration-id-2')
.mockImplementationOnce(() => 'network-configuration-id-3')
.mockImplementationOnce(() => 'network-configuration-id-4');
});
afterEach(() => {
jest.resetAllMocks();
});
it('should update the version metadata', async () => {
const oldStorage = {
meta: { meta: {
version: 83, version: 9999999,
}, },
data: {}, });
};
const newStorage = await migrate(oldStorage); const newVersionedData = await migrate(originalVersionedData);
expect(newStorage.meta).toStrictEqual({
version, expect(newVersionedData.meta).toStrictEqual({
version: 84,
}); });
}); });
it('should use the key of the networkConfigurations object to set the id of each network configuration', async () => { it('does not change the state if the network controller state does not exist', async () => {
const oldStorage = { const originalVersionedData = buildOriginalVersionedData({
meta: {
version,
},
data: { data: {
NetworkController: { test: '123',
networkConfigurations: {
'network-configuration-id-1': {
chainId: '0x539',
nickname: 'Localhost 8545',
rpcPrefs: {},
rpcUrl: 'http://localhost:8545',
ticker: 'ETH',
},
'network-configuration-id-2': {
chainId: '0xa4b1',
nickname: 'Arbitrum One',
rpcPrefs: {
blockExplorerUrl: 'https://explorer.arbitrum.io',
},
rpcUrl:
'https://arbitrum-mainnet.infura.io/v3/373266a93aab4acda48f89d4fe77c748',
ticker: 'ETH',
},
'network-configuration-id-3': {
chainId: '0x4e454152',
nickname: 'Aurora Mainnet',
rpcPrefs: {
blockExplorerUrl: 'https://aurorascan.dev/',
},
rpcUrl:
'https://aurora-mainnet.infura.io/v3/373266a93aab4acda48f89d4fe77c748',
ticker: 'Aurora ETH',
},
'network-configuration-id-4': {
chainId: '0x38',
nickname:
'BNB Smart Chain (previously Binance Smart Chain Mainnet)',
rpcPrefs: {
blockExplorerUrl: 'https://bscscan.com/',
},
rpcUrl: 'https://bsc-dataseed.binance.org/',
ticker: 'BNB',
},
},
},
}, },
}; });
const newStorage = await migrate(oldStorage); const newVersionedData = await migrate(originalVersionedData);
const expectedNewStorage = { expect(newVersionedData.data).toStrictEqual(originalVersionedData.data);
meta: {
version,
},
data: {
NetworkController: {
networkConfigurations: {
'network-configuration-id-1': {
chainId: '0x539',
nickname: 'Localhost 8545',
rpcPrefs: {},
rpcUrl: 'http://localhost:8545',
ticker: 'ETH',
id: 'network-configuration-id-1',
},
'network-configuration-id-2': {
chainId: '0xa4b1',
nickname: 'Arbitrum One',
rpcPrefs: {
blockExplorerUrl: 'https://explorer.arbitrum.io',
},
rpcUrl:
'https://arbitrum-mainnet.infura.io/v3/373266a93aab4acda48f89d4fe77c748',
ticker: 'ETH',
id: 'network-configuration-id-2',
},
'network-configuration-id-3': {
chainId: '0x4e454152',
nickname: 'Aurora Mainnet',
rpcPrefs: {
blockExplorerUrl: 'https://aurorascan.dev/',
},
rpcUrl:
'https://aurora-mainnet.infura.io/v3/373266a93aab4acda48f89d4fe77c748',
ticker: 'Aurora ETH',
id: 'network-configuration-id-3',
},
'network-configuration-id-4': {
chainId: '0x38',
nickname:
'BNB Smart Chain (previously Binance Smart Chain Mainnet)',
rpcPrefs: {
blockExplorerUrl: 'https://bscscan.com/',
},
rpcUrl: 'https://bsc-dataseed.binance.org/',
ticker: 'BNB',
id: 'network-configuration-id-4',
},
},
},
},
};
expect(newStorage).toStrictEqual(expectedNewStorage);
}); });
it('should not modify state if state.NetworkController is undefined', async () => { const nonObjects = [undefined, null, 'test', 1, ['test']];
const oldStorage = { for (const invalidState of nonObjects) {
meta: { it(`does not change the state if the network controller state is ${invalidState}`, async () => {
version, const originalVersionedData = buildOriginalVersionedData({
}, data: {
data: { NetworkController: invalidState,
testProperty: 'testValue', },
}, });
};
const newStorage = await migrate(oldStorage); const newVersionedData = await migrate(originalVersionedData);
const expectedNewStorage = { expect(newVersionedData.data).toStrictEqual(originalVersionedData.data);
meta: { });
version, }
},
it('does not change the state if the network controller state does not include "network"', async () => {
const originalVersionedData = buildOriginalVersionedData({
data: { data: {
testProperty: 'testValue', NetworkController: {
test: '123',
},
}, },
}; });
expect(newStorage).toStrictEqual(expectedNewStorage);
const newVersionedData = await migrate(originalVersionedData);
expect(newVersionedData.data).toStrictEqual(originalVersionedData.data);
}); });
it('should not modify state if state.NetworkController is not an object', async () => { it('replaces "network" in the network controller state with "networkId": null, "networkStatus": "unknown" if it is "loading"', async () => {
const oldStorage = { const originalVersionedData = buildOriginalVersionedData({
meta: {
version,
},
data: { data: {
NetworkController: false, NetworkController: {
testProperty: 'testValue', network: 'loading',
},
}, },
}; });
const newStorage = await migrate(oldStorage); const newVersionedData = await migrate(originalVersionedData);
const expectedNewStorage = { expect(newVersionedData.data).toStrictEqual({
meta: { NetworkController: {
version, networkId: null,
networkStatus: 'unknown',
}, },
data: { });
NetworkController: false,
testProperty: 'testValue',
},
};
expect(newStorage).toStrictEqual(expectedNewStorage);
}); });
it('should not modify state if state.NetworkController.networkConfigurations is undefined', async () => { it('replaces "network" in the network controller state with "networkId": network, "networkStatus": "available" if it is not "loading"', async () => {
const oldStorage = { const originalVersionedData = buildOriginalVersionedData({
meta: {
version,
},
data: { data: {
NetworkController: { NetworkController: {
testNetworkControllerProperty: 'testNetworkControllerValue', network: '12345',
networkConfigurations: undefined,
}, },
testProperty: 'testValue',
}, },
}; });
const newStorage = await migrate(oldStorage); const newVersionedData = await migrate(originalVersionedData);
const expectedNewStorage = { expect(newVersionedData.data).toStrictEqual({
meta: { NetworkController: {
version, networkId: '12345',
networkStatus: 'available',
}, },
data: { });
NetworkController: {
testNetworkControllerProperty: 'testNetworkControllerValue',
networkConfigurations: undefined,
},
testProperty: 'testValue',
},
};
expect(newStorage).toStrictEqual(expectedNewStorage);
});
it('should not modify state if state.NetworkController.networkConfigurations is an empty object', async () => {
const oldStorage = {
meta: {
version,
},
data: {
NetworkController: {
testNetworkControllerProperty: 'testNetworkControllerValue',
networkConfigurations: {},
},
testProperty: 'testValue',
},
};
const newStorage = await migrate(oldStorage);
const expectedNewStorage = {
meta: {
version,
},
data: {
NetworkController: {
testNetworkControllerProperty: 'testNetworkControllerValue',
networkConfigurations: {},
},
testProperty: 'testValue',
},
};
expect(newStorage).toStrictEqual(expectedNewStorage);
}); });
}); });
function buildOriginalVersionedData({ meta = {}, data = {} } = {}) {
return {
meta: { version: 999999, ...meta },
data: { ...data },
};
}

View File

@ -1,11 +1,10 @@
import { cloneDeep } from 'lodash'; import { cloneDeep } from 'lodash';
import { isObject } from '@metamask/utils'; import { hasProperty, isObject } from '@metamask/utils';
export const version = 84; export const version = 84;
/** /**
* Ensure that each networkConfigurations object in state.NetworkController.networkConfigurations has an * The `network` property in state was replaced with `networkId` and `networkStatus`.
* `id` property which matches the key pointing that object
* *
* @param originalVersionedData - Versioned MetaMask extension state, exactly what we persist to dist. * @param originalVersionedData - Versioned MetaMask extension state, exactly what we persist to dist.
* @param originalVersionedData.meta - State metadata. * @param originalVersionedData.meta - State metadata.
@ -24,35 +23,25 @@ export async function migrate(originalVersionedData: {
} }
function transformState(state: Record<string, unknown>) { function transformState(state: Record<string, unknown>) {
if (!isObject(state.NetworkController)) { if (
return state; !hasProperty(state, 'NetworkController') ||
} !isObject(state.NetworkController) ||
const { NetworkController } = state; !hasProperty(state.NetworkController, 'network')
) {
if (!isObject(NetworkController.networkConfigurations)) {
return state; return state;
} }
const { networkConfigurations } = NetworkController; const NetworkController = { ...state.NetworkController };
const newNetworkConfigurations: Record<string, Record<string, unknown>> = {}; if (NetworkController.network === 'loading') {
NetworkController.networkId = null;
for (const networkConfigurationId of Object.keys(networkConfigurations)) { NetworkController.networkStatus = 'unknown';
const networkConfiguration = networkConfigurations[networkConfigurationId]; } else {
if (!isObject(networkConfiguration)) { NetworkController.networkId = NetworkController.network;
return state; NetworkController.networkStatus = 'available';
}
newNetworkConfigurations[networkConfigurationId] = {
...networkConfiguration,
id: networkConfigurationId,
};
} }
return { delete NetworkController.network;
...state,
NetworkController: { return { ...state, NetworkController };
...NetworkController,
networkConfigurations: newNetworkConfigurations,
},
};
} }