Skip to content

Commit 7c65101

Browse files
authored
Feature/error on noninit (#404)
* More graceful shutdown in case of uninitialised client/node * Performing version check on binary `run`
1 parent 72e9fb1 commit 7c65101

File tree

6 files changed

+144
-60
lines changed

6 files changed

+144
-60
lines changed

clients/native/src/commands/run.rs

+36-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ use crate::client::NymClient;
1717
use crate::commands::override_config;
1818
use clap::{App, Arg, ArgMatches};
1919
use config::NymConfig;
20+
use log::*;
21+
use version_checker::is_minor_version_compatible;
2022

2123
pub fn command_args<'a, 'b>() -> clap::App<'a, 'b> {
2224
App::new("run")
@@ -66,14 +68,45 @@ pub fn command_args<'a, 'b>() -> clap::App<'a, 'b> {
6668
)
6769
}
6870

71+
// this only checks compatibility between config the binary. It does not take into consideration
72+
// network version. It might do so in the future.
73+
fn version_check(cfg: &Config) -> bool {
74+
let binary_version = env!("CARGO_PKG_VERSION");
75+
let config_version = cfg.get_base().get_version();
76+
if binary_version != config_version {
77+
warn!("The mixnode binary has different version than what is specified in config file! {} and {}", binary_version, config_version);
78+
if is_minor_version_compatible(binary_version, config_version) {
79+
info!("but they are still semver compatible. However, consider running the `upgrade` command");
80+
true
81+
} else {
82+
error!("and they are semver incompatible! - please run the `upgrade` command before attempting `run` again");
83+
false
84+
}
85+
} else {
86+
true
87+
}
88+
}
89+
6990
pub fn execute(matches: &ArgMatches) {
7091
let id = matches.value_of("id").unwrap();
7192

72-
let mut config =
73-
Config::load_from_file(matches.value_of("config").map(|path| path.into()), Some(id))
74-
.expect("Failed to load config file");
93+
let mut config = match Config::load_from_file(
94+
matches.value_of("config").map(|path| path.into()),
95+
Some(id),
96+
) {
97+
Ok(cfg) => cfg,
98+
Err(err) => {
99+
error!("Failed to load config for {}. Are you sure you have run `init` before? (Error was: {})", id, err);
100+
return;
101+
}
102+
};
75103

76104
config = override_config(config, matches);
77105

106+
if !version_check(&config) {
107+
error!("failed the local version check");
108+
return;
109+
}
110+
78111
NymClient::new(config).run_forever();
79112
}

clients/socks5/src/commands/run.rs

+36-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ use crate::client::NymClient;
1717
use crate::commands::override_config;
1818
use clap::{App, Arg, ArgMatches};
1919
use config::NymConfig;
20+
use log::*;
21+
use version_checker::is_minor_version_compatible;
2022

2123
pub fn command_args<'a, 'b>() -> clap::App<'a, 'b> {
2224
App::new("run")
@@ -67,14 +69,45 @@ pub fn command_args<'a, 'b>() -> clap::App<'a, 'b> {
6769
)
6870
}
6971

72+
// this only checks compatibility between config the binary. It does not take into consideration
73+
// network version. It might do so in the future.
74+
fn version_check(cfg: &Config) -> bool {
75+
let binary_version = env!("CARGO_PKG_VERSION");
76+
let config_version = cfg.get_base().get_version();
77+
if binary_version != config_version {
78+
warn!("The mixnode binary has different version than what is specified in config file! {} and {}", binary_version, config_version);
79+
if is_minor_version_compatible(binary_version, config_version) {
80+
info!("but they are still semver compatible. However, consider running the `upgrade` command");
81+
true
82+
} else {
83+
error!("and they are semver incompatible! - please run the `upgrade` command before attempting `run` again");
84+
false
85+
}
86+
} else {
87+
true
88+
}
89+
}
90+
7091
pub fn execute(matches: &ArgMatches) {
7192
let id = matches.value_of("id").unwrap();
7293

73-
let mut config =
74-
Config::load_from_file(matches.value_of("config").map(|path| path.into()), Some(id))
75-
.expect("Failed to load config file");
94+
let mut config = match Config::load_from_file(
95+
matches.value_of("config").map(|path| path.into()),
96+
Some(id),
97+
) {
98+
Ok(cfg) => cfg,
99+
Err(err) => {
100+
error!("Failed to load config for {}. Are you sure you have run `init` before? (Error was: {})", id, err);
101+
return;
102+
}
103+
};
76104

77105
config = override_config(config, matches);
78106

107+
if !version_check(&config) {
108+
error!("failed the local version check");
109+
return;
110+
}
111+
79112
NymClient::new(config).run_forever();
80113
}

gateway/src/commands/run.rs

+36-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ use crate::node::Gateway;
1919
use clap::{App, Arg, ArgMatches};
2020
use config::NymConfig;
2121
use crypto::asymmetric::{encryption, identity};
22+
use log::*;
23+
use version_checker::is_minor_version_compatible;
2224

2325
pub fn command_args<'a, 'b>() -> clap::App<'a, 'b> {
2426
App::new("run")
@@ -152,17 +154,48 @@ fn load_identity_keys(pathfinder: &GatewayPathfinder) -> identity::KeyPair {
152154
identity_keypair
153155
}
154156

157+
// this only checks compatibility between config the binary. It does not take into consideration
158+
// network version. It might do so in the future.
159+
fn version_check(cfg: &Config) -> bool {
160+
let binary_version = env!("CARGO_PKG_VERSION");
161+
let config_version = cfg.get_version();
162+
if binary_version != config_version {
163+
warn!("The mixnode binary has different version than what is specified in config file! {} and {}", binary_version, config_version);
164+
if is_minor_version_compatible(binary_version, config_version) {
165+
info!("but they are still semver compatible. However, consider running the `upgrade` command");
166+
true
167+
} else {
168+
error!("and they are semver incompatible! - please run the `upgrade` command before attempting `run` again");
169+
false
170+
}
171+
} else {
172+
true
173+
}
174+
}
175+
155176
pub fn execute(matches: &ArgMatches) {
156177
let id = matches.value_of("id").unwrap();
157178

158179
println!("Starting gateway {}...", id);
159180

160-
let mut config =
161-
Config::load_from_file(matches.value_of("config").map(|path| path.into()), Some(id))
162-
.expect("Failed to load config file");
181+
let mut config = match Config::load_from_file(
182+
matches.value_of("config").map(|path| path.into()),
183+
Some(id),
184+
) {
185+
Ok(cfg) => cfg,
186+
Err(err) => {
187+
error!("Failed to load config for {}. Are you sure you have run `init` before? (Error was: {})", id, err);
188+
return;
189+
}
190+
};
163191

164192
config = override_config(config, matches);
165193

194+
if !version_check(&config) {
195+
error!("failed the local version check");
196+
return;
197+
}
198+
166199
let pathfinder = GatewayPathfinder::new_from_config(&config);
167200
let sphinx_keypair = load_sphinx_keys(&pathfinder);
168201
let identity = load_identity_keys(&pathfinder);

gateway/src/node/mod.rs

-24
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ use log::*;
2222
use mixnet_client::forwarder::{MixForwardingSender, PacketForwarder};
2323
use std::sync::Arc;
2424
use tokio::runtime::Runtime;
25-
use version_checker::is_minor_version_compatible;
2625

2726
pub(crate) mod client_handling;
2827
pub(crate) mod mixnet_handling;
@@ -165,34 +164,11 @@ impl Gateway {
165164
.map(|node| node.identity())
166165
}
167166

168-
// this only checks compatibility between config the binary. It does not take into consideration
169-
// network version. It might do so in the future.
170-
fn version_check(&self) -> bool {
171-
let binary_version = env!("CARGO_PKG_VERSION");
172-
let config_version = self.config.get_version();
173-
if binary_version != config_version {
174-
warn!("The mixnode binary has different version than what is specified in config file! {} and {}", binary_version, config_version);
175-
if is_minor_version_compatible(binary_version, config_version) {
176-
info!("but they are still semver compatible. However, consider running the `upgrade` command");
177-
true
178-
} else {
179-
error!("and they are semver incompatible! - please run the `upgrade` command before attempting `run` again");
180-
false
181-
}
182-
} else {
183-
true
184-
}
185-
}
186-
187167
// Rather than starting all futures with explicit `&Handle` argument, let's see how it works
188168
// out if we make it implicit using `tokio::spawn` inside Runtime context.
189169
// Basically more or less equivalent of using #[tokio::main] attribute.
190170
pub fn run(&mut self) {
191171
info!("Starting nym gateway!");
192-
if !self.version_check() {
193-
error!("failed the local version check");
194-
return;
195-
}
196172

197173
let mut runtime = Runtime::new().unwrap();
198174

mixnode/src/commands/run.rs

+36-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ use crate::node::MixNode;
1818
use clap::{App, Arg, ArgMatches};
1919
use config::NymConfig;
2020
use crypto::asymmetric::{encryption, identity};
21+
use log::*;
22+
use version_checker::is_minor_version_compatible;
2123

2224
pub fn command_args<'a, 'b>() -> App<'a, 'b> {
2325
App::new("run")
@@ -127,17 +129,48 @@ fn load_sphinx_keys(pathfinder: &MixNodePathfinder) -> encryption::KeyPair {
127129
sphinx_keypair
128130
}
129131

132+
// this only checks compatibility between config the binary. It does not take into consideration
133+
// network version. It might do so in the future.
134+
fn version_check(cfg: &Config) -> bool {
135+
let binary_version = env!("CARGO_PKG_VERSION");
136+
let config_version = cfg.get_version();
137+
if binary_version != config_version {
138+
warn!("The mixnode binary has different version than what is specified in config file! {} and {}", binary_version, config_version);
139+
if is_minor_version_compatible(binary_version, config_version) {
140+
info!("but they are still semver compatible. However, consider running the `upgrade` command");
141+
true
142+
} else {
143+
error!("and they are semver incompatible! - please run the `upgrade` command before attempting `run` again");
144+
false
145+
}
146+
} else {
147+
true
148+
}
149+
}
150+
130151
pub fn execute(matches: &ArgMatches) {
131152
let id = matches.value_of("id").unwrap();
132153

133154
println!("Starting mixnode {}...", id);
134155

135-
let mut config =
136-
Config::load_from_file(matches.value_of("config").map(|path| path.into()), Some(id))
137-
.expect("Failed to load config file");
156+
let mut config = match Config::load_from_file(
157+
matches.value_of("config").map(|path| path.into()),
158+
Some(id),
159+
) {
160+
Ok(cfg) => cfg,
161+
Err(err) => {
162+
error!("Failed to load config for {}. Are you sure you have run `init` before? (Error was: {})", id, err);
163+
return;
164+
}
165+
};
138166

139167
config = override_config(config, matches);
140168

169+
if !version_check(&config) {
170+
error!("failed the local version check");
171+
return;
172+
}
173+
141174
let pathfinder = MixNodePathfinder::new_from_config(&config);
142175
let identity_keypair = load_identity_keys(&pathfinder);
143176
let sphinx_keypair = load_sphinx_keys(&pathfinder);

mixnode/src/node/mod.rs

-24
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use log::*;
2121
use mixnet_client::forwarder::{MixForwardingSender, PacketForwarder};
2222
use std::sync::Arc;
2323
use tokio::runtime::Runtime;
24-
use version_checker::is_minor_version_compatible;
2524

2625
mod listener;
2726
mod metrics;
@@ -126,31 +125,8 @@ impl MixNode {
126125
}
127126
}
128127

129-
// this only checks compatibility between config the binary. It does not take into consideration
130-
// network version. It might do so in the future.
131-
fn version_check(&self) -> bool {
132-
let binary_version = env!("CARGO_PKG_VERSION");
133-
let config_version = self.config.get_version();
134-
if binary_version != config_version {
135-
warn!("The mixnode binary has different version than what is specified in config file! {} and {}", binary_version, config_version);
136-
if is_minor_version_compatible(binary_version, config_version) {
137-
info!("but they are still semver compatible. However, consider running the `upgrade` command");
138-
true
139-
} else {
140-
error!("and they are semver incompatible! - please run the `upgrade` command before attempting `run` again");
141-
false
142-
}
143-
} else {
144-
true
145-
}
146-
}
147-
148128
pub fn run(&mut self) {
149129
info!("Starting nym mixnode");
150-
if !self.version_check() {
151-
error!("failed the local version check");
152-
return;
153-
}
154130

155131
let mut runtime = Runtime::new().unwrap();
156132

0 commit comments

Comments
 (0)