From 674554fcb22831875a7100b03da82b9652bd6c53 Mon Sep 17 00:00:00 2001 From: Yash Atreya <44857776+yash-atreya@users.noreply.github.com> Date: Mon, 23 Sep 2024 20:10:51 +0530 Subject: [PATCH] fix(rpc-client): add test for BuiltInConnString.connect_boxed --- crates/provider/src/provider/root.rs | 1 + crates/provider/src/provider/trait.rs | 18 ++++++++++++++++++ crates/rpc-client/src/builtin.rs | 21 ++++++++++++--------- 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/crates/provider/src/provider/root.rs b/crates/provider/src/provider/root.rs index a47c8f28824..8e7d76fa930 100644 --- a/crates/provider/src/provider/root.rs +++ b/crates/provider/src/provider/root.rs @@ -68,6 +68,7 @@ impl RootProvider { /// Creates a new root provider from the provided connection details. pub async fn connect_builtin(s: &str) -> Result { let conn: BuiltInConnectionString = s.parse()?; + let client = ClientBuilder::default().connect_boxed(conn).await?; Ok(Self::new(client)) } diff --git a/crates/provider/src/provider/trait.rs b/crates/provider/src/provider/trait.rs index 003569c9164..2badb9747f9 100644 --- a/crates/provider/src/provider/trait.rs +++ b/crates/provider/src/provider/trait.rs @@ -1038,6 +1038,7 @@ mod tests { use alloy_network::AnyNetwork; use alloy_node_bindings::Anvil; use alloy_primitives::{address, b256, bytes, keccak256}; + use alloy_rpc_client::BuiltInConnectionString; use alloy_rpc_types_eth::{request::TransactionRequest, Block}; // For layer transport tests #[cfg(feature = "hyper")] @@ -1672,6 +1673,23 @@ mod tests { } } + #[tokio::test] + async fn builtin_connect_boxed() { + init_tracing(); + let anvil = Anvil::new().spawn(); + + let conn: BuiltInConnectionString = anvil.endpoint().parse().unwrap(); + + let transport = conn.connect_boxed().await.unwrap(); + + let client = alloy_rpc_client::RpcClient::new(transport, true); + + let provider = RootProvider::::new(client); + + let num = provider.get_block_number().await.unwrap(); + assert_eq!(0, num); + } + #[tokio::test] async fn test_uncle_count() { init_tracing(); diff --git a/crates/rpc-client/src/builtin.rs b/crates/rpc-client/src/builtin.rs index 6b95963a090..911e115ef86 100644 --- a/crates/rpc-client/src/builtin.rs +++ b/crates/rpc-client/src/builtin.rs @@ -62,17 +62,15 @@ impl BuiltInConnectionString { // reqwest is enabled, hyper is not #[cfg(all(not(feature = "hyper"), feature = "reqwest"))] Self::Http(url) => { - Ok( - alloy_transport::Transport::boxed( - alloy_transport_http::Http::::new(url.clone()) - ) - ) - }, + Ok(alloy_transport::Transport::boxed( + alloy_transport_http::Http::::new(url.clone()), + )) + } // hyper is enabled, reqwest is not #[cfg(feature = "hyper")] - Self::Http(_) => Err(TransportErrorKind::custom_str( - "hyper not supported by BuiltinConnectionString. Please instantiate a hyper client manually", + Self::Http(url) => Ok(alloy_transport::Transport::boxed( + alloy_transport_http::HyperTransport::new_hyper(url.clone()), )), #[cfg(all(not(target_arch = "wasm32"), feature = "ws"))] @@ -95,7 +93,12 @@ impl BuiltInConnectionString { .await .map(alloy_transport::Transport::boxed), - #[cfg(not(any(feature = "reqwest", feature = "hyper", feature = "ws", feature = "ipc")))] + #[cfg(not(any( + feature = "reqwest", + feature = "hyper", + feature = "ws", + feature = "ipc" + )))] _ => Err(TransportErrorKind::custom_str( "No transports enabled. Enable one of: reqwest, hyper, ws, ipc", )),