You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[network-controller] Group network configurations by chain #3793
Today each network configuration represents a built-in RPC endpoint or a custom RPC endpoint. They are not grouped by chain, the same chain can appear multiple times.
We should instead group this configuration by chain, such that each chain has a single network configuration, with an array of one or more RPC endpoints. This would allow consolidating configuration/state that is related to the chain, not the RPC URL. This also unblocks some privacy enhancements for the clients.
The exact API changes we need to make here have not been determined yet.
As part of this issue, or as a follow-up issue, we should review where this change would allow us to simplify other controllers.
As part of the mutlichain controller refactors, many controllers have been updated to key state by networkClientId where it would be simpler to use chainId instead after this change lands. We may also want to consider parameterizing multi chain-aware methods by chainId rather than networkClientId.
I've added a new state property, networkConfigurationsByChainId, along with a couple of new major types, NetworkConfigurationV2 and RpcEndpoint.
The "V2" is there temporarily (I've left the existing types in place); I'll remove it shortly.
I've added three new methods: addNetwork, updateNetwork, and removeNetwork.
I've opted to use "network" rather than "network configuration" in the name, because it not only updates the network configurations but also adds or removes network clients.
I've opted not to combine addNetwork and updateNetwork, even though they contain duplicate logic at the moment, as these methods are quite long on their own, and so are the tests. However, I may work out a way to refactor this code later.
Overall, it works like this:
A network configuration now represents a single chain (instead of multiple network configurations representing a single chain).
A chain may be fronted by multiple RPC endpoints.
A network client is created for each RPC endpoint.
Collectively, a network configuration + its network clients is called a "network". This concept maps to how it will be displayed to users on the client side.
Some chains are special, in that they are supported by Infura (e.g., Mainnet, Sepolia, etc.)
There are two types of RPC endpoints: "default" and "custom".
"Default" RPC endpoints are named as such because the network controller will be initialized with networks (network configurations and network clients) for Infura-supported chains by default if not given any network configurations. These networks will be configured with endpoints that link them to Infura by default. The RPC URLs also do not contain the Infura project ID; these will be injected under the hood.
"Custom" RPC endpoints can link to any URL.
The default RPC endpoint for an Infura-supported chain may be removed.
A network configuration can only have one default RPC endpoint.
When adding a default RPC endpoint to a network configuration, the chain that is associated with the exact endpoint must match the chain of the network configuration (e.g. only https://mainnet.infura.io can be added as the default RPC endpoint for the network configuration representing chain 0x1).
A network configuration cannot have duplicate RPC endpoints. This means that no two RPC endpoints can use the same URL (matching case-insensitively).
When a network client is created for an RPC endpoint, the ID is stored with the RPC endpoint. This makes it possible to look up that network client later. All RPC endpoints therefore have such an ID.
What's left
Modify 51 NetworkController tests that still set networkConfigurations in state to use networkConfigurationsByChainId instead
Fix 173 broken NetworkController tests
Fix these NetworkController methods to use networkConfigurationsByChainId instead:
Activity
desi commentedon Apr 18, 2024
[note] multichain support related.
Gudahtt commentedon Apr 23, 2024
As part of this issue, or as a follow-up issue, we should review where this change would allow us to simplify other controllers.
As part of the mutlichain controller refactors, many controllers have been updated to key state by
networkClientIdwhere it would be simpler to usechainIdinstead after this change lands. We may also want to consider parameterizing multi chain-aware methods bychainIdrather thannetworkClientId.mcmire commentedon May 17, 2024
Progress update:
What I've done
networkConfigurationsByChainId, along with a couple of new major types,NetworkConfigurationV2andRpcEndpoint.addNetwork,updateNetwork, andremoveNetwork.addNetworkandupdateNetwork, even though they contain duplicate logic at the moment, as these methods are quite long on their own, and so are the tests. However, I may work out a way to refactor this code later.https://mainnet.infura.iocan be added as the default RPC endpoint for the network configuration representing chain 0x1).What's left
Modify 51 NetworkController tests that still setnetworkConfigurationsin state to usenetworkConfigurationsByChainIdinsteadFix 173 broken NetworkController testsFix these NetworkController methods to usenetworkConfigurationsByChainIdinstead:getNetworkConfigurationByNetworkClientIdloadBackupRemoveupsertNetworkConfiguration,removeNetworkConfiguration,networkConfigurationsRenameNetworkConfigurationV2toNetworkConfigurationAdd missing JSDocsAddress TODO withinupdateNetworkaccounts-controller(AccountsController.ts fails to compile)assets-controllers(no moredefaultStateexport;networkConfigurationsByChainIdmissing)gas-fee-controller(no moredefaultStateexport)ens-controller(no moredefaultStateexport)queued-request-controller(no moredefaultStateexport)selected-network-controller(no moredefaultStateexport;networkConfigurationsByChainIdmissing)transaction-controller(no moredefaultStateexport;networkConfigurationsByChainIdmissing)Group network configurations by chain (#4286)