Skip to content

Commit

Permalink
make quorum params accessible
Browse files Browse the repository at this point in the history
  • Loading branch information
mattiekat committed Jan 10, 2023
1 parent 85140ac commit 0bf27d1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 28 deletions.
2 changes: 1 addition & 1 deletion ethers-providers/src/transports/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ mod ws;
pub use ws::{ClientError as WsClientError, Ws};

mod quorum;
pub use quorum::{JsonRpcClientWrapper, Quorum, QuorumError, QuorumProvider, WeightedProvider};
pub use quorum::{JsonRpcClientWrapper, Quorum, QuorumError, QuorumProvider, WeightedProvider, WrappedParams};

mod rw;
pub use rw::{RwClient, RwClientError};
Expand Down
63 changes: 36 additions & 27 deletions ethers-providers/src/transports/quorum.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::{
convert::{TryFrom, TryInto},
fmt::Debug,
future::Future,
pin::Pin,
Expand Down Expand Up @@ -169,7 +170,7 @@ impl<T: JsonRpcClientWrapper> QuorumProvider<T> {
async fn get_quorum_number<N>(
&self,
method: &str,
params: QuorumParams,
params: WrappedParams,
) -> Result<N, QuorumError>
where
N: Serialize + DeserializeOwned + Ord + Copy,
Expand Down Expand Up @@ -230,12 +231,12 @@ impl<T: JsonRpcClientWrapper> QuorumProvider<T> {

/// Returns the block height that a _quorum_ of providers have reached.
async fn get_quorum_block_number(&self) -> Result<U64, QuorumError> {
self.get_quorum_number("eth_blockNumber", QuorumParams::Zst).await
self.get_quorum_number("eth_blockNumber", WrappedParams::Zst).await
}

/// Normalizes the request payload depending on the call
async fn normalize_request(&self, method: &str, q_params: &mut QuorumParams) {
let params = if let QuorumParams::Value(v) = q_params {
async fn normalize_request(&self, method: &str, q_params: &mut WrappedParams) {
let params = if let WrappedParams::Value(v) = q_params {
v
} else {
// at this time no normalization is required for calls with zero parameters.
Expand Down Expand Up @@ -433,7 +434,7 @@ impl From<QuorumError> for ProviderError {
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
pub trait JsonRpcClientWrapper: Send + Sync + Debug {
async fn request(&self, method: &str, params: QuorumParams) -> Result<Value, ProviderError>;
async fn request(&self, method: &str, params: WrappedParams) -> Result<Value, ProviderError>;
}
type NotificationStream =
Box<dyn futures_core::Stream<Item = Box<RawValue>> + Send + Unpin + 'static>;
Expand All @@ -449,8 +450,8 @@ pub trait PubsubClientWrapper: JsonRpcClientWrapper {
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
impl<C: JsonRpcClient> JsonRpcClientWrapper for C {
async fn request(&self, method: &str, params: QuorumParams) -> Result<Value, ProviderError> {
let fut = if let QuorumParams::Value(params) = params {
async fn request(&self, method: &str, params: WrappedParams) -> Result<Value, ProviderError> {
let fut = if let WrappedParams::Value(params) = params {
JsonRpcClient::request(self, method, params)
} else {
JsonRpcClient::request(self, method, ())
Expand All @@ -462,15 +463,15 @@ impl<C: JsonRpcClient> JsonRpcClientWrapper for C {
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
impl JsonRpcClientWrapper for Box<dyn JsonRpcClientWrapper> {
async fn request(&self, method: &str, params: QuorumParams) -> Result<Value, ProviderError> {
async fn request(&self, method: &str, params: WrappedParams) -> Result<Value, ProviderError> {
self.as_ref().request(method, params).await
}
}

#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
impl JsonRpcClientWrapper for Box<dyn PubsubClientWrapper> {
async fn request(&self, method: &str, params: QuorumParams) -> Result<Value, ProviderError> {
async fn request(&self, method: &str, params: WrappedParams) -> Result<Value, ProviderError> {
self.as_ref().request(method, params).await
}
}
Expand Down Expand Up @@ -511,12 +512,7 @@ where
method: &str,
params: T,
) -> Result<R, Self::Error> {
let mut params = if std::mem::size_of::<T>() == 0 {
// we don't want `()` to become `"null"`.
QuorumParams::Zst
} else {
QuorumParams::Value(serde_json::to_value(params)?)
};
let mut params = params.try_into()?;
self.normalize_request(method, &mut params).await;

match method {
Expand Down Expand Up @@ -699,16 +695,29 @@ where
/// This is necessary because the wrapper provider is supposed to skip the `params` if it's of
/// size 0, see `crate::transports::common::Request`
#[derive(Clone)]
pub enum QuorumParams {
pub enum WrappedParams {
Value(Value),
Zst,
}

impl<T: Serialize> TryFrom<T> for WrappedParams {
type Error = serde_json::Error;

fn try_from(value: T) -> Result<Self, Self::Error> {
Ok(if std::mem::size_of::<T>() == 0 {
// we don't want `()` to become `"null"`.
WrappedParams::Zst
} else {
WrappedParams::Value(serde_json::to_value(params)?)
})
}
}

#[cfg(test)]
#[cfg(not(target_arch = "wasm32"))]
mod tests {
use super::{Quorum, QuorumProvider, WeightedProvider};
use crate::{transports::quorum::QuorumParams, Middleware, MockProvider, Provider};
use crate::{transports::quorum::WrappedParams, Middleware, MockProvider, Provider};
use ethers_core::types::{U256, U64};

async fn test_quorum(q: Quorum) {
Expand Down Expand Up @@ -770,7 +779,7 @@ mod tests {
.quorum(Quorum::ProviderCount(5))
.build();
assert_eq!(
quorum.get_quorum_number::<U64>("foo", QuorumParams::Zst).await.unwrap().as_u64(),
quorum.get_quorum_number::<U64>("foo", WrappedParams::Zst).await.unwrap().as_u64(),
68
);

Expand All @@ -779,7 +788,7 @@ mod tests {
.quorum(Quorum::ProviderCount(4))
.build();
assert_eq!(
quorum.get_quorum_number::<U64>("foo", QuorumParams::Zst).await.unwrap().as_u64(),
quorum.get_quorum_number::<U64>("foo", WrappedParams::Zst).await.unwrap().as_u64(),
100
);

Expand All @@ -788,7 +797,7 @@ mod tests {
.quorum(Quorum::ProviderCount(3))
.build();
assert_eq!(
quorum.get_quorum_number::<U64>("foo", QuorumParams::Zst).await.unwrap().as_u64(),
quorum.get_quorum_number::<U64>("foo", WrappedParams::Zst).await.unwrap().as_u64(),
100
);

Expand All @@ -797,7 +806,7 @@ mod tests {
.quorum(Quorum::ProviderCount(2))
.build();
assert_eq!(
quorum.get_quorum_number::<U64>("foo", QuorumParams::Zst).await.unwrap().as_u64(),
quorum.get_quorum_number::<U64>("foo", WrappedParams::Zst).await.unwrap().as_u64(),
101
);

Expand All @@ -806,7 +815,7 @@ mod tests {
.quorum(Quorum::ProviderCount(1))
.build();
assert_eq!(
quorum.get_quorum_number::<U64>("foo", QuorumParams::Zst).await.unwrap().as_u64(),
quorum.get_quorum_number::<U64>("foo", WrappedParams::Zst).await.unwrap().as_u64(),
102
);

Expand All @@ -815,7 +824,7 @@ mod tests {
.quorum(Quorum::Majority)
.build();
assert_eq!(
quorum.get_quorum_number::<U64>("foo", QuorumParams::Zst).await.unwrap().as_u64(),
quorum.get_quorum_number::<U64>("foo", WrappedParams::Zst).await.unwrap().as_u64(),
100
);
}
Expand Down Expand Up @@ -846,7 +855,7 @@ mod tests {
.quorum(Quorum::ProviderCount(2))
.build();
assert_eq!(
quorum.get_quorum_number::<U64>("foo", QuorumParams::Zst).await.unwrap().as_u64(),
quorum.get_quorum_number::<U64>("foo", WrappedParams::Zst).await.unwrap().as_u64(),
100
);

Expand All @@ -855,7 +864,7 @@ mod tests {
.quorum(Quorum::Majority)
.build();
assert_eq!(
quorum.get_quorum_number::<U64>("foo", QuorumParams::Zst).await.unwrap().as_u64(),
quorum.get_quorum_number::<U64>("foo", WrappedParams::Zst).await.unwrap().as_u64(),
100
);
}
Expand All @@ -882,13 +891,13 @@ mod tests {
.add_providers(providers.clone())
.quorum(Quorum::ProviderCount(2))
.build();
assert!(quorum.get_quorum_number::<U64>("foo", QuorumParams::Zst).await.is_err());
assert!(quorum.get_quorum_number::<U64>("foo", WrappedParams::Zst).await.is_err());

let quorum = QuorumProvider::builder()
.add_providers(providers.clone())
.quorum(Quorum::Majority)
.build();
assert!(quorum.get_quorum_number::<U64>("foo", QuorumParams::Zst).await.is_err());
assert!(quorum.get_quorum_number::<U64>("foo", WrappedParams::Zst).await.is_err());
}

#[tokio::test]
Expand Down

0 comments on commit 0bf27d1

Please sign in to comment.