-
-
Notifications
You must be signed in to change notification settings - Fork 187
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did a first pass on this PR. Looks pretty straightforward in terms of implementation, I think all of the comments I had related to cleanup.
packages/network-controller/tests/provider-api-tests/block-hash-in-response.ts
Outdated
Show resolved
Hide resolved
packages/network-controller/tests/provider-api-tests/block-param.ts
Outdated
Show resolved
Hide resolved
3c484f6
to
e3aff5d
Compare
New dependency changes detected. Learn more about Socket for GitHub ↗︎ 👍 No new dependency issues detected in pull request Bot CommandsTo ignore an alert, reply with a comment starting with Ignoring: Pull request alert summary
📊 Modified Dependency Overview:
|
0acdfc5
to
e3aff5d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Glad to see the diff here is reasonably small! I didn't review the tests in detail yet because I'd rather do that after we document the functional differences we've found, so that I can cross-reference that with the test changes.
Could you update the PR description as well when you get the chance?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks nice! I too am surprised by how small this PR is, but I guess this speaks to how many changes we've made so far 😅 I did a first pass here oh I guess this is a second pass lol and had some suggestions. I'll do a closer comparison of the JavaScript and TypeScript next.
Echoing Mark's request from earlier — would you mind updating the PR description to explain what this PR does so we have something to go off of when writing the changelog (and also for posterity in general)?
7688b04
to
02f0651
Compare
eae8773
to
2ffb94e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for updating the PR description. Did another pass, this time in a little more detail. Tests look great! Mostly minor stuff.
packages/network-controller/tests/provider-api-tests/no-block-param.ts
Outdated
Show resolved
Hide resolved
5f9e7e2
to
addd063
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
addd063
to
434ca25
Compare
@mcmire just rebased |
packages/network-controller/tests/provider-api-tests/shared-tests.ts
Outdated
Show resolved
Hide resolved
await makeRpcCall(request); | ||
await waitForNextBlockTracker(blockTracker, clock); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we know why this step is no longer needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@BelfordZ Do you remember? I feel like it might have something to do with https://github.com/MetaMask/web3-provider-engine/blob/cf612f898002833c36730d23972fe4c4dd483c76/index.js#L50, but I'm not sure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sry for the delay.
We no longer need this because there was a subtle difference in the number of setTimeouts that occur between extension's network controller & the core one. Specifically the use of 'stoplight' which uses setTimeout if the lock is currently set. It only affected some tests based on whether or not the request would be made while the lock was held by another. Since we no longer are using this provider, this extra setTimeout goes away.
@@ -921,7 +961,9 @@ export function testsForRpcMethodAssumingNoBlockParam( | |||
}, | |||
); | |||
|
|||
expect(result).toBeUndefined(); | |||
await expect(promiseForResult).rejects.toThrow( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should the test description here be updated to say "throws" instead of "returns"? Likewise for the other test updates in this module that now throw when they hadn't previously.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, interesting. I guess it depends on how people are likely to think about methods that return promises. When used with await
, they act like synchronous methods, so we could say that they "return X" when they resolve and they "throw Y" when they reject. Our guideline is that await
should be preferred, of course, so it makes sense to think about it like that. More accurately, though we could say that they "return a promise that resolves with X" or "return a promise that rejects with Y". Do you think it's worth it to be accurate or does it not matter?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, I tend to prefer the shorter form just for brevity but I don't have a strong preference either way. Either way would be a lot more clear than the current description
The `setFakeProvider` test helper used for the network controller unit tests had a return value that was unused, and an unnecessary waiting step. These have both been removed. This reduces the scope of #1116
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good! I have finished reviewing this, and can't see any more clear opportunities to break this into smaller pieces.
Co-authored-by: Mark Stacey <[email protected]>
…-rebased * origin/main: Simplify the `setFakeProvider` test helper (#1317)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! The PR description looks complete as well.
The one pending comment about the no-block-param
test descriptions can wait for a later PR.
"uuid": "^8.3.2", | ||
"web3-provider-engine": "^16.0.5" | ||
"json-rpc-engine": "^6.1.0", | ||
"uuid": "^8.3.2" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎉🎉🎉🎉🎉🎉🎉
Our test helper modules have been covered by ESLint as though they were production code. This resulted in misleading coverage statistics and various erronous lint errors (which we've handled by disabling rules in affected files). The ESLint configuration has been updated to consider any modules under a `tests` directory to be test helpers. This helps reduce the scope of #1116
All unnecessary ESLint ignore comments have been removed from the network controller package. Most of these were added before we had decent types setup for the network client tests. The `mockRpcCall` function was converted from an arrow function to a function declaration to address lint errors, so the description was copied over from the extension. Relates to #1116
When constructing a non-Infura network client, the network `nickname` and `ticker` configuration properties were passed on down to `web3-provider-engine`. However these were entirely unused. There are no references to these in the `web3-provider-engine` package. Relates to #1116
The types used for the network client test helpers have been made more specific; the network type passed in is now required at a type-level to be an Infura network. This was a type-only change because we were already only passing in Infura neworks for this option. This was done to make a later refactor PR simpler. Relates to #1116
The network controller unit tests have been updated to avoid importing a constants file from another package using a relative path import. As a general rule we should be using package imports whenever referencing modules in other packages. This reduces the scope of #1116
JSDoc comments have been added to functions used for the network client tests. These were copied from the extension. This reduces the scope of #1116
Validation has been added to internal methods responsible for constructing a provider (upon initialization or after switching networks). They ensure that a custom provider has a chain ID and RPC URL set. This reduces the scope of #1116
The `setFakeProvider` test helper used for the network controller unit tests had a return value that was unused, and an unnecessary waiting step. These have both been removed. This reduces the scope of #1116
) The network controller in this repo exposes a JSON-RPC provider object built via the `web3-provider-engine` package. However, we have not supported this package for a long time, and the network controller in the extension uses a more modern provider built from `json-rpc-engine` and `eth-json-rpc-middleware`. In order to unify the two versions of the network controllers, we need to replace the middleware stack in this version with the one in the extension. Co-authored-by: Mark Stacey <[email protected]> Co-authored-by: Elliot Winkler <[email protected]> Co-authored-by: legobeat <[email protected]>
Our test helper modules have been covered by ESLint as though they were production code. This resulted in misleading coverage statistics and various erronous lint errors (which we've handled by disabling rules in affected files). The ESLint configuration has been updated to consider any modules under a `tests` directory to be test helpers. This helps reduce the scope of #1116
All unnecessary ESLint ignore comments have been removed from the network controller package. Most of these were added before we had decent types setup for the network client tests. The `mockRpcCall` function was converted from an arrow function to a function declaration to address lint errors, so the description was copied over from the extension. Relates to #1116
When constructing a non-Infura network client, the network `nickname` and `ticker` configuration properties were passed on down to `web3-provider-engine`. However these were entirely unused. There are no references to these in the `web3-provider-engine` package. Relates to #1116
The types used for the network client test helpers have been made more specific; the network type passed in is now required at a type-level to be an Infura network. This was a type-only change because we were already only passing in Infura neworks for this option. This was done to make a later refactor PR simpler. Relates to #1116
The network controller unit tests have been updated to avoid importing a constants file from another package using a relative path import. As a general rule we should be using package imports whenever referencing modules in other packages. This reduces the scope of #1116
JSDoc comments have been added to functions used for the network client tests. These were copied from the extension. This reduces the scope of #1116
Validation has been added to internal methods responsible for constructing a provider (upon initialization or after switching networks). They ensure that a custom provider has a chain ID and RPC URL set. This reduces the scope of #1116
The `setFakeProvider` test helper used for the network controller unit tests had a return value that was unused, and an unnecessary waiting step. These have both been removed. This reduces the scope of #1116
) The network controller in this repo exposes a JSON-RPC provider object built via the `web3-provider-engine` package. However, we have not supported this package for a long time, and the network controller in the extension uses a more modern provider built from `json-rpc-engine` and `eth-json-rpc-middleware`. In order to unify the two versions of the network controllers, we need to replace the middleware stack in this version with the one in the extension. Co-authored-by: Mark Stacey <[email protected]> Co-authored-by: Elliot Winkler <[email protected]> Co-authored-by: legobeat <[email protected]>
Description
The network controller in this repo exposes a JSON-RPC provider object built via the
web3-provider-engine
package. However, we have not supported this package for a long time, and the network controller in the extension uses a more modern provider built fromjson-rpc-engine
andeth-json-rpc-middleware
. In order to unify the two versions of the network controllers, we need to replace the middleware stack in this version with the one in the extension.Changes
@metamask/network-controller
] Update NetworkController to use a simpler middleware stack derived from pieces ofeth-json-rpc-middleware
instead ofweb3-provider-engine
. Consequences of this replacement are detailed in items below.@metamask/network-controller
] A call toeth_chainId
through a newly initialized provider where thetype
in the provider config state is "rpc" will now return thechainId
in the provider config rather than the chain ID returned by the network.@metamask/network-controller
] A call toeth_chainId
through a newly initialized provider where thetype
in the provider config state is an Infura network will now return a hard-coded network ID matching the chain ID rather than the chain ID returned by the network.@metamask/network-controller
] A call toeth_chainId
through the provider created from switching the network viasetActiveNetwork
will now return thechainId
in the network configuration object rather than the chain ID returned by the network.@metamask/network-controller
] A call tonet_version
through the provider created from switching the network viasetProviderType
will now return a hard-coded network ID matching the chain ID rather than the network ID returned by the network.@metamask/network-controller
] Previously, calls to RPC methods that took an object as its first parameter (e.g.eth_call
) were intercepted such that unknown properties were removed from the object and any hex strings present in the remaining properties were normalized. This no longer happens and these methods will pass through to the network unchanged.@metamask/network-controller
] A call toeth_getBalance
,eth_getBlockByNumber
,eth_getCode
,eth_getTransactionCount
, oreth_call
will now be intercepted such that a block tag parameter of"latest"
will be replaced with the latest known block number before being passed to the network.@metamask/network-controller
] Previously, a call toeth_getTransactionCount
was intercepted such that when the block tag was"pending"
, it would treat the count as the transaction nonce and cache it, using it for subsequent calls for the same address. This nonce would then be updated upon each call toeth_sendRawTransaction
based on the nonce of the transaction being sent. The whole nonce cache was also cleared upon a call toevm_revert
. This no longer happens, and these RPC methods will be passed to the network unchanged.nonceTracker
that@metamask/transaction-controller
exposes in order to obtain a unique nonce.@metamask/network-controller
] A call toweb3_clientVersion
is no longer intercepted to return a static result of"ProviderEngine/v<version>/javascript"
, but is passed through to the network.@metamask/network-controller
] A call tonet_listening
is no longer intercepted to return a static result oftrue
, but is passed through to the network.@metamask/network-controller
] A call toeth_hashrate
is no longer intercepted to return a static result of"0x00"
, but is passed through to the network.@metamask/network-controller
] A call toeth_mining
is no longer intercepted to return a static result offalse
, but is passed through to the network.@metamask/network-controller
] Previously,eth_subscribe
andeth_unsubscribe
would never hit the network; instead, the behavior was polyfilled by polling the network for new blocks. Additionally, thenewPendingTransactions
parameter foreth_subscribe
was unsupported. This polyfill is no longer present, andeth_subscribe
andeth_unsubscribe
are passed through to the network unchanged.eth-json-rpc-filters
package.@metamask/network-controller
] Previously,eth_newFilter
,eth_newBlockFilter
,eth_newPendingTransactionFilter
,eth_uninstallFilter
,eth_getFilterChanges
, andeth_getFilterLogs
would never hit the network; instead, the behavior was polyfilled by polling the network for new blocks and recording updates for registered filters. This polyfill is no longer present, and these RPC methods are passed through to the network unchanged.eth-json-rpc-filters
package.@metamask/network-controller
] Interfacing with a network that exposes a websocket is no longer supported.@metamask/network-controller
] Bump dependencyeth-json-rpc-infura
(now@metamask/eth-json-rpc-infura
) from ^7.0.0 to ^8.0.0.@metamask/network-controller
] Add dependencyeth-json-rpc-middleware
^11.0.0@metamask/network-controller
] Add dependencyeth-json-rpc-provider
^1.0.0@metamask/network-controller
] Add dependencyeth-block-tracker
^7.0.0@metamask/network-controller
] Add dependencyjson-rpc-engine
^6.1.0References
Fixes #970.
Checklist