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

chore(deps): update to hyper 1.0 #55

Merged
merged 4 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -87,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"
Expand Down
1 change: 0 additions & 1 deletion crates/consensus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
7 changes: 5 additions & 2 deletions crates/rpc-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ 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-util = { workspace = true, optional = true }

url = { workspace = true, optional = true }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
Expand All @@ -46,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"]
9 changes: 5 additions & 4 deletions crates/rpc-client/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,16 @@ impl<L> ClientBuilder<L> {
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<L::Service>
where
L: Layer<alloy_transport_http::Http<hyper::client::Client<hyper::client::HttpConnector>>>,
L: Layer<alloy_transport_http::Http<alloy_transport_http::HyperClient>>,
L::Service: Transport,
{
let transport = alloy_transport_http::Http::new(url);
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)
Expand Down
15 changes: 12 additions & 3 deletions crates/transport-http/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,24 @@ 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]
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: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"]
39 changes: 20 additions & 19 deletions crates/transport-http/src/hyper.rs
Original file line number Diff line number Diff line change
@@ -1,43 +1,42 @@
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<C> Http<Client<C>>
impl<C, B> Http<Client<C, Full<B>>>
where
C: Connect + Clone + Send + Sync + 'static,
B: From<Bytes> + 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<RawValue> into a hyper request<B>
let body: Box<str> = ser.into();
let body: Box<[u8]> = body.into();
let body = Full::from(Bytes::from(<Box<[u8]>>::from(<Box<str>>::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 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();

if status != hyper::StatusCode::OK {
return Err(TransportErrorKind::custom_str(&format!(
Expand All @@ -57,9 +56,10 @@ where
}
}

impl<C> Service<RequestPacket> for &Http<Client<C>>
impl<C, B> Service<RequestPacket> for &Http<Client<C, Full<B>>>
where
C: Connect + Clone + Send + Sync + 'static,
B: From<Bytes> + Buf + Send + 'static,
{
type Response = ResponsePacket;
type Error = TransportError;
Expand All @@ -73,13 +73,14 @@ where

#[inline]
fn call(&mut self, req: RequestPacket) -> Self::Future {
self.request(req)
self.request_hyper(req)
}
}

impl<C> Service<RequestPacket> for Http<Client<C>>
impl<C, B> Service<RequestPacket> for Http<Client<C, Full<B>>>
where
C: Connect + Clone + Send + Sync + 'static,
B: From<Bytes> + Buf + Send + 'static,
{
type Response = ResponsePacket;
type Error = TransportError;
Expand All @@ -93,6 +94,6 @@ where

#[inline]
fn call(&mut self, req: RequestPacket) -> Self::Future {
self.request(req)
self.request_hyper(req)
}
}
11 changes: 9 additions & 2 deletions crates/transport-http/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -33,8 +40,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<T> {
client: T,
Expand Down
6 changes: 3 additions & 3 deletions crates/transport-http/src/reqwest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use tower::Service;

impl Http<reqwest::Client> {
/// 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
Expand Down Expand Up @@ -40,7 +40,7 @@ impl Service<RequestPacket> for Http<reqwest::Client> {

#[inline]
fn call(&mut self, req: RequestPacket) -> Self::Future {
self.request(req)
self.request_reqwest(req)
}
}

Expand All @@ -57,6 +57,6 @@ impl Service<RequestPacket> for &Http<reqwest::Client> {

#[inline]
fn call(&mut self, req: RequestPacket) -> Self::Future {
self.request(req)
self.request_reqwest(req)
}
}
11 changes: 4 additions & 7 deletions crates/transport/src/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,11 @@ impl Service<RequestPacket> for BoxTransport {
#[cfg(test)]
mod test {
use super::*;
/// checks trait + send + sync + 'static

// checks trait + send + sync + 'static
fn __compile_check() {
fn inner<T: CloneTransport>(_: Option<T>) {
todo!()
}
fn inner_2<T: Transport>(_: Option<T>) {
todo!()
}
fn inner<T: CloneTransport>(_: Option<T>) {}
fn inner_2<T: Transport>(_: Option<T>) {}
inner::<BoxTransport>(None);
inner::<BoxTransport>(None);
}
Expand Down
Loading