mixnode: Refactor upgrade functions

Renaming the function to have a clear separation between different
parts of the version, especially for future patch upgrade functions.

Also switch to a Result return value for these functions, to gather
all the
```
eprintln!
print_failed_upgrade
exit(1)
```
calls in one place.

Signed-off-by: Bogdan-Ștefan Neacșu <bogdan@nymtech.net>
This commit is contained in:
Bogdan-Ștefan Neacșu
2021-06-23 16:12:59 +03:00
parent b7f197d278
commit 3bf02c558c
+38 -26
View File
@@ -189,12 +189,12 @@ fn pre_090_upgrade(from: &str, config: Config) -> Config {
upgraded_config
}
fn minor_010_upgrade(
fn minor_0_10_upgrade(
config: Config,
_matches: &ArgMatches,
config_version: &Version,
package_version: &Version,
) -> Config {
) -> Result<Config, (Version, String)> {
let to_version = if package_version.major == 0 && package_version.minor == 10 {
package_version.clone()
} else {
@@ -204,8 +204,7 @@ fn minor_010_upgrade(
print_start_upgrade(&config_version, &to_version);
if config.get_validator_mixnet_contract_address() != MISSING_VALUE {
eprintln!("existing config seems to have specified mixnet contract address which was only introduced in 0.10.0! Can't perform upgrade.");
fail_upgrade(&config_version, to_version)
return Err((to_version, "existing config seems to have specified mixnet contract address which was only introduced in 0.10.0! Can't perform upgrade.".to_string()));
}
println!(
@@ -223,22 +222,24 @@ fn minor_010_upgrade(
.with_custom_validators(default_validator_rest_endpoints())
.with_custom_mixnet_contract(DEFAULT_MIXNET_CONTRACT_ADDRESS);
upgraded_config.save_to_file(None).unwrap_or_else(|err| {
eprintln!("failed to overwrite config file! - {:?}", err);
fail_upgrade(&config_version, &to_version)
});
upgraded_config.save_to_file(None).map_err(|err| {
(
to_version.clone(),
format!("failed to overwrite config file! - {:?}", err),
)
})?;
print_successful_upgrade(config_version, to_version);
upgraded_config
Ok(upgraded_config)
}
fn patch_010_upgrade(
fn patch_0_10_1_upgrade(
config: Config,
_matches: &ArgMatches,
config_version: &Version,
package_version: &Version,
) -> Config {
) -> Result<Config, (Version, String)> {
// welp, stuff like ports are mostly hardcoded and not part of the config so all is changes is just the version
// number
let to_version = package_version;
@@ -247,14 +248,16 @@ fn patch_010_upgrade(
let upgraded_config = config.with_custom_version(to_version.to_string().as_ref());
upgraded_config.save_to_file(None).unwrap_or_else(|err| {
eprintln!("failed to overwrite config file! - {:?}", err);
fail_upgrade(&config_version, to_version)
});
upgraded_config.save_to_file(None).map_err(|err| {
(
to_version.clone(),
format!("failed to overwrite config file! - {:?}", err),
)
})?;
print_successful_upgrade(config_version, to_version);
upgraded_config
Ok(upgraded_config)
}
// TODO: to be renamed once the release version is decided (so presumably either 0.10.2 or 0.11.0)
@@ -263,7 +266,7 @@ fn undetermined_version_upgrade(
_matches: &ArgMatches,
config_version: &Version,
package_version: &Version,
) -> Config {
) -> Result<Config, (Version, String)> {
// If we decide this version should be tagged with 0.11.0, then the following code will be used instead:
// let to_version = if package_version.major == 0 && package_version.minor == 11 {
// package_version.clone()
@@ -281,8 +284,10 @@ fn undetermined_version_upgrade(
Err(_) => {
let announce_split = current_annnounce_addr.split(':').collect::<Vec<_>>();
if announce_split.len() != 2 {
eprintln!("failed to correctly parse current announce host");
fail_upgrade(&config_version, &to_version)
return Err((
to_version.clone(),
"failed to correctly parse current announce host".to_string(),
));
}
(
announce_split[0].to_string(),
@@ -296,14 +301,16 @@ fn undetermined_version_upgrade(
.with_announce_address(announce_address)
.with_mix_port(custom_mix_port);
upgraded_config.save_to_file(None).unwrap_or_else(|err| {
eprintln!("failed to overwrite config file! - {:?}", err);
fail_upgrade(&config_version, to_version)
});
upgraded_config.save_to_file(None).map_err(|err| {
(
to_version.clone(),
format!("failed to overwrite config file! - {:?}", err),
)
})?;
print_successful_upgrade(config_version, to_version);
upgraded_config
Ok(upgraded_config)
}
fn do_upgrade(mut config: Config, matches: &ArgMatches, package_version: Version) {
@@ -317,9 +324,9 @@ fn do_upgrade(mut config: Config, matches: &ArgMatches, package_version: Version
config = match config_version.major {
0 => match config_version.minor {
9 => minor_010_upgrade(config, matches, &config_version, &package_version),
9 => minor_0_10_upgrade(config, matches, &config_version, &package_version),
10 => match config_version.patch {
0 => patch_010_upgrade(config, matches, &config_version, &package_version),
0 => patch_0_10_1_upgrade(config, matches, &config_version, &package_version),
_ => undetermined_version_upgrade(
config,
matches,
@@ -331,6 +338,11 @@ fn do_upgrade(mut config: Config, matches: &ArgMatches, package_version: Version
},
_ => unsupported_upgrade(config_version, package_version),
}
.unwrap_or_else(|(to_version, err)| {
eprintln!("{:?}", err);
print_failed_upgrade(&config_version, &to_version);
process::exit(1);
});
}
}