Skip to content
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

NetworkController: Use the same middleware stack as the extension #1116

Merged
merged 87 commits into from
May 3, 2023
Merged
Show file tree
Hide file tree
Changes from 62 commits
Commits
Show all changes
87 commits
Select commit Hold shift + click to select a range
c33d89c
Added network clients
BelfordZ Dec 1, 2022
8189e3a
Implement PR feedback
BelfordZ Apr 5, 2023
48cad87
Fix linting issues
BelfordZ Apr 5, 2023
f213f24
Change chainId hex conversion to use controller utils
BelfordZ Apr 5, 2023
a3ad5d1
Remove dist from imports
BelfordZ Apr 5, 2023
87a2323
Change InfuraNetworkType to exclude keys from NetworkType
BelfordZ Apr 5, 2023
ab385f0
Remove throwing when using setProviderType, throws inside of configur…
BelfordZ Apr 5, 2023
1e83353
Fix typing issue after changing InfuraNetworkType
BelfordZ Apr 5, 2023
cb17fbe
lint fix
BelfordZ Apr 6, 2023
9acf3bb
Remove old networkclient test files
BelfordZ Apr 6, 2023
d5dacd5
fix more rebase issues
BelfordZ Apr 6, 2023
c115bb2
Added PR feedback
BelfordZ Apr 7, 2023
1de386f
Undo unnecessary change
Gudahtt Apr 18, 2023
95f152c
Extract FakeBlockTracker to separate file; improve stub
mcmire Apr 20, 2023
3835661
Rename fake-provider-engine to match class; simplify impl
mcmire Apr 20, 2023
a759057
No need to account for provider errors
mcmire Apr 20, 2023
d48f257
No need to 'stop' the provider anymore
mcmire Apr 20, 2023
998a611
Make InfuraNetworkType a 'real' enum
mcmire Apr 20, 2023
85735b3
Merge branch 'main' into added-network-clients-rebased
mcmire Apr 20, 2023
507710d
Extract INFURA_NETWORKS to match the extension
mcmire Apr 20, 2023
76643a8
Don't make the reader do hex conversion in their head
mcmire Apr 20, 2023
92040ea
Document this new behavior
mcmire Apr 20, 2023
2322306
No need to mock eth-json-rpc-middleware and eth-json-rpc-provider
mcmire Apr 20, 2023
f12a65e
Fix failing test resulting from merge conflicts
mcmire Apr 20, 2023
1fd7246
Fix formatting
mcmire Apr 20, 2023
30ca47f
Tweak these error messages
mcmire Apr 20, 2023
3d1ec25
Tweak these names to match the objects they represent
mcmire Apr 20, 2023
be564e7
Get most of the network client test files matching extension (except …
mcmire Apr 20, 2023
683d77f
Match shared-tests to the extension
mcmire Apr 20, 2023
dcc0d47
Fix lint issues
mcmire Apr 20, 2023
ccb87a3
Match the extension better in the unit tests
mcmire Apr 20, 2023
76145b8
Merge branch 'main' into added-network-clients-rebased
mcmire Apr 21, 2023
f702ba3
Fix TypeScript issues
mcmire Apr 21, 2023
41a5182
We can give Provider and BlockTracker proper types now
mcmire Apr 21, 2023
ef644f0
Rename buildFakeClient to buildFakeNetworkClient, have its provider b…
mcmire Apr 21, 2023
4650a6d
Fix all cases of using 0x-prefix hex strings when it would throw an e…
mcmire Apr 21, 2023
2c46e85
These arguments always get passed
mcmire Apr 21, 2023
306bf54
Throw if infuraProjectId is not set
mcmire Apr 21, 2023
fd14151
Revert these changes as they are unnecessary for now
mcmire Apr 21, 2023
8bc6de5
Fix failing tests
mcmire Apr 21, 2023
009e854
Use 'any' for TransactionController provider and blockTracker
mcmire Apr 22, 2023
366138c
Add missing tests
mcmire Apr 22, 2023
b7731fe
Change this back to make test copying easier
mcmire Apr 22, 2023
ae08915
Revert "We can give Provider and BlockTracker proper types now"
mcmire Apr 22, 2023
7afc2d3
Revert "Use 'any' for TransactionController provider and blockTracker"
mcmire Apr 22, 2023
6c76aa4
Merge branch 'main' into added-network-clients-rebased
legobeat Apr 26, 2023
61a255b
Merge branch 'main' into added-network-clients-rebased
legobeat Apr 26, 2023
61f8b9c
Merge branch 'main' into added-network-clients-rebased
mcmire Apr 27, 2023
5fae751
Merge branch 'main' into added-network-clients-rebased
mcmire Apr 27, 2023
02087e6
Remove unnecessary ESLint config file
mcmire Apr 27, 2023
c3e0b7a
Don't need this ESLint override anymore
mcmire Apr 27, 2023
6c0da5e
Fix build failure
mcmire Apr 27, 2023
d06f8e4
Merge branch 'main' into added-network-clients-rebased
mcmire Apr 28, 2023
7a27505
Align closer to refactor-network-client branch
mcmire Apr 28, 2023
b5f3b0f
Fix TransactionController tests
mcmire Apr 28, 2023
cf9ae84
Merge branch 'main' into added-network-clients-rebased
mcmire Apr 29, 2023
398b1ce
Fix lint issue
mcmire Apr 29, 2023
35b35f1
Merge branch 'main' into added-network-clients-rebased
mcmire May 1, 2023
de93343
Fixes
mcmire May 1, 2023
013ec7a
More fixes
mcmire May 1, 2023
2963d14
Fix failing test
mcmire May 1, 2023
e1c7795
Fix failing test
mcmire May 1, 2023
ced37b0
Merge branch 'main' into added-network-clients-rebased
mcmire May 2, 2023
53347ef
Fix lint
mcmire May 2, 2023
133b5fd
Fix tests
mcmire May 2, 2023
6d0d6f6
Merge branch 'main' into added-network-clients-rebased
mcmire May 2, 2023
14eec34
Merge branch 'main' into added-network-clients-rebased
mcmire May 2, 2023
f660310
I guess we don't need this change right now, we can always add this l…
mcmire May 2, 2023
3d24650
Merge branch 'main' into added-network-clients-rebased
mcmire May 2, 2023
c3b2849
Bump eth-block-tracker to 7.0.1
mcmire May 2, 2023
893d5ae
Fix lint
mcmire May 2, 2023
267eab4
Roll these particular changes back
mcmire May 2, 2023
32736e8
Merge remote-tracking branch 'origin/main' into added-network-clients…
Gudahtt May 3, 2023
e5940ee
Fix lint error
Gudahtt May 3, 2023
2fb0d47
Fix test
Gudahtt May 3, 2023
81cbd16
Remove unnecessary lint rule
Gudahtt May 3, 2023
956110a
Remove unnecessary test changes
Gudahtt May 3, 2023
4a2888d
Use unknown instead of any for fake provider params
Gudahtt May 3, 2023
8f6251a
Merge branch 'main' into added-network-clients-rebased
mcmire May 3, 2023
3281087
Fix failing AssetsContractController test
mcmire May 3, 2023
49dd75a
Oh, actually, we don't need these changes
mcmire May 3, 2023
12a73d5
We can test eth_subscribe and eth_unsubscribe now
mcmire May 3, 2023
322f457
Merge branch 'main' into added-network-clients-rebased
mcmire May 3, 2023
643b0fa
Drop these minor changes as well
mcmire May 3, 2023
39331ca
Make this chain ID more clear
mcmire May 3, 2023
9a23693
Update packages/network-controller/tests/NetworkController.test.ts
mcmire May 3, 2023
78e3bd0
Merge remote-tracking branch 'origin/main' into added-network-clients…
Gudahtt May 3, 2023
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
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ module.exports = {
'jest/no-test-return-statement': 'off',
// TODO: Re-enable this rule; we can accomodate this even in our test helpers
'jest/expect-expect': 'off',
'jest/no-identical-title': 'off',
},
},
{
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"@metamask/eslint-config-jest": "^9.0.0",
"@metamask/eslint-config-nodejs": "^9.0.0",
"@metamask/eslint-config-typescript": "^9.0.1",
"@metamask/eth-json-rpc-provider": "^1.0.0",
Gudahtt marked this conversation as resolved.
Show resolved Hide resolved
"@types/node": "^16.18.24",
"@typescript-eslint/eslint-plugin": "^4.33.0",
"@typescript-eslint/parser": "^4.33.0",
Expand All @@ -52,6 +53,7 @@
"eslint-plugin-prettier": "^3.4.1",
"execa": "^5.0.0",
"isomorphic-fetch": "^3.0.0",
"json-rpc-engine": "^6.1.0",
"prettier": "^2.6.2",
"prettier-plugin-packagejson": "^2.2.17",
"rimraf": "^3.0.2",
Expand Down
7 changes: 5 additions & 2 deletions packages/controller-utils/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/**
* The names of built-in Infura networks
* The names of Infura-supported built-in networks that can be used to create an
* Infura-specific network client.
*/
export const InfuraNetworkType = {
mainnet: 'mainnet',
Expand All @@ -11,7 +12,9 @@ export type InfuraNetworkType =
typeof InfuraNetworkType[keyof typeof InfuraNetworkType];

/**
* The "network type"; either the name of a built-in network, or "rpc" for custom networks.
* Symbols that can be used to create a network client: either a
* Infura-supported built-in network, "localhost" for a locally-operated custom
* network, or "rpc" for a remotely-operated custom network.
*/
export const NetworkType = {
...InfuraNetworkType,
Expand Down
6 changes: 2 additions & 4 deletions packages/gas-fee-controller/src/GasFeeController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,10 +271,8 @@ describe('GasFeeController', () => {

afterEach(() => {
gasFeeController.destroy();
const { provider } = networkController.getProviderAndBlockTracker();
if (provider) {
provider.stop();
}
const { blockTracker } = networkController.getProviderAndBlockTracker();
blockTracker?.destroy();
sinon.restore();
jest.clearAllMocks();
nock.enableNetConnect();
Expand Down
12 changes: 6 additions & 6 deletions packages/network-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,27 @@
"dependencies": {
"@metamask/base-controller": "workspace:^",
"@metamask/controller-utils": "workspace:^",
"@metamask/eth-json-rpc-infura": "^8.0.0",
"@metamask/eth-json-rpc-middleware": "^11.0.0",
"@metamask/eth-json-rpc-provider": "^1.0.0",
"@metamask/swappable-obj-proxy": "^2.1.0",
"@metamask/utils": "^5.0.2",
"async-mutex": "^0.2.6",
"babel-runtime": "^6.26.0",
"eth-json-rpc-infura": "^5.1.0",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked at updating this in a separate PR, but it's probably safer not to, since this is the version web3-provider-engine expects and uses as a default.

"eth-block-tracker": "^7.0.0",
"eth-query": "^2.1.2",
"eth-rpc-errors": "^4.0.2",
"immer": "^9.0.6",
"uuid": "^8.3.2",
"web3-provider-engine": "^16.0.5"
"json-rpc-engine": "^6.1.0",
"uuid": "^8.3.2"
Comment on lines -42 to +46
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉🎉🎉🎉🎉🎉🎉

},
"devDependencies": {
"@json-rpc-specification/meta-schema": "^1.0.6",
"@metamask/auto-changelog": "^3.1.0",
"@metamask/eth-json-rpc-provider": "^1.0.0",
"@types/jest": "^27.4.1",
"@types/lodash": "^4.14.191",
"deepmerge": "^4.2.2",
"eth-block-tracker": "^7.0.1",
"jest": "^27.5.1",
"json-rpc-engine": "^6.1.0",
"lodash": "^4.17.21",
"nock": "^13.0.7",
"sinon": "^9.2.4",
Expand Down
62 changes: 24 additions & 38 deletions packages/network-controller/src/NetworkController.ts
mcmire marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ import {
NetworkType,
isSafeChainId,
isNetworkType,
toHex,
} from '@metamask/controller-utils';
import {
Hex,
assertIsStrictHexString,
hasProperty,
isPlainObject,
Expand Down Expand Up @@ -257,8 +259,6 @@ export class NetworkController extends BaseControllerV2<

#previousNetworkSpecifier: NetworkType | NetworkConfigurationId | null;

#provider: Provider | undefined;

#providerProxy: ProviderProxy | undefined;

#blockTrackerProxy: BlockTrackerProxy | undefined;
Expand Down Expand Up @@ -320,10 +320,8 @@ export class NetworkController extends BaseControllerV2<

#configureProvider(
type: NetworkType,
rpcTarget?: string,
chainId?: string,
ticker?: string,
nickname?: string,
rpcTarget: string | undefined,
chainId: string | undefined,
) {
switch (type) {
case NetworkType.mainnet:
Expand All @@ -332,11 +330,22 @@ export class NetworkController extends BaseControllerV2<
this.#setupInfuraProvider(type);
break;
case NetworkType.localhost:
this.#setupStandardProvider(LOCALHOST_RPC_URL);
if (chainId === undefined) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The checks for chainId were added because we need to convert chainId from a decimal number to a hexadecimal number (the provider config object in this version of NetworkController stores a chainId in decimal, but createNetworkClient takes a chainId in hex in order to create a static middleware for eth_chainId). This requires that chainId be something.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: Add this validation in a separate PR

throw new Error('chainId must be provided for custom RPC endpoints');
}
this.#setupStandardProvider(LOCALHOST_RPC_URL, toHex(chainId));
break;
case NetworkType.rpc:
rpcTarget &&
this.#setupStandardProvider(rpcTarget, chainId, ticker, nickname);
if (chainId === undefined) {
throw new Error('chainId must be provided for custom RPC endpoints');
}

if (rpcTarget === undefined) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The check for rpcTarget was added because createNetworkClient needs one in order to pass it along to the fetch middleware.

throw new Error(
'rpcTarget must be provided for custom RPC endpoints',
);
}
this.#setupStandardProvider(rpcTarget, toHex(chainId));
break;
default:
throw new Error(`Unrecognized network type: '${type}'`);
Expand All @@ -359,16 +368,15 @@ export class NetworkController extends BaseControllerV2<
state.networkStatus = NetworkStatus.Unknown;
state.networkDetails = {};
});
const { rpcTarget, type, chainId, ticker } = this.state.providerConfig;
this.#configureProvider(type, rpcTarget, chainId, ticker);
const { rpcTarget, type, chainId } = this.state.providerConfig;
this.#configureProvider(type, rpcTarget, chainId);
await this.lookupNetwork();
}

#registerProvider() {
const { provider } = this.getProviderAndBlockTracker();

if (provider) {
provider.on('error', this.#verifyNetwork.bind(this));
this.#ethQuery = new EthQuery(provider);
}
}
Expand All @@ -383,54 +391,33 @@ export class NetworkController extends BaseControllerV2<
this.#updateProvider(provider, blockTracker);
}

#setupStandardProvider(
rpcTarget: string,
chainId?: string,
ticker?: string,
nickname?: string,
) {
#setupStandardProvider(rpcTarget: string, chainId: Hex) {
const { provider, blockTracker } = createNetworkClient({
chainId,
nickname,
rpcUrl: rpcTarget,
ticker,
type: NetworkClientType.Custom,
});

this.#updateProvider(provider, blockTracker);
}

#updateProvider(provider: Provider, blockTracker: any) {
this.#safelyStopProvider(this.#provider);
#updateProvider(provider: Provider, blockTracker: BlockTracker) {
this.#setProviderAndBlockTracker({
provider,
blockTracker,
});
this.#registerProvider();
}

#safelyStopProvider(provider: Provider | undefined) {
setTimeout(() => {
provider?.stop();
}, 500);
}

async #verifyNetwork() {
if (this.state.networkStatus !== NetworkStatus.Available) {
await this.lookupNetwork();
}
}

/**
* Method to inilialize the provider,
* Creates the provider and block tracker for the configured network,
* using the provider to gather details about the network.
*
*/
async initializeProvider() {
const { type, rpcTarget, chainId, ticker, nickname } =
this.state.providerConfig;
this.#configureProvider(type, rpcTarget, chainId, ticker, nickname);
const { type, rpcTarget, chainId } = this.state.providerConfig;
this.#configureProvider(type, rpcTarget, chainId);
this.#registerProvider();
await this.lookupNetwork();
}
Expand Down Expand Up @@ -669,7 +656,6 @@ export class NetworkController extends BaseControllerV2<
} else {
this.#providerProxy = createEventEmitterProxy(provider);
}
this.#provider = provider;

if (this.#blockTrackerProxy) {
this.#blockTrackerProxy.setTarget(blockTracker);
Expand Down
Loading