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 all 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
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ import {
} from './AssetsContractController';
import { SupportedTokenDetectionNetworks } from './assetsUtil';

const INFURA_PROJECT_ID = '341eacb578dd44a1a049cbc5f6fd4035';

const MAINNET_PROVIDER = new HttpProvider(
'https://mainnet.infura.io/v3/341eacb578dd44a1a049cbc5f6fd4035',
`https://mainnet.infura.io/v3/${INFURA_PROJECT_ID}`,
);

const ERC20_UNI_ADDRESS = '0x1f9840a85d5af5bf1d1762f925bdaddc4201f984';
Expand All @@ -37,7 +39,7 @@ const setupControllers = () => {
allowedActions: [],
});
const network = new NetworkController({
infuraProjectId: 'infura-project-id',
infuraProjectId: INFURA_PROJECT_ID,
messenger,
trackMetaMetricsEvent: jest.fn(),
});
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.1",
"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
31 changes: 10 additions & 21 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 @@ -255,8 +257,6 @@ export class NetworkController extends BaseControllerV2<

#previousNetworkSpecifier: NetworkType | NetworkConfigurationId | null;

#provider: Provider | undefined;

#providerProxy: ProviderProxy | undefined;

#blockTrackerProxy: BlockTrackerProxy | undefined;
Expand Down Expand Up @@ -316,7 +316,11 @@ export class NetworkController extends BaseControllerV2<
this.#previousNetworkSpecifier = this.state.providerConfig.type;
}

#configureProvider(type: NetworkType, rpcUrl?: string, chainId?: string) {
#configureProvider(
type: NetworkType,
rpcUrl: string | undefined,
chainId: string | undefined,
) {
switch (type) {
case NetworkType.mainnet:
case NetworkType.goerli:
Expand All @@ -331,7 +335,7 @@ export class NetworkController extends BaseControllerV2<
if (rpcUrl === undefined) {
throw new Error('rpcUrl must be provided for custom RPC endpoints');
}
this.#setupStandardProvider(rpcUrl, chainId);
this.#setupStandardProvider(rpcUrl, toHex(chainId));
break;
default:
throw new Error(`Unrecognized network type: '${type}'`);
Expand Down Expand Up @@ -363,7 +367,6 @@ export class NetworkController extends BaseControllerV2<
const { provider } = this.getProviderAndBlockTracker();

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

#setupStandardProvider(rpcUrl: string, chainId: string) {
#setupStandardProvider(rpcUrl: string, chainId: Hex) {
const { provider, blockTracker } = createNetworkClient({
chainId,
rpcUrl,
Expand All @@ -388,27 +391,14 @@ export class NetworkController extends BaseControllerV2<
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,
Expand Down Expand Up @@ -656,7 +646,6 @@ export class NetworkController extends BaseControllerV2<
} else {
this.#providerProxy = createEventEmitterProxy(provider);
}
this.#provider = provider;

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