Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
de3b7f8
feat: add NetworkController actions and events
Prithpal-Sooriya Sep 13, 2024
8844d55
test: add tests to cover calls through the controller messenger
Prithpal-Sooriya Nov 19, 2024
ef02bdf
feat: add lastUpdatedAt for NetworkConfiguration
Prithpal-Sooriya Sep 3, 2024
69acd6f
test: update test assertions that require lastUpdateAt property
Prithpal-Sooriya Nov 20, 2024
aa490e8
feat: use original networkClientId if provided when adding a network
Prithpal-Sooriya Nov 15, 2024
84301ae
feat: add dangerouslySetNetworkConfiguration method to override netwo…
Prithpal-Sooriya Nov 19, 2024
f5649fe
refactor: make dangerouslySetNetworkConfiguration a private method
Prithpal-Sooriya Nov 20, 2024
ccb17d2
feat: add main network sync controller integration
Prithpal-Sooriya Sep 13, 2024
a933a55
feat: add main network sync controller integration
Prithpal-Sooriya Sep 13, 2024
d291144
feat: add network sync callbacks
Prithpal-Sooriya Oct 24, 2024
7d48db3
test: add controller-integration - performMainSync() tests
Prithpal-Sooriya Oct 24, 2024
08c76ec
test: add test coverage
Prithpal-Sooriya Oct 24, 2024
dfa83b8
fix: fix syncing issues from manual integration testing
Prithpal-Sooriya Nov 19, 2024
c2531fb
test: fix controller integration tests
Prithpal-Sooriya Nov 19, 2024
d99306f
test: fix failing batch update network tests
Prithpal-Sooriya Nov 19, 2024
f8237c4
refactor: reuse user storage messengers and mocks
Prithpal-Sooriya Nov 19, 2024
b54490a
test: add test coverage for services and controller-integration
Prithpal-Sooriya Nov 19, 2024
8cf7bea
test: add controller syncNetwork tests
Prithpal-Sooriya Nov 19, 2024
49d7468
feat: add hasNetworkSyncingSyncedAtLeastOnce check
Prithpal-Sooriya Nov 21, 2024
75fce4a
refactor: use logging library instead of console
Prithpal-Sooriya Nov 21, 2024
f774423
feat: add max size bounds for networks to add
Prithpal-Sooriya Nov 21, 2024
0b2a267
fix: fix type issue
Prithpal-Sooriya Nov 21, 2024
86f5267
Merge branch 'main' into temp/network-syncing-all-changes
Prithpal-Sooriya Nov 25, 2024
9de1373
fix: fix type issue
Prithpal-Sooriya Nov 21, 2024
027ab3f
test: fix test
Prithpal-Sooriya Nov 25, 2024
50bb1e2
Merge branch 'main' into temp/network-syncing-all-changes
Prithpal-Sooriya Nov 26, 2024
d346ba8
feat: use existing network controller updateNetwork method
Prithpal-Sooriya Nov 26, 2024
d1104b6
refactor: remove dangerously set code
Prithpal-Sooriya Nov 26, 2024
1422588
refactor: clean up error logs
Prithpal-Sooriya Nov 27, 2024
5544fdb
feat: stuff
Prithpal-Sooriya Nov 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/network-controller/src/NetworkController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,11 @@ export type NetworkControllerRemoveNetworkAction = {
handler: NetworkController['removeNetwork'];
};

export type NetworkControllerUpdateNetworkAction = {
type: 'NetworkController:updateNetwork';
handler: NetworkController['updateNetwork'];
};

export type NetworkControllerDangerouslySetNetworkConfigurationAction = {
type: 'NetworkController:dangerouslySetNetworkConfiguration';
handler: (networkConfiguration: NetworkConfiguration) => Promise<void>;
Expand All @@ -519,6 +524,7 @@ export type NetworkControllerActions =
| NetworkControllerGetNetworkConfigurationByNetworkClientId
| NetworkControllerAddNetworkAction
| NetworkControllerRemoveNetworkAction
| NetworkControllerUpdateNetworkAction
| NetworkControllerDangerouslySetNetworkConfigurationAction;

export type NetworkControllerMessenger = RestrictedControllerMessenger<
Expand Down Expand Up @@ -1005,6 +1011,13 @@ export class NetworkController extends BaseController<
this.removeNetwork.bind(this),
);

this.messagingSystem.registerActionHandler(
// ESLint is mistaken here; `name` is a string.
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
`${this.name}:updateNetwork`,
this.updateNetwork.bind(this),
);

this.messagingSystem.registerActionHandler(
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
Expand Down
1 change: 1 addition & 0 deletions packages/network-controller/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export type {
NetworkControllerSetActiveNetworkAction,
NetworkControllerAddNetworkAction,
NetworkControllerRemoveNetworkAction,
NetworkControllerUpdateNetworkAction,
NetworkControllerDangerouslySetNetworkConfigurationAction,
NetworkControllerGetNetworkConfigurationByNetworkClientId,
NetworkControllerActions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ export type NetworkControllerRemoveNetworkAction = {
handler: NetworkController['removeNetwork'];
};
export type NetworkControllerDangerouslySetNetworkConfigurationAction = {
type: 'NetworkController:dangerouslySetNetworkConfiguration';
handler: (networkConfiguration: NetworkConfiguration) => Promise<void>;
type: 'NetworkController:updateNetwork';
handler: NetworkController['updateNetwork'];
};

// TODO: fix external dependencies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export function createCustomUserStorageMessenger(props?: {
'AccountsController:updateAccountMetadata',
'NetworkController:getState',
'NetworkController:addNetwork',
'NetworkController:dangerouslySetNetworkConfiguration',
'NetworkController:updateNetwork',
'NetworkController:removeNetwork',
],
allowedEvents: props?.overrideEvents ?? [
Expand Down Expand Up @@ -165,7 +165,7 @@ export function mockUserStorageMessenger(
);

const mockNetworkControllerDangerouslySetNetworkConfiguration = typedMockFn(
'NetworkController:dangerouslySetNetworkConfiguration',
'NetworkController:updateNetwork',
);

jest.spyOn(messenger, 'call').mockImplementation((...args) => {
Expand Down Expand Up @@ -254,7 +254,7 @@ export function mockUserStorageMessenger(
return mockNetworkControllerRemoveNetwork(...params);
}

if (actionType === 'NetworkController:dangerouslySetNetworkConfiguration') {
if (actionType === 'NetworkController:updateNetwork') {
const [, ...params] = typedArgs;
return mockNetworkControllerDangerouslySetNetworkConfiguration(...params);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import { RpcEndpointType } from '@metamask/network-controller';

import type {
NetworkConfiguration,
RemoteNetworkConfiguration,
} from '../types';

export type RPCEndpoint = NetworkConfiguration['rpcEndpoints'][number];

export const createMockNetworkConfiguration = (
override?: Partial<NetworkConfiguration>,
): NetworkConfiguration => {
return {
chainId: '0x1337',
blockExplorerUrls: [],
blockExplorerUrls: ['https://etherscan.io'],
defaultRpcEndpointIndex: 0,
name: 'Mock Network',
nativeCurrency: 'MOCK TOKEN',
Expand All @@ -27,3 +31,22 @@ export const createMockRemoteNetworkConfiguration = (
...override,
};
};

export const createMockCustomRpcEndpoint = (
override: Partial<Extract<RPCEndpoint, { type: RpcEndpointType.Custom }>>,
): RPCEndpoint => {
return {
type: RpcEndpointType.Custom,
networkClientId: '1111-1111-1111',
url: `https://FAKE_RPC/`,
...override,
} as RPCEndpoint;
};

export const createMockInfuraRpcEndpoint = (): RPCEndpoint => {
return {
type: RpcEndpointType.Infura,
networkClientId: 'mainnet',
url: `https://mainnet.infura.io/v3/{infuraProjectId}`,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,15 @@ describe('network-syncing/controller-integration - performMainSync()', () => {
it('should update local networks', async () => {
const { messenger, getStorageConfig, mockSync, mockServices, mockCalls } =
arrangeMocks();

mockCalls.mockNetworkControllerGetState.mockReturnValue({
networkConfigurationsByChainId: {
'0x1337': createMockNetworkConfiguration(),
},
selectedNetworkClientId: '1111-1111-1111',
networksMetadata: {},
});

mockSync.findNetworksToUpdate.mockReturnValue({
remoteNetworksToUpdate: [],
missingLocalNetworks: [],
Expand All @@ -348,9 +357,11 @@ describe('network-syncing/controller-integration - performMainSync()', () => {

expect(mockServices.mockBatchUpdateNetworks).not.toHaveBeenCalled();
expect(mockCalls.mockNetworkControllerAddNetwork).not.toHaveBeenCalled();
expect(
mockCalls.mockNetworkControllerDangerouslySetNetworkConfiguration,
).toHaveBeenCalled();
await waitFor(() =>
expect(
mockCalls.mockNetworkControllerDangerouslySetNetworkConfiguration,
).toHaveBeenCalled(),
);
expect(mockUpdateCallback).toHaveBeenCalledTimes(1);
expect(mockCalls.mockNetworkControllerRemoveNetwork).not.toHaveBeenCalled();
});
Expand Down Expand Up @@ -383,6 +394,15 @@ describe('network-syncing/controller-integration - performMainSync()', () => {
it('should handle multiple networks to update', async () => {
const { messenger, getStorageConfig, mockSync, mockServices, mockCalls } =
arrangeMocks();

mockCalls.mockNetworkControllerGetState.mockReturnValue({
networkConfigurationsByChainId: {
'0x1337': createMockNetworkConfiguration(),
},
selectedNetworkClientId: '1111-1111-1111',
networksMetadata: {},
});

mockSync.findNetworksToUpdate.mockReturnValue({
remoteNetworksToUpdate: [
createMockRemoteNetworkConfiguration(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { getAllRemoteNetworks } from './services';
import { findNetworksToUpdate } from './sync-all';
import { batchUpdateNetworks, deleteNetwork } from './sync-mutations';
import type { NetworkConfiguration } from './types';
import { createUpdateNetworkProps } from './update-network-utils';

type StartNetworkSyncingProps = {
messenger: UserStorageControllerMessenger;
Expand Down Expand Up @@ -120,6 +121,34 @@ export const getBoundedNetworksToAdd = (
.slice(0, numberOfNetworksToAppend);
};

export const dispatchUpdateNetwork = async (props: {
messenger: UserStorageControllerMessenger;
originalNetworkConfiguration: NetworkConfiguration;
newNetworkConfiguration: NetworkConfiguration;
selectedNetworkClientId: string;
}) => {
const {
messenger,
originalNetworkConfiguration,
newNetworkConfiguration,
selectedNetworkClientId,
} = props;

const { updateNetworkFields, newSelectedRpcEndpointIndex } =
createUpdateNetworkProps({
originalNetworkConfiguration,
newNetworkConfiguration,
selectedNetworkClientId,
});

await messenger.call(
'NetworkController:updateNetwork',
updateNetworkFields.chainId,
updateNetworkFields,
{ replacementSelectedRpcEndpointIndex: newSelectedRpcEndpointIndex },
);
};

/**
* Action to perform the main network sync.
* It will fetch local networks and remote networks, then determines which networks (local and remote) to add/update
Expand Down Expand Up @@ -213,10 +242,14 @@ export async function performMainNetworkSync(
const errors: unknown[] = [];
for (const n of networkChanges.localNetworksToUpdate) {
try {
await messenger.call(
'NetworkController:dangerouslySetNetworkConfiguration',
n,
);
await dispatchUpdateNetwork({
messenger,
originalNetworkConfiguration:
networkControllerState.networkConfigurationsByChainId[n.chainId],
newNetworkConfiguration: n,
selectedNetworkClientId:
networkControllerState.selectedNetworkClientId,
});
onNetworkUpdated?.(n.chainId);
} catch (e) {
/* istanbul ignore next - allocates logs, do not need to test */
Expand Down
Loading
Loading