diff --git a/clients/native/src/commands/run.rs b/clients/native/src/commands/run.rs index de577842057..96f0450d672 100644 --- a/clients/native/src/commands/run.rs +++ b/clients/native/src/commands/run.rs @@ -17,6 +17,8 @@ use crate::client::NymClient; use crate::commands::override_config; use clap::{App, Arg, ArgMatches}; use config::NymConfig; +use log::*; +use version_checker::is_minor_version_compatible; pub fn command_args<'a, 'b>() -> clap::App<'a, 'b> { App::new("run") @@ -66,14 +68,45 @@ pub fn command_args<'a, 'b>() -> clap::App<'a, 'b> { ) } +// this only checks compatibility between config the binary. It does not take into consideration +// network version. It might do so in the future. +fn version_check(cfg: &Config) -> bool { + let binary_version = env!("CARGO_PKG_VERSION"); + let config_version = cfg.get_base().get_version(); + if binary_version != config_version { + warn!("The mixnode binary has different version than what is specified in config file! {} and {}", binary_version, config_version); + if is_minor_version_compatible(binary_version, config_version) { + info!("but they are still semver compatible. However, consider running the `upgrade` command"); + true + } else { + error!("and they are semver incompatible! - please run the `upgrade` command before attempting `run` again"); + false + } + } else { + true + } +} + pub fn execute(matches: &ArgMatches) { let id = matches.value_of("id").unwrap(); - let mut config = - Config::load_from_file(matches.value_of("config").map(|path| path.into()), Some(id)) - .expect("Failed to load config file"); + let mut config = match Config::load_from_file( + matches.value_of("config").map(|path| path.into()), + Some(id), + ) { + Ok(cfg) => cfg, + Err(err) => { + error!("Failed to load config for {}. Are you sure you have run `init` before? (Error was: {})", id, err); + return; + } + }; config = override_config(config, matches); + if !version_check(&config) { + error!("failed the local version check"); + return; + } + NymClient::new(config).run_forever(); } diff --git a/clients/socks5/src/commands/run.rs b/clients/socks5/src/commands/run.rs index 04f76a4d768..6c39acae56f 100644 --- a/clients/socks5/src/commands/run.rs +++ b/clients/socks5/src/commands/run.rs @@ -17,6 +17,8 @@ use crate::client::NymClient; use crate::commands::override_config; use clap::{App, Arg, ArgMatches}; use config::NymConfig; +use log::*; +use version_checker::is_minor_version_compatible; pub fn command_args<'a, 'b>() -> clap::App<'a, 'b> { App::new("run") @@ -67,14 +69,45 @@ pub fn command_args<'a, 'b>() -> clap::App<'a, 'b> { ) } +// this only checks compatibility between config the binary. It does not take into consideration +// network version. It might do so in the future. +fn version_check(cfg: &Config) -> bool { + let binary_version = env!("CARGO_PKG_VERSION"); + let config_version = cfg.get_base().get_version(); + if binary_version != config_version { + warn!("The mixnode binary has different version than what is specified in config file! {} and {}", binary_version, config_version); + if is_minor_version_compatible(binary_version, config_version) { + info!("but they are still semver compatible. However, consider running the `upgrade` command"); + true + } else { + error!("and they are semver incompatible! - please run the `upgrade` command before attempting `run` again"); + false + } + } else { + true + } +} + pub fn execute(matches: &ArgMatches) { let id = matches.value_of("id").unwrap(); - let mut config = - Config::load_from_file(matches.value_of("config").map(|path| path.into()), Some(id)) - .expect("Failed to load config file"); + let mut config = match Config::load_from_file( + matches.value_of("config").map(|path| path.into()), + Some(id), + ) { + Ok(cfg) => cfg, + Err(err) => { + error!("Failed to load config for {}. Are you sure you have run `init` before? (Error was: {})", id, err); + return; + } + }; config = override_config(config, matches); + if !version_check(&config) { + error!("failed the local version check"); + return; + } + NymClient::new(config).run_forever(); } diff --git a/gateway/src/commands/run.rs b/gateway/src/commands/run.rs index 89d2a22af35..e013b8d7d3f 100644 --- a/gateway/src/commands/run.rs +++ b/gateway/src/commands/run.rs @@ -19,6 +19,8 @@ use crate::node::Gateway; use clap::{App, Arg, ArgMatches}; use config::NymConfig; use crypto::asymmetric::{encryption, identity}; +use log::*; +use version_checker::is_minor_version_compatible; pub fn command_args<'a, 'b>() -> clap::App<'a, 'b> { App::new("run") @@ -152,17 +154,48 @@ fn load_identity_keys(pathfinder: &GatewayPathfinder) -> identity::KeyPair { identity_keypair } +// this only checks compatibility between config the binary. It does not take into consideration +// network version. It might do so in the future. +fn version_check(cfg: &Config) -> bool { + let binary_version = env!("CARGO_PKG_VERSION"); + let config_version = cfg.get_version(); + if binary_version != config_version { + warn!("The mixnode binary has different version than what is specified in config file! {} and {}", binary_version, config_version); + if is_minor_version_compatible(binary_version, config_version) { + info!("but they are still semver compatible. However, consider running the `upgrade` command"); + true + } else { + error!("and they are semver incompatible! - please run the `upgrade` command before attempting `run` again"); + false + } + } else { + true + } +} + pub fn execute(matches: &ArgMatches) { let id = matches.value_of("id").unwrap(); println!("Starting gateway {}...", id); - let mut config = - Config::load_from_file(matches.value_of("config").map(|path| path.into()), Some(id)) - .expect("Failed to load config file"); + let mut config = match Config::load_from_file( + matches.value_of("config").map(|path| path.into()), + Some(id), + ) { + Ok(cfg) => cfg, + Err(err) => { + error!("Failed to load config for {}. Are you sure you have run `init` before? (Error was: {})", id, err); + return; + } + }; config = override_config(config, matches); + if !version_check(&config) { + error!("failed the local version check"); + return; + } + let pathfinder = GatewayPathfinder::new_from_config(&config); let sphinx_keypair = load_sphinx_keys(&pathfinder); let identity = load_identity_keys(&pathfinder); diff --git a/gateway/src/node/mod.rs b/gateway/src/node/mod.rs index 92d203a8287..adc5373b480 100644 --- a/gateway/src/node/mod.rs +++ b/gateway/src/node/mod.rs @@ -22,7 +22,6 @@ use log::*; use mixnet_client::forwarder::{MixForwardingSender, PacketForwarder}; use std::sync::Arc; use tokio::runtime::Runtime; -use version_checker::is_minor_version_compatible; pub(crate) mod client_handling; pub(crate) mod mixnet_handling; @@ -165,34 +164,11 @@ impl Gateway { .map(|node| node.identity()) } - // this only checks compatibility between config the binary. It does not take into consideration - // network version. It might do so in the future. - fn version_check(&self) -> bool { - let binary_version = env!("CARGO_PKG_VERSION"); - let config_version = self.config.get_version(); - if binary_version != config_version { - warn!("The mixnode binary has different version than what is specified in config file! {} and {}", binary_version, config_version); - if is_minor_version_compatible(binary_version, config_version) { - info!("but they are still semver compatible. However, consider running the `upgrade` command"); - true - } else { - error!("and they are semver incompatible! - please run the `upgrade` command before attempting `run` again"); - false - } - } else { - true - } - } - // Rather than starting all futures with explicit `&Handle` argument, let's see how it works // out if we make it implicit using `tokio::spawn` inside Runtime context. // Basically more or less equivalent of using #[tokio::main] attribute. pub fn run(&mut self) { info!("Starting nym gateway!"); - if !self.version_check() { - error!("failed the local version check"); - return; - } let mut runtime = Runtime::new().unwrap(); diff --git a/mixnode/src/commands/run.rs b/mixnode/src/commands/run.rs index c1159e1d267..fce0bb47288 100644 --- a/mixnode/src/commands/run.rs +++ b/mixnode/src/commands/run.rs @@ -18,6 +18,8 @@ use crate::node::MixNode; use clap::{App, Arg, ArgMatches}; use config::NymConfig; use crypto::asymmetric::{encryption, identity}; +use log::*; +use version_checker::is_minor_version_compatible; pub fn command_args<'a, 'b>() -> App<'a, 'b> { App::new("run") @@ -127,17 +129,48 @@ fn load_sphinx_keys(pathfinder: &MixNodePathfinder) -> encryption::KeyPair { sphinx_keypair } +// this only checks compatibility between config the binary. It does not take into consideration +// network version. It might do so in the future. +fn version_check(cfg: &Config) -> bool { + let binary_version = env!("CARGO_PKG_VERSION"); + let config_version = cfg.get_version(); + if binary_version != config_version { + warn!("The mixnode binary has different version than what is specified in config file! {} and {}", binary_version, config_version); + if is_minor_version_compatible(binary_version, config_version) { + info!("but they are still semver compatible. However, consider running the `upgrade` command"); + true + } else { + error!("and they are semver incompatible! - please run the `upgrade` command before attempting `run` again"); + false + } + } else { + true + } +} + pub fn execute(matches: &ArgMatches) { let id = matches.value_of("id").unwrap(); println!("Starting mixnode {}...", id); - let mut config = - Config::load_from_file(matches.value_of("config").map(|path| path.into()), Some(id)) - .expect("Failed to load config file"); + let mut config = match Config::load_from_file( + matches.value_of("config").map(|path| path.into()), + Some(id), + ) { + Ok(cfg) => cfg, + Err(err) => { + error!("Failed to load config for {}. Are you sure you have run `init` before? (Error was: {})", id, err); + return; + } + }; config = override_config(config, matches); + if !version_check(&config) { + error!("failed the local version check"); + return; + } + let pathfinder = MixNodePathfinder::new_from_config(&config); let identity_keypair = load_identity_keys(&pathfinder); let sphinx_keypair = load_sphinx_keys(&pathfinder); diff --git a/mixnode/src/node/mod.rs b/mixnode/src/node/mod.rs index ad1f99d0954..f6220cd9af9 100644 --- a/mixnode/src/node/mod.rs +++ b/mixnode/src/node/mod.rs @@ -21,7 +21,6 @@ use log::*; use mixnet_client::forwarder::{MixForwardingSender, PacketForwarder}; use std::sync::Arc; use tokio::runtime::Runtime; -use version_checker::is_minor_version_compatible; mod listener; mod metrics; @@ -127,31 +126,8 @@ impl MixNode { } } - // this only checks compatibility between config the binary. It does not take into consideration - // network version. It might do so in the future. - fn version_check(&self) -> bool { - let binary_version = env!("CARGO_PKG_VERSION"); - let config_version = self.config.get_version(); - if binary_version != config_version { - warn!("The mixnode binary has different version than what is specified in config file! {} and {}", binary_version, config_version); - if is_minor_version_compatible(binary_version, config_version) { - info!("but they are still semver compatible. However, consider running the `upgrade` command"); - true - } else { - error!("and they are semver incompatible! - please run the `upgrade` command before attempting `run` again"); - false - } - } else { - true - } - } - pub fn run(&mut self) { info!("Starting nym mixnode"); - if !self.version_check() { - error!("failed the local version check"); - return; - } let mut runtime = Runtime::new().unwrap();