[network-controller] Group network configurations by chain
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.
[note] multichain support related.
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.
Progress update:
What I've done
- I've added a new state property,
networkConfigurationsByChainId, along with a couple of new major types,NetworkConfigurationV2andRpcEndpoint.- 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, andremoveNetwork.- 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
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.
- 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.iocan 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
networkConfigurationsin state to usenetworkConfigurationsByChainIdinstead~ - ~Fix 173 broken NetworkController tests~
- ~Fix these NetworkController methods to use
networkConfigurationsByChainIdinstead:~-
getNetworkConfigurationByNetworkClientId -
loadBackup
-
- ~Remove
upsertNetworkConfiguration,removeNetworkConfiguration,networkConfigurations~ - ~Rename
NetworkConfigurationV2toNetworkConfiguration~ - ~Add missing JSDocs~
- ~Address TODO within
updateNetwork~ - Fix failing tests for other packages:
- ~
accounts-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)
- ~