Skip to content

Commit

Permalink
Merge branch 'main' into mrtenz/permitted-handler-type
Browse files Browse the repository at this point in the history
  • Loading branch information
legobeat authored Apr 19, 2023
2 parents 2c37d50 + 6844706 commit a785e17
Showing 1 changed file with 5 additions and 259 deletions.
264 changes: 5 additions & 259 deletions packages/network-controller/tests/NetworkController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1349,267 +1349,13 @@ describe('NetworkController', () => {
});

describe('setProviderType', () => {
describe('given a network type of "mainnet"', () => {
it('updates the provider config in state with the network type and the corresponding chain ID, using "ETH" for the ticker and clearing any existing RPC target and nickname', async () => {
const messenger = buildMessenger();
await withController(
{
messenger,
state: {
providerConfig: {
type: NetworkType.localhost,
rpcTarget: 'http://somethingexisting.com',
chainId: '99999',
ticker: 'something existing',
nickname: 'something existing',
},
},
},
async ({ controller }) => {
const fakeInfuraProvider = buildFakeInfuraProvider();
createInfuraProviderMock.mockReturnValue(fakeInfuraProvider);
const fakeInfuraSubprovider = buildFakeInfuraSubprovider();
SubproviderMock.mockReturnValue(fakeInfuraSubprovider);
const fakeMetamaskProvider = buildFakeMetamaskProvider();
createMetamaskProviderMock.mockReturnValue(fakeMetamaskProvider);

await controller.setProviderType(NetworkType.mainnet);

expect(controller.state.providerConfig).toStrictEqual({
type: NetworkType.mainnet,
...BUILT_IN_NETWORKS.mainnet,
rpcTarget: undefined,
nickname: undefined,
id: undefined,
});
},
);
});

it('sets the provider to an Infura provider pointed to Mainnet', async () => {
await withController(
{
infuraProjectId: 'infura-project-id',
},
async ({ controller }) => {
const fakeInfuraProvider = buildFakeInfuraProvider();
createInfuraProviderMock.mockReturnValue(fakeInfuraProvider);
const fakeInfuraSubprovider = buildFakeInfuraSubprovider();
SubproviderMock.mockReturnValue(fakeInfuraSubprovider);
const fakeMetamaskProvider = buildFakeMetamaskProvider([
{
request: {
method: 'eth_chainId',
},
response: {
result: '0x1337',
},
},
]);
createMetamaskProviderMock.mockReturnValue(fakeMetamaskProvider);

await controller.setProviderType(NetworkType.mainnet);

expect(createInfuraProviderMock).toHaveBeenCalledWith({
network: NetworkType.mainnet,
projectId: 'infura-project-id',
});
expect(createMetamaskProviderMock).toHaveBeenCalledWith({
dataSubprovider: fakeInfuraSubprovider,
engineParams: {
blockTrackerProvider: fakeInfuraProvider,
pollingInterval: 12000,
},
});
const { provider } = controller.getProviderAndBlockTracker();
const promisifiedSendAsync = promisify(provider.sendAsync).bind(
provider,
);
const chainIdResult = await promisifiedSendAsync({
method: 'eth_chainId',
});
expect(chainIdResult.result).toBe('0x1337');
},
);
});

it('updates networkDetails.isEIP1559Compatible in state based on the latest block (assuming that the request for eth_getBlockByNumber is made successfully)', async () => {
const messenger = buildMessenger();
await withController(
{
messenger,
},
async ({ controller }) => {
const fakeInfuraProvider = buildFakeInfuraProvider();
createInfuraProviderMock.mockReturnValue(fakeInfuraProvider);
const fakeInfuraSubprovider = buildFakeInfuraSubprovider();
SubproviderMock.mockReturnValue(fakeInfuraSubprovider);
const fakeMetamaskProvider = buildFakeMetamaskProvider([
{
request: {
method: 'eth_getBlockByNumber',
params: ['latest', false],
},
response: {
result: {
baseFeePerGas: '0x1',
},
},
},
]);
createMetamaskProviderMock.mockReturnValue(fakeMetamaskProvider);

await controller.setProviderType(NetworkType.mainnet);

expect(controller.state.networkDetails.isEIP1559Compatible).toBe(
true,
);
},
);
});

it('ensures that the existing provider is stopped while replacing it', async () => {
await withController(async ({ controller }) => {
const fakeInfuraProvider = buildFakeInfuraProvider();
createInfuraProviderMock.mockReturnValue(fakeInfuraProvider);
const fakeInfuraSubprovider = buildFakeInfuraSubprovider();
SubproviderMock.mockReturnValue(fakeInfuraSubprovider);
const fakeMetamaskProviders = [
buildFakeMetamaskProvider(),
buildFakeMetamaskProvider(),
];
jest.spyOn(fakeMetamaskProviders[0], 'stop');
createMetamaskProviderMock
.mockImplementationOnce(() => fakeMetamaskProviders[0])
.mockImplementationOnce(() => fakeMetamaskProviders[1]);

await controller.setProviderType(NetworkType.mainnet);
await controller.setProviderType(NetworkType.mainnet);
assert(controller.getProviderAndBlockTracker().provider);
jest.runAllTimers();

expect(fakeMetamaskProviders[0].stop).toHaveBeenCalled();
});
});

it('records the version of the current network in state (assuming that the request for net_version is made successfully)', async () => {
const messenger = buildMessenger();
await withController({ messenger }, async ({ controller }) => {
const fakeInfuraProvider = buildFakeInfuraProvider();
createInfuraProviderMock.mockReturnValue(fakeInfuraProvider);
const fakeInfuraSubprovider = buildFakeInfuraSubprovider();
SubproviderMock.mockReturnValue(fakeInfuraSubprovider);
const fakeMetamaskProvider = buildFakeMetamaskProvider([
{
request: {
method: 'net_version',
params: [],
},
response: {
result: '42',
},
},
]);
createMetamaskProviderMock.mockReturnValue(fakeMetamaskProvider);

await controller.setProviderType(NetworkType.mainnet);

expect(controller.state.networkId).toBe('42');
});
});

describe('when an "error" event occurs on the new provider', () => {
describe('if the network version could not be retrieved during the call to setProviderType', () => {
it('retrieves the network version again and, assuming success, persists it to state', async () => {
const messenger = buildMessenger();
await withController({ messenger }, async ({ controller }) => {
const fakeInfuraProvider = buildFakeInfuraProvider();
createInfuraProviderMock.mockReturnValue(fakeInfuraProvider);
const fakeInfuraSubprovider = buildFakeInfuraSubprovider();
SubproviderMock.mockReturnValue(fakeInfuraSubprovider);
const fakeMetamaskProvider = buildFakeMetamaskProvider([
{
request: {
method: 'net_version',
},
response: {
error: 'oops',
},
},
{
request: {
method: 'net_version',
},
response: {
result: '42',
},
},
]);
createMetamaskProviderMock.mockReturnValue(fakeMetamaskProvider);

await controller.setProviderType(NetworkType.mainnet);

await waitForStateChanges(messenger, {
propertyPath: ['networkId'],
produceStateChanges: () => {
controller
.getProviderAndBlockTracker()
.provider.emit('error', { some: 'error' });
},
});
expect(controller.state.networkId).toBe('42');
});
});
});

describe('if the network version could be retrieved during the call to setProviderType', () => {
it('does not retrieve the network version again', async () => {
const messenger = buildMessenger();
await withController({ messenger }, async ({ controller }) => {
const fakeInfuraProvider = buildFakeInfuraProvider();
createInfuraProviderMock.mockReturnValue(fakeInfuraProvider);
const fakeInfuraSubprovider = buildFakeInfuraSubprovider();
SubproviderMock.mockReturnValue(fakeInfuraSubprovider);
const fakeMetamaskProvider = buildFakeMetamaskProvider([
{
request: {
method: 'net_version',
},
response: {
result: '1',
},
},
{
request: {
method: 'net_version',
},
response: {
result: '2',
},
},
]);
createMetamaskProviderMock.mockReturnValue(fakeMetamaskProvider);

await controller.setProviderType(NetworkType.mainnet);

await waitForStateChanges(messenger, {
propertyPath: ['networkId'],
count: 0,
produceStateChanges: () => {
controller
.getProviderAndBlockTracker()
.provider.emit('error', { some: 'error' });
},
});
expect(controller.state.networkId).toBe('1');
});
});
});
});
});

(
[
{
networkType: NetworkType.mainnet,
ticker: NetworksTicker.mainnet,
chainId: NetworksChainId.mainnet,
},
{
networkType: NetworkType.goerli,
ticker: NetworksTicker.goerli,
Expand Down

0 comments on commit a785e17

Please sign in to comment.