Skip to content

Commit

Permalink
Group network configurations by chain (#4286)
Browse files Browse the repository at this point in the history
## Explanation

<!--
Thanks for your contribution! Take a moment to answer these questions so
that reviewers have the information they need to properly understand
your changes:

* What is the current state of things and why does it need to change?
* What is the solution your changes offer and how does it work?
* Are there any changes whose purpose might not obvious to those
unfamiliar with the domain?
* If your primary goal was to update one package but you found you had
to update another one along the way, why did you do so?
* If you had to upgrade a dependency, why did you do so?
-->

Currently, in the client, it is possible to have multiple networks with
different RPC endpoint URLs representing the same chain. This creates a
problem because if all we have is a chain ID, we don't know which URL to
use for requests.

To solve this, we plan on consolidating the UX on the client side such
that each network corresponds to exactly one chain. Users can then
select which default RPC URL they'd like to use for requests. This
commit implements the controller changes necessary to support this UX.

Here are some more details on the changes here:

- The concept of a network configuration has been repurposed such that
instead of representing an RPC endpoint, it now represents a whole
chain.

- A network configuration may have multiple RPC endpoints, and one of
them must be designated as the default.
- Some RPC endpoints are special in that they represent Infura API URLs;
these have the same object shape as "non-Infura" (custom) RPC endpoints,
but the Infura project ID is hidden and injected into the RPC URL when
creating the network client.
- There is no longer a 1-to-1 relationship between network configuration
and network client; rather, the 1-to-1 relationship exists between RPC
endpoint and network client. This means that the ID of the network
client which is created for an RPC endpoint is stored on that RPC
endpoint instead of the whole network configuration.

- The `networkConfigurations` state property has been replaced with
`networkConfigurationsByChainId`. This continues to be an object, but
the data inside is organized such that network configurations are
identified by chain ID instead of network client ID as they were
previously.

- The methods `upsertNetworkConfiguration` and
`removeNetworkConfiguration` have been removed. These methods always did
more than simply add or remove a network configuration; they also
updated the registry of network clients. Instead, these methods have
been replaced with `addNetwork`, `updateNetwork`, and `removeNetwork`.

- `addNetwork` creates new network clients for each RPC endpoint in the
given network configuration.
- `updateNetwork` takes a chain ID referring to a network configuration
and a draft version of that network configuration, and adds or removes
network clients for added or removed RPC
    endpoints.
- `removeNetwork` takes a chain ID referring to a network configuration
and removes the network clients for each of its RPC endpoints.

- In addition, due to the changes to network configuration itself, there
are new restrictions on `networkConfigurationsByChainId`, which are
validated on initialization and on update. These are:

- The network controller cannot be initialized with an empty collection
of network configurations. This is because there must be a selected
network client so that consumers have a provider to use out of the gate.
  - Consequently, the last network configuration cannot be removed.
- The network configuration that contains a reference to the currently
selected network client cannot be removed.
- The chain ID of a network configuration must match the same chain that
it's filed under in `networkConfigurationsByChainId`.
  - No two network configurations can have the same chain ID.
- A RPC endpoint in a network configuration must have a well-formed URL.
  - A network configuration cannot have duplicate RPC endpoints.
- No two RPC endpoints (regardless of network configuration) can have
the same URL. Equality is currently determined by normalizing URLs as
per RFC 3986 and may include data like request headers in the future.
- If a network configuration has an Infura RPC endpoint, its chain ID
must match the set chain ID of the network configuration.
- Changing the chain ID of a network configuration is possible, but any
existing Infura RPC endpoint must be replaced with the one that matches
the new chain ID.
- No two RPC endpoints (regardless of network configuration) can have
the same network client ID.

- Finally, the `trackMetaMetricsEvent` option has been removed from the
constructor. This was previously used in `upsertNetworkConfiguration` to
create a MetaMetrics event when a new network added, but I've added a
new event `NetworkController:networkAdded` to allow the client to do
this on its own accord.


## References

<!--
Are there any issues that this pull request is tied to? Are there other
links that reviewers should consult to understand these changes better?

For example:

* Fixes #12345
* Related to #67890
-->

Fixes #4189.
Fixes #3793.

## Changelog

<!--
If you're making any consumer-facing changes, list those changes here as
if you were updating a changelog, using the template below as a guide.

(CATEGORY is one of BREAKING, ADDED, CHANGED, DEPRECATED, REMOVED, or
FIXED. For security-related issues, follow the Security Advisory
process.)

Please take care to name the exact pieces of the API you've added or
changed (e.g. types, interfaces, functions, or methods).

If there are any breaking changes, make sure to offer a solution for
consumers to follow once they upgrade to the changes.

Finally, if you're only making changes to development scripts or tests,
you may replace the template below with "None".
-->

(Updated in the PR.)

## Checklist

- [ ] I've updated the test suite for new or updated code as appropriate
- [ ] I've updated documentation (JSDoc, Markdown, etc.) for new or
updated code as appropriate
- [ ] I've highlighted breaking changes using the "BREAKING" category
above as appropriate

---------

Co-authored-by: Jongsun Suh <[email protected]>
Co-authored-by: Brian Bergeron <[email protected]>
Co-authored-by: Michele Esposito <[email protected]>
Co-authored-by: Michele Esposito <[email protected]>
  • Loading branch information
5 people authored Sep 3, 2024
1 parent b4d5f3e commit 442a300
Show file tree
Hide file tree
Showing 29 changed files with 12,760 additions and 3,694 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { InternalAccount } from '@metamask/keyring-api';
import {
type NetworkClientId,
type NetworkClientConfiguration,
defaultState as defaultnetworkControllerState,
getDefaultNetworkControllerState,
} from '@metamask/network-controller';
import { getDefaultPreferencesState } from '@metamask/preferences-controller';
import * as sinon from 'sinon';
Expand Down Expand Up @@ -618,7 +618,7 @@ async function withController<ReturnValue>(
);

const mockNetworkState = jest.fn().mockReturnValue({
...defaultnetworkControllerState,
...getDefaultNetworkControllerState(),
chainId: initialChainId,
});
messenger.registerActionHandler(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ async function setupAssetContractControllers({
allowedActions: [],
allowedEvents: [],
}),
trackMetaMetricsEvent: jest.fn(),
});
if (useNetworkControllerProvider) {
await networkController.initializeProvider();
Expand Down
4 changes: 2 additions & 2 deletions packages/assets-controllers/src/NftController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import type {
NetworkClientConfiguration,
NetworkClientId,
} from '@metamask/network-controller';
import { defaultState as defaultNetworkState } from '@metamask/network-controller';
import { getDefaultNetworkControllerState } from '@metamask/network-controller';
import {
getDefaultPreferencesState,
type PreferencesState,
Expand Down Expand Up @@ -352,7 +352,7 @@ function setupController({
selectedNetworkClientId: NetworkClientId;
}) => {
messenger.publish('NetworkController:networkDidChange', {
...defaultNetworkState,
...getDefaultNetworkControllerState(),
selectedNetworkClientId,
});
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
InfuraNetworkType,
} from '@metamask/controller-utils';
import {
getDefaultNetworkControllerState,
NetworkClientType,
defaultState as defaultNetworkState,
} from '@metamask/network-controller';
import type {
NetworkClient,
Expand Down Expand Up @@ -1655,7 +1655,7 @@ async function withController<ReturnValue>(
messenger.registerActionHandler(
'NetworkController:getState',
jest.fn<NetworkState, []>().mockReturnValue({
...defaultNetworkState,
...getDefaultNetworkControllerState(),
...mockNetworkState,
}),
);
Expand Down
56 changes: 31 additions & 25 deletions packages/assets-controllers/src/TokenDetectionController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import {
ChainId,
NetworkType,
convertHexToDecimal,
BUILT_IN_NETWORKS,
InfuraNetworkType,
} from '@metamask/controller-utils';
import type { InternalAccount } from '@metamask/keyring-api';
import type { KeyringControllerState } from '@metamask/keyring-controller';
import { defaultState as defaultNetworkState } from '@metamask/network-controller';
import { getDefaultNetworkControllerState } from '@metamask/network-controller';
import type {
NetworkState,
NetworkConfiguration,
Expand All @@ -28,6 +28,10 @@ import * as sinon from 'sinon';

import { advanceTime } from '../../../tests/helpers';
import { createMockInternalAccount } from '../../accounts-controller/src/tests/mocks';
import {
buildCustomRpcEndpoint,
buildInfuraNetworkConfiguration,
} from '../../network-controller/tests/helpers';
import { formatAggregatorNames } from './assetsUtil';
import { TOKEN_END_POINT_API } from './token-service';
import type {
Expand Down Expand Up @@ -110,22 +114,24 @@ const sampleTokenB = {
};

const mockNetworkConfigurations: Record<string, NetworkConfiguration> = {
[NetworkType.mainnet]: {
...BUILT_IN_NETWORKS[NetworkType.mainnet],
rpcUrl: 'https://mainnet.infura.io/v3/fakekey',
},
[NetworkType.goerli]: {
...BUILT_IN_NETWORKS[NetworkType.goerli],
rpcUrl: 'https://goerli.infura.io/v3/fakekey',
},
[InfuraNetworkType.mainnet]: buildInfuraNetworkConfiguration(
InfuraNetworkType.mainnet,
),
[InfuraNetworkType.goerli]: buildInfuraNetworkConfiguration(
InfuraNetworkType.goerli,
),
polygon: {
blockExplorerUrls: ['https://polygonscan.com/'],
chainId: '0x89',
nickname: 'Polygon Mainnet',
rpcUrl: `https://polygon-mainnet.infura.io/v3/fakekey`,
ticker: 'MATIC',
rpcPrefs: {
blockExplorerUrl: 'https://polygonscan.com/',
},
defaultBlockExplorerUrlIndex: 0,
defaultRpcEndpointIndex: 0,
name: 'Polygon Mainnet',
nativeCurrency: 'MATIC',
rpcEndpoints: [
buildCustomRpcEndpoint({
url: 'https://polygon-mainnet.infura.io/v3/fakekey',
}),
],
},
};

Expand Down Expand Up @@ -306,7 +312,7 @@ describe('TokenDetectionController', () => {
},
async ({ controller, mockNetworkState }) => {
mockNetworkState({
...defaultNetworkState,
...getDefaultNetworkControllerState(),
selectedNetworkClientId: NetworkType.goerli,
});
await controller.start();
Expand Down Expand Up @@ -393,7 +399,7 @@ describe('TokenDetectionController', () => {
callActionSpy,
}) => {
mockNetworkState({
...defaultNetworkState,
...getDefaultNetworkControllerState(),
selectedNetworkClientId: 'polygon',
});
mockGetNetworkClientById(
Expand Down Expand Up @@ -1400,7 +1406,7 @@ describe('TokenDetectionController', () => {
});

triggerNetworkDidChange({
...defaultNetworkState,
...getDefaultNetworkControllerState(),
selectedNetworkClientId: 'polygon',
});
await advanceTime({ clock, duration: 1 });
Expand Down Expand Up @@ -1461,7 +1467,7 @@ describe('TokenDetectionController', () => {
});

triggerNetworkDidChange({
...defaultNetworkState,
...getDefaultNetworkControllerState(),
selectedNetworkClientId: 'goerli',
});
await advanceTime({ clock, duration: 1 });
Expand Down Expand Up @@ -1512,7 +1518,7 @@ describe('TokenDetectionController', () => {
});

triggerNetworkDidChange({
...defaultNetworkState,
...getDefaultNetworkControllerState(),
selectedNetworkClientId: 'mainnet',
});
await advanceTime({ clock, duration: 1 });
Expand Down Expand Up @@ -1565,7 +1571,7 @@ describe('TokenDetectionController', () => {
});

triggerNetworkDidChange({
...defaultNetworkState,
...getDefaultNetworkControllerState(),
selectedNetworkClientId: 'polygon',
});
await advanceTime({ clock, duration: 1 });
Expand Down Expand Up @@ -1619,7 +1625,7 @@ describe('TokenDetectionController', () => {
});

triggerNetworkDidChange({
...defaultNetworkState,
...getDefaultNetworkControllerState(),
selectedNetworkClientId: 'polygon',
});
await advanceTime({ clock, duration: 1 });
Expand Down Expand Up @@ -1955,7 +1961,7 @@ describe('TokenDetectionController', () => {
callActionSpy,
}) => {
mockNetworkState({
...defaultNetworkState,
...getDefaultNetworkControllerState(),
selectedNetworkClientId: NetworkType.goerli,
});
triggerPreferencesStateChange({
Expand Down Expand Up @@ -2372,7 +2378,7 @@ async function withController<ReturnValue>(
const mockNetworkState = jest.fn<NetworkState, []>();
controllerMessenger.registerActionHandler(
'NetworkController:getState',
mockNetworkState.mockReturnValue({ ...defaultNetworkState }),
mockNetworkState.mockReturnValue({ ...getDefaultNetworkControllerState() }),
);
const mockTokensState = jest.fn<TokensControllerState, []>();
controllerMessenger.registerActionHandler(
Expand Down
10 changes: 5 additions & 5 deletions packages/assets-controllers/src/TokenListController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ describe('TokenListController', () => {
);
onNetworkStateChangeCallback({
selectedNetworkClientId,
networkConfigurations: {},
networkConfigurationsByChainId: {},
networksMetadata: {},
// @ts-expect-error This property isn't used and will get removed later.
providerConfig: {},
Expand Down Expand Up @@ -996,7 +996,7 @@ describe('TokenListController', () => {
'NetworkController:stateChange',
{
selectedNetworkClientId: InfuraNetworkType.goerli,
networkConfigurations: {},
networkConfigurationsByChainId: {},
networksMetadata: {},
// @ts-expect-error This property isn't used and will get removed later.
providerConfig: {},
Expand All @@ -1017,7 +1017,7 @@ describe('TokenListController', () => {
'NetworkController:stateChange',
{
selectedNetworkClientId: selectedCustomNetworkClientId,
networkConfigurations: {},
networkConfigurationsByChainId: {},
networksMetadata: {},
// @ts-expect-error This property isn't used and will get removed later.
providerConfig: {},
Expand Down Expand Up @@ -1097,7 +1097,7 @@ describe('TokenListController', () => {
'NetworkController:stateChange',
{
selectedNetworkClientId: InfuraNetworkType.mainnet,
networkConfigurations: {},
networkConfigurationsByChainId: {},
networksMetadata: {},
// @ts-expect-error This property isn't used and will get removed later.
providerConfig: {},
Expand Down Expand Up @@ -1147,7 +1147,7 @@ describe('TokenListController', () => {
'NetworkController:stateChange',
{
selectedNetworkClientId: selectedCustomNetworkClientId,
networkConfigurations: {},
networkConfigurationsByChainId: {},
networksMetadata: {},
// @ts-expect-error This property isn't used and will get removed later.
providerConfig: {},
Expand Down
44 changes: 21 additions & 23 deletions packages/assets-controllers/src/TokenRatesController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type {
NetworkClientId,
NetworkState,
} from '@metamask/network-controller';
import { defaultState as defaultNetworkState } from '@metamask/network-controller';
import { getDefaultNetworkControllerState } from '@metamask/network-controller';
import type { Hex } from '@metamask/utils';
import { add0x } from '@metamask/utils';
import assert from 'assert';
Expand Down Expand Up @@ -48,8 +48,6 @@ const defaultSelectedAccount = createMockInternalAccount({
});
const mockTokenAddress = '0x0000000000000000000000000000000000000010';

const defaultSelectedNetworkClientId = 'AAAA-BBBB-CCCC-DDDD';

type MainControllerMessenger = ControllerMessenger<
AllowedActions | AddApprovalRequest,
AllowedEvents
Expand Down Expand Up @@ -638,8 +636,8 @@ describe('TokenRatesController', () => {
.spyOn(controller, 'updateExchangeRates')
.mockResolvedValue();
triggerNetworkStateChange({
...defaultNetworkState,
selectedNetworkClientId: defaultSelectedNetworkClientId,
...getDefaultNetworkControllerState(),
selectedNetworkClientId: 'AAAA-BBBB-CCCC-DDDD',
});

expect(updateExchangeRatesSpy).toHaveBeenCalledTimes(1);
Expand All @@ -666,8 +664,8 @@ describe('TokenRatesController', () => {
.spyOn(controller, 'updateExchangeRates')
.mockResolvedValue();
triggerNetworkStateChange({
...defaultNetworkState,
selectedNetworkClientId: defaultSelectedNetworkClientId,
...getDefaultNetworkControllerState(),
selectedNetworkClientId: 'AAAA-BBBB-CCCC-DDDD',
});

expect(updateExchangeRatesSpy).toHaveBeenCalledTimes(1);
Expand Down Expand Up @@ -720,8 +718,8 @@ describe('TokenRatesController', () => {
await controller.start();
jest.spyOn(controller, 'updateExchangeRates').mockResolvedValue();
triggerNetworkStateChange({
...defaultNetworkState,
selectedNetworkClientId: defaultSelectedNetworkClientId,
...getDefaultNetworkControllerState(),
selectedNetworkClientId: 'AAAA-BBBB-CCCC-DDDD',
});

expect(controller.state.marketData).toStrictEqual({});
Expand Down Expand Up @@ -774,8 +772,8 @@ describe('TokenRatesController', () => {
await controller.start();
jest.spyOn(controller, 'updateExchangeRates').mockResolvedValue();
triggerNetworkStateChange({
...defaultNetworkState,
selectedNetworkClientId: defaultSelectedNetworkClientId,
...getDefaultNetworkControllerState(),
selectedNetworkClientId: 'AAAA-BBBB-CCCC-DDDD',
});

expect(controller.state.marketData).toStrictEqual({});
Expand All @@ -802,8 +800,8 @@ describe('TokenRatesController', () => {
.spyOn(controller, 'updateExchangeRates')
.mockResolvedValue();
triggerNetworkStateChange({
...defaultNetworkState,
selectedNetworkClientId: defaultSelectedNetworkClientId,
...getDefaultNetworkControllerState(),
selectedNetworkClientId: 'AAAA-BBBB-CCCC-DDDD',
});

expect(updateExchangeRatesSpy).not.toHaveBeenCalled();
Expand Down Expand Up @@ -831,8 +829,8 @@ describe('TokenRatesController', () => {
.spyOn(controller, 'updateExchangeRates')
.mockResolvedValue();
triggerNetworkStateChange({
...defaultNetworkState,
selectedNetworkClientId: defaultSelectedNetworkClientId,
...getDefaultNetworkControllerState(),
selectedNetworkClientId: 'AAAA-BBBB-CCCC-DDDD',
});

expect(updateExchangeRatesSpy).not.toHaveBeenCalled();
Expand All @@ -858,8 +856,8 @@ describe('TokenRatesController', () => {
.spyOn(controller, 'updateExchangeRates')
.mockResolvedValue();
triggerNetworkStateChange({
...defaultNetworkState,
selectedNetworkClientId: defaultSelectedNetworkClientId,
...getDefaultNetworkControllerState(),
selectedNetworkClientId: 'AAAA-BBBB-CCCC-DDDD',
});

expect(updateExchangeRatesSpy).not.toHaveBeenCalled();
Expand Down Expand Up @@ -911,8 +909,8 @@ describe('TokenRatesController', () => {
async ({ controller, triggerNetworkStateChange }) => {
jest.spyOn(controller, 'updateExchangeRates').mockResolvedValue();
triggerNetworkStateChange({
...defaultNetworkState,
selectedNetworkClientId: defaultSelectedNetworkClientId,
...getDefaultNetworkControllerState(),
selectedNetworkClientId: 'AAAA-BBBB-CCCC-DDDD',
});

expect(controller.state.marketData).toStrictEqual({});
Expand Down Expand Up @@ -964,8 +962,8 @@ describe('TokenRatesController', () => {
async ({ controller, triggerNetworkStateChange }) => {
jest.spyOn(controller, 'updateExchangeRates').mockResolvedValue();
triggerNetworkStateChange({
...defaultNetworkState,
selectedNetworkClientId: defaultSelectedNetworkClientId,
...getDefaultNetworkControllerState(),
selectedNetworkClientId: 'AAAA-BBBB-CCCC-DDDD',
});

expect(controller.state.marketData).toStrictEqual({});
Expand Down Expand Up @@ -2327,7 +2325,7 @@ async function withController<ReturnValue>(
controllerMessenger.registerActionHandler(
'NetworkController:getState',
networkStateMock.mockReturnValue({
...defaultNetworkState,
...getDefaultNetworkControllerState(),
...mockNetworkState,
}),
);
Expand Down Expand Up @@ -2445,7 +2443,7 @@ async function callUpdateExchangeRatesMethod({
// As with many BaseControllerV1-based controllers, runtime config
// modification is allowed by the API but not supported in practice.
triggerNetworkStateChange({
...defaultNetworkState,
...getDefaultNetworkControllerState(),
selectedNetworkClientId,
});
}
Expand Down
Loading

0 comments on commit 442a300

Please sign in to comment.