From 21e1dc4d4cf8591caa6e6a87e19de623e1b45ec0 Mon Sep 17 00:00:00 2001 From: James Prestwich Date: Sat, 18 Jan 2025 10:37:41 -0500 Subject: [PATCH] refactor: change json-rpc trait names, relax bounds (#1921) * refactor: change json-rpc trait names, relax bounds * fix: missed one in a feature * fix: alphabetize imports --- crates/json-rpc/src/error.rs | 4 +- crates/json-rpc/src/lib.rs | 63 ++++++++++++++++------ crates/json-rpc/src/request.rs | 15 +++--- crates/json-rpc/src/response/error.rs | 10 ++-- crates/json-rpc/src/response/mod.rs | 10 ++-- crates/json-rpc/src/response/payload.rs | 10 ++-- crates/json-rpc/src/result.rs | 10 ++-- crates/provider/src/ext/debug.rs | 14 ++--- crates/provider/src/layers/cache.rs | 14 ++--- crates/provider/src/provider/caller.rs | 8 +-- crates/provider/src/provider/eth_call.rs | 22 ++++---- crates/provider/src/provider/prov_call.rs | 40 +++++++------- crates/provider/src/provider/root.rs | 2 +- crates/provider/src/provider/trait.rs | 12 ++--- crates/provider/src/provider/with_block.rs | 38 ++++++------- crates/rpc-client/src/batch.rs | 10 ++-- crates/rpc-client/src/call.rs | 34 ++++++------ crates/rpc-client/src/client.rs | 12 ++--- crates/rpc-client/src/poller.rs | 8 +-- 19 files changed, 184 insertions(+), 152 deletions(-) diff --git a/crates/json-rpc/src/error.rs b/crates/json-rpc/src/error.rs index 91a1ef69b52..acf4836c625 100644 --- a/crates/json-rpc/src/error.rs +++ b/crates/json-rpc/src/error.rs @@ -1,4 +1,4 @@ -use crate::{ErrorPayload, RpcReturn}; +use crate::{ErrorPayload, RpcRecv}; use serde_json::value::RawValue; /// An RPC error. @@ -55,7 +55,7 @@ pub enum RpcError> { impl RpcError where - ErrResp: RpcReturn, + ErrResp: RpcRecv, { /// Instantiate a new `ErrorResp` from an error response. pub const fn err_resp(err: ErrorPayload) -> Self { diff --git a/crates/json-rpc/src/lib.rs b/crates/json-rpc/src/lib.rs index c705b72e7ba..28975e9688c 100644 --- a/crates/json-rpc/src/lib.rs +++ b/crates/json-rpc/src/lib.rs @@ -76,7 +76,7 @@ #[macro_use] extern crate tracing; -use serde::{de::DeserializeOwned, Serialize}; +use serde::{de::DeserializeOwned, Deserialize, Serialize}; use std::fmt::Debug; mod common; @@ -105,33 +105,62 @@ pub use result::{ transform_response, transform_result, try_deserialize_ok, BorrowedRpcResult, RpcResult, }; -/// An object that can be used as a JSON-RPC parameter. +/// An object that can be sent over RPC. /// /// This marker trait is blanket-implemented for every qualifying type. It is -/// used to indicate that a type can be used as a JSON-RPC parameter. -pub trait RpcParam: Serialize + Clone + Debug + Send + Sync + Unpin {} +/// used to indicate that a type can be sent in the body of a JSON-RPC message. +pub trait RpcSend: Serialize + Clone + Debug + Send + Sync + Unpin {} -impl RpcParam for T where T: Serialize + Clone + Debug + Send + Sync + Unpin {} +impl RpcSend for T where T: Serialize + Clone + Debug + Send + Sync + Unpin {} -/// An object that can be used as a JSON-RPC return value. +/// An object that can be received over RPC. /// /// This marker trait is blanket-implemented for every qualifying type. It is -/// used to indicate that a type can be used as a JSON-RPC return value. +/// used to indicate that a type can be received in the body of a JSON-RPC +/// message. /// /// # Note /// -/// We add the `'static` lifetime bound to indicate that the type can't borrow. -/// This is a simplification that makes it easier to use the types in client -/// code. It is not suitable for use in server code. -pub trait RpcReturn: DeserializeOwned + Debug + Send + Sync + Unpin + 'static {} +/// We add the `'static` lifetime to the supertraits to indicate that the type +/// can't borrow. This is a simplification that makes it easier to use the +/// types in client code. Servers may prefer borrowing, using the [`RpcBorrow`] +/// trait. +pub trait RpcRecv: DeserializeOwned + Debug + Send + Sync + Unpin + 'static {} -impl RpcReturn for T where T: DeserializeOwned + Debug + Send + Sync + Unpin + 'static {} +impl RpcRecv for T where T: DeserializeOwned + Debug + Send + Sync + Unpin + 'static {} -/// An object that can be used as a JSON-RPC parameter and return value. +/// An object that can be received over RPC, borrowing from the the +/// deserialization context. /// /// This marker trait is blanket-implemented for every qualifying type. It is -/// used to indicate that a type can be used as both a JSON-RPC parameter and -/// return value. -pub trait RpcObject: RpcParam + RpcReturn {} +/// used to indicate that a type can be borrowed from the body of a wholly or +/// partially serialized JSON-RPC message. +pub trait RpcBorrow<'de>: Deserialize<'de> + Debug + Send + Sync + Unpin {} -impl RpcObject for T where T: RpcParam + RpcReturn {} +impl<'de, T> RpcBorrow<'de> for T where T: Deserialize<'de> + Debug + Send + Sync + Unpin {} + +/// An object that can be both sent and received over RPC. +/// +/// This marker trait is blanket-implemented for every qualifying type. It is +/// used to indicate that a type can be both sent and received in the body of a +/// JSON-RPC message. +/// +/// # Note +/// +/// We add the `'static` lifetime to the supertraits to indicate that the type +/// can't borrow. This is a simplification that makes it easier to use the +/// types in client code. Servers may prefer borrowing, using the +/// [`BorrowedRpcObject`] trait. +pub trait RpcObject: RpcSend + RpcRecv {} + +impl RpcObject for T where T: RpcSend + RpcRecv {} + +/// An object that can be both sent and received over RPC, borrowing from the +/// the deserialization context. +/// +/// This marker trait is blanket-implemented for every qualifying type. It is +/// used to indicate that a type can be both sent and received in the body of a +/// JSON-RPC message, and can borrow from the deserialization context. +pub trait BorrowedRpcObject<'de>: RpcBorrow<'de> + RpcSend {} + +impl<'de, T> BorrowedRpcObject<'de> for T where T: RpcBorrow<'de> + RpcSend {} diff --git a/crates/json-rpc/src/request.rs b/crates/json-rpc/src/request.rs index 4b413db44d6..e94757fb2ee 100644 --- a/crates/json-rpc/src/request.rs +++ b/crates/json-rpc/src/request.rs @@ -1,4 +1,4 @@ -use crate::{common::Id, RpcObject, RpcParam}; +use crate::{common::Id, RpcBorrow, RpcSend}; use alloy_primitives::{keccak256, B256}; use serde::{ de::{DeserializeOwned, MapAccess}, @@ -105,7 +105,7 @@ pub type PartiallySerializedRequest = Request>; impl Request where - Params: RpcParam, + Params: RpcSend, { /// Serialize the request parameters as a boxed [`RawValue`]. /// @@ -126,7 +126,7 @@ where impl Request<&Params> where Params: ToOwned, - Params::Owned: RpcParam, + Params::Owned: RpcSend, { /// Clone the request, including the request parameters. pub fn into_owned_params(self) -> Request { @@ -164,7 +164,7 @@ where // `jsonrpc` field impl Serialize for Request where - Params: RpcParam, + Params: RpcSend, { fn serialize(&self, serializer: S) -> Result where @@ -188,7 +188,7 @@ where impl<'de, Params> Deserialize<'de> for Request where - Params: RpcObject, + Params: RpcBorrow<'de>, { fn deserialize(deserializer: D) -> Result where @@ -197,7 +197,7 @@ where struct Visitor(PhantomData); impl<'de, Params> serde::de::Visitor<'de> for Visitor where - Params: RpcObject, + Params: RpcBorrow<'de>, { type Value = Request; @@ -297,7 +297,7 @@ pub struct SerializedRequest { impl std::convert::TryFrom> for SerializedRequest where - Params: RpcParam, + Params: RpcSend, { type Error = serde_json::Error; @@ -390,6 +390,7 @@ impl Serialize for SerializedRequest { #[cfg(test)] mod test { use super::*; + use crate::RpcObject; fn test_inner(t: T) { let ser = serde_json::to_string(&t).unwrap(); diff --git a/crates/json-rpc/src/response/error.rs b/crates/json-rpc/src/response/error.rs index 8b81d3e3be8..59cf7ec4f4a 100644 --- a/crates/json-rpc/src/response/error.rs +++ b/crates/json-rpc/src/response/error.rs @@ -14,7 +14,7 @@ use std::{ marker::PhantomData, }; -use crate::RpcObject; +use crate::RpcSend; const INTERNAL_ERROR: Cow<'static, str> = Cow::Borrowed("Internal error"); @@ -68,7 +68,7 @@ impl ErrorPayload { /// and additional data. pub const fn internal_error_with_obj(data: E) -> Self where - E: RpcObject, + E: RpcSend, { Self { code: -32603, message: INTERNAL_ERROR, data: Some(data) } } @@ -76,7 +76,7 @@ impl ErrorPayload { /// Create a new error payload for an internal error with a custom message pub const fn internal_error_with_message_and_obj(message: Cow<'static, str>, data: E) -> Self where - E: RpcObject, + E: RpcSend, { Self { code: -32603, message, data: Some(data) } } @@ -129,7 +129,7 @@ impl ErrorPayload { impl From for ErrorPayload where - T: std::error::Error + RpcObject, + T: std::error::Error + RpcSend, { fn from(value: T) -> Self { Self { code: -32603, message: INTERNAL_ERROR, data: Some(value) } @@ -138,7 +138,7 @@ where impl ErrorPayload where - E: RpcObject, + E: RpcSend, { /// Serialize the inner data into a [`RawValue`]. pub fn serialize_payload(&self) -> serde_json::Result { diff --git a/crates/json-rpc/src/response/mod.rs b/crates/json-rpc/src/response/mod.rs index 335ca84f42d..c3ced20d587 100644 --- a/crates/json-rpc/src/response/mod.rs +++ b/crates/json-rpc/src/response/mod.rs @@ -1,4 +1,4 @@ -use crate::{common::Id, RpcObject}; +use crate::{common::Id, RpcSend}; use serde::{ de::{DeserializeOwned, MapAccess, Visitor}, ser::SerializeMap, @@ -85,7 +85,7 @@ impl Response { /// Create a new error response for an internal error with additional data. pub const fn internal_error_with_obj(id: Id, data: ErrData) -> Self where - ErrData: RpcObject, + ErrData: RpcSend, { Self { id, payload: ResponsePayload::Failure(ErrorPayload::internal_error_with_obj(data)) } } @@ -98,7 +98,7 @@ impl Response { data: ErrData, ) -> Self where - ErrData: RpcObject, + ErrData: RpcSend, { Self { id, @@ -121,8 +121,8 @@ impl Response { impl Response where - Payload: RpcObject, - ErrData: RpcObject, + Payload: RpcSend, + ErrData: RpcSend, { /// Serialize the payload of this response. pub fn serialize_payload(&self) -> serde_json::Result { diff --git a/crates/json-rpc/src/response/payload.rs b/crates/json-rpc/src/response/payload.rs index 47527739bbc..bd92be7e830 100644 --- a/crates/json-rpc/src/response/payload.rs +++ b/crates/json-rpc/src/response/payload.rs @@ -1,4 +1,4 @@ -use crate::{ErrorPayload, RpcObject}; +use crate::{ErrorPayload, RpcSend}; use serde::{de::DeserializeOwned, Deserialize}; use serde_json::value::{to_raw_value, RawValue}; use std::borrow::{Borrow, Cow}; @@ -78,7 +78,7 @@ impl ResponsePayload { /// and additional data. pub const fn internal_error_with_obj(data: ErrData) -> Self where - ErrData: RpcObject, + ErrData: RpcSend, { Self::Failure(ErrorPayload::internal_error_with_obj(data)) } @@ -90,7 +90,7 @@ impl ResponsePayload { data: ErrData, ) -> Self where - ErrData: RpcObject, + ErrData: RpcSend, { Self::Failure(ErrorPayload::internal_error_with_message_and_obj(message, data)) } @@ -124,8 +124,8 @@ impl ResponsePayload { impl ResponsePayload where - Payload: RpcObject, - ErrData: RpcObject, + Payload: RpcSend, + ErrData: RpcSend, { /// Convert the inner types into a [`RawValue`] by serializing them. pub fn serialize_payload(&self) -> serde_json::Result { diff --git a/crates/json-rpc/src/result.rs b/crates/json-rpc/src/result.rs index 280d0505d39..b1b29daccf1 100644 --- a/crates/json-rpc/src/result.rs +++ b/crates/json-rpc/src/result.rs @@ -1,4 +1,4 @@ -use crate::{Response, ResponsePayload, RpcError, RpcReturn}; +use crate::{Response, ResponsePayload, RpcError, RpcRecv}; use serde_json::value::RawValue; use std::borrow::Borrow; @@ -24,7 +24,7 @@ pub type BorrowedRpcResult<'a, E> = RpcResult<&'a RawValue, E, &'a RawValue>; /// [`Id`]: crate::Id pub fn transform_response(response: Response) -> RpcResult where - ErrResp: RpcReturn, + ErrResp: RpcRecv, { match response { Response { payload: ResponsePayload::Failure(err_resp), .. } => { @@ -41,7 +41,7 @@ pub fn transform_result( response: Result, E>, ) -> Result> where - ErrResp: RpcReturn, + ErrResp: RpcRecv, { match response { Ok(resp) => transform_response(resp), @@ -55,8 +55,8 @@ pub fn try_deserialize_ok( ) -> RpcResult where J: Borrow, - T: RpcReturn, - ErrResp: RpcReturn, + T: RpcRecv, + ErrResp: RpcRecv, { let json = result?; let json = json.borrow().get(); diff --git a/crates/provider/src/ext/debug.rs b/crates/provider/src/ext/debug.rs index 137f53a8b47..10cd818ac45 100644 --- a/crates/provider/src/ext/debug.rs +++ b/crates/provider/src/ext/debug.rs @@ -1,6 +1,6 @@ //! This module extends the Ethereum JSON-RPC provider with the Debug namespace's RPC methods. use crate::Provider; -use alloy_json_rpc::RpcReturn; +use alloy_json_rpc::RpcRecv; use alloy_network::Network; use alloy_primitives::{hex, Bytes, TxHash, B256}; use alloy_rpc_types_debug::ExecutionWitness; @@ -72,7 +72,7 @@ pub trait DebugApi: Send + Sync { /// Reruns the transaction specified by the hash and returns the trace in a specified format. /// - /// This method allows for the trace to be returned as a type that implements `RpcReturn` and + /// This method allows for the trace to be returned as a type that implements `RpcRecv` and /// `serde::de::DeserializeOwned`. /// /// [GethDebugTracingOptions] can be used to specify the trace options. @@ -86,7 +86,7 @@ pub trait DebugApi: Send + Sync { trace_options: GethDebugTracingOptions, ) -> TransportResult where - R: RpcReturn + serde::de::DeserializeOwned; + R: RpcRecv + serde::de::DeserializeOwned; /// Reruns the transaction specified by the hash and returns the trace as a JSON object. /// @@ -122,7 +122,7 @@ pub trait DebugApi: Send + Sync { /// Reruns the transaction specified by the hash and returns the trace in a specified format. /// - /// This method allows for the trace to be returned as a type that implements `RpcReturn` and + /// This method allows for the trace to be returned as a type that implements `RpcRecv` and /// `serde::de::DeserializeOwned`. /// /// [GethDebugTracingOptions] can be used to specify the trace options. @@ -137,7 +137,7 @@ pub trait DebugApi: Send + Sync { trace_options: GethDebugTracingCallOptions, ) -> TransportResult where - R: RpcReturn + serde::de::DeserializeOwned; + R: RpcRecv + serde::de::DeserializeOwned; /// Reruns the transaction specified by the hash and returns the trace as a JSON object. /// @@ -309,7 +309,7 @@ where trace_options: GethDebugTracingOptions, ) -> TransportResult where - R: RpcReturn, + R: RpcRecv, { self.client().request("debug_traceTransaction", (hash, trace_options)).await } @@ -337,7 +337,7 @@ where trace_options: GethDebugTracingCallOptions, ) -> TransportResult where - R: RpcReturn, + R: RpcRecv, { self.client().request("debug_traceCall", (tx, block, trace_options)).await } diff --git a/crates/provider/src/layers/cache.rs b/crates/provider/src/layers/cache.rs index 17d4afdc9a5..91397a07072 100644 --- a/crates/provider/src/layers/cache.rs +++ b/crates/provider/src/layers/cache.rs @@ -1,6 +1,6 @@ use crate::{ParamsWithBlock, Provider, ProviderCall, ProviderLayer, RootProvider, RpcWithBlock}; use alloy_eips::BlockId; -use alloy_json_rpc::{RpcError, RpcObject, RpcParam}; +use alloy_json_rpc::{RpcError, RpcObject, RpcSend}; use alloy_network::Network; use alloy_primitives::{ keccak256, Address, BlockHash, Bytes, StorageKey, StorageValue, TxHash, B256, U256, U64, @@ -377,13 +377,13 @@ where } /// Internal type to handle different types of requests and generating their param hashes. -struct RequestType { +struct RequestType { method: &'static str, params: Params, block_id: Option, } -impl RequestType { +impl RequestType { const fn new(method: &'static str, params: Params) -> Self { Self { method, params, block_id: None } } @@ -505,12 +505,14 @@ impl SharedCache { } } -/// Attempts to fetch the response from the cache by using the hash of the request params. +/// Attempts to fetch the response from the cache by using the hash of the +/// request params. /// -/// In case of a cache miss, fetches from the RPC and saves the response to the cache. +/// In case of a cache miss, fetches from the RPC and saves the response to the +/// cache. /// /// This helps overriding [`Provider`] methods that return [`TransportResult`]. -async fn cache_get_or_fetch( +async fn cache_get_or_fetch( cache: &SharedCache, req: RequestType, fetch_fn: impl std::future::Future>>, diff --git a/crates/provider/src/provider/caller.rs b/crates/provider/src/provider/caller.rs index 78cebeb0946..e8e1825bb5e 100644 --- a/crates/provider/src/provider/caller.rs +++ b/crates/provider/src/provider/caller.rs @@ -1,6 +1,6 @@ use super::EthCallParams; use crate::ProviderCall; -use alloy_json_rpc::RpcReturn; +use alloy_json_rpc::RpcRecv; use alloy_network::Network; use alloy_rpc_client::WeakClient; use alloy_transport::{TransportErrorKind, TransportResult}; @@ -9,7 +9,7 @@ use alloy_transport::{TransportErrorKind, TransportResult}; pub trait Caller: Send + Sync where N: Network, - Resp: RpcReturn, + Resp: RpcRecv, { /// Method that needs to be implemented to convert to a `ProviderCall`. /// @@ -30,7 +30,7 @@ where impl Caller for WeakClient where N: Network, - Resp: RpcReturn, + Resp: RpcRecv, { fn call( &self, @@ -47,7 +47,7 @@ where } } -fn provider_rpc_call( +fn provider_rpc_call( client: &WeakClient, method: &'static str, params: EthCallParams<'_, N>, diff --git a/crates/provider/src/provider/eth_call.rs b/crates/provider/src/provider/eth_call.rs index 429bfcefd2f..094f50b3986 100644 --- a/crates/provider/src/provider/eth_call.rs +++ b/crates/provider/src/provider/eth_call.rs @@ -1,5 +1,5 @@ use alloy_eips::BlockId; -use alloy_json_rpc::RpcReturn; +use alloy_json_rpc::RpcRecv; use alloy_network::Network; use alloy_rpc_types_eth::state::StateOverride; use alloy_transport::TransportResult; @@ -89,7 +89,7 @@ impl serde::Serialize for EthCallParams<'_, N> { pub struct EthCallFut<'req, N, Resp, Output, Map> where N: Network, - Resp: RpcReturn, + Resp: RpcRecv, Output: 'static, Map: Fn(Resp) -> Output, { @@ -99,7 +99,7 @@ where enum EthCallFutInner<'req, N, Resp, Output, Map> where N: Network, - Resp: RpcReturn, + Resp: RpcRecv, Map: Fn(Resp) -> Output, { Preparing { @@ -118,7 +118,7 @@ where impl core::fmt::Debug for EthCallFutInner<'_, N, Resp, Output, Map> where N: Network, - Resp: RpcReturn, + Resp: RpcRecv, Output: 'static, Map: Fn(Resp) -> Output, { @@ -136,7 +136,7 @@ where impl EthCallFut<'_, N, Resp, Output, Map> where N: Network, - Resp: RpcReturn, + Resp: RpcRecv, Output: 'static, Map: Fn(Resp) -> Output, { @@ -177,7 +177,7 @@ where impl Future for EthCallFut<'_, N, Resp, Output, Map> where N: Network, - Resp: RpcReturn, + Resp: RpcRecv, Output: 'static, Map: Fn(Resp) -> Output, { @@ -207,7 +207,7 @@ where pub struct EthCall<'req, N, Resp, Output = Resp, Map = fn(Resp) -> Output> where N: Network, - Resp: RpcReturn, + Resp: RpcRecv, Map: Fn(Resp) -> Output, { caller: Arc>, @@ -220,7 +220,7 @@ where impl core::fmt::Debug for EthCall<'_, N, Resp> where N: Network, - Resp: RpcReturn, + Resp: RpcRecv, { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { f.debug_struct("EthCall") @@ -233,7 +233,7 @@ where impl<'req, N, Resp> EthCall<'req, N, Resp> where N: Network, - Resp: RpcReturn, + Resp: RpcRecv, { /// Create a new [`EthCall`]. pub fn new( @@ -267,7 +267,7 @@ where impl<'req, N, Resp, Output, Map> EthCall<'req, N, Resp, Output, Map> where N: Network, - Resp: RpcReturn, + Resp: RpcRecv, Map: Fn(Resp) -> Output, { /// Map the response to a different type. This is usable for converting @@ -313,7 +313,7 @@ where impl<'req, N, Resp, Output, Map> std::future::IntoFuture for EthCall<'req, N, Resp, Output, Map> where N: Network, - Resp: RpcReturn, + Resp: RpcRecv, Output: 'static, Map: Fn(Resp) -> Output, { diff --git a/crates/provider/src/provider/prov_call.rs b/crates/provider/src/provider/prov_call.rs index e6e2bc928fa..7db97dfa1fa 100644 --- a/crates/provider/src/provider/prov_call.rs +++ b/crates/provider/src/provider/prov_call.rs @@ -1,4 +1,4 @@ -use alloy_json_rpc::{RpcParam, RpcReturn}; +use alloy_json_rpc::{RpcRecv, RpcSend}; use alloy_rpc_client::{RpcCall, Waiter}; use alloy_transport::TransportResult; use futures::FutureExt; @@ -24,8 +24,8 @@ use tokio::sync::oneshot; #[pin_project(project = ProviderCallProj)] pub enum ProviderCall Output> where - Params: RpcParam, - Resp: RpcReturn, + Params: RpcSend, + Resp: RpcRecv, Map: Fn(Resp) -> Output, { /// An underlying call to an RPC server. @@ -40,8 +40,8 @@ where impl ProviderCall where - Params: RpcParam, - Resp: RpcReturn, + Params: RpcSend, + Resp: RpcRecv, Map: Fn(Resp) -> Output, { /// Instantiate a new [`ProviderCall`] from the output. @@ -156,9 +156,9 @@ where impl ProviderCall<&Params, Resp, Output, Map> where - Params: RpcParam + ToOwned, - Params::Owned: RpcParam, - Resp: RpcReturn, + Params: RpcSend + ToOwned, + Params::Owned: RpcSend, + Resp: RpcRecv, Map: Fn(Resp) -> Output, { /// Convert this call into one with owned params, by cloning the params. @@ -176,8 +176,8 @@ where impl std::fmt::Debug for ProviderCall where - Params: RpcParam, - Resp: RpcReturn, + Params: RpcSend, + Resp: RpcRecv, { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { @@ -192,8 +192,8 @@ where impl From> for ProviderCall where - Params: RpcParam, - Resp: RpcReturn, + Params: RpcSend, + Resp: RpcRecv, Map: Fn(Resp) -> Output, { fn from(call: RpcCall) -> Self { @@ -203,8 +203,8 @@ where impl From> for ProviderCall Resp> where - Params: RpcParam, - Resp: RpcReturn, + Params: RpcSend, + Resp: RpcRecv, { fn from(waiter: Waiter) -> Self { Self::Waiter(waiter) @@ -214,8 +214,8 @@ where impl From> + Send>>> for ProviderCall where - Params: RpcParam, - Resp: RpcReturn, + Params: RpcSend, + Resp: RpcRecv, Map: Fn(Resp) -> Output, { fn from(fut: Pin> + Send>>) -> Self { @@ -226,8 +226,8 @@ where impl From>>> for ProviderCall where - Params: RpcParam, - Resp: RpcReturn, + Params: RpcSend, + Resp: RpcRecv, { fn from(rx: oneshot::Receiver>>) -> Self { Waiter::from(rx).into() @@ -236,8 +236,8 @@ where impl Future for ProviderCall where - Params: RpcParam, - Resp: RpcReturn, + Params: RpcSend, + Resp: RpcRecv, Output: 'static, Map: Fn(Resp) -> Output, { diff --git a/crates/provider/src/provider/root.rs b/crates/provider/src/provider/root.rs index 0c929343e67..3aa75eabc9c 100644 --- a/crates/provider/src/provider/root.rs +++ b/crates/provider/src/provider/root.rs @@ -90,7 +90,7 @@ impl RootProvider { /// Gets the subscription corresponding to the given RPC subscription ID. #[cfg(feature = "pubsub")] - pub async fn get_subscription( + pub async fn get_subscription( &self, id: alloy_primitives::B256, ) -> alloy_transport::TransportResult> { diff --git a/crates/provider/src/provider/trait.rs b/crates/provider/src/provider/trait.rs index 7f3be71891a..13ab21db79d 100644 --- a/crates/provider/src/provider/trait.rs +++ b/crates/provider/src/provider/trait.rs @@ -10,7 +10,7 @@ use crate::{ }; use alloy_consensus::BlockHeader; use alloy_eips::eip2718::Encodable2718; -use alloy_json_rpc::{RpcError, RpcParam, RpcReturn}; +use alloy_json_rpc::{RpcError, RpcRecv, RpcSend}; use alloy_network::{Ethereum, Network}; use alloy_network_primitives::{BlockResponse, BlockTransactionsKind, ReceiptResponse}; use alloy_primitives::{ @@ -501,7 +501,7 @@ pub trait Provider: Send + Sync { /// The return value depends on what stream `id` corresponds to. /// See [`FilterChanges`] for all possible return values. #[auto_impl(keep_default_for(&, &mut, Rc, Arc, Box))] - async fn get_filter_changes(&self, id: U256) -> TransportResult> + async fn get_filter_changes(&self, id: U256) -> TransportResult> where Self: Sized, { @@ -951,8 +951,8 @@ pub trait Provider: Send + Sync { #[auto_impl(keep_default_for(&, &mut, Rc, Arc, Box))] async fn subscribe(&self, params: P) -> TransportResult> where - P: RpcParam, - R: RpcReturn, + P: RpcSend, + R: RpcRecv, Self: Sized, { self.root().pubsub_frontend()?; @@ -1017,8 +1017,8 @@ pub trait Provider: Send + Sync { /// [`PubsubUnavailable`]: alloy_transport::TransportErrorKind::PubsubUnavailable async fn raw_request(&self, method: Cow<'static, str>, params: P) -> TransportResult where - P: RpcParam, - R: RpcReturn, + P: RpcSend, + R: RpcRecv, Self: Sized, { self.client().request(method, ¶ms).await diff --git a/crates/provider/src/provider/with_block.rs b/crates/provider/src/provider/with_block.rs index 9f8caf8a0a1..742ab2a2db3 100644 --- a/crates/provider/src/provider/with_block.rs +++ b/crates/provider/src/provider/with_block.rs @@ -1,5 +1,5 @@ use alloy_eips::BlockId; -use alloy_json_rpc::{RpcParam, RpcReturn}; +use alloy_json_rpc::{RpcRecv, RpcSend}; use alloy_primitives::B256; use alloy_rpc_client::RpcCall; use alloy_transport::TransportResult; @@ -9,14 +9,14 @@ use crate::ProviderCall; /// Helper struct that houses the params along with the BlockId. #[derive(Debug, Clone)] -pub struct ParamsWithBlock { +pub struct ParamsWithBlock { /// The params to be sent to the RPC call. pub params: Params, /// The block id to be used for the RPC call. pub block_id: BlockId, } -impl serde::Serialize for ParamsWithBlock { +impl serde::Serialize for ParamsWithBlock { fn serialize(&self, serializer: S) -> Result where S: serde::Serializer, @@ -45,8 +45,8 @@ type ProviderCallProducer = /// Container for varous types of calls dependent on a block id. enum WithBlockInner Output> where - Params: RpcParam, - Resp: RpcReturn, + Params: RpcSend, + Resp: RpcRecv, Map: Fn(Resp) -> Output, { /// [RpcCall] which params are getting wrapped into [ParamsWithBlock] once the block id is set. @@ -57,8 +57,8 @@ where impl core::fmt::Debug for WithBlockInner where - Params: RpcParam, - Resp: RpcReturn, + Params: RpcSend, + Resp: RpcRecv, Map: Fn(Resp) -> Output, { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { @@ -78,8 +78,8 @@ where #[derive(Debug)] pub struct RpcWithBlock Output> where - Params: RpcParam, - Resp: RpcReturn, + Params: RpcSend, + Resp: RpcRecv, Map: Fn(Resp) -> Output + Clone, { inner: WithBlockInner, @@ -88,8 +88,8 @@ where impl RpcWithBlock where - Params: RpcParam, - Resp: RpcReturn, + Params: RpcSend, + Resp: RpcRecv, Map: Fn(Resp) -> Output + Clone, { /// Create a new [`RpcWithBlock`] from a [`RpcCall`]. @@ -110,8 +110,8 @@ where impl From> for RpcWithBlock where - Params: RpcParam, - Resp: RpcReturn, + Params: RpcSend, + Resp: RpcRecv, Map: Fn(Resp) -> Output + Clone, { fn from(inner: RpcCall) -> Self { @@ -121,8 +121,8 @@ where impl From for RpcWithBlock where - Params: RpcParam, - Resp: RpcReturn, + Params: RpcSend, + Resp: RpcRecv, Map: Fn(Resp) -> Output + Clone, F: Fn(BlockId) -> ProviderCall, Resp, Output, Map> + Send + 'static, { @@ -133,8 +133,8 @@ where impl RpcWithBlock where - Params: RpcParam, - Resp: RpcReturn, + Params: RpcSend, + Resp: RpcRecv, Map: Fn(Resp) -> Output + Clone, { /// Set the block id. @@ -188,8 +188,8 @@ where impl IntoFuture for RpcWithBlock where - Params: RpcParam, - Resp: RpcReturn, + Params: RpcSend, + Resp: RpcRecv, Output: 'static, Map: Fn(Resp) -> Output + Clone, { diff --git a/crates/rpc-client/src/batch.rs b/crates/rpc-client/src/batch.rs index d0aad50a00e..7112e2f6d1f 100644 --- a/crates/rpc-client/src/batch.rs +++ b/crates/rpc-client/src/batch.rs @@ -1,7 +1,7 @@ use crate::{client::RpcClientInner, ClientRef}; use alloy_json_rpc::{ - transform_response, try_deserialize_ok, Id, Request, RequestPacket, ResponsePacket, RpcParam, - RpcReturn, SerializedRequest, + transform_response, try_deserialize_ok, Id, Request, RequestPacket, ResponsePacket, RpcRecv, + RpcSend, SerializedRequest, }; use alloy_primitives::map::HashMap; use alloy_transport::{ @@ -80,7 +80,7 @@ impl From>>> for Waiter std::future::Future for Waiter where - Resp: RpcReturn, + Resp: RpcRecv, Map: FnOnce(Resp) -> Output, { type Output = TransportResult; @@ -135,7 +135,7 @@ impl<'a> BatchRequest<'a> { rx } - fn push( + fn push( &mut self, request: Request, ) -> TransportResult> { @@ -148,7 +148,7 @@ impl<'a> BatchRequest<'a> { /// ### Errors /// /// If the request cannot be serialized, this will return an error. - pub fn add_call( + pub fn add_call( &mut self, method: impl Into>, params: &Params, diff --git a/crates/rpc-client/src/call.rs b/crates/rpc-client/src/call.rs index 4efa940a7cb..f2e9a60a61e 100644 --- a/crates/rpc-client/src/call.rs +++ b/crates/rpc-client/src/call.rs @@ -1,6 +1,6 @@ use alloy_json_rpc::{ - transform_response, try_deserialize_ok, Request, RequestPacket, ResponsePacket, RpcParam, - RpcResult, RpcReturn, + transform_response, try_deserialize_ok, Request, RequestPacket, ResponsePacket, RpcRecv, + RpcResult, RpcSend, }; use alloy_transport::{BoxTransport, IntoBoxTransport, RpcFut, TransportError, TransportResult}; use core::panic; @@ -20,7 +20,7 @@ use tower::Service; #[pin_project::pin_project(project = CallStateProj)] enum CallState where - Params: RpcParam, + Params: RpcSend, { Prepared { request: Option>, @@ -35,7 +35,7 @@ where impl Clone for CallState where - Params: RpcParam, + Params: RpcSend, { fn clone(&self) -> Self { match self { @@ -49,7 +49,7 @@ where impl fmt::Debug for CallState where - Params: RpcParam, + Params: RpcSend, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str(match self { @@ -62,7 +62,7 @@ where impl Future for CallState where - Params: RpcParam, + Params: RpcSend, { type Output = TransportResult>; @@ -134,7 +134,7 @@ where #[derive(Clone)] pub struct RpcCall Output> where - Params: RpcParam, + Params: RpcSend, Map: FnOnce(Resp) -> Output, { #[pin] @@ -145,7 +145,7 @@ where impl core::fmt::Debug for RpcCall where - Params: RpcParam, + Params: RpcSend, Map: FnOnce(Resp) -> Output, { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { @@ -155,7 +155,7 @@ where impl RpcCall where - Params: RpcParam, + Params: RpcSend, { #[doc(hidden)] pub fn new(req: Request, connection: impl IntoBoxTransport) -> Self { @@ -172,7 +172,7 @@ where impl RpcCall where - Params: RpcParam, + Params: RpcSend, Map: FnOnce(Resp) -> Output, { /// Map the response to a different type. This is usable for converting @@ -257,7 +257,7 @@ where } /// Map the params of the request into a new type. - pub fn map_params( + pub fn map_params( self, map: impl Fn(Params) -> NewParams, ) -> RpcCall { @@ -275,8 +275,8 @@ where impl RpcCall<&Params, Resp, Output, Map> where - Params: RpcParam + ToOwned, - Params::Owned: RpcParam, + Params: RpcSend + ToOwned, + Params::Owned: RpcSend, Map: FnOnce(Resp) -> Output, { /// Convert this call into one with owned params, by cloning the params. @@ -300,8 +300,8 @@ where impl<'a, Params, Resp, Output, Map> RpcCall where - Params: RpcParam + 'a, - Resp: RpcReturn, + Params: RpcSend + 'a, + Resp: RpcRecv, Output: 'static, Map: FnOnce(Resp) -> Output + Send + 'a, { @@ -313,8 +313,8 @@ where impl Future for RpcCall where - Params: RpcParam, - Resp: RpcReturn, + Params: RpcSend, + Resp: RpcRecv, Output: 'static, Map: FnOnce(Resp) -> Output, { diff --git a/crates/rpc-client/src/client.rs b/crates/rpc-client/src/client.rs index 653875cc3a5..6ad6a3911e4 100644 --- a/crates/rpc-client/src/client.rs +++ b/crates/rpc-client/src/client.rs @@ -1,5 +1,5 @@ use crate::{poller::PollerBuilder, BatchRequest, ClientBuilder, RpcCall}; -use alloy_json_rpc::{Id, Request, RpcParam, RpcReturn}; +use alloy_json_rpc::{Id, Request, RpcRecv, RpcSend}; use alloy_transport::{BoxTransport, IntoBoxTransport}; use alloy_transport_http::Http; use std::{ @@ -101,8 +101,8 @@ impl RpcClient { params: Params, ) -> PollerBuilder where - Params: RpcParam + 'static, - Resp: RpcReturn + Clone, + Params: RpcSend + 'static, + Resp: RpcRecv + Clone, { PollerBuilder::new(self.get_weak(), method, params) } @@ -230,7 +230,7 @@ impl RpcClientInner { /// /// To send a request, use [`RpcClientInner::request`] and await the returned [`RpcCall`]. #[inline] - pub fn make_request( + pub fn make_request( &self, method: impl Into>, params: Params, @@ -278,7 +278,7 @@ impl RpcClientInner { /// This means that if a serializer error occurs, it will not be caught until the call is /// awaited. #[doc(alias = "prepare")] - pub fn request( + pub fn request( &self, method: impl Into>, params: Params, @@ -290,7 +290,7 @@ impl RpcClientInner { /// Prepares an [`RpcCall`] with no parameters. /// /// See [`request`](Self::request) for more details. - pub fn request_noparams( + pub fn request_noparams( &self, method: impl Into>, ) -> RpcCall { diff --git a/crates/rpc-client/src/poller.rs b/crates/rpc-client/src/poller.rs index cd9c40cc818..3ffd9e0a306 100644 --- a/crates/rpc-client/src/poller.rs +++ b/crates/rpc-client/src/poller.rs @@ -1,5 +1,5 @@ use crate::WeakClient; -use alloy_json_rpc::{RpcError, RpcParam, RpcReturn}; +use alloy_json_rpc::{RpcError, RpcRecv, RpcSend}; use alloy_transport::utils::Spawnable; use futures::{Stream, StreamExt}; use serde::Serialize; @@ -81,8 +81,8 @@ pub struct PollerBuilder { impl PollerBuilder where - Params: RpcParam + 'static, - Resp: RpcReturn + Clone, + Params: RpcSend + 'static, + Resp: RpcRecv + Clone, { /// Create a new poller task. pub fn new(client: WeakClient, method: impl Into>, params: Params) -> Self { @@ -248,7 +248,7 @@ impl DerefMut for PollChannel { impl PollChannel where - Resp: RpcReturn + Clone, + Resp: RpcRecv + Clone, { /// Resubscribe to the poller task. pub fn resubscribe(&self) -> Self {