Skip to content

Commit

Permalink
fix(transport): use HttpsConnector in HyperTransport (#1899)
Browse files Browse the repository at this point in the history
* fix(`transport`): use `HttpsConnector` in `HyperTransport`

* feature gate hyper-tls
  • Loading branch information
yash-atreya authored Jan 13, 2025
1 parent d89fc01 commit 249e6d8
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ wasmtimer = "0.4.0"

hyper = { version = "1.2", default-features = false }
hyper-util = "0.1"
hyper-tls = "0.6.0"
http-body-util = "0.1"
tokio = "1"
tokio-util = "0.7"
Expand Down
1 change: 1 addition & 0 deletions crates/provider/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ reqwest = [
"alloy-rpc-client/reqwest",
]
hyper = ["dep:alloy-transport-http", "dep:url", "alloy-rpc-client/hyper"]
hyper-tls = ["hyper", "alloy-transport-http/hyper-tls"]
ws = ["pubsub", "alloy-rpc-client/ws", "alloy-transport-ws"]
ipc = ["pubsub", "alloy-rpc-client/ipc", "alloy-transport-ipc"]
reqwest-default-tls = ["alloy-transport-http?/reqwest-default-tls"]
Expand Down
12 changes: 12 additions & 0 deletions crates/provider/src/provider/trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1828,6 +1828,18 @@ mod tests {
assert_eq!(result.to_string(), "0x");
}

#[tokio::test]
#[cfg(feature = "hyper-tls")]
async fn hyper_https() {
let url = "https://eth-mainnet.alchemyapi.io/v2/jGiK5vwDfC3F4r0bqukm-W2GqgdrxdSr";

// With the `hyper` feature enabled .on_builtin builds the provider based on
// `HyperTransport`.
let provider = ProviderBuilder::new().on_builtin(url).await.unwrap();

let _num = provider.get_block_number().await.unwrap();
}

#[tokio::test]
async fn test_empty_transactions() {
let provider = ProviderBuilder::new().on_anvil();
Expand Down
2 changes: 2 additions & 0 deletions crates/transport-http/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ tracing = { workspace = true, 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 }
hyper-tls = { workspace = true, optional = true }

# auth layer
alloy-rpc-types-engine = { workspace = true, optional = true }
Expand All @@ -60,6 +61,7 @@ hyper = [
"dep:tower",
"dep:tracing",
]
hyper-tls = ["hyper", "dep:hyper-tls"]
jwt-auth = [
"hyper",
"dep:alloy-rpc-types-engine",
Expand Down
13 changes: 12 additions & 1 deletion crates/transport-http/src/hyper_transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ use std::{future::Future, marker::PhantomData, pin::Pin, task};
use tower::Service;
use tracing::{debug, debug_span, trace, Instrument};

#[cfg(feature = "hyper-tls")]
type Hyper = hyper_util::client::legacy::Client<
hyper_tls::HttpsConnector<hyper_util::client::legacy::connect::HttpConnector>,
http_body_util::Full<::hyper::body::Bytes>,
>;

#[cfg(not(feature = "hyper-tls"))]
type Hyper = hyper_util::client::legacy::Client<
hyper_util::client::legacy::connect::HttpConnector,
http_body_util::Full<::hyper::body::Bytes>,
Expand Down Expand Up @@ -49,9 +56,13 @@ impl HyperClient {
pub fn new() -> Self {
let executor = hyper_util::rt::TokioExecutor::new();

#[cfg(feature = "hyper-tls")]
let service = hyper_util::client::legacy::Client::builder(executor)
.build(hyper_tls::HttpsConnector::new());

#[cfg(not(feature = "hyper-tls"))]
let service =
hyper_util::client::legacy::Client::builder(executor).build_http::<Full<Bytes>>();

Self { service, _pd: PhantomData }
}
}
Expand Down

0 comments on commit 249e6d8

Please sign in to comment.