1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-22 09:57:02 +01:00

Sync NetworkController getEIP1559Compatibility tests w/ core (#19419)

This makes it easier to visually compare differences in the
NetworkController unit tests between core and this repo.
This commit is contained in:
Elliot Winkler 2023-06-14 08:35:43 -07:00 committed by GitHub
parent 312dadf91f
commit 7701b8b417
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -722,204 +722,271 @@ describe('NetworkController', () => {
}); });
describe('getEIP1559Compatibility', () => { describe('getEIP1559Compatibility', () => {
describe('when the latest block has a baseFeePerGas property', () => { describe('if no provider has been set yet', () => {
it('stores the fact that the network supports EIP-1559', async () => { it('does not make any state changes', async () => {
await withController(
{
state: {
networkDetails: {
EIPS: {},
},
},
},
async ({ controller }) => {
const fakeProvider = buildFakeProvider([
{
request: {
method: 'eth_getBlockByNumber',
},
response: {
result: POST_1559_BLOCK,
},
},
]);
const fakeNetworkClient = buildFakeClient(fakeProvider);
mockCreateNetworkClient().mockReturnValue(fakeNetworkClient);
await controller.initializeProvider();
await controller.getEIP1559Compatibility();
expect(controller.store.getState().networkDetails.EIPS[1559]).toBe(
true,
);
},
);
});
it('returns true', async () => {
await withController(async ({ controller }) => { await withController(async ({ controller }) => {
const fakeProvider = buildFakeProvider([ const promiseForNoStateChanges = waitForStateChanges({
{ controller,
request: { count: 0,
method: 'eth_getBlockByNumber', operation: async () => {
}, await controller.getEIP1559Compatibility();
response: {
result: POST_1559_BLOCK,
},
}, },
]); });
const fakeNetworkClient = buildFakeClient(fakeProvider);
mockCreateNetworkClient().mockReturnValue(fakeNetworkClient);
await controller.initializeProvider();
const supportsEIP1559 = await controller.getEIP1559Compatibility(); expect(Boolean(promiseForNoStateChanges)).toBe(true);
expect(supportsEIP1559).toBeTruthy();
}); });
}); });
});
describe('when the latest block does not have a baseFeePerGas property', () => {
it('stores the fact that the network does not support EIP-1559', async () => {
await withController(
{
state: {
networkDetails: {
EIPS: {},
},
},
},
async ({ controller }) => {
const fakeProvider = buildFakeProvider([
{
request: {
method: 'eth_getBlockByNumber',
},
response: {
result: PRE_1559_BLOCK,
},
},
]);
const fakeNetworkClient = buildFakeClient(fakeProvider);
mockCreateNetworkClient().mockReturnValue(fakeNetworkClient);
await controller.initializeProvider();
await controller.getEIP1559Compatibility();
expect(controller.store.getState().networkDetails.EIPS[1559]).toBe(
false,
);
},
);
});
it('returns false', async () => { it('returns false', async () => {
await withController(async ({ controller }) => { await withController(async ({ controller }) => {
const fakeProvider = buildFakeProvider([ const isEIP1559Compatible =
{ await controller.getEIP1559Compatibility();
request: {
method: 'eth_getBlockByNumber',
},
response: {
result: PRE_1559_BLOCK,
},
},
]);
const fakeNetworkClient = buildFakeClient(fakeProvider);
mockCreateNetworkClient().mockReturnValue(fakeNetworkClient);
await controller.initializeProvider();
const supportsEIP1559 = await controller.getEIP1559Compatibility(); expect(isEIP1559Compatible).toBe(false);
expect(supportsEIP1559).toBe(false);
}); });
}); });
}); });
describe('when the request for the latest block responds with null', () => { describe('if a provider has been set but networkDetails.EIPS in state already has a "1559" property', () => {
it('persists false to state as whether the network supports EIP-1559', async () => { it('does not make any state changes', async () => {
await withController( await withController(
{ {
state: { state: {
networkDetails: { networkDetails: {
EIPS: {}, EIPS: {
1559: true,
},
}, },
}, },
}, },
async ({ controller }) => { async ({ controller }) => {
const fakeProvider = buildFakeProvider([ setFakeProvider(controller, {
{ stubLookupNetworkWhileSetting: true,
request: { });
method: 'eth_getBlockByNumber', const promiseForNoStateChanges = waitForStateChanges({
}, controller,
response: { count: 0,
result: null, operation: async () => {
}, await controller.getEIP1559Compatibility();
}, },
]); });
const fakeNetworkClient = buildFakeClient(fakeProvider);
mockCreateNetworkClient().mockReturnValue(fakeNetworkClient);
await controller.initializeProvider();
await controller.getEIP1559Compatibility(); expect(Boolean(promiseForNoStateChanges)).toBe(true);
expect(controller.store.getState().networkDetails.EIPS[1559]).toBe(
false,
);
}, },
); );
}); });
it('returns false', async () => { it('returns the value of the "1559" property', async () => {
await withController(async ({ controller }) => { await withController(
const fakeProvider = buildFakeProvider([ {
{ state: {
request: { networkDetails: {
method: 'eth_getBlockByNumber', EIPS: {
}, 1559: true,
response: { },
result: null,
}, },
}, },
]); },
const fakeNetworkClient = buildFakeClient(fakeProvider); async ({ controller }) => {
mockCreateNetworkClient().mockReturnValue(fakeNetworkClient); setFakeProvider(controller, {
await controller.initializeProvider(); stubLookupNetworkWhileSetting: true,
});
const isEIP1559Compatible =
await controller.getEIP1559Compatibility();
const supportsEIP1559 = await controller.getEIP1559Compatibility(); expect(isEIP1559Compatible).toBe(true);
},
expect(supportsEIP1559).toBe(false); );
});
}); });
}); });
it('does not make multiple requests to eth_getBlockByNumber when called multiple times and the request to eth_getBlockByNumber succeeded the first time', async () => { describe('if a provider has been set and networkDetails.EIPS in state does not already have a "1559" property', () => {
await withController(async ({ controller }) => { describe('if the request for the latest block is successful', () => {
const fakeProvider = buildFakeProvider([ describe('if the latest block has a "baseFeePerGas" property', () => {
{ it('sets the "1559" property to true', async () => {
request: { await withController(async ({ controller }) => {
method: 'eth_getBlockByNumber', setFakeProvider(controller, {
}, stubs: [
response: SUCCESSFUL_ETH_GET_BLOCK_BY_NUMBER_RESPONSE, {
}, request: {
]); method: 'eth_getBlockByNumber',
const fakeNetworkClient = buildFakeClient(fakeProvider); params: ['latest', false],
mockCreateNetworkClient().mockReturnValue(fakeNetworkClient); },
await withoutCallingGetEIP1559Compatibility({ response: {
controller, result: POST_1559_BLOCK,
operation: async () => { },
await controller.initializeProvider(); },
}, ],
stubLookupNetworkWhileSetting: true,
});
await controller.getEIP1559Compatibility();
expect(
controller.store.getState().networkDetails.EIPS[1559],
).toBe(true);
});
});
it('returns true', async () => {
await withController(async ({ controller }) => {
setFakeProvider(controller, {
stubs: [
{
request: {
method: 'eth_getBlockByNumber',
params: ['latest', false],
},
response: {
result: POST_1559_BLOCK,
},
},
],
stubLookupNetworkWhileSetting: true,
});
const isEIP1559Compatible =
await controller.getEIP1559Compatibility();
expect(isEIP1559Compatible).toBe(true);
});
});
}); });
await controller.getEIP1559Compatibility(); describe('if the latest block does not have a "baseFeePerGas" property', () => {
await controller.getEIP1559Compatibility(); it('sets the "1559" property to false', async () => {
await withController(async ({ controller }) => {
setFakeProvider(controller, {
stubs: [
{
request: {
method: 'eth_getBlockByNumber',
params: ['latest', false],
},
response: {
result: PRE_1559_BLOCK,
},
},
],
stubLookupNetworkWhileSetting: true,
});
expect( await controller.getEIP1559Compatibility();
fakeProvider.calledStubs.filter(
(stub) => stub.request.method === 'eth_getBlockByNumber', expect(
), controller.store.getState().networkDetails.EIPS[1559],
).toHaveLength(1); ).toBe(false);
});
});
it('returns false', async () => {
await withController(async ({ controller }) => {
setFakeProvider(controller, {
stubs: [
{
request: {
method: 'eth_getBlockByNumber',
params: ['latest', false],
},
response: {
result: PRE_1559_BLOCK,
},
},
],
stubLookupNetworkWhileSetting: true,
});
const isEIP1559Compatible =
await controller.getEIP1559Compatibility();
expect(isEIP1559Compatible).toBe(false);
});
});
});
describe('if the request for the latest block responds with null', () => {
it('sets the "1559" property to false', async () => {
await withController(async ({ controller }) => {
setFakeProvider(controller, {
stubs: [
{
request: {
method: 'eth_getBlockByNumber',
params: ['latest', false],
},
response: {
result: null,
},
},
],
stubLookupNetworkWhileSetting: true,
});
await controller.getEIP1559Compatibility();
expect(
controller.store.getState().networkDetails.EIPS[1559],
).toBe(false);
});
});
it('returns false', async () => {
await withController(async ({ controller }) => {
setFakeProvider(controller, {
stubs: [
{
request: {
method: 'eth_getBlockByNumber',
params: ['latest', false],
},
response: {
result: null,
},
},
],
stubLookupNetworkWhileSetting: true,
});
const isEIP1559Compatible =
await controller.getEIP1559Compatibility();
expect(isEIP1559Compatible).toBe(false);
});
});
});
});
describe('if the request for the latest block is unsuccessful', () => {
it('does not make any state changes', async () => {
await withController(async ({ controller }) => {
setFakeProvider(controller, {
stubs: [
{
request: {
method: 'eth_getBlockByNumber',
params: ['latest', false],
},
error: GENERIC_JSON_RPC_ERROR,
},
],
stubLookupNetworkWhileSetting: true,
});
const promiseForNoStateChanges = waitForStateChanges({
controller,
count: 0,
operation: async () => {
try {
await controller.getEIP1559Compatibility();
} catch (error) {
// ignore error
}
},
});
expect(Boolean(promiseForNoStateChanges)).toBe(true);
});
});
}); });
}); });
}); });
@ -6463,33 +6530,6 @@ async function withoutCallingLookupNetwork({
spy.mockRestore(); spy.mockRestore();
} }
/**
* For each kind of way that the provider can be set, `getEIP1559Compatibility`
* is always called. This can cause difficulty when testing the behavior of
* `getEIP1559Compatibility` itself, as extra requests then have to be
* mocked. This function takes a function that presumably sets the provider,
* stubbing `getEIP1559Compatibility` before the function and releasing the stub
* afterward.
*
* @param args - The arguments.
* @param args.controller - The network controller.
* @param args.operation - The function that presumably involves
* `getEIP1559Compatibility`.
*/
async function withoutCallingGetEIP1559Compatibility({
controller,
operation,
}: {
controller: NetworkController;
operation: () => void | Promise<void>;
}) {
const spy = jest
.spyOn(controller, 'getEIP1559Compatibility')
.mockResolvedValue(false);
await operation();
spy.mockRestore();
}
/** /**
* Waits for changes to the primary observable store of a controller to occur * Waits for changes to the primary observable store of a controller to occur
* before proceeding. May be called with a function, in which case waiting will * before proceeding. May be called with a function, in which case waiting will
@ -6534,7 +6574,7 @@ async function waitForStateChanges({
}, },
}: { }: {
controller: NetworkController; controller: NetworkController;
propertyPath: string[]; propertyPath?: string[];
count?: number | null; count?: number | null;
duration?: number; duration?: number;
operation?: () => void | Promise<void>; operation?: () => void | Promise<void>;
@ -6614,11 +6654,16 @@ async function waitForStateChanges({
}; };
eventListener = (newState) => { eventListener = (newState) => {
const isInteresting = isStateChangeInteresting( const isInteresting =
newState, propertyPath === undefined
allStates.length > 0 ? allStates[allStates.length - 1] : initialState, ? true
propertyPath, : isStateChangeInteresting(
); newState,
allStates.length > 0
? allStates[allStates.length - 1]
: initialState,
propertyPath,
);
allStates.push({ ...newState }); allStates.push({ ...newState });