-
-
Notifications
You must be signed in to change notification settings - Fork 249
chore(network-controller): Speed up network client tests #6377
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
...network-controller/src/create-network-client/ethereum-spec/block-hash-in-response.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { testsForRpcMethodsThatCheckForBlockHashInResponse } from '../../../tests/network-client/block-hash-in-response'; | ||
import { NetworkClientType } from '../../types'; | ||
|
||
describe('createNetworkClient - methods included in the Ethereum JSON-RPC spec - methods with block hashes in their result', () => { | ||
for (const networkClientType of [ | ||
NetworkClientType.Infura, | ||
NetworkClientType.Custom, | ||
]) { | ||
const methodsWithBlockHashInResponse = [ | ||
{ name: 'eth_getTransactionByHash', numberOfParameters: 1 }, | ||
{ name: 'eth_getTransactionReceipt', numberOfParameters: 1 }, | ||
]; | ||
methodsWithBlockHashInResponse.forEach(({ name, numberOfParameters }) => { | ||
describe(`method name: ${name}`, () => { | ||
testsForRpcMethodsThatCheckForBlockHashInResponse(name, { | ||
numberOfParameters, | ||
providerType: networkClientType, | ||
}); | ||
}); | ||
}); | ||
} | ||
}); |
49 changes: 49 additions & 0 deletions
49
packages/network-controller/src/create-network-client/ethereum-spec/block-param.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { testsForRpcMethodSupportingBlockParam } from '../../../tests/network-client/block-param'; | ||
import { NetworkClientType } from '../../types'; | ||
|
||
describe('createNetworkClient - methods included in the Ethereum JSON-RPC spec - methods that have a param to specify the block', () => { | ||
for (const networkClientType of [ | ||
NetworkClientType.Infura, | ||
NetworkClientType.Custom, | ||
]) { | ||
const supportingBlockParam = [ | ||
{ | ||
name: 'eth_call', | ||
blockParamIndex: 1, | ||
numberOfParameters: 2, | ||
}, | ||
{ | ||
name: 'eth_getBalance', | ||
blockParamIndex: 1, | ||
numberOfParameters: 2, | ||
}, | ||
{ | ||
name: 'eth_getBlockByNumber', | ||
blockParamIndex: 0, | ||
numberOfParameters: 2, | ||
}, | ||
{ name: 'eth_getCode', blockParamIndex: 1, numberOfParameters: 2 }, | ||
{ | ||
name: 'eth_getStorageAt', | ||
blockParamIndex: 2, | ||
numberOfParameters: 3, | ||
}, | ||
{ | ||
name: 'eth_getTransactionCount', | ||
blockParamIndex: 1, | ||
numberOfParameters: 2, | ||
}, | ||
]; | ||
supportingBlockParam.forEach( | ||
({ name, blockParamIndex, numberOfParameters }) => { | ||
describe(`method name: ${name}`, () => { | ||
testsForRpcMethodSupportingBlockParam(name, { | ||
providerType: networkClientType, | ||
blockParamIndex, | ||
numberOfParameters, | ||
}); | ||
}); | ||
}, | ||
); | ||
} | ||
}); |
49 changes: 49 additions & 0 deletions
49
packages/network-controller/src/create-network-client/ethereum-spec/no-block-param.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { testsForRpcMethodAssumingNoBlockParam } from '../../../tests/network-client/no-block-param'; | ||
import { NetworkClientType } from '../../types'; | ||
|
||
describe('createNetworkClient - methods included in the Ethereum JSON-RPC spec - methods that assume there is no block param', () => { | ||
for (const networkClientType of [ | ||
NetworkClientType.Infura, | ||
NetworkClientType.Custom, | ||
]) { | ||
const assumingNoBlockParam = [ | ||
{ name: 'eth_getFilterLogs', numberOfParameters: 1 }, | ||
{ name: 'eth_blockNumber', numberOfParameters: 0 }, | ||
{ name: 'eth_estimateGas', numberOfParameters: 2 }, | ||
{ name: 'eth_gasPrice', numberOfParameters: 0 }, | ||
{ name: 'eth_getBlockByHash', numberOfParameters: 2 }, | ||
{ | ||
name: 'eth_getBlockTransactionCountByHash', | ||
numberOfParameters: 1, | ||
}, | ||
{ | ||
name: 'eth_getTransactionByBlockHashAndIndex', | ||
numberOfParameters: 2, | ||
}, | ||
{ name: 'eth_getUncleByBlockHashAndIndex', numberOfParameters: 2 }, | ||
{ name: 'eth_getUncleCountByBlockHash', numberOfParameters: 1 }, | ||
]; | ||
const blockParamIgnored = [ | ||
{ name: 'eth_getUncleCountByBlockNumber', numberOfParameters: 1 }, | ||
{ name: 'eth_getUncleByBlockNumberAndIndex', numberOfParameters: 2 }, | ||
{ | ||
name: 'eth_getTransactionByBlockNumberAndIndex', | ||
numberOfParameters: 2, | ||
}, | ||
{ | ||
name: 'eth_getBlockTransactionCountByNumber', | ||
numberOfParameters: 1, | ||
}, | ||
]; | ||
assumingNoBlockParam | ||
.concat(blockParamIgnored) | ||
.forEach(({ name, numberOfParameters }) => | ||
describe(`method name: ${name}`, () => { | ||
testsForRpcMethodAssumingNoBlockParam(name, { | ||
providerType: networkClientType, | ||
numberOfParameters, | ||
}); | ||
}), | ||
); | ||
} | ||
}); |
51 changes: 51 additions & 0 deletions
51
...work-controller/src/create-network-client/ethereum-spec/not-handled-by-middleware.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { testsForRpcMethodNotHandledByMiddleware } from '../../../tests/network-client/not-handled-by-middleware'; | ||
import { NetworkClientType } from '../../types'; | ||
|
||
describe('createNetworkClient - methods included in the Ethereum JSON-RPC spec - methods not handled by middleware', () => { | ||
for (const networkClientType of [ | ||
NetworkClientType.Infura, | ||
NetworkClientType.Custom, | ||
]) { | ||
const notHandledByMiddleware = [ | ||
{ name: 'eth_newFilter', numberOfParameters: 1 }, | ||
{ name: 'eth_getFilterChanges', numberOfParameters: 1 }, | ||
{ name: 'eth_newBlockFilter', numberOfParameters: 0 }, | ||
{ name: 'eth_newPendingTransactionFilter', numberOfParameters: 0 }, | ||
{ name: 'eth_uninstallFilter', numberOfParameters: 1 }, | ||
|
||
{ name: 'eth_sendRawTransaction', numberOfParameters: 1 }, | ||
{ name: 'eth_sendTransaction', numberOfParameters: 1 }, | ||
{ name: 'eth_createAccessList', numberOfParameters: 2 }, | ||
{ name: 'eth_getLogs', numberOfParameters: 1 }, | ||
{ name: 'eth_getProof', numberOfParameters: 3 }, | ||
{ name: 'eth_getWork', numberOfParameters: 0 }, | ||
{ name: 'eth_maxPriorityFeePerGas', numberOfParameters: 0 }, | ||
{ name: 'eth_submitHashRate', numberOfParameters: 2 }, | ||
{ name: 'eth_submitWork', numberOfParameters: 3 }, | ||
{ name: 'eth_syncing', numberOfParameters: 0 }, | ||
{ name: 'eth_feeHistory', numberOfParameters: 3 }, | ||
{ name: 'debug_getRawHeader', numberOfParameters: 1 }, | ||
{ name: 'debug_getRawBlock', numberOfParameters: 1 }, | ||
{ name: 'debug_getRawTransaction', numberOfParameters: 1 }, | ||
{ name: 'debug_getRawReceipts', numberOfParameters: 1 }, | ||
{ name: 'debug_getBadBlocks', numberOfParameters: 0 }, | ||
|
||
{ name: 'eth_accounts', numberOfParameters: 0 }, | ||
{ name: 'eth_coinbase', numberOfParameters: 0 }, | ||
{ name: 'eth_hashrate', numberOfParameters: 0 }, | ||
{ name: 'eth_mining', numberOfParameters: 0 }, | ||
|
||
{ name: 'eth_signTransaction', numberOfParameters: 1 }, | ||
]; | ||
notHandledByMiddleware.forEach(({ name, numberOfParameters }) => { | ||
// This is a valid title. | ||
// eslint-disable-next-line jest/valid-title | ||
describe(name, () => { | ||
testsForRpcMethodNotHandledByMiddleware(name, { | ||
providerType: networkClientType, | ||
numberOfParameters, | ||
}); | ||
}); | ||
}); | ||
} | ||
}); |
93 changes: 93 additions & 0 deletions
93
packages/network-controller/src/create-network-client/ethereum-spec/other-methods.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { | ||
withMockedCommunications, | ||
withNetworkClient, | ||
} from '../../../tests/network-client/helpers'; | ||
import { NetworkClientType } from '../../types'; | ||
|
||
describe('createNetworkClient - methods included in the Ethereum JSON-RPC spec - other methods', () => { | ||
for (const networkClientType of [ | ||
NetworkClientType.Infura, | ||
NetworkClientType.Custom, | ||
]) { | ||
describe('eth_getTransactionByHash', () => { | ||
it("refreshes the block tracker's current block if it is less than the block number that comes back in the response", async () => { | ||
const method = 'eth_getTransactionByHash'; | ||
|
||
await withMockedCommunications( | ||
{ providerType: networkClientType }, | ||
async (comms) => { | ||
const request = { method }; | ||
|
||
comms.mockNextBlockTrackerRequest({ blockNumber: '0x100' }); | ||
// This is our request. | ||
comms.mockRpcCall({ | ||
request, | ||
response: { | ||
result: { | ||
blockNumber: '0x200', | ||
}, | ||
}, | ||
}); | ||
comms.mockNextBlockTrackerRequest({ blockNumber: '0x300' }); | ||
|
||
await withNetworkClient( | ||
{ providerType: networkClientType }, | ||
async ({ makeRpcCall, blockTracker }) => { | ||
await makeRpcCall(request); | ||
expect(blockTracker.getCurrentBlock()).toBe('0x300'); | ||
}, | ||
); | ||
}, | ||
); | ||
}); | ||
}); | ||
|
||
describe('eth_getTransactionReceipt', () => { | ||
it("refreshes the block tracker's current block if it is less than the block number that comes back in the response", async () => { | ||
const method = 'eth_getTransactionReceipt'; | ||
|
||
await withMockedCommunications( | ||
{ providerType: networkClientType }, | ||
async (comms) => { | ||
const request = { method }; | ||
|
||
comms.mockNextBlockTrackerRequest({ blockNumber: '0x100' }); | ||
// This is our request. | ||
comms.mockRpcCall({ | ||
request, | ||
response: { | ||
result: { | ||
blockNumber: '0x200', | ||
}, | ||
}, | ||
}); | ||
comms.mockNextBlockTrackerRequest({ blockNumber: '0x300' }); | ||
|
||
await withNetworkClient( | ||
{ providerType: networkClientType }, | ||
async ({ makeRpcCall, blockTracker }) => { | ||
await makeRpcCall(request); | ||
expect(blockTracker.getCurrentBlock()).toBe('0x300'); | ||
}, | ||
); | ||
}, | ||
); | ||
}); | ||
}); | ||
|
||
if (networkClientType === NetworkClientType.Custom) { | ||
describe('eth_chainId', () => { | ||
it('does not hit the RPC endpoint, instead returning the configured chain id', async () => { | ||
const chainId = await withNetworkClient( | ||
{ providerType: networkClientType, customChainId: '0x1' }, | ||
({ makeRpcCall }) => { | ||
return makeRpcCall({ method: 'eth_chainId' }); | ||
}, | ||
); | ||
|
||
expect(chainId).toBe('0x1'); | ||
}); | ||
}); | ||
} | ||
} | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); |
22 changes: 22 additions & 0 deletions
22
...ages/network-controller/src/create-network-client/ex-ethereum-spec/no-block-param.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { testsForRpcMethodAssumingNoBlockParam } from '../../../tests/network-client/no-block-param'; | ||
import { NetworkClientType } from '../../types'; | ||
|
||
describe('createNetworkClient - methods not included in the Ethereum JSON-RPC spec - methods that assume there is no block param', () => { | ||
for (const networkClientType of [ | ||
NetworkClientType.Infura, | ||
NetworkClientType.Custom, | ||
]) { | ||
const assumingNoBlockParam = [ | ||
{ name: 'web3_clientVersion', numberOfParameters: 0 }, | ||
{ name: 'eth_protocolVersion', numberOfParameters: 0 }, | ||
]; | ||
assumingNoBlockParam.forEach(({ name, numberOfParameters }) => | ||
describe(`method name: ${name}`, () => { | ||
testsForRpcMethodAssumingNoBlockParam(name, { | ||
providerType: networkClientType, | ||
numberOfParameters, | ||
}); | ||
}), | ||
); | ||
} | ||
}); |
26 changes: 26 additions & 0 deletions
26
...k-controller/src/create-network-client/ex-ethereum-spec/not-handled-by-middleware.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { testsForRpcMethodNotHandledByMiddleware } from '../../../tests/network-client/not-handled-by-middleware'; | ||
import { NetworkClientType } from '../../types'; | ||
|
||
describe('createNetworkClient - methods not included in the Ethereum JSON-RPC spec - methods not handled by middleware', () => { | ||
for (const networkClientType of [ | ||
NetworkClientType.Infura, | ||
NetworkClientType.Custom, | ||
]) { | ||
const notHandledByMiddleware = [ | ||
{ name: 'net_listening', numberOfParameters: 0 }, | ||
{ name: 'eth_subscribe', numberOfParameters: 1 }, | ||
{ name: 'eth_unsubscribe', numberOfParameters: 1 }, | ||
{ name: 'custom_rpc_method', numberOfParameters: 1 }, | ||
{ name: 'net_peerCount', numberOfParameters: 0 }, | ||
{ name: 'parity_nextNonce', numberOfParameters: 1 }, | ||
]; | ||
notHandledByMiddleware.forEach(({ name, numberOfParameters }) => { | ||
describe(`method name: ${name}`, () => { | ||
testsForRpcMethodNotHandledByMiddleware(name, { | ||
providerType: networkClientType, | ||
numberOfParameters, | ||
}); | ||
}); | ||
}); | ||
} | ||
}); |
43 changes: 43 additions & 0 deletions
43
packages/network-controller/src/create-network-client/ex-ethereum-spec/other-methods.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { TESTNET } from '../../../tests/helpers'; | ||
import { | ||
withMockedCommunications, | ||
withNetworkClient, | ||
} from '../../../tests/network-client/helpers'; | ||
import { NetworkClientType } from '../../types'; | ||
|
||
describe('createNetworkClient - methods not included in the Ethereum JSON-RPC spec - other methods', () => { | ||
for (const networkClientType of [ | ||
NetworkClientType.Infura, | ||
NetworkClientType.Custom, | ||
]) { | ||
describe('net_version', () => { | ||
const networkArgs = { | ||
providerType: networkClientType, | ||
infuraNetwork: | ||
networkClientType === NetworkClientType.Infura | ||
? TESTNET.networkType | ||
: undefined, | ||
} as const; | ||
|
||
it('hits the RPC endpoint', async () => { | ||
await withMockedCommunications(networkArgs, async (comms) => { | ||
comms.mockRpcCall({ | ||
request: { method: 'net_version' }, | ||
response: { result: '1' }, | ||
}); | ||
|
||
const networkId = await withNetworkClient( | ||
networkArgs, | ||
({ makeRpcCall }) => { | ||
return makeRpcCall({ | ||
method: 'net_version', | ||
}); | ||
}, | ||
); | ||
|
||
expect(networkId).toBe('1'); | ||
}); | ||
}); | ||
}); | ||
} | ||
}); |
8 changes: 0 additions & 8 deletions
8
packages/network-controller/tests/create-network-client.test.ts
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.