From 900179f1591b105e6f215f7eda6a2ef8614ade80 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Thu, 21 Mar 2024 05:12:56 +0100 Subject: [PATCH 1/4] chore(deps): bump hyper to 1.0 --- Cargo.toml | 4 ++- crates/consensus/Cargo.toml | 1 - crates/rpc-client/Cargo.toml | 6 ++++- crates/rpc-client/src/builder.rs | 17 ++++++++++--- crates/transport-http/Cargo.toml | 6 +++-- crates/transport-http/src/hyper.rs | 38 ++++++++++++++-------------- crates/transport-http/src/reqwest.rs | 6 ++--- 7 files changed, 47 insertions(+), 31 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c08b5214b98..5714b88719e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -69,7 +69,9 @@ futures-util = "0.3" futures-executor = "0.3" futures-utils-wasm = "0.1" -hyper = "0.14" +hyper = { version = "1.2", default-features = false } +hyper-util = "0.1" +http-body-util = "0.1" tokio = "1" tokio-util = "0.7" tokio-stream = "0.1" diff --git a/crates/consensus/Cargo.toml b/crates/consensus/Cargo.toml index aa0bc90de86..6a0d9df0297 100644 --- a/crates/consensus/Cargo.toml +++ b/crates/consensus/Cargo.toml @@ -24,7 +24,6 @@ arbitrary = { workspace = true, features = ["derive"], optional = true } [dev-dependencies] alloy-signer.workspace = true -# arbitrary arbitrary = { workspace = true, features = ["derive"] } k256.workspace = true tokio = { workspace = true, features = ["macros"] } diff --git a/crates/rpc-client/Cargo.toml b/crates/rpc-client/Cargo.toml index 83ac2e89d71..1ad8d859cbb 100644 --- a/crates/rpc-client/Cargo.toml +++ b/crates/rpc-client/Cargo.toml @@ -28,8 +28,12 @@ tracing.workspace = true alloy-primitives = { workspace = true, optional = true } alloy-pubsub = { workspace = true, optional = true } alloy-transport-ws = { workspace = true, optional = true } -hyper = { workspace = true, optional = true } + reqwest = { workspace = true, optional = true } + +hyper = { workspace = true, optional = true } +hyper-util = { workspace = true, optional = true } + url = { workspace = true, optional = true } [target.'cfg(not(target_arch = "wasm32"))'.dependencies] diff --git a/crates/rpc-client/src/builder.rs b/crates/rpc-client/src/builder.rs index 212915c7b18..028009b0a99 100644 --- a/crates/rpc-client/src/builder.rs +++ b/crates/rpc-client/src/builder.rs @@ -65,13 +65,22 @@ impl ClientBuilder { #[cfg(all(not(target_arch = "wasm32"), feature = "hyper"))] pub fn hyper_http(self, url: url::Url) -> RpcClient where - L: Layer>>, + L: Layer< + alloy_transport_http::Http< + hyper_util::client::legacy::Client< + hyper_util::client::legacy::connect::HttpConnector, + hyper::body::Bytes, + >, + >, + >, L::Service: Transport, { - let transport = alloy_transport_http::Http::new(url); - let is_local = transport.guess_local(); + let _ = url; + todo!() + // let transport = alloy_transport_http::Http::new(url); + // let is_local = transport.guess_local(); - self.transport(transport, is_local) + // self.transport(transport, is_local) } /// Connect a pubsub transport, producing an [`RpcClient`] with the provided diff --git a/crates/transport-http/Cargo.toml b/crates/transport-http/Cargo.toml index 052831d51f4..246e6251c64 100644 --- a/crates/transport-http/Cargo.toml +++ b/crates/transport-http/Cargo.toml @@ -22,12 +22,14 @@ tower = { workspace = true, optional = true } reqwest = { workspace = true, features = ["serde_json", "json"], optional = true } [target.'cfg(not(target_arch = "wasm32"))'.dependencies] -hyper = { workspace = true, features = ["full"], optional = true } +http-body-util = { workspace = true, optional = true } +hyper = { workspace = true, default-features = false, optional = true } +hyper-util = { workspace = true, features = ["full"], optional = true } [features] default = ["reqwest", "reqwest-default-tls"] reqwest = ["dep:reqwest", "dep:alloy-json-rpc", "dep:serde_json", "dep:tower"] -hyper = ["dep:hyper", "dep:alloy-json-rpc", "dep:serde_json", "dep:tower"] +hyper = ["dep:hyper", "dep:hyper-util", "dep:alloy-json-rpc", "dep:serde_json", "dep:tower"] reqwest-default-tls = ["reqwest?/default-tls"] reqwest-native-tls = ["reqwest?/native-tls"] reqwest-rustls-tls = ["reqwest?/rustls-tls"] diff --git a/crates/transport-http/src/hyper.rs b/crates/transport-http/src/hyper.rs index c8ae8686831..a519614ea20 100644 --- a/crates/transport-http/src/hyper.rs +++ b/crates/transport-http/src/hyper.rs @@ -1,43 +1,41 @@ use crate::Http; use alloy_json_rpc::{RequestPacket, ResponsePacket}; use alloy_transport::{TransportError, TransportErrorKind, TransportFut}; +use http_body_util::{BodyExt, Full}; use hyper::{ - body::Bytes, - client::{connect::Connect, Client}, + body::{Buf, Bytes}, + header, }; +use hyper_util::client::legacy::{connect::Connect, Client}; use std::task; use tower::Service; -impl Http> +impl Http>> where C: Connect + Clone + Send + Sync + 'static, + B: From + Buf + Send + 'static, { /// Make a request. - fn request(&self, req: RequestPacket) -> TransportFut<'static> { + fn request_hyper(&self, req: RequestPacket) -> TransportFut<'static> { let this = self.clone(); Box::pin(async move { let ser = req.serialize().map_err(TransportError::ser_err)?; // convert the Box into a hyper request - let body: Box = ser.into(); - let body: Box<[u8]> = body.into(); + let body = Full::from(Bytes::from(>::from(>::from(ser)))); let req = hyper::Request::builder() .method(hyper::Method::POST) .uri(this.url.as_str()) - .header("content-type", "application/json") - .body(hyper::Body::from(Bytes::from(body))) + .header(header::CONTENT_TYPE, header::HeaderValue::from_static("application/json")) + .body(body) .expect("request parts are valid"); let resp = this.client.request(req).await.map_err(TransportErrorKind::custom)?; - let status = resp.status(); - // unpack data from the response body. We do this regardless of - // the status code, as we want to return the error in the body if - // there is one. - let body = hyper::body::to_bytes(resp.into_body()) - .await - .map_err(TransportErrorKind::custom)?; + // unpack json from the response body + let body = + resp.into_body().collect().await.map_err(TransportErrorKind::custom)?.to_bytes(); if status != hyper::StatusCode::OK { return Err(TransportErrorKind::custom_str(&format!( @@ -57,9 +55,10 @@ where } } -impl Service for &Http> +impl Service for &Http>> where C: Connect + Clone + Send + Sync + 'static, + B: From + Buf + Send + 'static, { type Response = ResponsePacket; type Error = TransportError; @@ -73,13 +72,14 @@ where #[inline] fn call(&mut self, req: RequestPacket) -> Self::Future { - self.request(req) + self.request_hyper(req) } } -impl Service for Http> +impl Service for Http>> where C: Connect + Clone + Send + Sync + 'static, + B: From + Buf + Send + 'static, { type Response = ResponsePacket; type Error = TransportError; @@ -93,6 +93,6 @@ where #[inline] fn call(&mut self, req: RequestPacket) -> Self::Future { - self.request(req) + self.request_hyper(req) } } diff --git a/crates/transport-http/src/reqwest.rs b/crates/transport-http/src/reqwest.rs index aef8d89faf9..c9e3bba9185 100644 --- a/crates/transport-http/src/reqwest.rs +++ b/crates/transport-http/src/reqwest.rs @@ -7,7 +7,7 @@ use tower::Service; impl Http { /// Make a request. - fn request(&self, req: RequestPacket) -> TransportFut<'static> { + fn request_reqwest(&self, req: RequestPacket) -> TransportFut<'static> { let this = self.clone(); Box::pin(async move { let resp = this @@ -40,7 +40,7 @@ impl Service for Http { #[inline] fn call(&mut self, req: RequestPacket) -> Self::Future { - self.request(req) + self.request_reqwest(req) } } @@ -57,6 +57,6 @@ impl Service for &Http { #[inline] fn call(&mut self, req: RequestPacket) -> Self::Future { - self.request(req) + self.request_reqwest(req) } } From c236dac4ed663a7da56a64fbd929efe13715aa7d Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Thu, 21 Mar 2024 05:19:19 +0100 Subject: [PATCH 2/4] fixes --- crates/transport-http/Cargo.toml | 9 ++++++++- crates/transport-http/src/hyper.rs | 3 ++- crates/transport-http/src/lib.rs | 4 ++-- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/crates/transport-http/Cargo.toml b/crates/transport-http/Cargo.toml index 246e6251c64..68aa58403b0 100644 --- a/crates/transport-http/Cargo.toml +++ b/crates/transport-http/Cargo.toml @@ -29,7 +29,14 @@ hyper-util = { workspace = true, features = ["full"], optional = true } [features] default = ["reqwest", "reqwest-default-tls"] reqwest = ["dep:reqwest", "dep:alloy-json-rpc", "dep:serde_json", "dep:tower"] -hyper = ["dep:hyper", "dep:hyper-util", "dep:alloy-json-rpc", "dep:serde_json", "dep:tower"] +hyper = [ + "dep:hyper", + "dep:hyper-util", + "dep:http-body-util", + "dep:alloy-json-rpc", + "dep:serde_json", + "dep:tower", +] reqwest-default-tls = ["reqwest?/default-tls"] reqwest-native-tls = ["reqwest?/native-tls"] reqwest-rustls-tls = ["reqwest?/rustls-tls"] diff --git a/crates/transport-http/src/hyper.rs b/crates/transport-http/src/hyper.rs index a519614ea20..b5b33844582 100644 --- a/crates/transport-http/src/hyper.rs +++ b/crates/transport-http/src/hyper.rs @@ -33,7 +33,8 @@ where let resp = this.client.request(req).await.map_err(TransportErrorKind::custom)?; let status = resp.status(); - // unpack json from the response body + // Unpack data from the response body. We do this regardless of the status code, as we + // want to return the error in the body if there is one. let body = resp.into_body().collect().await.map_err(TransportErrorKind::custom)?.to_bytes(); diff --git a/crates/transport-http/src/lib.rs b/crates/transport-http/src/lib.rs index e72dd651153..ae5cc6ae56e 100644 --- a/crates/transport-http/src/lib.rs +++ b/crates/transport-http/src/lib.rs @@ -33,8 +33,8 @@ mod reqwest; /// [`Transport`]: alloy_transport::Transport /// /// Currently supported clients are: -#[cfg_attr(feature = "reqwest", doc = " - [`::reqwest::Client`]")] -#[cfg_attr(feature = "hyper", doc = " - [`::hyper::client::Client`]")] +#[cfg_attr(feature = "reqwest", doc = " - [`reqwest`](::reqwest::Client)")] +#[cfg_attr(feature = "hyper", doc = " - [`hyper`](hyper_util::client::legacy::Client)")] #[derive(Debug, Clone)] pub struct Http { client: T, From 466d729f1e25c1dbcae2e12176f5ca4898c3609d Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Thu, 21 Mar 2024 05:28:34 +0100 Subject: [PATCH 3/4] fix: un-todo --- crates/rpc-client/Cargo.toml | 3 +-- crates/rpc-client/src/builder.rs | 22 +++++++--------------- crates/transport-http/src/lib.rs | 7 +++++++ crates/transport/src/boxed.rs | 11 ++++------- 4 files changed, 19 insertions(+), 24 deletions(-) diff --git a/crates/rpc-client/Cargo.toml b/crates/rpc-client/Cargo.toml index 1ad8d859cbb..406e500848d 100644 --- a/crates/rpc-client/Cargo.toml +++ b/crates/rpc-client/Cargo.toml @@ -31,7 +31,6 @@ alloy-transport-ws = { workspace = true, optional = true } reqwest = { workspace = true, optional = true } -hyper = { workspace = true, optional = true } hyper-util = { workspace = true, optional = true } url = { workspace = true, optional = true } @@ -50,7 +49,7 @@ futures-util.workspace = true [features] default = ["reqwest"] reqwest = ["dep:url", "dep:reqwest", "alloy-transport-http/reqwest"] -hyper = ["dep:url", "dep:hyper", "alloy-transport-http/hyper"] +hyper = ["dep:url", "dep:hyper-util", "alloy-transport-http/hyper"] pubsub = ["dep:alloy-pubsub", "dep:alloy-primitives"] ws = ["pubsub", "dep:alloy-transport-ws"] ipc = ["pubsub", "dep:alloy-transport-ipc"] diff --git a/crates/rpc-client/src/builder.rs b/crates/rpc-client/src/builder.rs index 028009b0a99..2c5a3e1371b 100644 --- a/crates/rpc-client/src/builder.rs +++ b/crates/rpc-client/src/builder.rs @@ -60,27 +60,19 @@ impl ClientBuilder { self.transport(transport, is_local) } - /// Convenience function to create a new [`RpcClient`] with a [`hyper`] - /// HTTP transport. + /// Convenience function to create a new [`RpcClient`] with a `hyper` HTTP transport. #[cfg(all(not(target_arch = "wasm32"), feature = "hyper"))] pub fn hyper_http(self, url: url::Url) -> RpcClient where - L: Layer< - alloy_transport_http::Http< - hyper_util::client::legacy::Client< - hyper_util::client::legacy::connect::HttpConnector, - hyper::body::Bytes, - >, - >, - >, + L: Layer>, L::Service: Transport, { - let _ = url; - todo!() - // let transport = alloy_transport_http::Http::new(url); - // let is_local = transport.guess_local(); + let executor = hyper_util::rt::TokioExecutor::new(); + let client = hyper_util::client::legacy::Client::builder(executor).build_http(); + let transport = alloy_transport_http::Http::with_client(client, url); + let is_local = transport.guess_local(); - // self.transport(transport, is_local) + self.transport(transport, is_local) } /// Connect a pubsub transport, producing an [`RpcClient`] with the provided diff --git a/crates/transport-http/src/lib.rs b/crates/transport-http/src/lib.rs index ae5cc6ae56e..9d446491e1b 100644 --- a/crates/transport-http/src/lib.rs +++ b/crates/transport-http/src/lib.rs @@ -21,6 +21,13 @@ use url::Url; #[cfg(all(not(target_arch = "wasm32"), feature = "hyper"))] mod hyper; +/// A [`hyper`](::hyper) HTTP client. +#[cfg(all(not(target_arch = "wasm32"), feature = "hyper"))] +pub type HyperClient = hyper_util::client::legacy::Client< + hyper_util::client::legacy::connect::HttpConnector, + http_body_util::Full<::hyper::body::Bytes>, +>; + #[cfg(feature = "reqwest")] mod reqwest; diff --git a/crates/transport/src/boxed.rs b/crates/transport/src/boxed.rs index 8ab8acc79a3..6571e7322e4 100644 --- a/crates/transport/src/boxed.rs +++ b/crates/transport/src/boxed.rs @@ -85,14 +85,11 @@ impl Service for BoxTransport { #[cfg(test)] mod test { use super::*; - /// checks trait + send + sync + 'static + + // checks trait + send + sync + 'static fn __compile_check() { - fn inner(_: Option) { - todo!() - } - fn inner_2(_: Option) { - todo!() - } + fn inner(_: Option) {} + fn inner_2(_: Option) {} inner::(None); inner::(None); } From 298e9e1309def301ce61fa4a45a86ef604714dde Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Thu, 21 Mar 2024 05:34:59 +0100 Subject: [PATCH 4/4] chore: bump reqwest --- Cargo.toml | 2 +- crates/transport-http/Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5714b88719e..4bcdd9b9a74 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -89,7 +89,7 @@ itertools = "0.12" once_cell = "1.19" pin-project = "1.1" rand = "0.8" -reqwest = { version = "0.11", default-features = false } +reqwest = { version = "0.12", default-features = false } semver = "1.0" thiserror = "1.0" thiserror-no-std = "2.0.2" diff --git a/crates/transport-http/Cargo.toml b/crates/transport-http/Cargo.toml index 68aa58403b0..818172343e4 100644 --- a/crates/transport-http/Cargo.toml +++ b/crates/transport-http/Cargo.toml @@ -19,7 +19,7 @@ url.workspace = true serde_json = { workspace = true, optional = true } tower = { workspace = true, optional = true } -reqwest = { workspace = true, features = ["serde_json", "json"], optional = true } +reqwest = { workspace = true, features = ["json"], optional = true } [target.'cfg(not(target_arch = "wasm32"))'.dependencies] http-body-util = { workspace = true, optional = true }