Skip to content
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
15 changes: 14 additions & 1 deletion packages/gas-fee-controller/src/GasFeeController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,16 @@ type AllEvents = AllGasFeeControllerEvents | AllNetworkControllerEvents;
type RootMessenger = Messenger<MockAnyNamespace, AllActions, AllEvents>;

const getRootMessenger = (): RootMessenger => {
return new Messenger({ namespace: MOCK_ANY_NAMESPACE });
const rootMessenger = new Messenger<
Copy link
Contributor Author

@mcmire mcmire Nov 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the tests for GasFeeController instantiate a real NetworkController, we also need to make sure that we register ErrorReportingService:captureException, as NetworkController may call it during initialization if the initial state is incoherent. These changes should have been here all along, but they have not been needed so far. The reason they are needed now is that in some tests, the initial NetworkController state sets networksMetadata for networks that aren't present in networkConfigurationsByChainId. We could make the initial state more accurate, but it doesn't really matter here, we just need to register ErrorReportingService:captureException with a dummy handler.

MockAnyNamespace,
MessengerActions<NetworkControllerMessenger>,
MessengerEvents<NetworkControllerMessenger>
>({ namespace: MOCK_ANY_NAMESPACE });
rootMessenger.registerActionHandler(
'ErrorReportingService:captureException',
jest.fn(),
);
return rootMessenger;
};

const setupNetworkController = async ({
Expand All @@ -86,6 +95,10 @@ const setupNetworkController = async ({
namespace: 'NetworkController',
parent: rootMessenger,
});
rootMessenger.delegate({
messenger: networkControllerMessenger,
actions: ['ErrorReportingService:captureException'],
});

const infuraProjectId = '123';

Expand Down
6 changes: 6 additions & 0 deletions packages/network-controller/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Providers accessible either via network clients or global proxies no longer emit events (or inherit from EventEmitter, for that matter).
- Bump `@metamask/controller-utils` from `^11.14.1` to `^11.15.0` ([#7003](https://github.com/MetaMask/core/pull/7003))

### Fixed

- Ensure `networksMetadata` never references old network client IDs ([#7047](https://github.com/MetaMask/core/pull/7047))
- When removing a network configuration, ensure that metadata for all RPC endpoints in the network configuration are also removed from `networksMetadata`
- When initializing the controller, remove metadata for RPC endpoints in `networksMetadata` that are not present in a network configuration

## [25.0.0]

### Changed
Expand Down
25 changes: 23 additions & 2 deletions packages/network-controller/src/NetworkController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1082,12 +1082,17 @@ function correctInitialState(
const networkConfigurationsSortedByChainId = getNetworkConfigurations(
state,
).sort((a, b) => a.chainId.localeCompare(b.chainId));
const networkClientIds = getAvailableNetworkClientIds(
const availableNetworkClientIds = getAvailableNetworkClientIds(
networkConfigurationsSortedByChainId,
);
const invalidNetworkClientIdsWithMetadata = Object.keys(
state.networksMetadata,
).filter(
(networkClientId) => !availableNetworkClientIds.includes(networkClientId),
);

return produce(state, (newState) => {
if (!networkClientIds.includes(state.selectedNetworkClientId)) {
if (!availableNetworkClientIds.includes(state.selectedNetworkClientId)) {
const firstNetworkConfiguration = networkConfigurationsSortedByChainId[0];
const newSelectedNetworkClientId =
firstNetworkConfiguration.rpcEndpoints[
Expand All @@ -1101,6 +1106,18 @@ function correctInitialState(
);
newState.selectedNetworkClientId = newSelectedNetworkClientId;
}

if (invalidNetworkClientIdsWithMetadata.length > 0) {
for (const invalidNetworkClientId of invalidNetworkClientIdsWithMetadata) {
delete newState.networksMetadata[invalidNetworkClientId];
}
messenger.call(
'ErrorReportingService:captureException',
new Error(
'`networksMetadata` had invalid network client IDs, which have been removed',
),
);
}
});
}

Expand Down Expand Up @@ -2401,6 +2418,10 @@ export class NetworkController extends BaseController<
mode: 'remove',
existingNetworkConfiguration,
});

for (const rpcEndpoint of existingNetworkConfiguration.rpcEndpoints) {
delete state.networksMetadata[rpcEndpoint.networkClientId];
}
});

this.messenger.publish(
Expand Down
Loading
Loading