-
-
Notifications
You must be signed in to change notification settings - Fork 205
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
Create RPC middleware using RPC services #5290
Conversation
5a4f6f1
to
62bea3b
Compare
5b7e301
to
40fa2ca
Compare
40fa2ca
to
eee47d9
Compare
Removed dependencies detected. Learn more about Socket for GitHub ↗︎ 🚮 Removed packages: npm/@metamask/[email protected] |
eee47d9
to
da61e84
Compare
When creating the middleware stack for a network, NetworkController makes use of functions from two packages for sending requests: - `createInfuraMiddleware` from `@metamask/eth-json-rpc-infura` for Infura networks - `createFetchMiddleware` from `@metamask/eth-json-rpc-middleware` for custom RPC endpoints Currently, each of these middleware implements similar heuristics for determining when the network is down and retrying requests appropriately. We want to improve upon this logic so that we incorporate the circuit breaker pattern, exponential backoff, and jitter for retries, and we have implemented an RpcService class which extracts all of this logic into one place. We can vastly simplify the Infura and fetch middleware creation functions so that they only need to take an RPC service and the RPC service will handle the rest. To accommodate these changes, this commit: - Upgrades the `@metamask/eth-json-rpc-infura` and `@metamask/eth-json-rpc-middleware` packages to allow an RPC service to be passed. - Adds two new required options to NetworkController, `fetch` and `btoa`. These are also requirements of RpcService and allows consumers to pass in whatever versions of these functions is specific to their platform. - Updates `createAutoManagedNetworkClient` to take `fetch` and `btoa` and pass them along to `createNetworkClient`. - Updates `createNetworkClient` to create an RPC service and pass it along to the two middleware creation functions. Consequently, these functions now handle requests in exactly the same way.
da61e84
to
e690c89
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.
Great work!
* @param args.fetch - A function that can be used to make an HTTP request, | ||
* compatible with the Fetch API. | ||
* @param args.btoa - A function that can be used to convert a binary string | ||
* into base-64. |
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.
nit:
* into base-64. | |
* into a base64-encoded ASCII string. |
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.
Ah, makes sense. I plan on making a sort of cleanup PR later so I will make this change then.
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!
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.
There are lots of changes to tests here but this is because of the changes to the signature of the NetworkController constructor and also createNetworkClient
.
@@ -385,167 +380,45 @@ export function testsForRpcMethodSupportingBlockParam( | |||
}); | |||
}); | |||
|
|||
// There is a difference in how we are testing the Infura middleware vs. the |
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 commit consolidates the request handling logic between the Infura and fetch middleware, so they now have the same behavior, and as a result, we can simplify the tests that exercise the middleware.
@@ -195,6 +194,10 @@ function mockRpcCall({ | |||
// TODO: Replace `any` with type | |||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | |||
return nockRequest.reply(httpStatus, (_, requestBody: any) => { | |||
if (typeof completeResponse === 'string') { |
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.
In tests we now need the ability to completely override the response body to test for what happens when the response is not JSON-parseable.
@@ -265,114 +260,32 @@ export function testsForRpcMethodAssumingNoBlockParam( | |||
}); | |||
}); | |||
|
|||
// There is a difference in how we are testing the Infura middleware vs. the |
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.
Similar as block-param.ts
: We can simplify these tests because both the Infura and fetch middleware now handle requests in the same way.
* @param args.fetch - A function that can be used to make an HTTP request, | ||
* compatible with the Fetch API. | ||
* @param args.btoa - A function that can be used to convert a binary string | ||
* into base-64. |
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.
Ah, makes sense. I plan on making a sort of cleanup PR later so I will make this change then.
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.
Confirmation changes LGTM
Explanation
When creating the middleware stack for a network, NetworkController makes use of functions from two packages for sending requests:
createInfuraMiddleware
from@metamask/eth-json-rpc-infura
for Infura networkscreateFetchMiddleware
from@metamask/eth-json-rpc-middleware
for custom RPC endpointsCurrently, each of these middleware implements similar heuristics for determining when the network is down and retrying requests appropriately. We want to improve upon this logic so that we incorporate the circuit breaker pattern, exponential backoff, and jitter for retries, and, in the case of Infura, we can fail over to an alternate RPC endpoint when it is down. To make this happen, we have implemented an RpcService class which extracts all of this logic into one place. As a result, we can vastly simplify the Infura and fetch middleware creation functions so that they only need to take an RPC service and the RPC service will handle the rest.
To accommodate these changes, this commit:
@metamask/eth-json-rpc-infura
and@metamask/eth-json-rpc-middleware
packages to allow an RPC service to be passed.fetch
andbtoa
. These are also requirements of RpcService and allows consumers to pass in whatever versions of these functions is specific to their platform.createAutoManagedNetworkClient
to takefetch
andbtoa
and pass them along tocreateNetworkClient
.createNetworkClient
to create an RPC service and pass it along to the two middleware creation functions. Consequently, these functions now handle requests in exactly the same way.References
Closes #4991.
Changelog
(Updated in PR.)
Checklist