Skip to content

Commit

Permalink
Add NetworkController RPC service class
Browse files Browse the repository at this point in the history
This commit adds an RPC service class that will ultimately be used
inside of middleware to make JSON-RPC requests to an RPC endpoint. It
makes uses of `createServicePolicy`, added in a previous commit, to
encapsulate the request code using the retry and circuit breaker
policies. As this service class is designed to replace large parts of
the fetch and Infura middleware, it customizes the service policy so
that the request will be retried only when the network is perceived to
be "down". This occurs when:

- The `fetch` call throws a "Failed to fetch" error (or something
  similar to it; see code for the full list of variations)
- The `fetch` call throws an ETIMEDOUT or ECONNRESET error
- The response status is 503 or 504
- The response body is invalid JSON

In contrast, the network is not perceived to be "down" if:

- The `fetch` call throws an unexpected error (e.g. if the request
  options are invalid)
- The response status is not 2xx, but is also not 503 or 504
- The response body is an unsuccessful JSON-RPC response
- The response body is a successful, but empty, JSON-RPC response
  • Loading branch information
mcmire committed Jan 16, 2025
1 parent 054025c commit 2b6e317
Show file tree
Hide file tree
Showing 7 changed files with 960 additions and 1 deletion.
2 changes: 1 addition & 1 deletion eslint-warning-thresholds.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"jsdoc/check-tag-names": 375,
"jsdoc/require-returns": 22,
"jsdoc/tag-lines": 328,
"n/no-unsupported-features/node-builtins": 18,
"n/no-unsupported-features/node-builtins": 1,
"n/prefer-global/text-encoder": 4,
"n/prefer-global/text-decoder": 4,
"prettier/prettier": 110,
Expand Down
1 change: 1 addition & 0 deletions packages/controller-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { createServicePolicy } from './create-service-policy';
export type { IServicePolicy } from './create-service-policy';
export * from './constants';
export type { NonEmptyArray } from './util';
export {
Expand Down
1 change: 1 addition & 0 deletions packages/network-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"@metamask/swappable-obj-proxy": "^2.3.0",
"@metamask/utils": "^11.0.1",
"async-mutex": "^0.5.0",
"cockatiel": "^3.1.2",
"fast-deep-equal": "^3.1.3",
"immer": "^9.0.6",
"loglevel": "^1.8.1",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { Json } from '@metamask/utils';

/**
* The interface for a service class responsible for making a request to an RPC
* endpoint.
*/
export type AbstractRpcService = {
request(options?: RequestInit): Promise<Json>;
};
Loading

0 comments on commit 2b6e317

Please sign in to comment.