From 8518c7a549081755a63710f0eeeedc9d796e57fb Mon Sep 17 00:00:00 2001 From: ngutech21 Date: Mon, 27 Nov 2023 12:10:14 +0100 Subject: [PATCH] chore: upgrade to axum 0.7.1 --- Cargo.lock | 224 +++++++++++++++++++++++++++++++------- moksha-mint/Cargo.toml | 7 +- moksha-mint/src/error.rs | 3 +- moksha-mint/src/server.rs | 80 ++++++++------ 4 files changed, 235 insertions(+), 79 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c12746a9..4d6baa07 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -246,13 +246,46 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3b829e4e32b91e643de6eafe82b1d90675f5874230191a4ffbc1b336dec4d6bf" dependencies = [ "async-trait", - "axum-core", + "axum-core 0.3.4", "bitflags 1.3.2", "bytes", "futures-util", - "http", - "http-body", - "hyper", + "http 0.2.11", + "http-body 0.4.5", + "hyper 0.14.27", + "itoa", + "matchit", + "memchr", + "mime", + "percent-encoding", + "pin-project-lite", + "rustversion", + "serde", + "serde_json", + "serde_path_to_error", + "serde_urlencoded", + "sync_wrapper", + "tokio", + "tower", + "tower-layer", + "tower-service", +] + +[[package]] +name = "axum" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "810a80b128d70e6ed2bdf3fe8ed72c0ae56f5f5948d01c2753282dd92a84fce8" +dependencies = [ + "async-trait", + "axum-core 0.4.0", + "bytes", + "futures-util", + "http 1.0.0", + "http-body 1.0.0", + "http-body-util", + "hyper 1.0.1", + "hyper-util", "itoa", "matchit", "memchr", @@ -280,14 +313,34 @@ dependencies = [ "async-trait", "bytes", "futures-util", - "http", - "http-body", + "http 0.2.11", + "http-body 0.4.5", "mime", "rustversion", "tower-layer", "tower-service", ] +[[package]] +name = "axum-core" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de0ddc355eab88f4955090a823715df47acf0b7660aab7a69ad5ce6301ee3b73" +dependencies = [ + "async-trait", + "bytes", + "futures-util", + "http 1.0.0", + "http-body 1.0.0", + "http-body-util", + "mime", + "pin-project-lite", + "rustversion", + "sync_wrapper", + "tower-layer", + "tower-service", +] + [[package]] name = "backtrace" version = "0.3.69" @@ -1266,8 +1319,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9a1213680212363f551be5913506ceba1222fa88dfbe46757a85220eeacb4fad" dependencies = [ "hex", - "http-body", - "hyper", + "http-body 0.4.5", + "hyper 0.14.27", "hyper-rustls", "prost", "rustls 0.21.9", @@ -1610,7 +1663,7 @@ dependencies = [ "futures-core", "futures-sink", "gloo-utils 0.2.0", - "http", + "http 0.2.11", "js-sys", "pin-project", "serde", @@ -1693,7 +1746,26 @@ dependencies = [ "futures-core", "futures-sink", "futures-util", - "http", + "http 0.2.11", + "indexmap 2.1.0", + "slab", + "tokio", + "tokio-util", + "tracing", +] + +[[package]] +name = "h2" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e1d308f63daf4181410c242d34c11f928dcb3aa105852019e043c9d1f4e4368a" +dependencies = [ + "bytes", + "fnv", + "futures-core", + "futures-sink", + "futures-util", + "http 1.0.0", "indexmap 2.1.0", "slab", "tokio", @@ -1802,6 +1874,17 @@ dependencies = [ "itoa", ] +[[package]] +name = "http" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b32afd38673a8016f7c9ae69e5af41a58f81b1d31689040f2f1959594ce194ea" +dependencies = [ + "bytes", + "fnv", + "itoa", +] + [[package]] name = "http-body" version = "0.4.5" @@ -1809,15 +1892,38 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d5f38f16d184e36f2408a55281cd658ecbd3ca05cce6d6510a176eca393e26d1" dependencies = [ "bytes", - "http", + "http 0.2.11", + "pin-project-lite", +] + +[[package]] +name = "http-body" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1cac85db508abc24a2e48553ba12a996e87244a0395ce011e62b37158745d643" +dependencies = [ + "bytes", + "http 1.0.0", +] + +[[package]] +name = "http-body-util" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41cb79eb393015dadd30fc252023adb0b2400a0caee0fa2a077e6e21a551e840" +dependencies = [ + "bytes", + "futures-util", + "http 1.0.0", + "http-body 1.0.0", "pin-project-lite", ] [[package]] name = "http-range-header" -version = "0.3.1" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "add0ab9360ddbd88cfeb3bd9574a1d85cfdfa14db10b3e21d3700dbc4328758f" +checksum = "3ce4ef31cda248bbdb6e6820603b82dfcd9e833db65a43e997a0ccec777d11fe" [[package]] name = "httparse" @@ -1841,9 +1947,9 @@ dependencies = [ "futures-channel", "futures-core", "futures-util", - "h2", - "http", - "http-body", + "h2 0.3.22", + "http 0.2.11", + "http-body 0.4.5", "httparse", "httpdate", "itoa", @@ -1855,6 +1961,25 @@ dependencies = [ "want", ] +[[package]] +name = "hyper" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "403f9214f3e703236b221f1a9cd88ec8b4adfa5296de01ab96216361f4692f56" +dependencies = [ + "bytes", + "futures-channel", + "futures-util", + "h2 0.4.0", + "http 1.0.0", + "http-body 1.0.0", + "httparse", + "httpdate", + "itoa", + "pin-project-lite", + "tokio", +] + [[package]] name = "hyper-rustls" version = "0.24.2" @@ -1862,8 +1987,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ec3efd23720e2049821a693cbc7e65ea87c72f1c58ff2f9522ff332b1491e590" dependencies = [ "futures-util", - "http", - "hyper", + "http 0.2.11", + "hyper 0.14.27", "rustls 0.21.9", "tokio", "tokio-rustls 0.24.1", @@ -1875,12 +2000,32 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bbb958482e8c7be4bc3cf272a766a2b0bf1a6755e7a6ae777f017a31d11b13b1" dependencies = [ - "hyper", + "hyper 0.14.27", "pin-project-lite", "tokio", "tokio-io-timeout", ] +[[package]] +name = "hyper-util" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ca339002caeb0d159cc6e023dff48e199f081e42fa039895c7c6f38b37f2e9d" +dependencies = [ + "bytes", + "futures-channel", + "futures-util", + "http 1.0.0", + "http-body 1.0.0", + "hyper 1.0.1", + "pin-project-lite", + "socket2 0.5.5", + "tokio", + "tower", + "tower-service", + "tracing", +] + [[package]] name = "iana-time-zone" version = "0.1.58" @@ -2079,7 +2224,7 @@ dependencies = [ "futures-channel", "futures-util", "gloo-net 0.2.6", - "http", + "http 0.2.11", "jsonrpsee-core", "pin-project", "soketto", @@ -2145,7 +2290,7 @@ version = "0.18.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a64b2589680ba1ad7863f279cd2d5083c1dc0a7c0ea959d22924553050f8ab9f" dependencies = [ - "http", + "http 0.2.11", "jsonrpsee-client-transport", "jsonrpsee-core", "jsonrpsee-types", @@ -2516,9 +2661,9 @@ name = "moksha-integrationtests" version = "0.1.2" dependencies = [ "anyhow", - "axum", + "axum 0.6.20", "bitcoin_hashes 0.11.0", - "hyper", + "hyper 0.14.27", "lightning 0.0.116", "lightning-invoice 0.24.0", "moksha-core", @@ -2541,13 +2686,14 @@ version = "0.1.2" dependencies = [ "anyhow", "async-trait", - "axum", + "axum 0.7.1", "bitcoin_hashes 0.12.0", "dotenvy", "envy", "fedimint-tonic-lnd", "hex", - "hyper", + "http-body-util", + "hyper 0.14.27", "lightning-invoice 0.24.0", "mockall", "moksha-core", @@ -3159,10 +3305,10 @@ dependencies = [ "encoding_rs", "futures-core", "futures-util", - "h2", - "http", - "http-body", - "hyper", + "h2 0.3.22", + "http 0.2.11", + "http-body 0.4.5", + "hyper 0.14.27", "hyper-rustls", "ipnet", "js-sys", @@ -4229,13 +4375,13 @@ checksum = "d560933a0de61cf715926b9cac824d4c883c2c43142f787595e48280c40a1d0e" dependencies = [ "async-stream", "async-trait", - "axum", + "axum 0.6.20", "base64 0.21.5", "bytes", - "h2", - "http", - "http-body", - "hyper", + "h2 0.3.22", + "http 0.2.11", + "http-body 0.4.5", + "hyper 0.14.27", "hyper-timeout", "percent-encoding", "pin-project", @@ -4286,16 +4432,16 @@ dependencies = [ [[package]] name = "tower-http" -version = "0.4.4" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "61c5bb1d698276a2443e5ecfabc1008bf15a36c12e6a7176e7bf089ea9131140" +checksum = "09e12e6351354851911bdf8c2b8f2ab15050c567d70a8b9a37ae7b8301a4080d" dependencies = [ "bitflags 2.4.1", "bytes", - "futures-core", "futures-util", - "http", - "http-body", + "http 1.0.0", + "http-body 1.0.0", + "http-body-util", "http-range-header", "httpdate", "mime", diff --git a/moksha-mint/Cargo.toml b/moksha-mint/Cargo.toml index f8c81f31..17befbc6 100644 --- a/moksha-mint/Cargo.toml +++ b/moksha-mint/Cargo.toml @@ -21,7 +21,7 @@ hex = "0.4.3" async-trait = "0.1.74" bitcoin_hashes = "0.12.0" anyhow = { version = "1.0.75", features = ["backtrace"] } -axum = { version = "0.6.20", features = ["http2"] } +axum = { version = "0.7.1", features = ["http2"] } hyper = "0.14.27" serde = { version = "1.0.193", features = ["derive"] } @@ -29,7 +29,7 @@ serde_derive = "1.0.171" serde_json = "1.0.108" serde_with = "3.4.0" tokio = { version = "1.34.0", features = ["full"] } -tower-http = { version = "0.4.4", features = ["trace", "cors", "fs", "set-header"] } +tower-http = { version = "0.5.0", features = ["trace", "cors", "fs", "set-header"] } tower-service = { version = "0.3.2" } tracing = "0.1.40" tracing-subscriber = "0.3.18" @@ -46,6 +46,7 @@ fedimint-tonic-lnd = { version = "0.1.3", features = ["lightningrpc"] } [dev-dependencies] tempfile = "3.8.1" -tower = "0.4.13" +tower = { version = "0.4", features = ["util"] } mockall = "0.11.4" hex = "0.4.3" +http-body-util = "0.1.0" diff --git a/moksha-mint/src/error.rs b/moksha-mint/src/error.rs index a9e7b643..a1a13b38 100644 --- a/moksha-mint/src/error.rs +++ b/moksha-mint/src/error.rs @@ -1,11 +1,12 @@ use std::string::FromUtf8Error; use axum::{ + http::StatusCode, response::{IntoResponse, Response}, Json, }; use fedimint_tonic_lnd::ConnectError; -use hyper::StatusCode; + use lightning_invoice::ParseOrSemanticError; use serde_json::json; use thiserror::Error; diff --git a/moksha-mint/src/server.rs b/moksha-mint/src/server.rs index 7322c625..ac8f8f92 100644 --- a/moksha-mint/src/server.rs +++ b/moksha-mint/src/server.rs @@ -5,6 +5,7 @@ use std::path::PathBuf; use crate::error::MokshaMintError; use axum::extract::{Query, State}; +use axum::http::HeaderName; use axum::routing::{get_service, post}; use axum::Router; use axum::{routing::get, Json}; @@ -12,7 +13,7 @@ use moksha_core::keyset::Keysets; use crate::mint::Mint; use crate::model::{GetMintQuery, PostMintQuery}; -use hyper::http::{HeaderName, HeaderValue}; +use hyper::http::HeaderValue; use hyper::Method; use moksha_core::primitives::{ CheckFeesRequest, CheckFeesResponse, MintInfoResponse, PaymentRequest, PostMeltRequest, @@ -50,23 +51,25 @@ pub async fn run_server( ); } - axum::Server::bind(&addr) - .serve( - app(mint, serve_wallet_path, api_prefix) - .layer( - CorsLayer::new() - .allow_origin(Any) - .allow_headers(Any) - .allow_methods([Method::GET, Method::POST]), - ) - .into_make_service(), - ) - .await?; + let listener = tokio::net::TcpListener::bind(&addr).await.unwrap(); + + axum::serve( + listener, + app(mint, serve_wallet_path, api_prefix) + .layer( + CorsLayer::new() + .allow_origin(Any) + .allow_headers(Any) + .allow_methods([axum::http::Method::GET, axum::http::Method::POST]), + ) + .into_make_service(), + ) + .await?; Ok(()) } -fn app(mint: Mint, serve_wallet_path: Option, prefix: Option) -> Router { +fn app(mint: Mint, _serve_wallet_path: Option, prefix: Option) -> Router { let routes = Router::new() .route("/keys", get(get_keys)) .route("/keysets", get(get_keysets)) @@ -76,26 +79,27 @@ fn app(mint: Mint, serve_wallet_path: Option, prefix: Option) - .route("/split", post(post_split)) .route("/info", get(get_info)); - let router = Router::new() + Router::new() .nest(&prefix.unwrap_or_else(|| "".to_owned()), routes) .with_state(mint) - .layer(TraceLayer::new_for_http()); - - if let Some(serve_wallet_path) = serve_wallet_path { - return router.nest_service( - "/", - get_service(ServeDir::new(serve_wallet_path)) - .layer::<_, _, Infallible>(SetResponseHeaderLayer::if_not_present( - HeaderName::from_static("cross-origin-embedder-policy"), - HeaderValue::from_static("require-corp"), - )) - .layer(SetResponseHeaderLayer::if_not_present( - HeaderName::from_static("cross-origin-opener-policy"), - HeaderValue::from_static("same-origin"), - )), - ); - } - router + .layer(TraceLayer::new_for_http()) + + // if let Some(serve_wallet_path) = serve_wallet_path { + // return router.nest_service( + // "/", + // get_service(ServeDir::new(serve_wallet_path)).layer::<_, _, Infallible>( + // SetResponseHeaderLayer::if_not_present( + // HeaderName::from_static("cross-origin-embedder-policy"), + // HeaderValue::from_static("require-corp"), + // ), + // ), // .layer(SetResponseHeaderLayer::if_not_present( + // // HeaderName::from_static("cross-origin-opener-policy"), + // // HeaderValue::from_static("same-origin"), + // // )), + // ); + // } + //router + // FIXME doesn't compile anymore with axum 0.7.1 } async fn post_split( @@ -205,7 +209,11 @@ mod tests { use std::{collections::HashMap, sync::Arc}; use crate::server::app; - use hyper::{Body, Request, StatusCode}; + use axum::{ + body::Body, + http::{Request, StatusCode}, + }; + use http_body_util::BodyExt; use moksha_core::{keyset::Keysets, primitives::MintInfoResponse}; use secp256k1::PublicKey; use tower::ServiceExt; @@ -225,7 +233,7 @@ mod tests { .await?; assert_eq!(response.status(), StatusCode::OK); - let body = hyper::body::to_bytes(response.into_body()).await?; + let body = response.into_body().collect().await.unwrap().to_bytes(); let keys: HashMap = serde_json::from_slice(&body)?; assert_eq!(64, keys.len()); Ok(()) @@ -239,7 +247,7 @@ mod tests { .await?; assert_eq!(response.status(), StatusCode::OK); - let body = hyper::body::to_bytes(response.into_body()).await?; + let body = response.into_body().collect().await.unwrap().to_bytes(); let keysets = serde_json::from_slice::(&body)?; assert_eq!(Keysets::new(vec!["53eJP2+qJyTd".to_string()]), keysets); Ok(()) @@ -260,7 +268,7 @@ mod tests { .await?; assert_eq!(response.status(), StatusCode::OK); - let body = hyper::body::to_bytes(response.into_body()).await?; + let body = response.into_body().collect().await.unwrap().to_bytes(); let info = serde_json::from_slice::(&body)?; assert!(!info.parameter.peg_out_only); assert_eq!(info.nuts.len(), 8);