Skip to content

Commit d1104b6

Browse files
refactor: remove dangerously set code
1 parent d346ba8 commit d1104b6

File tree

4 files changed

+1
-369
lines changed

4 files changed

+1
-369
lines changed

packages/network-controller/src/NetworkController.ts

+1-137
Original file line numberDiff line numberDiff line change
@@ -506,11 +506,6 @@ export type NetworkControllerUpdateNetworkAction = {
506506
handler: NetworkController['updateNetwork'];
507507
};
508508

509-
export type NetworkControllerDangerouslySetNetworkConfigurationAction = {
510-
type: 'NetworkController:dangerouslySetNetworkConfiguration';
511-
handler: (networkConfiguration: NetworkConfiguration) => Promise<void>;
512-
};
513-
514509
export type NetworkControllerActions =
515510
| NetworkControllerGetStateAction
516511
| NetworkControllerGetEthQueryAction
@@ -524,8 +519,7 @@ export type NetworkControllerActions =
524519
| NetworkControllerGetNetworkConfigurationByNetworkClientId
525520
| NetworkControllerAddNetworkAction
526521
| NetworkControllerRemoveNetworkAction
527-
| NetworkControllerUpdateNetworkAction
528-
| NetworkControllerDangerouslySetNetworkConfigurationAction;
522+
| NetworkControllerUpdateNetworkAction;
529523

530524
export type NetworkControllerMessenger = RestrictedControllerMessenger<
531525
typeof controllerName,
@@ -1017,13 +1011,6 @@ export class NetworkController extends BaseController<
10171011
`${this.name}:updateNetwork`,
10181012
this.updateNetwork.bind(this),
10191013
);
1020-
1021-
this.messagingSystem.registerActionHandler(
1022-
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
1023-
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
1024-
`${this.name}:dangerouslySetNetworkConfiguration`,
1025-
this.#dangerouslySetNetworkConfiguration.bind(this),
1026-
);
10271014
}
10281015

10291016
/**
@@ -2017,129 +2004,6 @@ export class NetworkController extends BaseController<
20172004
);
20182005
}
20192006

2020-
/**
2021-
* This is used to override an existing network configuration.
2022-
* This is only meant for internal use only and not to be exposed via the UI.
2023-
* It is used as part of "Network Syncing", to sync networks, RPCs and block explorers cross devices.
2024-
*
2025-
* This will subsequently update the network client registry; state.networksMetadata, and state.selectedNetworkClientId
2026-
* @param networkConfiguration - the network configuration to override
2027-
*/
2028-
async #dangerouslySetNetworkConfiguration(
2029-
networkConfiguration: NetworkConfiguration,
2030-
) {
2031-
const prevNetworkConfig: NetworkConfiguration | undefined =
2032-
networkConfiguration.chainId in this.state.networkConfigurationsByChainId
2033-
? this.state.networkConfigurationsByChainId[
2034-
networkConfiguration.chainId
2035-
]
2036-
: undefined;
2037-
2038-
if (!prevNetworkConfig) {
2039-
// We only want to perform overrides, not add new network configurations
2040-
return;
2041-
}
2042-
2043-
// Update Registry (remove old and add new)
2044-
const updateRegistry = () => {
2045-
// Unregister old networks we want to override
2046-
const autoManagedNetworkClientRegistry =
2047-
this.#ensureAutoManagedNetworkClientRegistryPopulated();
2048-
const networkClientRemoveOperations = prevNetworkConfig.rpcEndpoints.map(
2049-
(rpcEndpoint) => {
2050-
return {
2051-
type: 'remove' as const,
2052-
rpcEndpoint,
2053-
};
2054-
},
2055-
);
2056-
this.#unregisterNetworkClientsAsNeeded({
2057-
networkClientOperations: networkClientRemoveOperations,
2058-
autoManagedNetworkClientRegistry,
2059-
});
2060-
2061-
// Register new networks we want to override
2062-
const networkClientAddOperations = networkConfiguration.rpcEndpoints.map(
2063-
(rpcEndpoint) => {
2064-
return {
2065-
type: 'add' as const,
2066-
rpcEndpoint,
2067-
};
2068-
},
2069-
);
2070-
this.#registerNetworkClientsAsNeeded({
2071-
networkFields: networkConfiguration,
2072-
networkClientOperations: networkClientAddOperations,
2073-
autoManagedNetworkClientRegistry,
2074-
});
2075-
};
2076-
2077-
// Replace the networkConfiguration with our new networkConfiguration
2078-
// This is a full replace (no merging)
2079-
const replaceNetworkConfiguration = () => {
2080-
// Update State
2081-
this.update((state) => {
2082-
state.networkConfigurationsByChainId[networkConfiguration.chainId] =
2083-
networkConfiguration;
2084-
});
2085-
2086-
// Update Cache
2087-
this.#networkConfigurationsByNetworkClientId =
2088-
buildNetworkConfigurationsByNetworkClientId(
2089-
this.state.networkConfigurationsByChainId,
2090-
);
2091-
};
2092-
2093-
// Updates the NetworksMetadata State
2094-
const updateNetworksMetadata = async () => {
2095-
// Remove old metadata state
2096-
this.update((state) => {
2097-
prevNetworkConfig.rpcEndpoints.forEach((r) => {
2098-
if (state.networksMetadata?.[r.networkClientId]) {
2099-
delete state.networksMetadata[r.networkClientId];
2100-
}
2101-
});
2102-
});
2103-
2104-
// Add new metadata state
2105-
for (const r of networkConfiguration.rpcEndpoints) {
2106-
await this.lookupNetwork(r.networkClientId);
2107-
}
2108-
};
2109-
2110-
// Update selectedNetworkId State
2111-
// Will try to keep the same OR will select a new RPC from new network OR any network (edge case)
2112-
const updateSelectedNetworkId = async () => {
2113-
const selectedClientId = this.state.selectedNetworkClientId;
2114-
const wasClientIdReplaced = prevNetworkConfig.rpcEndpoints.some(
2115-
(r) => r.networkClientId === selectedClientId,
2116-
);
2117-
const doesExistInNewNetwork = networkConfiguration.rpcEndpoints.some(
2118-
(r) => r.networkClientId === selectedClientId,
2119-
);
2120-
2121-
const shouldUpdateSelectedNetworkId =
2122-
wasClientIdReplaced && !doesExistInNewNetwork;
2123-
if (shouldUpdateSelectedNetworkId) {
2124-
// Update the clientId to "something" that exists
2125-
const newRPCClientId = networkConfiguration.rpcEndpoints.find(
2126-
(r) => r.networkClientId in this.state.networksMetadata,
2127-
)?.networkClientId;
2128-
const anyRPCClientId = Object.keys(this.state.networksMetadata)[0];
2129-
/* istanbul ignore next: anyRPCClientId and selectedClientId are fallbacks and should be impossible to reach */
2130-
const newlySelectedNetwork =
2131-
newRPCClientId ?? anyRPCClientId ?? selectedClientId;
2132-
await this.#refreshNetwork(newlySelectedNetwork);
2133-
}
2134-
};
2135-
2136-
// Execute Set Network Config
2137-
updateRegistry();
2138-
replaceNetworkConfiguration();
2139-
await updateNetworksMetadata();
2140-
await updateSelectedNetworkId();
2141-
}
2142-
21432007
/**
21442008
* Assuming that the network has been previously switched, switches to this
21452009
* new network.

packages/network-controller/src/index.ts

-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ export type {
3131
NetworkControllerAddNetworkAction,
3232
NetworkControllerRemoveNetworkAction,
3333
NetworkControllerUpdateNetworkAction,
34-
NetworkControllerDangerouslySetNetworkConfigurationAction,
3534
NetworkControllerGetNetworkConfigurationByNetworkClientId,
3635
NetworkControllerActions,
3736
NetworkControllerMessenger,

packages/network-controller/tests/NetworkController.test.ts

-200
Original file line numberDiff line numberDiff line change
@@ -11551,206 +11551,6 @@ describe('NetworkController', () => {
1155111551
});
1155211552
});
1155311553

11554-
describe('dangerouslySetNetworkConfiguration', () => {
11555-
const TEST_CHAIN_ID = '0x1337';
11556-
const ORIGINAL_NETWORK_CLIENT_ID = '1111';
11557-
11558-
const arrangeTestUtils = (props: { newNetworkClientIds: string[] }) => {
11559-
mockCreateNetworkClient().mockReturnValue(buildFakeClient());
11560-
11561-
const originalNetwork = buildCustomNetworkConfiguration({
11562-
chainId: TEST_CHAIN_ID,
11563-
rpcEndpoints: [
11564-
buildCustomRpcEndpoint({
11565-
networkClientId: ORIGINAL_NETWORK_CLIENT_ID,
11566-
}),
11567-
],
11568-
});
11569-
11570-
const overrideNetwork = buildCustomNetworkConfiguration({
11571-
chainId: TEST_CHAIN_ID,
11572-
rpcEndpoints: props.newNetworkClientIds.map((id) =>
11573-
buildCustomRpcEndpoint({ networkClientId: id }),
11574-
),
11575-
});
11576-
11577-
const controllerState =
11578-
buildNetworkControllerStateWithDefaultSelectedNetworkClientId({
11579-
networkConfigurationsByChainId: {
11580-
[originalNetwork.chainId]: originalNetwork,
11581-
},
11582-
networksMetadata: {
11583-
[ORIGINAL_NETWORK_CLIENT_ID]: {
11584-
EIPS: {
11585-
'1559': true,
11586-
},
11587-
status: NetworkStatus.Available,
11588-
},
11589-
},
11590-
});
11591-
11592-
return { originalNetwork, overrideNetwork, controllerState };
11593-
};
11594-
11595-
const actTest = async (
11596-
props: Pick<
11597-
ReturnType<typeof arrangeTestUtils>,
11598-
'controllerState' | 'overrideNetwork'
11599-
>,
11600-
) => {
11601-
const result = await withController(
11602-
{ state: props.controllerState },
11603-
async ({ controller, messenger }) => {
11604-
await messenger.call(
11605-
'NetworkController:dangerouslySetNetworkConfiguration',
11606-
props.overrideNetwork,
11607-
);
11608-
return {
11609-
state: controller.state,
11610-
controller,
11611-
};
11612-
},
11613-
);
11614-
11615-
return result;
11616-
};
11617-
11618-
const arrangeActTest = async (props: { newNetworkClientIds: string[] }) => {
11619-
const arrange = arrangeTestUtils(props);
11620-
11621-
// Act
11622-
const result = await actTest({
11623-
controllerState: arrange.controllerState,
11624-
overrideNetwork: arrange.overrideNetwork,
11625-
});
11626-
return { ...arrange, result };
11627-
};
11628-
11629-
const assertStateHasBeenUpdated = (props: {
11630-
state: NetworkState;
11631-
newNetworkConfiguration: NetworkConfiguration;
11632-
networkClientIdsRemoved: string[];
11633-
}) => {
11634-
const { state, newNetworkConfiguration, networkClientIdsRemoved } = props;
11635-
11636-
// Assert - new network config has been set
11637-
expect(state.networkConfigurationsByChainId[TEST_CHAIN_ID]).toStrictEqual(
11638-
newNetworkConfiguration,
11639-
);
11640-
11641-
// Assert - networks metadata removed some endpoints (from original network)
11642-
networkClientIdsRemoved.forEach((id) => {
11643-
expect(state.networksMetadata[id]).toBeUndefined();
11644-
});
11645-
11646-
// Assert - networks metadata has been set
11647-
newNetworkConfiguration.rpcEndpoints.forEach((r) => {
11648-
expect(state.networksMetadata[r.networkClientId]).toBeDefined();
11649-
});
11650-
};
11651-
11652-
const assertNetworkRegistryHasBeenUpdated = (props: {
11653-
controller: NetworkController;
11654-
newNetworkConfiguration: NetworkConfiguration;
11655-
networkClientIdsRemoved: string[];
11656-
}) => {
11657-
const { controller, newNetworkConfiguration, networkClientIdsRemoved } =
11658-
props;
11659-
const registry = controller.getNetworkClientRegistry();
11660-
11661-
// Assert - networks that were removed (from original network that was overwritten)
11662-
networkClientIdsRemoved.forEach((id) => {
11663-
expect(registry[id]).toBeUndefined();
11664-
});
11665-
11666-
// Assert - new network config RPCs has been updated in the network registry
11667-
newNetworkConfiguration.rpcEndpoints.forEach((r) => {
11668-
expect(registry[r.networkClientId]).toBeDefined();
11669-
});
11670-
};
11671-
11672-
const assertNetworkConfigurationsByIdCacheHasBeenUpdated = (props: {
11673-
controller: NetworkController;
11674-
newNetworkConfiguration: NetworkConfiguration;
11675-
}) => {
11676-
const { controller, newNetworkConfiguration } = props;
11677-
expect(
11678-
controller.getNetworkConfigurationByChainId(TEST_CHAIN_ID),
11679-
).toStrictEqual(newNetworkConfiguration);
11680-
};
11681-
11682-
it('overrides a set network configuration', async () => {
11683-
const { result, overrideNetwork } = await arrangeActTest({
11684-
newNetworkClientIds: ['2222', '3333'],
11685-
});
11686-
11687-
assertStateHasBeenUpdated({
11688-
state: result.state,
11689-
newNetworkConfiguration: overrideNetwork,
11690-
networkClientIdsRemoved: [ORIGINAL_NETWORK_CLIENT_ID],
11691-
});
11692-
assertNetworkRegistryHasBeenUpdated({
11693-
controller: result.controller,
11694-
newNetworkConfiguration: overrideNetwork,
11695-
networkClientIdsRemoved: [ORIGINAL_NETWORK_CLIENT_ID],
11696-
});
11697-
assertNetworkConfigurationsByIdCacheHasBeenUpdated({
11698-
controller: result.controller,
11699-
newNetworkConfiguration: overrideNetwork,
11700-
});
11701-
11702-
// Selected network has changed (since original was removed)
11703-
// We will select next available RPC for the given chain
11704-
expect(result.state.selectedNetworkClientId).toBe('2222');
11705-
});
11706-
11707-
it('overrides network config, but keeps same selected network', async () => {
11708-
const { result, overrideNetwork } = await arrangeActTest({
11709-
newNetworkClientIds: [ORIGINAL_NETWORK_CLIENT_ID, '2222'],
11710-
});
11711-
11712-
assertStateHasBeenUpdated({
11713-
state: result.state,
11714-
newNetworkConfiguration: overrideNetwork,
11715-
// no networks were removed, as the new network config contains the original network client id
11716-
networkClientIdsRemoved: [],
11717-
});
11718-
assertNetworkRegistryHasBeenUpdated({
11719-
controller: result.controller,
11720-
newNetworkConfiguration: overrideNetwork,
11721-
// no networks were removed, as the new network config contains the original network client id
11722-
networkClientIdsRemoved: [],
11723-
});
11724-
assertNetworkConfigurationsByIdCacheHasBeenUpdated({
11725-
controller: result.controller,
11726-
newNetworkConfiguration: overrideNetwork,
11727-
});
11728-
11729-
// selected RPC has not changed, as it was not removed
11730-
expect(result.state.selectedNetworkClientId).toBe(
11731-
ORIGINAL_NETWORK_CLIENT_ID,
11732-
);
11733-
});
11734-
11735-
it('does nothing if there is no network to override', async () => {
11736-
const { controllerState, overrideNetwork } = arrangeTestUtils({
11737-
newNetworkClientIds: ['2222'],
11738-
});
11739-
11740-
// shim/mock the controller state to not contain a network we are overriding
11741-
controllerState.networkConfigurationsByChainId['0xDiffChain'] =
11742-
controllerState.networkConfigurationsByChainId[TEST_CHAIN_ID];
11743-
delete controllerState.networkConfigurationsByChainId[TEST_CHAIN_ID];
11744-
controllerState.networkConfigurationsByChainId['0xDiffChain'].chainId =
11745-
'0xDiffChain';
11746-
11747-
const result = await actTest({ controllerState, overrideNetwork });
11748-
11749-
// No changes should have occurred
11750-
expect(result.state).toStrictEqual(controllerState);
11751-
});
11752-
});
11753-
1175411554
describe('rollbackToPreviousProvider', () => {
1175511555
describe('when called not following any network switches', () => {
1175611556
for (const infuraNetworkType of Object.values(InfuraNetworkType)) {

0 commit comments

Comments
 (0)