Skip to content

Commit 8e79856

Browse files
author
Yeastplume
authored
Upgrade Tokio to v1.x and Hyper to v0.14 (#3804)
* update of tokio and related dependencies to 1.x * update to hyper 0.14 * fixes to http connector for tests
1 parent b93d88b commit 8e79856

File tree

12 files changed

+320
-457
lines changed

12 files changed

+320
-457
lines changed

Cargo.lock

Lines changed: 186 additions & 361 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/Cargo.toml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ edition = "2018"
1111

1212
[dependencies]
1313
easy-jsonrpc-mw = "0.5.4"
14-
hyper = "0.13"
14+
hyper = { version = "0.14", features = ["full"] }
1515
lazy_static = "1"
1616
regex = "1"
1717
ring = "0.16"
@@ -20,15 +20,17 @@ serde_derive = "1"
2020
serde_json = "1"
2121
thiserror = "1"
2222
log = "0.4"
23-
tokio = { version = "0.2", features = ["full"] }
24-
tokio-rustls = "0.13"
23+
tokio = { version = "1", features = ["full"] }
24+
tokio-rustls = "0.23"
2525
http = "0.2"
26-
hyper-rustls = "0.20"
27-
hyper-timeout = "0.3"
26+
hyper-rustls = "0.23"
27+
hyper-timeout = "0.4"
2828
futures = "0.3"
29-
rustls = "0.17"
29+
rustls = "0.20"
30+
rustls-pemfile = "1.0"
31+
async-stream = "0.3"
3032
url = "2.1"
31-
bytes = "0.5"
33+
bytes = "1"
3234

3335
grin_core = { path = "../core", version = "5.4.0-alpha.0" }
3436
grin_chain = { path = "../chain", version = "5.4.0-alpha.0" }

api/src/client.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,12 @@ where
216216
}
217217

218218
async fn send_request_async(req: Request<Body>, timeout: TimeOut) -> Result<String, Error> {
219-
let https = hyper_rustls::HttpsConnector::new();
219+
let https = hyper_rustls::HttpsConnectorBuilder::new()
220+
.with_native_roots()
221+
.https_or_http()
222+
.enable_http1()
223+
.build();
224+
220225
let (connect, read, write) = (
221226
Some(timeout.connect),
222227
Some(timeout.read),
@@ -242,16 +247,15 @@ async fn send_request_async(req: Request<Body>, timeout: TimeOut) -> Result<Stri
242247
.into());
243248
}
244249

245-
let raw = body::to_bytes(resp)
250+
let raw = body::to_bytes(resp.into_body())
246251
.await
247252
.map_err(|e| Error::RequestError(format!("Cannot read response body: {}", e)))?;
248253

249254
Ok(String::from_utf8_lossy(&raw).to_string())
250255
}
251256

252257
pub fn send_request(req: Request<Body>, timeout: TimeOut) -> Result<String, Error> {
253-
let mut rt = Builder::new()
254-
.basic_scheduler()
258+
let rt = Builder::new_current_thread()
255259
.enable_all()
256260
.build()
257261
.map_err(|e| Error::RequestError(format!("{}", e)))?;

api/src/json_rpc.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ pub enum Error {
9797
/// Json error
9898
Json(serde_json::Error),
9999
/// Client error
100-
Hyper(hyper::error::Error),
100+
Hyper(hyper::Error),
101101
/// Error response
102102
Rpc(RpcError),
103103
/// Response to a request did not have the expected nonce
@@ -120,8 +120,8 @@ impl From<serde_json::Error> for Error {
120120
}
121121
}
122122

123-
impl From<hyper::error::Error> for Error {
124-
fn from(e: hyper::error::Error) -> Error {
123+
impl From<hyper::Error> for Error {
124+
fn from(e: hyper::Error) -> Error {
125125
Error::Hyper(e)
126126
}
127127
}

api/src/rest.rs

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,17 @@
2121
use crate::router::{Handler, HandlerObj, ResponseFuture, Router, RouterError};
2222
use crate::web::response;
2323
use futures::channel::oneshot;
24-
use futures::TryStreamExt;
2524
use hyper::server::accept;
2625
use hyper::service::make_service_fn;
2726
use hyper::{Body, Request, Server, StatusCode};
28-
use rustls::internal::pemfile;
27+
use rustls_pemfile as pemfile;
2928
use std::convert::Infallible;
3029
use std::fs::File;
3130
use std::net::SocketAddr;
3231
use std::sync::Arc;
3332
use std::{io, thread};
3433
use tokio::net::TcpListener;
3534
use tokio::runtime::Runtime;
36-
use tokio::stream::StreamExt;
3735
use tokio_rustls::TlsAcceptor;
3836

3937
/// Errors that can be returned by an ApiEndpoint implementation.
@@ -83,8 +81,10 @@ impl TLSConfig {
8381
})?;
8482
let mut reader = io::BufReader::new(certfile);
8583

86-
pemfile::certs(&mut reader)
87-
.map_err(|_| Error::Internal("failed to load certificate".to_string()))
84+
let certs = pemfile::certs(&mut reader)
85+
.map_err(|_| Error::Internal("failed to load certificate".to_string()))?;
86+
87+
Ok(certs.into_iter().map(rustls::Certificate).collect())
8888
}
8989

9090
fn load_private_key(&self) -> Result<rustls::PrivateKey, Error> {
@@ -97,15 +97,19 @@ impl TLSConfig {
9797
if keys.len() != 1 {
9898
return Err(Error::Internal("expected a single private key".to_string()));
9999
}
100-
Ok(keys[0].clone())
100+
Ok(rustls::PrivateKey(keys[0].clone()))
101101
}
102102

103103
pub fn build_server_config(&self) -> Result<Arc<rustls::ServerConfig>, Error> {
104104
let certs = self.load_certs()?;
105105
let key = self.load_private_key()?;
106-
let mut cfg = rustls::ServerConfig::new(rustls::NoClientAuth::new());
107-
cfg.set_single_cert(certs, key)
106+
107+
let cfg = rustls::ServerConfig::builder()
108+
.with_safe_defaults()
109+
.with_no_client_auth()
110+
.with_single_cert(certs, key)
108111
.map_err(|e| Error::Internal(format!("set single certificate failed {}", e)))?;
112+
109113
Ok(Arc::new(cfg))
110114
}
111115
}
@@ -175,7 +179,7 @@ impl ApiServer {
175179
server.await
176180
};
177181

178-
let mut rt = Runtime::new()
182+
let rt = Runtime::new()
179183
.map_err(|e| eprintln!("HTTP API server error: {}", e))
180184
.unwrap();
181185
if let Err(e) = rt.block_on(server) {
@@ -214,13 +218,26 @@ impl ApiServer {
214218
.name("apis".to_string())
215219
.spawn(move || {
216220
let server = async move {
217-
let mut listener = TcpListener::bind(&addr).await.expect("failed to bind");
218-
let listener = listener
219-
.incoming()
220-
.and_then(move |s| acceptor.accept(s))
221-
.filter(|r| r.is_ok());
221+
let listener = TcpListener::bind(&addr).await.expect("failed to bind");
222+
223+
let tls_stream = async_stream::stream! {
224+
loop {
225+
let (socket, _addr) = match listener.accept().await {
226+
Ok(conn) => conn,
227+
Err(e) => {
228+
eprintln!("Error accepting connection: {}", e);
229+
continue;
230+
}
231+
};
232+
233+
match acceptor.accept(socket).await {
234+
Ok(stream) => yield Ok::<_, std::io::Error>(stream),
235+
Err(_) => continue,
236+
}
237+
}
238+
};
222239

223-
let server = Server::builder(accept::from_stream(listener))
240+
let server = Server::builder(accept::from_stream(tls_stream))
224241
.serve(make_service_fn(move |_| {
225242
let router = router.clone();
226243
async move { Ok::<_, Infallible>(router) }
@@ -232,7 +249,7 @@ impl ApiServer {
232249
server.await
233250
};
234251

235-
let mut rt = Runtime::new()
252+
let rt = Runtime::new()
236253
.map_err(|e| eprintln!("HTTP API server error: {}", e))
237254
.unwrap();
238255
if let Err(e) = rt.block_on(server) {

api/src/web.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use crate::rest::*;
22
use crate::router::ResponseFuture;
3-
use bytes::Buf;
43
use futures::future::ok;
54
use hyper::body;
65
use hyper::{Body, Request, Response, StatusCode};
76
use serde::{Deserialize, Serialize};
87
use std::collections::HashMap;
98
use std::fmt::Debug;
9+
use std::io::Cursor;
1010
use url::form_urlencoded;
1111

1212
/// Parse request body
@@ -18,7 +18,8 @@ where
1818
.await
1919
.map_err(|e| Error::RequestError(format!("Failed to read request: {}", e)))?;
2020

21-
serde_json::from_reader(raw.bytes())
21+
let cursor = Cursor::new(raw);
22+
serde_json::from_reader(cursor)
2223
.map_err(|e| Error::RequestError(format!("Invalid request body: {}", e)))
2324
}
2425

core/src/core/pmmr/pmmr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ where
188188
B: 'a + Backend<T>,
189189
{
190190
/// Build a new prunable Merkle Mountain Range using the provided backend.
191-
pub fn new(backend: &'a mut B) -> PMMR<'_, T, B> {
191+
pub fn new(backend: &'a mut B) -> PMMR<'a, T, B> {
192192
PMMR {
193193
backend,
194194
size: 0,
@@ -198,7 +198,7 @@ where
198198

199199
/// Build a new prunable Merkle Mountain Range pre-initialized until
200200
/// size with the provided backend.
201-
pub fn at(backend: &'a mut B, size: u64) -> PMMR<'_, T, B> {
201+
pub fn at(backend: &'a mut B, size: u64) -> PMMR<'a, T, B> {
202202
PMMR {
203203
backend,
204204
size,

core/src/core/pmmr/readonly_pmmr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ where
4141
B: 'a + Backend<T>,
4242
{
4343
/// Build a new readonly PMMR.
44-
pub fn new(backend: &'a B) -> ReadonlyPMMR<'_, T, B> {
44+
pub fn new(backend: &'a B) -> ReadonlyPMMR<'a, T, B> {
4545
ReadonlyPMMR {
4646
backend,
4747
size: 0,
@@ -51,7 +51,7 @@ where
5151

5252
/// Build a new readonly PMMR pre-initialized to
5353
/// size with the provided backend.
54-
pub fn at(backend: &'a B, size: u64) -> ReadonlyPMMR<'_, T, B> {
54+
pub fn at(backend: &'a B, size: u64) -> ReadonlyPMMR<'a, T, B> {
5555
ReadonlyPMMR {
5656
backend,
5757
size,

core/src/core/pmmr/rewindable_pmmr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ where
4040
B: 'a + Backend<T>,
4141
{
4242
/// Build a new readonly PMMR.
43-
pub fn new(backend: &'a B) -> RewindablePMMR<'_, T, B> {
43+
pub fn new(backend: &'a B) -> RewindablePMMR<'a, T, B> {
4444
RewindablePMMR {
4545
backend,
4646
last_pos: 0,
@@ -50,7 +50,7 @@ where
5050

5151
/// Build a new readonly PMMR pre-initialized to
5252
/// last_pos with the provided backend.
53-
pub fn at(backend: &'a B, last_pos: u64) -> RewindablePMMR<'_, T, B> {
53+
pub fn at(backend: &'a B, last_pos: u64) -> RewindablePMMR<'a, T, B> {
5454
RewindablePMMR {
5555
backend,
5656
last_pos,

servers/Cargo.toml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ workspace = ".."
1010
edition = "2018"
1111

1212
[dependencies]
13-
hyper = "0.13"
14-
hyper-rustls = "0.20"
13+
hyper = { version = "0.14", features = ["full"] }
14+
hyper-rustls = "0.23"
1515
fs2 = "0.4"
1616
futures = "0.3"
1717
http = "0.2"
@@ -22,8 +22,10 @@ log = "0.4"
2222
serde_derive = "1"
2323
serde_json = "1"
2424
chrono = "0.4.11"
25-
tokio = {version = "0.2", features = ["full"] }
26-
tokio-util = { version = "0.2", features = ["codec"] }
25+
tokio = { version = "1", features = ["full"] }
26+
tokio-util = { version = "0.7", features = ["codec"] }
27+
async-stream = "0.3"
28+
rustls = "0.20"
2729
walkdir = "2.3.1"
2830

2931
grin_api = { path = "../api", version = "5.4.0-alpha.0" }

0 commit comments

Comments
 (0)