Skip to content

feat: JSON RPC conversion layer#375

Merged
gregorydemay merged 36 commits intomainfrom
gdemay/XC-287-json-rpc-layer
Mar 7, 2025
Merged

feat: JSON RPC conversion layer#375
gregorydemay merged 36 commits intomainfrom
gdemay/XC-287-json-rpc-layer

Conversation

@gregorydemay
Copy link
Contributor

@gregorydemay gregorydemay commented Feb 28, 2025

Follow-up on #370, #364, and #374 to add a conversion layer between request and responses to handle JSON RPC over HTTP.

Concretely:

  1. Refactor existing infrastructure to handle errors explicitly, instead of having to downcast BoxError. This is beneficial since errors may happen at different layers and they all need to be handled by the above observability layer, e.g., add the corresponding metric in case the response has a non successful status.
  2. Refactor existing infrastructure to transform request and response to use a single Convert trait to convert one type into another with some potential error.
  3. Add a layer for http::Request and http::Response to transform their body into a JSON RPC type. This uses the above Convert trait. This conversion layer is behind a feature flag (json) since it requires serde_json to handle the serialization/deserialization
  4. Add a new layer to filter out non-successful HTTP responses.

@gregorydemay gregorydemay changed the title feat: JSON RPC layer feat: JSON RPC conversion layer Mar 4, 2025
@gregorydemay gregorydemay marked this pull request as ready for review March 4, 2025 14:34
@gregorydemay gregorydemay requested a review from a team as a code owner March 4, 2025 14:34
@gregorydemay gregorydemay requested review from lpahlavi and ninegua March 4, 2025 14:34
pub struct Client;

impl Client {
pub fn new_with_error<CustomError: From<IcError>>(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The idea is that the service uses the CustomError type, so that any additional layer that may produce errors need to be mapped to the service error type. This forces the user of the library to handle errors explicitly.

where
Converter: Convert<NewRequest, Output = Request>,
S: Service<Request, Error = Error>,
Converter::Error: Into<Error>,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We cannot use tower::filter::Predicate because the use of BoxError is hard-coded.

Copy link
Member

@ninegua ninegua left a comment

Choose a reason for hiding this comment

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

Nice documentation! Thanks!

@gregorydemay gregorydemay merged commit 4cc3a5a into main Mar 7, 2025
10 checks passed
@gregorydemay gregorydemay deleted the gdemay/XC-287-json-rpc-layer branch March 7, 2025 07:41
@gregorydemay gregorydemay mentioned this pull request Mar 7, 2025
gregorydemay added a commit that referenced this pull request Mar 11, 2025
Follow-up on #370, #364, #374, and #375 to automatically retry requests
by doubling its `max_response_bytes` when the response was too big.
gregorydemay added a commit to dfinity/canhttp that referenced this pull request Jul 9, 2025
Follow-up on dfinity/evm-rpc-canister#370, dfinity/evm-rpc-canister#364, and dfinity/evm-rpc-canister#374 to add a conversion layer between
request and responses to handle [JSON
RPC](https://www.jsonrpc.org/specification) over HTTP.

Concretely:
1. Refactor existing infrastructure to handle errors explicitly, instead
of having to downcast `BoxError`. This is beneficial since errors may
happen at different layers and they all need to be handled by the above
observability layer, e.g., add the corresponding metric in case the
response has a non successful status.
2. Refactor existing infrastructure to transform request and response to
use a single `Convert` trait to convert one type into another with some
potential error.
3. Add a layer for `http::Request` and `http::Response` to transform
their body into a JSON RPC type. This uses the above `Convert` trait.
This conversion layer is behind a feature flag (`json`) since it
requires `serde_json` to handle the serialization/deserialization
4. Add a new layer to filter out non-successful HTTP responses.
gregorydemay added a commit to dfinity/canhttp that referenced this pull request Jul 9, 2025
Follow-up on dfinity/evm-rpc-canister#370, dfinity/evm-rpc-canister#364, dfinity/evm-rpc-canister#374, and dfinity/evm-rpc-canister#375 to automatically retry requests
by doubling its `max_response_bytes` when the response was too big.
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.

2 participants