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

Add NetworkController RPC service class #5163

Draft
wants to merge 2 commits into
base: extract-create-service-policy-3
Choose a base branch
from

Conversation

mcmire
Copy link
Contributor

@mcmire mcmire commented Jan 16, 2025

Explanation

References

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

Changelog

(TODO)

Checklist

  • I've updated the test suite for new or updated code as appropriate
  • I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate
  • I've highlighted breaking changes using the "BREAKING" category above as appropriate
  • I've prepared draft pull requests for clients and consumer packages to resolve any breaking changes

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
@@ -3,6 +3,8 @@ import jest from '@metamask/eslint-config-jest';
import nodejs from '@metamask/eslint-config-nodejs';
import typescript from '@metamask/eslint-config-typescript';

const NODE_LTS_VERSION = 22;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ignore this — this is already extracted to #5158 but is here to avoid from needing to add a bunch of eslint-disable lines.

@mcmire mcmire force-pushed the create-nc-rpc-service branch from 31debed to 2b6e317 Compare January 16, 2025 18:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant