Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NetworkController: Remove providerConfigChange event #1329

Merged
merged 4 commits into from
May 5, 2023

Conversation

mcmire
Copy link
Contributor

@mcmire mcmire commented May 4, 2023

Description

Despite its name, the providerConfigChange event in NetworkController doesn't get emitted whenever the providerConfig property in state is changed. Rather, it gets updated as the very last step in lookupNetwork. This method is called:

  • When the provider is initialized
  • When the network is switched
  • When the network is rolled back to the previous setting
  • When resetConnection is called

lookupNetwork can also be called on its own.

If this were not enough, we already provide a way to listen for changes to a particular property in state: by passing a selector function to the subscribe method on the controller messenger.

So, this commit removes the providerConfigChange event from NetworkController. Instead of the following code:

messenger.subscribe(
  'NetworkController:providerConfigChange',
  (providerConfig) => {
    // do something with the providerConfig
  },
)

consumers should be able to say:

messenger.subscribe(
  'NetworkController:stateChange',
  (providerConfig) => {
    // do something with the providerConfig
  },
  (networkControllerState) => networkControllerState.providerConfig,
)

Changes

  • BREAKING: [@metamask/network-controller] Update initializeProvider, lookupNetwork, setProviderType, setActiveNetwork, rollbackToPreviousProvider, and resetConnection so that NetworkController:providerConfigChange is no longer emitted; remove this event entirely from the set of allowed events along with the NetworkControllerProviderConfigChangeEvent type
    • Consumers are encouraged to subscribe to NetworkController:stateChange with a selector function that returns providerConfig if they want to perform an action when providerConfig changes.
  • ADDED: [@metamask/network-controller] Add NetworkController:getState to the set of allowed messenger actions, along with a NetworkControllerGetStateAction type
  • BREAKING: [@metamask/assets-controllers] Update signature of onNetworkStateChange callback for TokenListController so that it must be given a NetworkController state change object; it will no longer also accept a provider config object
  • BREAKING: [@metamask/assets-controllers] Update type of TokenListController messenger to require NetworkController:stateChange as an allowed event
  • BREAKING: [@metamask/gas-fee-controller] Update type of GasFeeMessenger to require NetworkController:getState as an allowed event

References

Fixes #1205.

Checklist

  • I've updated the test suite for new or updated code as appropriate
  • I've updated documentation for new or updated code as appropriate (note: this will usually be JSDoc)
  • I've highlighted breaking changes using the "BREAKING" category above as appropriate

@mcmire mcmire requested a review from a team as a code owner May 4, 2023 23:07
Despite its name, the `providerConfigChange` event in NetworkController
doesn't get emitted whenever the `providerConfig` property in state is
changed. Rather, it gets updated as the very last step in
`lookupNetwork`. This method is called:

- When the provider is initialized
- When the network is switched
- When the network is rolled back to the previous setting
- When `resetConnection` is called

`lookupNetwork` can also be called on its own.

If this were not enough, we already provide a way to listen for changes
to a particular property in state: by passing a selector function to the
`subscribe` method on the controller messenger.

So, this commit removes the `providerConfigChange` event from
NetworkController. Instead of the following code:

```
messenger.subscribe(
  'NetworkController:providerConfigChange',
  (providerConfig) => {
    // do something with the providerConfig
  },
)
```

consumers should be able to say:

```
messenger.subscribe(
  'NetworkController:stateChange',
  (providerConfig) => {
    // do something with the providerConfig
  },
  (networkControllerState) => networkControllerState.providerConfig,
)
```
@mcmire mcmire force-pushed the remove-provider-config-change-event branch from 14d2598 to c11fb0a Compare May 4, 2023 23:09
);
}
onNetworkStateChange(async (networkControllerState) => {
await this.#onNetworkControllerStateChange(networkControllerState);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can simulate a network controller state object in tests, so we don't need to make this check.

this.currentChainId = newProviderConfig.chainId;
await this.resetPolling();
}
'NetworkController:stateChange',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note that we have a similar pattern of using onNetworkStateChange or subscribing to an event through the messenger in TokenListController, so I made them match each other.

this.EIP1559APIEndpoint = EIP1559APIEndpoint;
this.legacyAPIEndpoint = legacyAPIEndpoint;
this.clientId = clientId;

this.ethQuery = new EthQuery(this.#getProvider());
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We don't need to call getProvider only if onNetworkStateChange and getChainId are given; this is a required option, so we can always get the provider this way. So I chose to use getProvider in the state change callback below to refresh the EthQuery instance. However, if we want to continue using the getEthQuery action we can — I just didn't know if we planned on getting rid of that or not.

Gudahtt
Gudahtt previously approved these changes May 5, 2023
Copy link
Member

@Gudahtt Gudahtt left a comment

Choose a reason for hiding this comment

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

LGTM! I was concerned this would affect more controllers, but 2 is not bad. Nice that the gas fee controller and token list controller network change handling is now more concise and standardized.

I would suggest that the PR description be updated to include a note on the gas fee controller though; that change to the restricted messenger type is a breaking change as well.

@mcmire
Copy link
Contributor Author

mcmire commented May 5, 2023

PR description updated and conflicts resolved.

Gudahtt
Gudahtt previously approved these changes May 5, 2023
Copy link
Member

@Gudahtt Gudahtt left a comment

Choose a reason for hiding this comment

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

LGTM!

Though for:

BREAKING: [@metamask/assets-controllers] Update type of messenger option for TokenListController to remove NetworkController:providerConfigChange from the set of allowed events

We should mention that the NetworkController:stateChange event is now required as well, since that's the breaking part. Removing an event is non-breaking.

@mcmire mcmire merged commit dca5d30 into main May 5, 2023
@mcmire mcmire deleted the remove-provider-config-change-event branch May 5, 2023 17:07
MajorLift pushed a commit that referenced this pull request Oct 11, 2023
Despite its name, the `providerConfigChange` event in NetworkController
doesn't get emitted whenever the `providerConfig` property in state is
changed. Rather, it gets updated as the very last step in
`lookupNetwork`. This method is called:

- When the provider is initialized
- When the network is switched
- When the network is rolled back to the previous setting
- When `resetConnection` is called

`lookupNetwork` can also be called on its own.

If this were not enough, we already provide a way to listen for changes
to a particular property in state: by passing a selector function to the
`subscribe` method on the controller messenger.

So, this commit removes the `providerConfigChange` event from
NetworkController. Instead of the following code:

```
messenger.subscribe(
  'NetworkController:providerConfigChange',
  (providerConfig) => {
    // do something with the providerConfig
  },
)
```

consumers should be able to say:

```
messenger.subscribe(
  'NetworkController:stateChange',
  (providerConfig) => {
    // do something with the providerConfig
  },
  (networkControllerState) => networkControllerState.providerConfig,
)
```
MajorLift pushed a commit that referenced this pull request Oct 11, 2023
Despite its name, the `providerConfigChange` event in NetworkController
doesn't get emitted whenever the `providerConfig` property in state is
changed. Rather, it gets updated as the very last step in
`lookupNetwork`. This method is called:

- When the provider is initialized
- When the network is switched
- When the network is rolled back to the previous setting
- When `resetConnection` is called

`lookupNetwork` can also be called on its own.

If this were not enough, we already provide a way to listen for changes
to a particular property in state: by passing a selector function to the
`subscribe` method on the controller messenger.

So, this commit removes the `providerConfigChange` event from
NetworkController. Instead of the following code:

```
messenger.subscribe(
  'NetworkController:providerConfigChange',
  (providerConfig) => {
    // do something with the providerConfig
  },
)
```

consumers should be able to say:

```
messenger.subscribe(
  'NetworkController:stateChange',
  (providerConfig) => {
    // do something with the providerConfig
  },
  (networkControllerState) => networkControllerState.providerConfig,
)
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

NetworkController: Remove NetworkController:providerConfigChange controller event
2 participants