From e5835ac45900d00bb8b619dad79695c9559e0a35 Mon Sep 17 00:00:00 2001 From: Mark Stacey Date: Mon, 17 Apr 2023 15:45:22 -0230 Subject: [PATCH] Make `resetConnection` async (#18601) The network controller method `resetConnection` is now async. This has no functional impact. Relates to https://github.com/MetaMask/metamask-extension/issues/18587 --- .../network/network-controller.test.ts | 71 +++++++------------ .../controllers/network/network-controller.ts | 4 +- 2 files changed, 29 insertions(+), 46 deletions(-) diff --git a/app/scripts/controllers/network/network-controller.test.ts b/app/scripts/controllers/network/network-controller.test.ts index c5d5975e1..503f23a72 100644 --- a/app/scripts/controllers/network/network-controller.test.ts +++ b/app/scripts/controllers/network/network-controller.test.ts @@ -4925,6 +4925,8 @@ describe('NetworkController', () => { messenger: unrestrictedMessenger, eventType: NetworkControllerEventType.NetworkWillChange, operation: () => { + // Intentionally not awaited because we want to capture an + // event emitted partway throught this operation controller.resetConnection(); }, }); @@ -4965,6 +4967,8 @@ describe('NetworkController', () => { // happens before networkDidChange count: 1, operation: () => { + // Intentionally not awaited because we want to capture a + // state change made partway through the operation controller.resetConnection(); }, }); @@ -5007,6 +5011,8 @@ describe('NetworkController', () => { // happens before networkDidChange count: 1, operation: () => { + // Intentionally not awaited because we want to check state + // partway through the operation controller.resetConnection(); }, }); @@ -5034,7 +5040,7 @@ describe('NetworkController', () => { async ({ controller, network }) => { network.mockEssentialRpcCalls(); - controller.resetConnection(); + await controller.resetConnection(); const { provider } = controller.getProviderAndBlockTracker(); assert(provider, 'Provider is somehow unset'); @@ -5073,7 +5079,7 @@ describe('NetworkController', () => { const { provider: providerBefore } = controller.getProviderAndBlockTracker(); - controller.resetConnection(); + await controller.resetConnection(); const { provider: providerAfter } = controller.getProviderAndBlockTracker(); @@ -5104,8 +5110,8 @@ describe('NetworkController', () => { const networkDidChange = await waitForPublishedEvents({ messenger: unrestrictedMessenger, eventType: NetworkControllerEventType.NetworkDidChange, - operation: () => { - controller.resetConnection(); + operation: async () => { + await controller.resetConnection(); }, }); @@ -5147,7 +5153,7 @@ describe('NetworkController', () => { eventType: NetworkControllerEventType.InfuraIsBlocked, }); - controller.resetConnection(); + await controller.resetConnection(); expect(await promiseForNoInfuraIsUnblockedEvents).toBeTruthy(); expect(await promiseForInfuraIsBlocked).toBeTruthy(); @@ -5170,13 +5176,7 @@ describe('NetworkController', () => { async ({ controller, network }) => { network.mockEssentialRpcCalls(); - await waitForStateChanges({ - controller, - propertyPath: ['networkStatus'], - operation: () => { - controller.resetConnection(); - }, - }); + await controller.resetConnection(); expect(controller.store.getState().networkStatus).toBe( 'available', @@ -5208,13 +5208,7 @@ describe('NetworkController', () => { }, }); - await waitForStateChanges({ - controller, - propertyPath: ['networkDetails'], - operation: () => { - controller.resetConnection(); - }, - }); + await controller.resetConnection(); expect(controller.store.getState().networkDetails).toStrictEqual({ EIPS: { @@ -5259,6 +5253,8 @@ describe('NetworkController', () => { messenger: unrestrictedMessenger, eventType: NetworkControllerEventType.NetworkWillChange, operation: () => { + // Intentionally not awaited because we're capturing an event + // emitted partway through the operation controller.resetConnection(); }, }); @@ -5309,6 +5305,8 @@ describe('NetworkController', () => { // before networkDidChange count: 1, operation: () => { + // Intentionally not awaited because we want to check state + // partway through the operation controller.resetConnection(); }, }); @@ -5359,6 +5357,8 @@ describe('NetworkController', () => { // before networkDidChange count: 1, operation: () => { + // Intentionally not awaited because we want to check state + // partway through the operation controller.resetConnection(); }, }); @@ -5394,7 +5394,7 @@ describe('NetworkController', () => { async ({ controller, network }) => { network.mockEssentialRpcCalls(); - controller.resetConnection(); + await controller.resetConnection(); const { provider } = controller.getProviderAndBlockTracker(); assert(provider, 'Provider is somehow unset'); @@ -5441,12 +5441,7 @@ describe('NetworkController', () => { const { provider: providerBefore } = controller.getProviderAndBlockTracker(); - await waitForLookupNetworkToComplete({ - controller, - operation: () => { - controller.resetConnection(); - }, - }); + await controller.resetConnection(); const { provider: providerAfter } = controller.getProviderAndBlockTracker(); @@ -5485,8 +5480,8 @@ describe('NetworkController', () => { const networkDidChange = await waitForPublishedEvents({ messenger: unrestrictedMessenger, eventType: NetworkControllerEventType.NetworkDidChange, - operation: () => { - controller.resetConnection(); + operation: async () => { + await controller.resetConnection(); }, }); @@ -5525,8 +5520,8 @@ describe('NetworkController', () => { const infuraIsUnblocked = await waitForPublishedEvents({ messenger: unrestrictedMessenger, eventType: NetworkControllerEventType.InfuraIsUnblocked, - operation: () => { - controller.resetConnection(); + operation: async () => { + await controller.resetConnection(); }, }); @@ -5563,13 +5558,7 @@ describe('NetworkController', () => { }); expect(controller.store.getState().networkStatus).toBe('unknown'); - await waitForStateChanges({ - controller, - propertyPath: ['networkStatus'], - operation: () => { - controller.resetConnection(); - }, - }); + await controller.resetConnection(); expect(controller.store.getState().networkStatus).toBe('available'); }, @@ -5607,13 +5596,7 @@ describe('NetworkController', () => { }, }); - await waitForStateChanges({ - controller, - propertyPath: ['networkDetails'], - operation: () => { - controller.resetConnection(); - }, - }); + await controller.resetConnection(); expect(controller.store.getState().networkDetails).toStrictEqual({ EIPS: { diff --git a/app/scripts/controllers/network/network-controller.ts b/app/scripts/controllers/network/network-controller.ts index 783e368a1..51653ab4a 100644 --- a/app/scripts/controllers/network/network-controller.ts +++ b/app/scripts/controllers/network/network-controller.ts @@ -753,8 +753,8 @@ export class NetworkController extends EventEmitter { /** * Re-initializes the provider and block tracker for the current network. */ - resetConnection(): void { - this.#setProviderConfig(this.store.getState().provider); + async resetConnection() { + await this.#setProviderConfig(this.store.getState().provider); } /**