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

refactor: use generic trait bound for RpcClient #483

Merged
merged 19 commits into from
Jun 4, 2024
Merged

Conversation

Wodann
Copy link
Member

@Wodann Wodann commented May 31, 2024

This PR splits off the RpcClient into two separate crates:

  1. edr_rpc_client: contains a genericised version of RpcClient that accepts any type which implements the trait RpcMethod. As RpcClient's caching mechanism requires knowledge about the block number and chain ID I added the chain_id_request and block_number_request methods to trait RpcMethod, alongside an associated type that specifies the CacheableMethod type.
    The RpcClient type can be reused by chain types with varying JSON-RPC methods, as listed here.
  2. edr_rpc_eth: introduces an EthRpcClient for all chains that support the Ethereum L1 JSON-RPC specification. It allows variance in the Block and Transaction types, but this could be extended to Receipt or other types as necessary. It's main goal is to avoid duplication of pub enum RequestMethod between Optimism, Arbitrum, Ethereum L1.

Alternatively to the block_number_request and chain_id_request methods in 1, they could be reworked to block_number_without_caching and chain_id_without_caching.

@Wodann Wodann self-assigned this May 31, 2024
Copy link

changeset-bot bot commented May 31, 2024

🦋 Changeset detected

Latest commit: 5225095

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@nomicfoundation/edr Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@Wodann Wodann had a problem deploying to github-action-benchmark May 31, 2024 06:12 — with GitHub Actions Failure
@Wodann Wodann had a problem deploying to github-action-benchmark May 31, 2024 06:20 — with GitHub Actions Failure
@Wodann Wodann had a problem deploying to github-action-benchmark May 31, 2024 06:29 — with GitHub Actions Failure
@Wodann Wodann temporarily deployed to github-action-benchmark June 1, 2024 01:05 — with GitHub Actions Inactive
@Wodann Wodann temporarily deployed to github-action-benchmark June 3, 2024 15:15 — with GitHub Actions Inactive
@Wodann Wodann had a problem deploying to github-action-benchmark June 3, 2024 18:21 — with GitHub Actions Error
@Wodann Wodann temporarily deployed to github-action-benchmark June 3, 2024 18:44 — with GitHub Actions Inactive
@Wodann Wodann marked this pull request as ready for review June 3, 2024 18:44
@Wodann Wodann requested a review from agostbiro June 3, 2024 19:35
Copy link
Member

@agostbiro agostbiro left a comment

Choose a reason for hiding this comment

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

Thanks LGTM overall, just one change request:

Please use the same approach (unit vs integration test) for testing the client in edr_rpc_client and edr_rpc_eth, unless I'm missing a reason not to. I don't have strong views which is preferable here, but I like to default to unit tests due to easier code sharing.

@Wodann
Copy link
Member Author

Wodann commented Jun 4, 2024

unless I'm missing a reason not to.

In the edr_rpc_client crate, the test functions use a private member to validate logic:

assert!(client.cached_block_number.read().await.is_some());

edr_rpc_eth only uses the public API in combination with 1) a mocking server; and 2) remote access to Alchemy. If you don't think that's a valid reason to consider them integration tests, please let me know.

@Wodann Wodann requested a review from agostbiro June 4, 2024 15:20
@agostbiro
Copy link
Member

In the edr_rpc_client crate, the test functions use a private member to validate logic:

assert!(client.cached_block_number.read().await.is_some());

edr_rpc_eth only uses the public API in combination with 1) a mocking server; and 2) remote access to Alchemy. If you don't think that's a valid reason to consider them integration tests, please let me know.

Ok so there is a hard reason why client tests in edr_rpc_client need to be "unit tests".

I can see why you implemented client tests as "integration tests" in edr_rpc_eth from a logical perspective, but from a practical perspective I think this differentiation will be annoying, because often changes to one crate will need changes in the other and the different testing pattern can trip us up. So I'd prefer to have client tests in edr_rpc_eth as "unit tests" unless them being "integration tests" brings a practical benefit (since the crates are internal I can't think of any, but happy to be convinced otherwise).

@Wodann
Copy link
Member Author

Wodann commented Jun 4, 2024

The main benefit I see is separation of the tests into dedicated test files, instead of either being interspersed with source files or resulting in incredibly long source files with a test submodule.

I'm happy to move the tests into the client.rs source file if it hampers your dev experience, though. Changed in 5225095

@Wodann Wodann temporarily deployed to github-action-benchmark June 4, 2024 18:39 — with GitHub Actions Inactive
@agostbiro
Copy link
Member

Awesome thanks! We can move the tests to a submodule client/tests.rs if it gets too large.

@Wodann Wodann merged commit 47d71e7 into main Jun 4, 2024
34 checks passed
@Wodann Wodann deleted the refactor/split-rpc branch June 4, 2024 19:54
@Wodann Wodann added this to the EDR v0.4.1 milestone Jul 11, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 11, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
2 participants