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

[mixnode] replace rocket with axum #4071

Merged
merged 3 commits into from
Oct 30, 2023
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
4 changes: 3 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 1 addition & 7 deletions common/mixnode-common/src/verloc/measurement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::sync::Arc;
use std::time::Duration;
use tokio::sync::RwLock;

#[derive(Clone)]
pub struct AtomicVerlocResult {
inner: Arc<RwLock<VerlocResult>>,
}
Expand All @@ -35,13 +36,6 @@ impl AtomicVerlocResult {
}
}

// this could have also been achieved with a normal #[derive(Clone)] but I prefer to be explicit about it
pub(crate) fn clone_data_pointer(&self) -> Self {
AtomicVerlocResult {
inner: Arc::clone(&self.inner),
}
}

pub(crate) async fn reset_results(&self, new_tested: usize) {
let mut write_permit = self.inner.write().await;
write_permit.total_tested = new_tested;
Expand Down
2 changes: 1 addition & 1 deletion common/mixnode-common/src/verloc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ impl VerlocMeasurer {
}

pub fn get_verloc_results_pointer(&self) -> AtomicVerlocResult {
self.results.clone_data_pointer()
self.results.clone()
}

fn start_listening(&self) -> JoinHandle<()> {
Expand Down
2 changes: 1 addition & 1 deletion gateway/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ tokio = { workspace = true, features = [
tokio-stream = { version = "0.1.11", features = ["fs"] }
tokio-tungstenite = { version = "0.20.1" }
tokio-util = { version = "0.7.4", features = ["codec"] }
url = { version = "2.2", features = ["serde"] }
url = { workspace = true, features = ["serde"] }
zeroize = { workspace = true }

# internal
Expand Down
9 changes: 6 additions & 3 deletions mixnode/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ rust-version = "1.58.1"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
axum = { workspace = true }
anyhow = "1.0.40"
bs58 = "0.4.0"
clap = { version = "4.0", features = ["cargo", "derive"] }
Expand All @@ -28,22 +29,24 @@ lazy_static = "1.4.0"
log = { workspace = true }
pretty_env_logger = "0.4.0"
rand = "0.7.3"
rocket = { version = "0.5.0-rc.2", features = ["json"] }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
sysinfo = "0.27.7"
tokio = { version = "1.21.2", features = ["rt-multi-thread", "net", "signal"] }
tokio-util = { version = "0.7.3", features = ["codec"] }
toml = "0.5.8"
url = { version = "2.2", features = ["serde"] }
url = { workspace = true, features = ["serde"] }
cfg-if = "1.0.0"
thiserror = { workspace = true }

## tracing
tracing = { version = "0.1.37", optional = true }
opentelemetry = { version = "0.19.0", optional = true }


## internal
# internal
nym-node = { path = "../nym-node" }

nym-config = { path = "../common/config" }
nym-crypto = { path = "../common/crypto" }
nym-contracts-common = { path = "../common/cosmwasm-smart-contracts/contracts-common" }
Expand Down
1 change: 1 addition & 0 deletions mixnode/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use anyhow::anyhow;
use clap::CommandFactory;
use clap::Subcommand;
use colored::Colorize;
use log::{error, info, warn};
use nym_bin_common::completions::{fig_generate, ArgShell};
use nym_bin_common::version_checker;
use nym_config::defaults::var_names::{BECH32_PREFIX, NYM_API};
Expand Down
2 changes: 1 addition & 1 deletion mixnode/src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ use crate::commands::{override_config, try_load_current_config, version_check};
use crate::node::MixNode;
use anyhow::bail;
use clap::Args;
use log::error;
use nym_bin_common::output_format::OutputFormat;
use nym_config::helpers::SPECIAL_ADDRESSES;
use nym_validator_client::nyxd;
use std::net::IpAddr;

#[derive(Args, Clone)]
pub(crate) struct Run {
/// Id of the nym-mixnode we want to run
Expand Down
4 changes: 1 addition & 3 deletions mixnode/src/commands/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ use crate::commands::{try_load_current_config, validate_bech32_address_or_exit};
use crate::node::MixNode;
use anyhow::{bail, Result};
use clap::{ArgGroup, Args};
use log::error;
use nym_bin_common::output_format::OutputFormat;
use nym_crypto::asymmetric::identity;
use nym_types::helpers::ConsoleSigningOutput;
use nym_validator_client::nyxd;
use std::convert::TryFrom;

#[cfg(feature = "cpucycles")]
use tracing::error;

use super::version_check;

#[derive(Args, Clone)]
Expand Down
11 changes: 11 additions & 0 deletions mixnode/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright 2023 - Nym Technologies SA <contact@nymtech.net>
// SPDX-License-Identifier: Apache-2.0

use thiserror::Error;

#[derive(Debug, Error)]
pub(crate) enum MixnodeError {
// TODO: in the future this should work the other way, i.e. NymNode depending on Gateway errors
#[error(transparent)]
NymNodeError(#[from] nym_node::error::NymNodeError),
}
5 changes: 2 additions & 3 deletions mixnode/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
// Copyright 2020-2023 - Nym Technologies SA <contact@nymtech.net>
// SPDX-License-Identifier: Apache-2.0

#[macro_use]
extern crate rocket;

use ::nym_config::defaults::setup_env;
use clap::{crate_name, crate_version, Parser};
use lazy_static::lazy_static;
use log::info;
use nym_bin_common::bin_info;

#[allow(unused_imports)]
Expand All @@ -20,6 +18,7 @@ use tracing::instrument;

mod commands;
mod config;
pub(crate) mod error;
mod node;

lazy_static! {
Expand Down
15 changes: 10 additions & 5 deletions mixnode/src/node/http/description.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
use crate::node::node_description::NodeDescription;
use rocket::serde::json::Json;
use rocket::State;
use axum::extract::Query;
use nym_node::http::api::{FormattedResponse, OutputParams};

/// Returns a description of the node and why someone might want to delegate stake to it.
#[get("/description")]
pub(crate) fn description(description: &State<NodeDescription>) -> Json<NodeDescription> {
Json(description.inner().clone())
pub(crate) async fn description(
description: NodeDescription,
Query(output): Query<OutputParams>,
) -> MixnodeDescriptionResponse {
let output = output.output.unwrap_or_default();
output.to_response(description)
}

pub type MixnodeDescriptionResponse = FormattedResponse<NodeDescription>;
16 changes: 9 additions & 7 deletions mixnode/src/node/http/hardware.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
use axum::extract::Query;
use cupid::TopologyType;
use rocket::serde::{json::Json, Serialize};
use nym_node::http::api::{FormattedResponse, OutputParams};
use serde::Serialize;
use sysinfo::{System, SystemExt};

#[derive(Serialize, Debug)]
#[serde(crate = "rocket::serde")]
pub(crate) struct Hardware {
pub struct Hardware {
ram: String,
num_cores: usize,
crypto_hardware: Option<CryptoHardware>,
}

#[allow(clippy::struct_excessive_bools)]
#[derive(Serialize, Debug)]
#[serde(crate = "rocket::serde")]
pub(crate) struct CryptoHardware {
aesni: bool,
avx2: bool,
Expand All @@ -24,11 +24,13 @@ pub(crate) struct CryptoHardware {
}

/// Provides hardware information which Nym can use to optimize mixnet speed over time (memory, crypto hardware, CPU, cores, etc).
#[get("/hardware")]
pub(crate) fn hardware() -> Json<Option<Hardware>> {
Json(hardware_info())
pub(crate) async fn hardware(Query(output): Query<OutputParams>) -> MixnodeHardwareResponse {
let output = output.output.unwrap_or_default();
output.to_response(hardware_info())
}

pub type MixnodeHardwareResponse = FormattedResponse<Option<Hardware>>;

/// Gives back a summary report of whatever system hardware info we can get for this platform.
fn hardware_info() -> Option<Hardware> {
let crypto_hardware = hardware_info_from_cupid();
Expand Down
12 changes: 8 additions & 4 deletions mixnode/src/node/http/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
pub(crate) mod description;
pub(crate) mod hardware;
pub(crate) mod state;
pub(crate) mod stats;
pub(crate) mod verloc;

use rocket::Request;
use axum::http::{StatusCode, Uri};
use axum::response::IntoResponse;

#[catch(404)]
pub(crate) fn not_found(req: &Request<'_>) -> String {
format!("I couldn't find '{}'. Try something else?", req.uri())
pub(crate) async fn not_found(uri: Uri) -> impl IntoResponse {
(
StatusCode::NOT_FOUND,
format!("I couldn't find '{uri}'. Try something else?"),
)
}
25 changes: 25 additions & 0 deletions mixnode/src/node/http/state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2023 - Nym Technologies SA <contact@nymtech.net>
// SPDX-License-Identifier: Apache-2.0

use crate::node::http::verloc::VerlocState;
use crate::node::node_statistics::SharedNodeStats;
use axum::extract::FromRef;

// this is a temporary thing for the transition period
#[derive(Clone)]
pub(crate) struct MixnodeAppState {
pub(crate) verloc: VerlocState,
pub(crate) stats: SharedNodeStats,
}

impl FromRef<MixnodeAppState> for VerlocState {
fn from_ref(app_state: &MixnodeAppState) -> Self {
app_state.verloc.clone()
}
}

impl FromRef<MixnodeAppState> for SharedNodeStats {
fn from_ref(app_state: &MixnodeAppState) -> Self {
app_state.stats.clone()
}
}
39 changes: 24 additions & 15 deletions mixnode/src/node/http/stats.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,38 @@
use crate::node::node_statistics::{NodeStats, NodeStatsSimple, SharedNodeStats};
use rocket::serde::json::Json;
use rocket::State;
use serde::Serialize;
use axum::extract::{Query, State};
use nym_node::http::api::{FormattedResponse, Output};
use serde::{Deserialize, Serialize};

#[derive(Serialize)]
#[serde(untagged)]
pub(crate) enum NodeStatsResponse {
pub enum NodeStatsResponse {
Full(NodeStats),
Simple(NodeStatsSimple),
}

/// Returns a running stats of the node.
#[get("/stats?<debug>")]
pub(crate) async fn stats(
stats: &State<SharedNodeStats>,
debug: Option<bool>,
) -> Json<NodeStatsResponse> {
Query(params): Query<StatsQueryParams>,
State(stats): State<SharedNodeStats>,
) -> MixnodeStatsResponse {
let output = params.output.unwrap_or_default();

let snapshot_data = stats.clone_data().await;

// there's no point in returning the entire hashmap of sending destinations in regular mode
if let Some(debug) = debug {
if debug {
return Json(NodeStatsResponse::Full(snapshot_data));
}
}
let response = if params.debug {
NodeStatsResponse::Full(snapshot_data)
} else {
NodeStatsResponse::Simple(snapshot_data.simplify())
};
output.to_response(response)
}

pub type MixnodeStatsResponse = FormattedResponse<NodeStatsResponse>;

Json(NodeStatsResponse::Simple(snapshot_data.simplify()))
#[derive(Default, Debug, Serialize, Deserialize, Copy, Clone)]
// #[derive(Default, Debug, Serialize, Deserialize, Copy, Clone, IntoParams, ToSchema)]
#[serde(default)]
pub(crate) struct StatsQueryParams {
debug: bool,
pub output: Option<Output>,
}
17 changes: 11 additions & 6 deletions mixnode/src/node/http/verloc.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use axum::extract::{Query, State};
use nym_mixnode_common::verloc::{AtomicVerlocResult, VerlocResult};
use rocket::serde::json::Json;
use rocket::State;
use nym_node::http::api::{FormattedResponse, OutputParams};

#[derive(Clone)]
pub(crate) struct VerlocState {
shared: AtomicVerlocResult,
}
Expand All @@ -16,8 +17,12 @@ impl VerlocState {

/// Provides verifiable location (verloc) measurements for this mixnode - a list of the
/// round-trip times, in milliseconds, for all other mixnodes that this node knows about.
#[get("/verloc")]
pub(crate) async fn verloc(state: &State<VerlocState>) -> Json<VerlocResult> {
// since it's impossible to get a mutable reference to the state, we can't cache any results outside the lock : (
Json(state.shared.clone_data().await)
pub(crate) async fn verloc(
State(verloc): State<VerlocState>,
Query(output): Query<OutputParams>,
) -> MixnodeVerlocResponse {
let output = output.output.unwrap_or_default();
output.to_response(verloc.shared.clone_data().await)
}

pub type MixnodeVerlocResponse = FormattedResponse<VerlocResult>;
5 changes: 4 additions & 1 deletion mixnode/src/node/listener/connection_handler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use crate::node::listener::connection_handler::packet_processing::{
use crate::node::packet_delayforwarder::PacketDelayForwardSender;
use crate::node::TaskClient;
use futures::StreamExt;
use log::debug;
use log::{error, info, warn};
use nym_mixnode_common::measure;
use nym_sphinx::forwarding::packet::MixPacket;
use nym_sphinx::framing::codec::NymCodec;
Expand All @@ -16,8 +18,9 @@ use std::net::SocketAddr;
use tokio::net::TcpStream;
use tokio::time::Instant;
use tokio_util::codec::Framed;

#[cfg(feature = "cpucycles")]
use tracing::{error, info, instrument};
use tracing::instrument;

pub(crate) mod packet_processing;

Expand Down
3 changes: 1 addition & 2 deletions mixnode/src/node/listener/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
// SPDX-License-Identifier: Apache-2.0

use crate::node::listener::connection_handler::ConnectionHandler;
use log::{error, info, warn};
use std::net::SocketAddr;
use std::process;
use tokio::net::TcpListener;
use tokio::task::JoinHandle;
#[cfg(feature = "cpucycles")]
use tracing::error;

use super::TaskClient;

Expand Down
Loading
Loading