Skip to content

Commit

Permalink
Replace rustls with boring-ssl
Browse files Browse the repository at this point in the history
This removes all re-attempts present in monero-serai's RPC and is an attempt to
narrow down the sporadic failures.

Inspired by hyperium/hyper#3427
  • Loading branch information
kayabaNerve committed Nov 29, 2023
1 parent d1122a6 commit 53d4902
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 25 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,5 @@ lazy_static = { git = "https://github.com/rust-lang-nursery/lazy-static.rs", rev
# subxt *can* pull these off crates.io yet there's no benefit to this
sp-core-hashing = { git = "https://github.com/serai-dex/substrate" }
sp-std = { git = "https://github.com/serai-dex/substrate" }

hyper-boring = { git = "https://github.com/cloudflare/boring", rev = "423c260d87b69a926594ded0dd693b5cf1220452" }
6 changes: 3 additions & 3 deletions coins/bitcoin/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use thiserror::Error;
use serde::{Deserialize, de::DeserializeOwned};
use serde_json::json;

use simple_request::{hyper, Request, Client};
use simple_request::{hyper, Full, Request, Client};

use bitcoin::{
hashes::{Hash, hex::FromHex},
Expand Down Expand Up @@ -110,11 +110,11 @@ impl Rpc {
let mut request = Request::from(
hyper::Request::post(&self.url)
.header("Content-Type", "application/json")
.body(
.body(Full::new(
serde_json::to_vec(&json!({ "jsonrpc": "2.0", "method": method, "params": params }))
.unwrap()
.into(),
)
))
.unwrap(),
);
request.with_basic_auth();
Expand Down
15 changes: 12 additions & 3 deletions coins/monero/src/rpc/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use tokio::sync::Mutex;

use digest_auth::{WwwAuthenticateHeader, AuthContext};
use simple_request::{
hyper::{StatusCode, header::HeaderValue, Request},
hyper::{header::HeaderValue, Request},
Response, Client,
};

Expand Down Expand Up @@ -102,7 +102,9 @@ impl HttpRpc {
connection: Arc::new(Mutex::new((challenge, client))),
}
} else {
Authentication::Unauthenticated(Client::with_connection_pool())
Authentication::Unauthenticated(
Client::with_connection_pool().map_err(|e| RpcError::ConnectionError(format!("{e:?}")))?,
)
};

Ok(Rpc(HttpRpc { authentication, url }))
Expand Down Expand Up @@ -258,7 +260,14 @@ impl HttpRpc {
});
}

unreachable!()
let mut res = Vec::with_capacity(128);
response
.body()
.await
.map_err(|e| RpcError::ConnectionError(format!("{e:?}")))?
.read_to_end(&mut res)
.unwrap();
Ok(res)
}
}

Expand Down
6 changes: 1 addition & 5 deletions coins/monero/src/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,7 @@ impl<R: RpcConnection> Rpc<R> {
.0
.post(
route,
if let Some(params) = params {
serde_json::to_string(&params).unwrap().into_bytes()
} else {
vec![]
},
if let Some(params) = params { serde_json::to_vec(&params).unwrap() } else { vec![] },
)
.await?;
let res_str = std_shims::str::from_utf8(&res)
Expand Down
4 changes: 2 additions & 2 deletions common/request/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ rustdoc-args = ["--cfg", "docsrs"]
hyper = { version = "0.14", default-features = false, features = ["http1", "tcp", "client", "runtime", "backports", "deprecated"] }
tokio = { version = "1", default-features = false }

hyper-rustls = { version = "0.24", default-features = false, features = ["http1", "native-tokio"], optional = true }
hyper-boring = { version = "4", default-features = false, features = ["runtime"], optional = true }

zeroize = { version = "1", optional = true }
base64ct = { version = "1", features = ["alloc"], optional = true }

[features]
tls = ["hyper-rustls"]
tls = ["hyper-boring"]
basic-auth = ["zeroize", "base64ct"]
default = ["tls"]
26 changes: 15 additions & 11 deletions common/request/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::sync::Arc;
use tokio::sync::Mutex;

#[cfg(feature = "tls")]
use hyper_rustls::{HttpsConnectorBuilder, HttpsConnector};
use hyper_boring::HttpsConnector;
use hyper::{
Uri,
header::HeaderValue,
Expand Down Expand Up @@ -36,37 +36,41 @@ type Connector = HttpConnector;
#[cfg(feature = "tls")]
type Connector = HttpsConnector<HttpConnector>;

#[derive(Clone, Debug)]
#[derive(Clone)]
enum Connection {
ConnectionPool(hyper::Client<Connector>),
Connection { connector: Connector, host: Uri, connection: Arc<Mutex<Option<SendRequest<Body>>>> },
}
impl core::fmt::Debug for Connection {
fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
fmt.debug_struct("Connection").finish_non_exhaustive()
}
}

#[derive(Clone, Debug)]
pub struct Client {
connection: Connection,
}

impl Client {
fn connector() -> Connector {
fn connector() -> Result<Connector, Error> {
#[cfg(feature = "tls")]
let res =
HttpsConnectorBuilder::new().with_native_roots().https_or_http().enable_http1().build();
let res = HttpsConnector::new().map_err(|e| Error::ConnectionError(format!("{e:?}").into()))?;
#[cfg(not(feature = "tls"))]
let res = HttpConnector::new();
res
Ok(res)
}

pub fn with_connection_pool() -> Client {
Client {
connection: Connection::ConnectionPool(hyper::Client::builder().build(Self::connector())),
}
pub fn with_connection_pool() -> Result<Client, Error> {
Ok(Client {
connection: Connection::ConnectionPool(hyper::Client::builder().build(Self::connector()?)),
})
}

pub fn without_connection_pool(host: String) -> Result<Client, Error> {
Ok(Client {
connection: Connection::Connection {
connector: Self::connector(),
connector: Self::connector()?,
host: {
let uri: Uri = host.parse().map_err(|_| Error::InvalidUri)?;
if uri.host().is_none() {
Expand Down
2 changes: 1 addition & 1 deletion substrate/client/src/serai/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ impl Serai {
}

pub async fn new(url: String) -> Result<Self, SeraiError> {
let client = Client::with_connection_pool();
let client = Client::with_connection_pool().map_err(|_| SeraiError::ConnectionError)?;
let mut res = Serai { url, client, genesis: [0xfe; 32] };
res.genesis = res.block_hash(0).await?.ok_or_else(|| {
SeraiError::InvalidNode("node didn't have the first block's hash".to_string())
Expand Down

0 comments on commit 53d4902

Please sign in to comment.