2021-02-04 19:15:23 +01:00
|
|
|
import { strict as assert } from 'assert';
|
|
|
|
import sinon from 'sinon';
|
2021-03-16 22:00:08 +01:00
|
|
|
import { getNetworkDisplayName } from './util';
|
2021-06-25 18:24:00 +02:00
|
|
|
import NetworkController, { NETWORK_EVENTS } from './network';
|
2017-05-23 08:12:28 +02:00
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
describe('NetworkController', function () {
|
|
|
|
describe('controller', function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
let networkController;
|
2021-06-25 18:24:00 +02:00
|
|
|
let getLatestBlockStub;
|
|
|
|
let setProviderTypeAndWait;
|
2021-02-04 19:15:23 +01:00
|
|
|
const noop = () => undefined;
|
2020-02-11 17:51:13 +01:00
|
|
|
const networkControllerProviderConfig = {
|
|
|
|
getAccounts: noop,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2017-05-23 08:12:28 +02:00
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
beforeEach(function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
networkController = new NetworkController();
|
2021-06-25 18:24:00 +02:00
|
|
|
getLatestBlockStub = sinon
|
|
|
|
.stub(networkController, 'getLatestBlock')
|
|
|
|
.callsFake(() => Promise.resolve({}));
|
2021-02-04 19:15:23 +01:00
|
|
|
networkController.setInfuraProjectId('foo');
|
2021-06-25 18:24:00 +02:00
|
|
|
setProviderTypeAndWait = () =>
|
|
|
|
new Promise((resolve) => {
|
|
|
|
networkController.on(NETWORK_EVENTS.NETWORK_DID_CHANGE, () => {
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
networkController.setProviderType('mainnet');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
getLatestBlockStub.reset();
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-03-09 20:20:18 +01:00
|
|
|
|
2017-05-28 20:18:07 +02:00
|
|
|
describe('#provider', function () {
|
2018-05-25 00:53:06 +02:00
|
|
|
it('provider should be updatable without reassignment', function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
networkController.initializeProvider(networkControllerProviderConfig);
|
2020-11-03 00:41:28 +01:00
|
|
|
const providerProxy = networkController.getProviderAndBlockTracker()
|
2021-02-04 19:15:23 +01:00
|
|
|
.provider;
|
|
|
|
assert.equal(providerProxy.test, undefined);
|
|
|
|
providerProxy.setTarget({ test: true });
|
|
|
|
assert.equal(providerProxy.test, true);
|
|
|
|
});
|
|
|
|
});
|
2020-07-25 20:25:34 +02:00
|
|
|
|
2017-05-23 08:12:28 +02:00
|
|
|
describe('#getNetworkState', function () {
|
2020-07-28 00:31:10 +02:00
|
|
|
it('should return "loading" when new', function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
const networkState = networkController.getNetworkState();
|
|
|
|
assert.equal(networkState, 'loading', 'network is loading');
|
|
|
|
});
|
|
|
|
});
|
2017-05-23 08:12:28 +02:00
|
|
|
|
|
|
|
describe('#setNetworkState', function () {
|
|
|
|
it('should update the network', function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
networkController.setNetworkState('1');
|
|
|
|
const networkState = networkController.getNetworkState();
|
|
|
|
assert.equal(networkState, '1', 'network is 1');
|
|
|
|
});
|
|
|
|
});
|
2017-05-23 08:12:28 +02:00
|
|
|
|
|
|
|
describe('#setProviderType', function () {
|
|
|
|
it('should update provider.type', function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
networkController.initializeProvider(networkControllerProviderConfig);
|
|
|
|
networkController.setProviderType('mainnet');
|
|
|
|
const { type } = networkController.getProviderConfig();
|
|
|
|
assert.equal(type, 'mainnet', 'provider type is updated');
|
|
|
|
});
|
2020-10-06 19:57:02 +02:00
|
|
|
|
2017-05-23 08:12:28 +02:00
|
|
|
it('should set the network to loading', function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
networkController.initializeProvider(networkControllerProviderConfig);
|
2020-10-06 19:57:02 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const spy = sinon.spy(networkController, 'setNetworkState');
|
|
|
|
networkController.setProviderType('mainnet');
|
2020-10-06 19:57:02 +02:00
|
|
|
|
|
|
|
assert.equal(
|
2020-11-03 00:41:28 +01:00
|
|
|
spy.callCount,
|
|
|
|
1,
|
2020-10-06 19:57:02 +02:00
|
|
|
'should have called setNetworkState 2 times',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-10-06 19:57:02 +02:00
|
|
|
assert.ok(
|
|
|
|
spy.calledOnceWithExactly('loading'),
|
|
|
|
'should have called with "loading" first',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2021-06-25 18:24:00 +02:00
|
|
|
|
|
|
|
describe('#getEIP1559Compatibility', function () {
|
|
|
|
it('should return false when baseFeePerGas is not in the block header', async function () {
|
|
|
|
networkController.initializeProvider(networkControllerProviderConfig);
|
|
|
|
const supportsEIP1559 = await networkController.getEIP1559Compatibility();
|
|
|
|
assert.equal(supportsEIP1559, false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return true when baseFeePerGas is in block header', async function () {
|
|
|
|
networkController.initializeProvider(networkControllerProviderConfig);
|
|
|
|
getLatestBlockStub.callsFake(() =>
|
|
|
|
Promise.resolve({ baseFeePerGas: '0xa ' }),
|
|
|
|
);
|
|
|
|
const supportsEIP1559 = await networkController.getEIP1559Compatibility();
|
|
|
|
assert.equal(supportsEIP1559, true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should store EIP1559 support in state to reduce calls to getLatestBlock', async function () {
|
|
|
|
networkController.initializeProvider(networkControllerProviderConfig);
|
|
|
|
getLatestBlockStub.callsFake(() =>
|
|
|
|
Promise.resolve({ baseFeePerGas: '0xa ' }),
|
|
|
|
);
|
|
|
|
await networkController.getEIP1559Compatibility();
|
|
|
|
const supportsEIP1559 = await networkController.getEIP1559Compatibility();
|
|
|
|
assert.equal(getLatestBlockStub.calledOnce, true);
|
|
|
|
assert.equal(supportsEIP1559, true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should clear stored EIP1559 support when changing networks', async function () {
|
|
|
|
networkController.initializeProvider(networkControllerProviderConfig);
|
|
|
|
networkController.consoleThis = true;
|
|
|
|
getLatestBlockStub.callsFake(() =>
|
|
|
|
Promise.resolve({ baseFeePerGas: '0xa ' }),
|
|
|
|
);
|
|
|
|
await networkController.getEIP1559Compatibility();
|
|
|
|
assert.equal(
|
|
|
|
networkController.networkDetails.getState().EIPS[1559],
|
|
|
|
true,
|
|
|
|
);
|
|
|
|
getLatestBlockStub.callsFake(() => Promise.resolve({}));
|
|
|
|
await setProviderTypeAndWait('mainnet');
|
|
|
|
assert.equal(
|
|
|
|
networkController.networkDetails.getState().EIPS[1559],
|
|
|
|
undefined,
|
|
|
|
);
|
|
|
|
await networkController.getEIP1559Compatibility();
|
|
|
|
assert.equal(
|
|
|
|
networkController.networkDetails.getState().EIPS[1559],
|
|
|
|
false,
|
|
|
|
);
|
|
|
|
assert.equal(getLatestBlockStub.calledTwice, true);
|
|
|
|
});
|
|
|
|
});
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-04-12 23:17:36 +02:00
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
describe('utils', function () {
|
|
|
|
it('getNetworkDisplayName should return the correct network name', function () {
|
|
|
|
const tests = [
|
|
|
|
{
|
2020-05-20 17:57:45 +02:00
|
|
|
input: '3',
|
2020-02-11 17:51:13 +01:00
|
|
|
expected: 'Ropsten',
|
2020-11-03 00:41:28 +01:00
|
|
|
},
|
|
|
|
{
|
2020-05-20 17:57:45 +02:00
|
|
|
input: '4',
|
2020-02-11 17:51:13 +01:00
|
|
|
expected: 'Rinkeby',
|
2020-11-03 00:41:28 +01:00
|
|
|
},
|
|
|
|
{
|
2020-05-20 17:57:45 +02:00
|
|
|
input: '42',
|
|
|
|
expected: 'Kovan',
|
2020-11-03 00:41:28 +01:00
|
|
|
},
|
|
|
|
{
|
2020-05-20 17:57:45 +02:00
|
|
|
input: '0x3',
|
|
|
|
expected: 'Ropsten',
|
2020-11-03 00:41:28 +01:00
|
|
|
},
|
|
|
|
{
|
2020-05-20 17:57:45 +02:00
|
|
|
input: '0x4',
|
|
|
|
expected: 'Rinkeby',
|
2020-11-03 00:41:28 +01:00
|
|
|
},
|
|
|
|
{
|
2020-07-03 19:14:43 +02:00
|
|
|
input: '0x2a',
|
2020-02-11 17:51:13 +01:00
|
|
|
expected: 'Kovan',
|
2020-11-03 00:41:28 +01:00
|
|
|
},
|
|
|
|
{
|
2020-02-11 17:51:13 +01:00
|
|
|
input: 'ropsten',
|
|
|
|
expected: 'Ropsten',
|
2020-11-03 00:41:28 +01:00
|
|
|
},
|
|
|
|
{
|
2020-02-11 17:51:13 +01:00
|
|
|
input: 'rinkeby',
|
|
|
|
expected: 'Rinkeby',
|
2020-11-03 00:41:28 +01:00
|
|
|
},
|
|
|
|
{
|
2020-02-11 17:51:13 +01:00
|
|
|
input: 'kovan',
|
|
|
|
expected: 'Kovan',
|
2020-11-03 00:41:28 +01:00
|
|
|
},
|
|
|
|
{
|
2020-02-11 17:51:13 +01:00
|
|
|
input: 'mainnet',
|
2020-09-15 21:34:16 +02:00
|
|
|
expected: 'Ethereum Mainnet',
|
2020-11-03 00:41:28 +01:00
|
|
|
},
|
|
|
|
{
|
2020-02-11 17:51:13 +01:00
|
|
|
input: 'goerli',
|
|
|
|
expected: 'Goerli',
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
];
|
2018-04-12 23:17:36 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
tests.forEach(({ input, expected }) =>
|
|
|
|
assert.equal(getNetworkDisplayName(input), expected),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|