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

Make rollbackToPreviousProvider async (#18599)

The network controller method `rollbackToPreviousProvider` is now
async. It will resolve when the network switch has completed.

Relates to https://github.com/MetaMask/metamask-extension/issues/18587
This commit is contained in:
Mark Stacey 2023-04-15 10:31:59 -02:30 committed by GitHub
parent 1406885115
commit 73efd2edeb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 33 deletions

View File

@ -5761,8 +5761,8 @@ describe('NetworkController', () => {
await waitForLookupNetworkToComplete({ await waitForLookupNetworkToComplete({
controller, controller,
operation: () => { operation: async () => {
controller.rollbackToPreviousProvider(); await controller.rollbackToPreviousProvider();
}, },
}); });
expect(controller.store.getState().provider).toStrictEqual({ expect(controller.store.getState().provider).toStrictEqual({
@ -5827,8 +5827,8 @@ describe('NetworkController', () => {
const networkWillChange = await waitForPublishedEvents({ const networkWillChange = await waitForPublishedEvents({
messenger: unrestrictedMessenger, messenger: unrestrictedMessenger,
eventType: NetworkControllerEventType.NetworkWillChange, eventType: NetworkControllerEventType.NetworkWillChange,
operation: () => { operation: async () => {
controller.rollbackToPreviousProvider(); await controller.rollbackToPreviousProvider();
}, },
}); });
@ -5891,6 +5891,8 @@ describe('NetworkController', () => {
// happens before networkDidChange // happens before networkDidChange
count: 1, count: 1,
operation: () => { operation: () => {
// Intentionally not awaited because we want to check state
// while this operation is in-progress
controller.rollbackToPreviousProvider(); controller.rollbackToPreviousProvider();
}, },
}); });
@ -5959,6 +5961,8 @@ describe('NetworkController', () => {
// happens before networkDidChange // happens before networkDidChange
count: 1, count: 1,
operation: () => { operation: () => {
// Intentionally not awaited because we want to check state
// while this operation is in-progress
controller.rollbackToPreviousProvider(); controller.rollbackToPreviousProvider();
}, },
}); });
@ -6015,8 +6019,8 @@ describe('NetworkController', () => {
await waitForLookupNetworkToComplete({ await waitForLookupNetworkToComplete({
controller, controller,
operation: () => { operation: async () => {
controller.rollbackToPreviousProvider(); await controller.rollbackToPreviousProvider();
}, },
}); });
@ -6077,8 +6081,8 @@ describe('NetworkController', () => {
controller.getProviderAndBlockTracker(); controller.getProviderAndBlockTracker();
await waitForLookupNetworkToComplete({ await waitForLookupNetworkToComplete({
controller, controller,
operation: () => { operation: async () => {
controller.rollbackToPreviousProvider(); await controller.rollbackToPreviousProvider();
}, },
}); });
const { provider: providerAfter } = const { provider: providerAfter } =
@ -6138,8 +6142,8 @@ describe('NetworkController', () => {
const networkDidChange = await waitForPublishedEvents({ const networkDidChange = await waitForPublishedEvents({
messenger: unrestrictedMessenger, messenger: unrestrictedMessenger,
eventType: NetworkControllerEventType.NetworkDidChange, eventType: NetworkControllerEventType.NetworkDidChange,
operation: () => { operation: async () => {
controller.rollbackToPreviousProvider(); await controller.rollbackToPreviousProvider();
}, },
}); });
expect(networkDidChange).toBeTruthy(); expect(networkDidChange).toBeTruthy();
@ -6203,7 +6207,7 @@ describe('NetworkController', () => {
await waitForLookupNetworkToComplete({ await waitForLookupNetworkToComplete({
controller, controller,
operation: async () => { operation: async () => {
controller.rollbackToPreviousProvider(); await controller.rollbackToPreviousProvider();
}, },
}); });
@ -6266,8 +6270,8 @@ describe('NetworkController', () => {
await waitForLookupNetworkToComplete({ await waitForLookupNetworkToComplete({
controller, controller,
operation: () => { operation: async () => {
controller.rollbackToPreviousProvider(); await controller.rollbackToPreviousProvider();
}, },
}); });
expect(controller.store.getState().networkStatus).toBe( expect(controller.store.getState().networkStatus).toBe(
@ -6323,8 +6327,8 @@ describe('NetworkController', () => {
await waitForLookupNetworkToComplete({ await waitForLookupNetworkToComplete({
controller, controller,
numberOfNetworkDetailsChanges: 2, numberOfNetworkDetailsChanges: 2,
operation: () => { operation: async () => {
controller.rollbackToPreviousProvider(); await controller.rollbackToPreviousProvider();
}, },
}); });
expect(controller.store.getState().networkDetails).toStrictEqual({ expect(controller.store.getState().networkDetails).toStrictEqual({
@ -6408,8 +6412,8 @@ describe('NetworkController', () => {
await waitForLookupNetworkToComplete({ await waitForLookupNetworkToComplete({
controller, controller,
operation: () => { operation: async () => {
controller.rollbackToPreviousProvider(); await controller.rollbackToPreviousProvider();
}, },
}); });
expect(controller.store.getState().provider).toStrictEqual({ expect(controller.store.getState().provider).toStrictEqual({
@ -6475,8 +6479,8 @@ describe('NetworkController', () => {
const networkWillChange = await waitForPublishedEvents({ const networkWillChange = await waitForPublishedEvents({
messenger: unrestrictedMessenger, messenger: unrestrictedMessenger,
eventType: NetworkControllerEventType.NetworkWillChange, eventType: NetworkControllerEventType.NetworkWillChange,
operation: () => { operation: async () => {
controller.rollbackToPreviousProvider(); await controller.rollbackToPreviousProvider();
}, },
}); });
@ -6532,6 +6536,8 @@ describe('NetworkController', () => {
// happens before networkDidChange // happens before networkDidChange
count: 1, count: 1,
operation: () => { operation: () => {
// Intentionally not awaited because we want to check state
// while this operation is in-progress
controller.rollbackToPreviousProvider(); controller.rollbackToPreviousProvider();
}, },
}); });
@ -6595,6 +6601,8 @@ describe('NetworkController', () => {
// happens before networkDidChange // happens before networkDidChange
count: 1, count: 1,
operation: () => { operation: () => {
// Intentionally not awaited because we want to check state
// while this operation is in-progress
controller.rollbackToPreviousProvider(); controller.rollbackToPreviousProvider();
}, },
}); });
@ -6646,8 +6654,8 @@ describe('NetworkController', () => {
await waitForLookupNetworkToComplete({ await waitForLookupNetworkToComplete({
controller, controller,
operation: () => { operation: async () => {
controller.rollbackToPreviousProvider(); await controller.rollbackToPreviousProvider();
}, },
}); });
@ -6703,8 +6711,8 @@ describe('NetworkController', () => {
controller.getProviderAndBlockTracker(); controller.getProviderAndBlockTracker();
await waitForLookupNetworkToComplete({ await waitForLookupNetworkToComplete({
controller, controller,
operation: () => { operation: async () => {
controller.rollbackToPreviousProvider(); await controller.rollbackToPreviousProvider();
}, },
}); });
const { provider: providerAfter } = const { provider: providerAfter } =
@ -6759,8 +6767,8 @@ describe('NetworkController', () => {
const networkDidChange = await waitForPublishedEvents({ const networkDidChange = await waitForPublishedEvents({
messenger: unrestrictedMessenger, messenger: unrestrictedMessenger,
eventType: NetworkControllerEventType.NetworkDidChange, eventType: NetworkControllerEventType.NetworkDidChange,
operation: () => { operation: async () => {
controller.rollbackToPreviousProvider(); await controller.rollbackToPreviousProvider();
}, },
}); });
expect(networkDidChange).toBeTruthy(); expect(networkDidChange).toBeTruthy();
@ -6814,8 +6822,8 @@ describe('NetworkController', () => {
const infuraIsUnblocked = await waitForPublishedEvents({ const infuraIsUnblocked = await waitForPublishedEvents({
messenger: unrestrictedMessenger, messenger: unrestrictedMessenger,
eventType: NetworkControllerEventType.InfuraIsUnblocked, eventType: NetworkControllerEventType.InfuraIsUnblocked,
operation: () => { operation: async () => {
controller.rollbackToPreviousProvider(); await controller.rollbackToPreviousProvider();
}, },
}); });
@ -6871,8 +6879,8 @@ describe('NetworkController', () => {
await waitForLookupNetworkToComplete({ await waitForLookupNetworkToComplete({
controller, controller,
operation: () => { operation: async () => {
controller.rollbackToPreviousProvider(); await controller.rollbackToPreviousProvider();
}, },
}); });
expect(controller.store.getState().networkStatus).toBe('unknown'); expect(controller.store.getState().networkStatus).toBe('unknown');
@ -6925,8 +6933,8 @@ describe('NetworkController', () => {
await waitForLookupNetworkToComplete({ await waitForLookupNetworkToComplete({
controller, controller,
operation: () => { operation: async () => {
controller.rollbackToPreviousProvider(); await controller.rollbackToPreviousProvider();
}, },
}); });
expect(controller.store.getState().networkDetails).toStrictEqual({ expect(controller.store.getState().networkDetails).toStrictEqual({

View File

@ -787,10 +787,10 @@ export class NetworkController extends EventEmitter {
* different than the initial network (if it is, then this is equivalent to * different than the initial network (if it is, then this is equivalent to
* calling `resetConnection`). * calling `resetConnection`).
*/ */
rollbackToPreviousProvider(): void { async rollbackToPreviousProvider() {
const config = this.#previousProviderConfig; const config = this.#previousProviderConfig;
this.providerStore.putState(config); this.providerStore.putState(config);
this._switchNetwork(config); await this._switchNetwork(config);
} }
/** /**