From 1d4420820f9dc0dfca229f74d9556ce97382c766 Mon Sep 17 00:00:00 2001 From: Dave Hrycyszyn Date: Tue, 30 Mar 2021 15:36:16 +0100 Subject: [PATCH 1/6] Feature/export coin helper (#550) * Annoying whitesapce commit * ibid * Re-exporting `coin` so client applications don't need to import it from some random spot --- clients/validator/src/index.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/clients/validator/src/index.ts b/clients/validator/src/index.ts index 277b1116b8..049e491a58 100644 --- a/clients/validator/src/index.ts +++ b/clients/validator/src/index.ts @@ -8,9 +8,9 @@ import { BroadcastTxResponse } from "@cosmjs/stargate" import { ExecuteResult, InstantiateOptions, InstantiateResult, UploadMeta, UploadResult } from "@cosmjs/cosmwasm"; import { CoinMap, displayAmountToNative, MappedCoin, nativeCoinToDisplay, printableBalance, printableCoin } from "./currency"; import GatewaysCache from "./caches/gateways"; -import QueryClient, {IQueryClient} from "./query-client"; +import QueryClient, { IQueryClient } from "./query-client"; -export { coins }; +export { coins, coin }; export { Coin }; export { displayAmountToNative, nativeCoinToDisplay, printableCoin, printableBalance, MappedCoin, CoinMap } @@ -115,7 +115,7 @@ export default class ValidatorClient { async bond(mixNode: MixNode): Promise { if (this.client instanceof NetClient) { const bond = [{ amount: "1000000000", denom: this.stakeDenom }]; - const result = await this.client.executeContract(this.client.clientAddress, this.contractAddress, {register_mixnode: {mix_node: mixNode}}, "adding mixnode", bond); + const result = await this.client.executeContract(this.client.clientAddress, this.contractAddress, { register_mixnode: { mix_node: mixNode } }, "adding mixnode", bond); console.log(`account ${this.client.clientAddress} added mixnode with ${mixNode.host}`); return result; } else { @@ -129,7 +129,7 @@ export default class ValidatorClient { */ async unbond(): Promise { if (this.client instanceof NetClient) { - const result = await this.client.executeContract(this.client.clientAddress, this.contractAddress, {un_register_mixnode: {}}) + const result = await this.client.executeContract(this.client.clientAddress, this.contractAddress, { un_register_mixnode: {} }) console.log(`account ${this.client.clientAddress} unbonded mixnode`); return result; } else { @@ -196,7 +196,7 @@ export default class ValidatorClient { async bondGateway(gateway: Gateway): Promise { if (this.client instanceof NetClient) { const bond = this.minimumGatewayBond() - const result = await this.client.executeContract(this.client.clientAddress, this.contractAddress, {bond_gateway: {gateway: gateway}}, "adding gateway", [bond]); + const result = await this.client.executeContract(this.client.clientAddress, this.contractAddress, { bond_gateway: { gateway: gateway } }, "adding gateway", [bond]); console.log(`account ${this.client.clientAddress} added gateway with ${gateway.mix_host}`); return result; } else { @@ -209,7 +209,7 @@ export default class ValidatorClient { */ async unbondGateway(): Promise { if (this.client instanceof NetClient) { - const result = await this.client.executeContract(this.client.clientAddress, this.contractAddress, {unbond_gateway: {}}) + const result = await this.client.executeContract(this.client.clientAddress, this.contractAddress, { unbond_gateway: {} }) console.log(`account ${this.client.clientAddress} unbonded gateway`); return result; } else { From a97377fdeaabbfdf6cb1de391bdab9c362d5a01a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C4=99drzej=20Stuczy=C5=84ski?= Date: Tue, 30 Mar 2021 15:47:59 +0100 Subject: [PATCH 2/6] Feature/upgrade update (#551) * Native client upgrade command capable of upgrading to 0.10.0 * Ibid for the socks5 client * Mixnode upgrade command for 0.10.0 * Fixed clippy warnings * Gateway upgrade command for 0.10.0 * Removed unused import --- Cargo.lock | 10 +- clients/client-core/Cargo.toml | 2 +- clients/native/Cargo.toml | 2 +- clients/native/src/commands/upgrade.rs | 183 +++++++++++++++---------- clients/socks5/Cargo.toml | 2 +- clients/socks5/src/commands/upgrade.rs | 183 +++++++++++++++---------- gateway/Cargo.toml | 2 +- gateway/src/commands/upgrade.rs | 176 ++++++++++++++---------- mixnode/Cargo.toml | 2 +- mixnode/src/commands/upgrade.rs | 177 ++++++++++++++---------- 10 files changed, 431 insertions(+), 308 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0a37989170..528c51a738 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -345,7 +345,7 @@ dependencies = [ [[package]] name = "client-core" -version = "0.9.2" +version = "0.10.0" dependencies = [ "config", "crypto", @@ -1420,7 +1420,7 @@ dependencies = [ [[package]] name = "nym-client" -version = "0.9.2" +version = "0.10.0" dependencies = [ "clap", "client-core", @@ -1471,7 +1471,7 @@ dependencies = [ [[package]] name = "nym-gateway" -version = "0.9.2" +version = "0.10.0" dependencies = [ "clap", "config", @@ -1501,7 +1501,7 @@ dependencies = [ [[package]] name = "nym-mixnode" -version = "0.9.2" +version = "0.10.0" dependencies = [ "bs58 0.4.0", "clap", @@ -1574,7 +1574,7 @@ dependencies = [ [[package]] name = "nym-socks5-client" -version = "0.9.2" +version = "0.10.0" dependencies = [ "clap", "client-core", diff --git a/clients/client-core/Cargo.toml b/clients/client-core/Cargo.toml index 76bb6579cc..61d5221557 100644 --- a/clients/client-core/Cargo.toml +++ b/clients/client-core/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "client-core" -version = "0.9.2" +version = "0.10.0" authors = ["Dave Hrycyszyn "] edition = "2018" diff --git a/clients/native/Cargo.toml b/clients/native/Cargo.toml index 62a87ca092..ebc641deb6 100644 --- a/clients/native/Cargo.toml +++ b/clients/native/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "nym-client" -version = "0.9.2" +version = "0.10.0" authors = ["Dave Hrycyszyn ", "Jędrzej Stuczyński "] edition = "2018" diff --git a/clients/native/src/commands/upgrade.rs b/clients/native/src/commands/upgrade.rs index 44e2c04231..d27dc5048e 100644 --- a/clients/native/src/commands/upgrade.rs +++ b/clients/native/src/commands/upgrade.rs @@ -14,7 +14,7 @@ use crate::client::config::{Config, MISSING_VALUE}; use clap::{App, Arg, ArgMatches}; -use client_core::config::DEFAULT_VALIDATOR_REST_ENDPOINT; +use client_core::config::{DEFAULT_MIXNET_CONTRACT_ADDRESS, DEFAULT_VALIDATOR_REST_ENDPOINT}; use config::NymConfig; use std::fmt::Display; use std::process; @@ -41,6 +41,62 @@ fn print_successful_upgrade(from: D1, to: D2) { ); } +pub fn command_args<'a, 'b>() -> App<'a, 'b> { + App::new("upgrade").about("Try to upgrade the client") + .arg( + Arg::with_name("id") + .long("id") + .help("Id of the nym-client we want to upgrade") + .takes_value(true) + .required(true), + ) + // the rest of arguments depend on the upgrade path + .arg(Arg::with_name("current version") + .long("current-version") + .help("REQUIRED FOR PRE-0.9.0 UPGRADES. Specifies current version of the configuration file to help to determine a valid upgrade path. Valid formats include '0.8.1', 'v0.8.1' or 'V0.8.1'") + .takes_value(true) + ) +} + +fn unsupported_upgrade(config_version: Version, package_version: Version) -> ! { + eprintln!("Cannot perform upgrade from {} to {}. Please let the developers know about this issue if you expected it to work!", config_version, package_version); + process::exit(1) +} + +fn parse_config_version(config: &Config) -> Version { + let version = Version::parse(config.get_base().get_version()).unwrap_or_else(|err| { + eprintln!("failed to parse client version! - {:?}", err); + process::exit(1) + }); + + if version.is_prerelease() || !version.build.is_empty() { + eprintln!( + "Trying to upgrade from a non-released version {}. This is not supported!", + version + ); + process::exit(1) + } + + version +} + +fn parse_package_version() -> Version { + let version = Version::parse(env!("CARGO_PKG_VERSION")).unwrap(); + + // technically this is not a correct way of checking it as a released version might contain valid build identifiers + // however, we are not using them ourselves at the moment and hence it should be fine. + // if we change our mind, we could easily tweak this code + if version.is_prerelease() || !version.build.is_empty() { + eprintln!( + "Trying to upgrade to a non-released version {}. This is not supported!", + version + ); + process::exit(1) + } + + version +} + fn pre_090_upgrade(from: &str, mut config: Config) -> Config { // this is not extracted to separate function as you only have to manually pass version // if upgrading from pre090 version @@ -112,89 +168,86 @@ fn pre_090_upgrade(from: &str, mut config: Config) -> Config { config } -fn patch_09x_upgrade(mut config: Config, _matches: &ArgMatches) -> Config { - // this call must succeed as it was already called before - let from_version = Version::parse(config.get_base().get_version()).unwrap(); - let to_version = Version::parse(env!("CARGO_PKG_VERSION")).unwrap(); +/* +changes: +- introduction of mixnet contract address field +- change to default validator rest endpoint + */ +fn minor_010_upgrade( + mut config: Config, + _matches: &ArgMatches, + config_version: &Version, + package_version: &Version, +) -> Config { + let to_version = if package_version.major == 0 && package_version.minor == 10 { + package_version.clone() + } else { + Version::new(0, 10, 0) + }; - print_start_upgrade(&from_version, &to_version); + print_start_upgrade(&config_version, &to_version); - // 0.9.1 upgrade: config .get_base_mut() .set_custom_version(to_version.to_string().as_ref()); + if config.get_base().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."); + print_failed_upgrade(&config_version, &to_version); + process::exit(1); + } + + println!( + "Setting mixnet contract address to {}", + DEFAULT_MIXNET_CONTRACT_ADDRESS + ); + + config + .get_base_mut() + .set_mixnet_contract(DEFAULT_MIXNET_CONTRACT_ADDRESS); + + // The default validator endpoint changed + println!( + "Setting validator REST endpoint to to {}", + DEFAULT_VALIDATOR_REST_ENDPOINT + ); + + config + .get_base_mut() + .set_custom_validator(DEFAULT_VALIDATOR_REST_ENDPOINT); + config.save_to_file(None).unwrap_or_else(|err| { eprintln!("failed to overwrite config file! - {:?}", err); - print_failed_upgrade(&from_version, &to_version); + print_failed_upgrade(&config_version, &to_version); process::exit(1); }); - print_successful_upgrade(from_version, to_version); + print_successful_upgrade(config_version, to_version); config } -pub fn command_args<'a, 'b>() -> App<'a, 'b> { - App::new("upgrade").about("Try to upgrade the client") - .arg( - Arg::with_name("id") - .long("id") - .help("Id of the nym-client we want to upgrade") - .takes_value(true) - .required(true), - ) - // the rest of arguments depend on the upgrade path - .arg(Arg::with_name("current version") - .long("current-version") - .help("REQUIRED FOR PRE-0.9.0 UPGRADES. Specifies current version of the configuration file to help to determine a valid upgrade path. Valid formats include '0.8.1', 'v0.8.1' or 'V0.8.1'") - .takes_value(true) - ) -} - -fn unsupported_upgrade(current_version: Version, config_version: Version) -> ! { - eprintln!("Cannot perform upgrade from {} to {}. Please let the developers know about this issue if you expected it to work!", config_version, current_version); - process::exit(1) -} - -fn do_upgrade(mut config: Config, matches: &ArgMatches) { - let current = Version::parse(env!("CARGO_PKG_VERSION")).unwrap(); - +fn do_upgrade(mut config: Config, matches: &ArgMatches, package_version: Version) { loop { - let config_version = - Version::parse(config.get_base().get_version()).unwrap_or_else(|err| { - eprintln!("failed to parse client version! - {:?}", err); - process::exit(1) - }); + let config_version = parse_config_version(&config); - if config_version == current { + if config_version == package_version { println!("You're using the most recent version!"); return; } config = match config_version.major { 0 => match config_version.minor { - 9 => patch_09x_upgrade(config, &matches), - _ => unsupported_upgrade(current, config_version), + 9 => minor_010_upgrade(config, &matches, &config_version, &package_version), + _ => unsupported_upgrade(config_version, package_version), }, - _ => unsupported_upgrade(current, config_version), + _ => unsupported_upgrade(config_version, package_version), } } } pub fn execute(matches: &ArgMatches) { - let current = Version::parse(env!("CARGO_PKG_VERSION")).unwrap(); - - // technically this is not a correct way of checking it as a released version might contain valid build identifiers - // however, we are not using them ourselves at the moment and hence it should be fine. - // if we change our mind, we could easily tweak this code - if current.is_prerelease() || !current.build.is_empty() { - eprintln!( - "Trying to upgrade to a non-released version {}. This is not supported!", - current - ); - process::exit(1) - } + let package_version = parse_package_version(); let id = matches.value_of("id").unwrap(); @@ -216,24 +269,6 @@ pub fn execute(matches: &ArgMatches) { existing_config = pre_090_upgrade(self_reported_version, existing_config); } - let config_version = - Version::parse(existing_config.get_base().get_version()).unwrap_or_else(|err| { - eprintln!("failed to parse client version! - {:?}", err); - process::exit(1) - }); - - if config_version.is_prerelease() || !config_version.build.is_empty() { - eprintln!( - "Trying to upgrade from non-released version {}. This is not supported!", - config_version - ); - process::exit(1) - } - // here be upgrade path to 0.9.X and beyond based on version number from config - if config_version == current { - println!("You're using the most recent version!"); - } else { - do_upgrade(existing_config, matches) - } + do_upgrade(existing_config, matches, package_version) } diff --git a/clients/socks5/Cargo.toml b/clients/socks5/Cargo.toml index 3fd1724abb..35d224aab6 100644 --- a/clients/socks5/Cargo.toml +++ b/clients/socks5/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "nym-socks5-client" -version = "0.9.2" +version = "0.10.0" authors = ["Dave Hrycyszyn "] edition = "2018" diff --git a/clients/socks5/src/commands/upgrade.rs b/clients/socks5/src/commands/upgrade.rs index 0615983ed6..6a783d8824 100644 --- a/clients/socks5/src/commands/upgrade.rs +++ b/clients/socks5/src/commands/upgrade.rs @@ -14,7 +14,7 @@ use crate::client::config::{Config, MISSING_VALUE}; use clap::{App, Arg, ArgMatches}; -use client_core::config::DEFAULT_VALIDATOR_REST_ENDPOINT; +use client_core::config::{DEFAULT_MIXNET_CONTRACT_ADDRESS, DEFAULT_VALIDATOR_REST_ENDPOINT}; use config::NymConfig; use std::fmt::Display; use std::process; @@ -41,6 +41,62 @@ fn print_successful_upgrade(from: D1, to: D2) { ); } +pub fn command_args<'a, 'b>() -> App<'a, 'b> { + App::new("upgrade").about("Try to upgrade the client") + .arg( + Arg::with_name("id") + .long("id") + .help("Id of the nym-socks5-client we want to upgrade") + .takes_value(true) + .required(true), + ) + // the rest of arguments depend on the upgrade path + .arg(Arg::with_name("current version") + .long("current-version") + .help("REQUIRED FOR PRE-0.9.0 UPGRADES. Specifies current version of the configuration file to help to determine a valid upgrade path. Valid formats include '0.8.1', 'v0.8.1' or 'V0.8.1'") + .takes_value(true) + ) +} + +fn unsupported_upgrade(config_version: Version, package_version: Version) -> ! { + eprintln!("Cannot perform upgrade from {} to {}. Please let the developers know about this issue if you expected it to work!", config_version, package_version); + process::exit(1) +} + +fn parse_config_version(config: &Config) -> Version { + let version = Version::parse(config.get_base().get_version()).unwrap_or_else(|err| { + eprintln!("failed to parse client version! - {:?}", err); + process::exit(1) + }); + + if version.is_prerelease() || !version.build.is_empty() { + eprintln!( + "Trying to upgrade from a non-released version {}. This is not supported!", + version + ); + process::exit(1) + } + + version +} + +fn parse_package_version() -> Version { + let version = Version::parse(env!("CARGO_PKG_VERSION")).unwrap(); + + // technically this is not a correct way of checking it as a released version might contain valid build identifiers + // however, we are not using them ourselves at the moment and hence it should be fine. + // if we change our mind, we could easily tweak this code + if version.is_prerelease() || !version.build.is_empty() { + eprintln!( + "Trying to upgrade to a non-released version {}. This is not supported!", + version + ); + process::exit(1) + } + + version +} + fn pre_090_upgrade(from: &str, mut config: Config) -> Config { // this is not extracted to separate function as you only have to manually pass version // if upgrading from pre090 version @@ -112,89 +168,86 @@ fn pre_090_upgrade(from: &str, mut config: Config) -> Config { config } -fn patch_09x_upgrade(mut config: Config, _matches: &ArgMatches) -> Config { - // this call must succeed as it was already called before - let from_version = Version::parse(config.get_base().get_version()).unwrap(); - let to_version = Version::parse(env!("CARGO_PKG_VERSION")).unwrap(); +/* +changes: +- introduction of mixnet contract address field +- change to default validator rest endpoint + */ +fn minor_010_upgrade( + mut config: Config, + _matches: &ArgMatches, + config_version: &Version, + package_version: &Version, +) -> Config { + let to_version = if package_version.major == 0 && package_version.minor == 10 { + package_version.clone() + } else { + Version::new(0, 10, 0) + }; - print_start_upgrade(&from_version, &to_version); + print_start_upgrade(&config_version, &to_version); - // 0.9.1 upgrade: config .get_base_mut() .set_custom_version(to_version.to_string().as_ref()); + if config.get_base().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."); + print_failed_upgrade(&config_version, &to_version); + process::exit(1); + } + + println!( + "Setting mixnet contract address to {}", + DEFAULT_MIXNET_CONTRACT_ADDRESS + ); + + config + .get_base_mut() + .set_mixnet_contract(DEFAULT_MIXNET_CONTRACT_ADDRESS); + + // The default validator endpoint changed + println!( + "Setting validator REST endpoint to to {}", + DEFAULT_VALIDATOR_REST_ENDPOINT + ); + + config + .get_base_mut() + .set_custom_validator(DEFAULT_VALIDATOR_REST_ENDPOINT); + config.save_to_file(None).unwrap_or_else(|err| { eprintln!("failed to overwrite config file! - {:?}", err); - print_failed_upgrade(&from_version, &to_version); + print_failed_upgrade(&config_version, &to_version); process::exit(1); }); - print_successful_upgrade(from_version, to_version); + print_successful_upgrade(config_version, to_version); config } -pub fn command_args<'a, 'b>() -> App<'a, 'b> { - App::new("upgrade").about("Try to upgrade the client") - .arg( - Arg::with_name("id") - .long("id") - .help("Id of the nym-socks5-client we want to upgrade") - .takes_value(true) - .required(true), - ) - // the rest of arguments depend on the upgrade path - .arg(Arg::with_name("current version") - .long("current-version") - .help("REQUIRED FOR PRE-0.9.0 UPGRADES. Specifies current version of the configuration file to help to determine a valid upgrade path. Valid formats include '0.8.1', 'v0.8.1' or 'V0.8.1'") - .takes_value(true) - ) -} - -fn unsupported_upgrade(current_version: Version, config_version: Version) -> ! { - eprintln!("Cannot perform upgrade from {} to {}. Please let the developers know about this issue if you expected it to work!", config_version, current_version); - process::exit(1) -} - -fn do_upgrade(mut config: Config, matches: &ArgMatches) { - let current = Version::parse(env!("CARGO_PKG_VERSION")).unwrap(); - +fn do_upgrade(mut config: Config, matches: &ArgMatches, package_version: Version) { loop { - let config_version = - Version::parse(config.get_base().get_version()).unwrap_or_else(|err| { - eprintln!("failed to parse client version! - {:?}", err); - process::exit(1) - }); + let config_version = parse_config_version(&config); - if config_version == current { + if config_version == package_version { println!("You're using the most recent version!"); return; } config = match config_version.major { 0 => match config_version.minor { - 9 => patch_09x_upgrade(config, &matches), - _ => unsupported_upgrade(current, config_version), + 9 => minor_010_upgrade(config, &matches, &config_version, &package_version), + _ => unsupported_upgrade(config_version, package_version), }, - _ => unsupported_upgrade(current, config_version), + _ => unsupported_upgrade(config_version, package_version), } } } pub fn execute(matches: &ArgMatches) { - let current = Version::parse(env!("CARGO_PKG_VERSION")).unwrap(); - - // technically this is not a correct way of checking it as a released version might contain valid build identifiers - // however, we are not using them ourselves at the moment and hence it should be fine. - // if we change our mind, we could easily tweak this code - if current.is_prerelease() || !current.build.is_empty() { - eprintln!( - "Trying to upgrade to a non-released version {}. This is not supported!", - current - ); - process::exit(1) - } + let package_version = parse_package_version(); let id = matches.value_of("id").unwrap(); @@ -216,24 +269,6 @@ pub fn execute(matches: &ArgMatches) { existing_config = pre_090_upgrade(self_reported_version, existing_config); } - let config_version = - Version::parse(existing_config.get_base().get_version()).unwrap_or_else(|err| { - eprintln!("failed to parse client version! - {:?}", err); - process::exit(1) - }); - - if config_version.is_prerelease() || !config_version.build.is_empty() { - eprintln!( - "Trying to upgrade from non-released version {}. This is not supported!", - config_version - ); - process::exit(1) - } - // here be upgrade path to 0.9.X and beyond based on version number from config - if config_version == current { - println!("You're using the most recent version!"); - } else { - do_upgrade(existing_config, matches) - } + do_upgrade(existing_config, matches, package_version) } diff --git a/gateway/Cargo.toml b/gateway/Cargo.toml index 0686dc4fd9..c09a21d361 100644 --- a/gateway/Cargo.toml +++ b/gateway/Cargo.toml @@ -3,7 +3,7 @@ [package] name = "nym-gateway" -version = "0.9.2" +version = "0.10.0" authors = ["Dave Hrycyszyn ", "Jędrzej Stuczyński "] edition = "2018" diff --git a/gateway/src/commands/upgrade.rs b/gateway/src/commands/upgrade.rs index 942049d8bd..822231daed 100644 --- a/gateway/src/commands/upgrade.rs +++ b/gateway/src/commands/upgrade.rs @@ -1,8 +1,8 @@ // Copyright 2020 - Nym Technologies SA // SPDX-License-Identifier: Apache-2.0 -use crate::config::DEFAULT_VALIDATOR_REST_ENDPOINT; use crate::config::{Config, MISSING_VALUE}; +use crate::config::{DEFAULT_MIXNET_CONTRACT_ADDRESS, DEFAULT_VALIDATOR_REST_ENDPOINT}; use clap::{App, Arg, ArgMatches}; use config::NymConfig; use std::fmt::Display; @@ -30,7 +30,63 @@ fn print_successful_upgrade(from: D1, to: D2) { ); } -fn pre_090_upgrade(from: &str, config: Config, _matches: &ArgMatches) -> Config { +pub fn command_args<'a, 'b>() -> App<'a, 'b> { + App::new("upgrade").about("Try to upgrade the gateway") + .arg( + Arg::with_name("id") + .long("id") + .help("Id of the nym-gateway we want to upgrade") + .takes_value(true) + .required(true), + ) + // the rest of arguments depend on the upgrade path + .arg(Arg::with_name("current version") + .long("current-version") + .help("REQUIRED FOR PRE-0.9.0 UPGRADES. Specifies current version of the configuration file to help to determine a valid upgrade path. Valid formats include '0.8.1', 'v0.8.1' or 'V0.8.1'") + .takes_value(true) + ) +} + +fn unsupported_upgrade(current_version: Version, config_version: Version) -> ! { + eprintln!("Cannot perform upgrade from {} to {}. Please let the developers know about this issue if you expected it to work!", config_version, current_version); + process::exit(1) +} + +fn parse_config_version(config: &Config) -> Version { + let version = Version::parse(config.get_version()).unwrap_or_else(|err| { + eprintln!("failed to parse client version! - {:?}", err); + process::exit(1) + }); + + if version.is_prerelease() || !version.build.is_empty() { + eprintln!( + "Trying to upgrade from a non-released version {}. This is not supported!", + version + ); + process::exit(1) + } + + version +} + +fn parse_package_version() -> Version { + let version = Version::parse(env!("CARGO_PKG_VERSION")).unwrap(); + + // technically this is not a correct way of checking it as a released version might contain valid build identifiers + // however, we are not using them ourselves at the moment and hence it should be fine. + // if we change our mind, we could easily tweak this code + if version.is_prerelease() || !version.build.is_empty() { + eprintln!( + "Trying to upgrade to a non-released version {}. This is not supported!", + version + ); + process::exit(1) + } + + version +} + +fn pre_090_upgrade(from: &str, config: Config) -> Config { // this is not extracted to separate function as you only have to manually pass version // if upgrading from pre090 version let from = match from.strip_prefix("v") { @@ -97,86 +153,73 @@ fn pre_090_upgrade(from: &str, config: Config, _matches: &ArgMatches) -> Config upgraded_config } -fn patch_09x_upgrade(config: Config, _matches: &ArgMatches) -> Config { - // this call must succeed as it was already called before - let from_version = Version::parse(config.get_version()).unwrap(); - let to_version = Version::parse(env!("CARGO_PKG_VERSION")).unwrap(); +fn minor_010_upgrade( + config: Config, + _matches: &ArgMatches, + config_version: &Version, + package_version: &Version, +) -> Config { + let to_version = if package_version.major == 0 && package_version.minor == 10 { + package_version.clone() + } else { + Version::new(0, 10, 0) + }; - print_start_upgrade(&from_version, &to_version); + print_start_upgrade(&config_version, &to_version); - // 0.9.1 upgrade: - let upgraded_config = config.with_custom_version(to_version.to_string().as_ref()); + 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."); + print_failed_upgrade(&config_version, &to_version); + process::exit(1); + } + + println!( + "Setting validator REST endpoint to {}", + DEFAULT_VALIDATOR_REST_ENDPOINT + ); + + println!( + "Setting mixnet contract address to {}", + DEFAULT_MIXNET_CONTRACT_ADDRESS + ); + + let upgraded_config = config + .with_custom_version(to_version.to_string().as_ref()) + .with_custom_validator(DEFAULT_VALIDATOR_REST_ENDPOINT) + .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); - print_failed_upgrade(&from_version, &to_version); + print_failed_upgrade(&config_version, &to_version); process::exit(1); }); - print_successful_upgrade(from_version, to_version); + print_successful_upgrade(config_version, to_version); upgraded_config } -pub fn command_args<'a, 'b>() -> App<'a, 'b> { - App::new("upgrade").about("Try to upgrade the gateway") - .arg( - Arg::with_name("id") - .long("id") - .help("Id of the nym-gateway we want to upgrade") - .takes_value(true) - .required(true), - ) - // the rest of arguments depend on the upgrade path - .arg(Arg::with_name("current version") - .long("current-version") - .help("REQUIRED FOR PRE-0.9.0 UPGRADES. Specifies current version of the configuration file to help to determine a valid upgrade path. Valid formats include '0.8.1', 'v0.8.1' or 'V0.8.1'") - .takes_value(true) - ) -} - -fn unsupported_upgrade(current_version: Version, config_version: Version) -> ! { - eprintln!("Cannot perform upgrade from {} to {}. Please let the developers know about this issue if you expected it to work!", config_version, current_version); - process::exit(1) -} - -fn do_upgrade(mut config: Config, matches: &ArgMatches) { - let current = Version::parse(env!("CARGO_PKG_VERSION")).unwrap(); - +fn do_upgrade(mut config: Config, matches: &ArgMatches, package_version: Version) { loop { - let config_version = Version::parse(config.get_version()).unwrap_or_else(|err| { - eprintln!("failed to parse node version! - {:?}", err); - process::exit(1) - }); + let config_version = parse_config_version(&config); - if config_version == current { + if config_version == package_version { println!("You're using the most recent version!"); return; } config = match config_version.major { 0 => match config_version.minor { - 9 => patch_09x_upgrade(config, &matches), - _ => unsupported_upgrade(current, config_version), + 9 => minor_010_upgrade(config, &matches, &config_version, &package_version), + _ => unsupported_upgrade(config_version, package_version), }, - _ => unsupported_upgrade(current, config_version), + _ => unsupported_upgrade(config_version, package_version), } } } pub fn execute(matches: &ArgMatches) { - let current = Version::parse(env!("CARGO_PKG_VERSION")).unwrap(); - - // technically this is not a correct way of checking it as a released version might contain valid build identifiers - // however, we are not using them ourselves at the moment and hence it should be fine. - // if we change our mind, we could easily tweak this code - if current.is_prerelease() || !current.build.is_empty() { - eprintln!( - "Trying to upgrade to a non-released version {}. This is not supported!", - current - ); - process::exit(1) - } + let package_version = parse_package_version(); let id = matches.value_of("id").unwrap(); @@ -195,26 +238,9 @@ pub fn execute(matches: &ArgMatches) { }); // upgrades up to 0.9.0 - existing_config = pre_090_upgrade(self_reported_version, existing_config, &matches); - } - - let config_version = Version::parse(existing_config.get_version()).unwrap_or_else(|err| { - eprintln!("failed to parse node version! - {:?}", err); - process::exit(1) - }); - - if config_version.is_prerelease() || !config_version.build.is_empty() { - eprintln!( - "Trying to upgrade from non-released version {}. This is not supported!", - config_version - ); - process::exit(1) + existing_config = pre_090_upgrade(self_reported_version, existing_config); } // here be upgrade path to 0.9.X and beyond based on version number from config - if config_version == current { - println!("You're using the most recent version!"); - } else { - do_upgrade(existing_config, matches) - } + do_upgrade(existing_config, matches, package_version) } diff --git a/mixnode/Cargo.toml b/mixnode/Cargo.toml index 741b4f01f6..6fc8938dfa 100644 --- a/mixnode/Cargo.toml +++ b/mixnode/Cargo.toml @@ -3,7 +3,7 @@ [package] name = "nym-mixnode" -version = "0.9.2" +version = "0.10.0" authors = ["Dave Hrycyszyn ", "Jędrzej Stuczyński "] edition = "2018" diff --git a/mixnode/src/commands/upgrade.rs b/mixnode/src/commands/upgrade.rs index 78c3988a47..0a8f56f090 100644 --- a/mixnode/src/commands/upgrade.rs +++ b/mixnode/src/commands/upgrade.rs @@ -2,7 +2,8 @@ // SPDX-License-Identifier: Apache-2.0 use crate::config::{ - missing_string_value, Config, DEFAULT_METRICS_SERVER, DEFAULT_VALIDATOR_REST_ENDPOINT, + missing_string_value, Config, DEFAULT_METRICS_SERVER, DEFAULT_MIXNET_CONTRACT_ADDRESS, + DEFAULT_VALIDATOR_REST_ENDPOINT, MISSING_VALUE, }; use clap::{App, Arg, ArgMatches}; use config::NymConfig; @@ -33,7 +34,63 @@ fn print_successful_upgrade(from: D1, to: D2) { ); } -fn pre_090_upgrade(from: &str, config: Config, _matches: &ArgMatches) -> Config { +pub fn command_args<'a, 'b>() -> App<'a, 'b> { + App::new("upgrade").about("Try to upgrade the mixnode") + .arg( + Arg::with_name("id") + .long("id") + .help("Id of the nym-mixnode we want to upgrade") + .takes_value(true) + .required(true), + ) + // the rest of arguments depend on the upgrade path + .arg(Arg::with_name("current version") + .long("current-version") + .help("REQUIRED FOR PRE-0.9.0 UPGRADES. Specifies current version of the configuration file to help to determine a valid upgrade path. Valid formats include '0.8.1', 'v0.8.1' or 'V0.8.1'") + .takes_value(true) + ) +} + +fn unsupported_upgrade(config_version: Version, package_version: Version) -> ! { + eprintln!("Cannot perform upgrade from {} to {}. Please let the developers know about this issue if you expected it to work!", config_version, package_version); + process::exit(1) +} + +fn parse_config_version(config: &Config) -> Version { + let version = Version::parse(config.get_version()).unwrap_or_else(|err| { + eprintln!("failed to parse client version! - {:?}", err); + process::exit(1) + }); + + if version.is_prerelease() || !version.build.is_empty() { + eprintln!( + "Trying to upgrade from a non-released version {}. This is not supported!", + version + ); + process::exit(1) + } + + version +} + +fn parse_package_version() -> Version { + let version = Version::parse(env!("CARGO_PKG_VERSION")).unwrap(); + + // technically this is not a correct way of checking it as a released version might contain valid build identifiers + // however, we are not using them ourselves at the moment and hence it should be fine. + // if we change our mind, we could easily tweak this code + if version.is_prerelease() || !version.build.is_empty() { + eprintln!( + "Trying to upgrade to a non-released version {}. This is not supported!", + version + ); + process::exit(1) + } + + version +} + +fn pre_090_upgrade(from: &str, config: Config) -> Config { // note: current is guaranteed to not have any `build` information suffix (nor pre-release // information), as this was asserted at the beginning of this command) // @@ -135,86 +192,73 @@ fn pre_090_upgrade(from: &str, config: Config, _matches: &ArgMatches) -> Config upgraded_config } -fn patch_09x_upgrade(config: Config, _matches: &ArgMatches) -> Config { - // this call must succeed as it was already called before - let from_version = Version::parse(config.get_version()).unwrap(); - let to_version = Version::parse(env!("CARGO_PKG_VERSION")).unwrap(); +fn minor_010_upgrade( + config: Config, + _matches: &ArgMatches, + config_version: &Version, + package_version: &Version, +) -> Config { + let to_version = if package_version.major == 0 && package_version.minor == 10 { + package_version.clone() + } else { + Version::new(0, 10, 0) + }; - print_start_upgrade(&from_version, &to_version); + print_start_upgrade(&config_version, &to_version); - // 0.9.1 upgrade: - let upgraded_config = config.with_custom_version(to_version.to_string().as_ref()); + 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."); + print_failed_upgrade(&config_version, &to_version); + process::exit(1); + } + + println!( + "Setting validator REST endpoint to {}", + DEFAULT_VALIDATOR_REST_ENDPOINT + ); + + println!( + "Setting mixnet contract address to {}", + DEFAULT_MIXNET_CONTRACT_ADDRESS + ); + + let upgraded_config = config + .with_custom_version(to_version.to_string().as_ref()) + .with_custom_validator(DEFAULT_VALIDATOR_REST_ENDPOINT) + .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); - print_failed_upgrade(&from_version, &to_version); + print_failed_upgrade(&config_version, &to_version); process::exit(1); }); - print_successful_upgrade(from_version, to_version); + print_successful_upgrade(config_version, to_version); upgraded_config } -pub fn command_args<'a, 'b>() -> App<'a, 'b> { - App::new("upgrade").about("Try to upgrade the mixnode") - .arg( - Arg::with_name("id") - .long("id") - .help("Id of the nym-mixnode we want to upgrade") - .takes_value(true) - .required(true), - ) - // the rest of arguments depend on the upgrade path - .arg(Arg::with_name("current version") - .long("current-version") - .help("REQUIRED FOR PRE-0.9.0 UPGRADES. Specifies current version of the configuration file to help to determine a valid upgrade path. Valid formats include '0.8.1', 'v0.8.1' or 'V0.8.1'") - .takes_value(true) - ) -} - -fn unsupported_upgrade(current_version: Version, config_version: Version) -> ! { - eprintln!("Cannot perform upgrade from {} to {}. Please let the developers know about this issue if you expected it to work!", config_version, current_version); - process::exit(1) -} - -fn do_upgrade(mut config: Config, matches: &ArgMatches) { - let current = Version::parse(env!("CARGO_PKG_VERSION")).unwrap(); - +fn do_upgrade(mut config: Config, matches: &ArgMatches, package_version: Version) { loop { - let config_version = Version::parse(config.get_version()).unwrap_or_else(|err| { - eprintln!("failed to parse node version! - {:?}", err); - process::exit(1) - }); + let config_version = parse_config_version(&config); - if config_version == current { + if config_version == package_version { println!("You're using the most recent version!"); return; } config = match config_version.major { 0 => match config_version.minor { - 9 => patch_09x_upgrade(config, &matches), - _ => unsupported_upgrade(current, config_version), + 9 => minor_010_upgrade(config, &matches, &config_version, &package_version), + _ => unsupported_upgrade(config_version, package_version), }, - _ => unsupported_upgrade(current, config_version), + _ => unsupported_upgrade(config_version, package_version), } } } pub fn execute(matches: &ArgMatches) { - let current = Version::parse(env!("CARGO_PKG_VERSION")).unwrap(); - - // technically this is not a correct way of checking it as a released version might contain valid build identifiers - // however, we are not using them ourselves at the moment and hence it should be fine. - // if we change our mind, we could easily tweak this code - if current.is_prerelease() || !current.build.is_empty() { - eprintln!( - "Trying to upgrade to a non-released version {}. This is not supported!", - current - ); - process::exit(1) - } + let package_version = parse_package_version(); let id = matches.value_of("id").unwrap(); @@ -233,26 +277,9 @@ pub fn execute(matches: &ArgMatches) { }); // upgrades up to 0.9.0 - existing_config = pre_090_upgrade(self_reported_version, existing_config, &matches); - } - - let config_version = Version::parse(existing_config.get_version()).unwrap_or_else(|err| { - eprintln!("failed to parse node version! - {:?}", err); - process::exit(1) - }); - - if config_version.is_prerelease() || !config_version.build.is_empty() { - eprintln!( - "Trying to upgrade from non-released version {}. This is not supported!", - config_version - ); - process::exit(1) + existing_config = pre_090_upgrade(self_reported_version, existing_config); } // here be upgrade path to 0.9.X and beyond based on version number from config - if config_version == current { - println!("You're using the most recent version!"); - } else { - do_upgrade(existing_config, matches) - } + do_upgrade(existing_config, matches, package_version) } From ee71ae89f717b3a703144982f25b3fd848477107 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C4=99drzej=20Stuczy=C5=84ski?= Date: Wed, 31 Mar 2021 09:58:57 +0100 Subject: [PATCH 3/6] Feature/validator client rc3 (#552) * Bumped the version number * Additional currency helper --- clients/validator/package.json | 2 +- clients/validator/src/currency.ts | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/clients/validator/package.json b/clients/validator/package.json index f662b549fb..644ac097dc 100644 --- a/clients/validator/package.json +++ b/clients/validator/package.json @@ -1,6 +1,6 @@ { "name": "@nymproject/nym-validator-client", - "version": "0.10.0-rc2", + "version": "0.10.0-rc3", "description": "A TypeScript client for interacting with smart contracts in Nym validators", "repository": "https://github.com/nymtech/nym", "main": "./dist/index.js", diff --git a/clients/validator/src/currency.ts b/clients/validator/src/currency.ts index 608a5526f0..221db062c4 100644 --- a/clients/validator/src/currency.ts +++ b/clients/validator/src/currency.ts @@ -21,6 +21,15 @@ export function printableBalance(balance?: readonly Coin[]): string { return balance.map(printableCoin).join(", "); } +// converts display amount, such as "12.0346" to its native token representation, +// with 6 fractional digits. So in that case it would result in "12034600" +// Basically does the same job as `displayAmountToNative` but without the requirement +// of having the coinMap +export function printableBalanceToNative(amountToDisplay: string): string { + const decimalAmount = Decimal.fromUserInput(amountToDisplay, 6); + return decimalAmount.atomics; +} + export interface MappedCoin { readonly denom: string; readonly fractionalDigits: number; From a21ed92ac3f30223644a6f02e15d621a5e494307 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C4=99drzej=20Stuczy=C5=84ski?= Date: Wed, 31 Mar 2021 10:15:42 +0100 Subject: [PATCH 4/6] Feature/validator client address getter (#554) * Added address getter for the client * Might as well bump the version number --- clients/validator/package.json | 2 +- clients/validator/src/index.ts | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/clients/validator/package.json b/clients/validator/package.json index 644ac097dc..8ef5351bfc 100644 --- a/clients/validator/package.json +++ b/clients/validator/package.json @@ -1,6 +1,6 @@ { "name": "@nymproject/nym-validator-client", - "version": "0.10.0-rc3", + "version": "0.10.0-rc4", "description": "A TypeScript client for interacting with smart contracts in Nym validators", "repository": "https://github.com/nymtech/nym", "main": "./dist/index.js", diff --git a/clients/validator/src/index.ts b/clients/validator/src/index.ts index 049e491a58..40874ea94d 100644 --- a/clients/validator/src/index.ts +++ b/clients/validator/src/index.ts @@ -43,6 +43,14 @@ export default class ValidatorClient { return new ValidatorClient(url, queryClient, contractAddress, stakeDenom) } + public get address(): string { + if (this.client instanceof NetClient) { + return this.client.clientAddress + } else { + return "" + } + } + /** * TODO: re-enable this once we move back to client-side wallets running on people's machines * instead of the web wallet. From 48dda32c3e41adea339b37ec3ad0f559ea9be5af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C4=99drzej=20Stuczy=C5=84ski?= Date: Wed, 31 Mar 2021 16:01:06 +0100 Subject: [PATCH 5/6] Feature/bonding info on init (#555) * Mixnode bonding info on init * Gateway bonding info on init --- gateway/src/commands/init.rs | 51 ++++++++++++++++++++++++++++++++++++ mixnode/src/commands/init.rs | 44 +++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) diff --git a/gateway/src/commands/init.rs b/gateway/src/commands/init.rs index 6997a7d8b6..eb3de0b6df 100644 --- a/gateway/src/commands/init.rs +++ b/gateway/src/commands/init.rs @@ -94,6 +94,56 @@ pub fn command_args<'a, 'b>() -> clap::App<'a, 'b> { ) } +fn show_bonding_info(config: &Config) { + fn load_sphinx_keys(pathfinder: &GatewayPathfinder) -> encryption::KeyPair { + let sphinx_keypair: encryption::KeyPair = + pemstore::load_keypair(&pemstore::KeyPairPath::new( + pathfinder.private_encryption_key().to_owned(), + pathfinder.public_encryption_key().to_owned(), + )) + .expect("Failed to read stored sphinx key files"); + println!( + "Public sphinx key: {}\n", + sphinx_keypair.public_key().to_base58_string() + ); + sphinx_keypair + } + + fn load_identity_keys(pathfinder: &GatewayPathfinder) -> identity::KeyPair { + let identity_keypair: identity::KeyPair = + pemstore::load_keypair(&pemstore::KeyPairPath::new( + pathfinder.private_identity_key().to_owned(), + pathfinder.public_identity_key().to_owned(), + )) + .expect("Failed to read stored identity key files"); + println!( + "Public identity key: {}\n", + identity_keypair.public_key().to_base58_string() + ); + identity_keypair + } + + let pathfinder = GatewayPathfinder::new_from_config(&config); + let identity_keypair = load_identity_keys(&pathfinder); + let sphinx_keypair = load_sphinx_keys(&pathfinder); + + println!( + "\nTo bond your gateway you will [most likely] need to provide the following: + Identity key: {} + Sphinx key: {} + Mix Host: {} + Clients Host: {} + Location: [physical location of your node's server] + Version: {} + ", + identity_keypair.public_key().to_base58_string(), + sphinx_keypair.public_key().to_base58_string(), + config.get_mix_announce_address(), + config.get_clients_announce_address(), + config.get_version(), + ); +} + pub fn execute(matches: &ArgMatches) { let id = matches.value_of("id").unwrap(); println!("Initialising gateway {}...", id); @@ -144,4 +194,5 @@ pub fn execute(matches: &ArgMatches) { println!("Saved configuration file to {:?}", config_save_location); println!("Gateway configuration completed.\n\n\n"); + show_bonding_info(&config); } diff --git a/mixnode/src/commands/init.rs b/mixnode/src/commands/init.rs index aee518d3b5..fc39f0ca49 100644 --- a/mixnode/src/commands/init.rs +++ b/mixnode/src/commands/init.rs @@ -128,6 +128,48 @@ async fn choose_layer( *layer_with_fewest } +fn show_bonding_info(config: &Config) { + fn load_identity_keys(pathfinder: &MixNodePathfinder) -> identity::KeyPair { + let identity_keypair: identity::KeyPair = + pemstore::load_keypair(&pemstore::KeyPairPath::new( + pathfinder.private_identity_key().to_owned(), + pathfinder.public_identity_key().to_owned(), + )) + .expect("Failed to read stored identity key files"); + identity_keypair + } + + fn load_sphinx_keys(pathfinder: &MixNodePathfinder) -> encryption::KeyPair { + let sphinx_keypair: encryption::KeyPair = + pemstore::load_keypair(&pemstore::KeyPairPath::new( + pathfinder.private_encryption_key().to_owned(), + pathfinder.public_encryption_key().to_owned(), + )) + .expect("Failed to read stored sphinx key files"); + sphinx_keypair + } + + let pathfinder = MixNodePathfinder::new_from_config(&config); + let identity_keypair = load_identity_keys(&pathfinder); + let sphinx_keypair = load_sphinx_keys(&pathfinder); + + println!( + "\nTo bond your mixnode you will need to provide the following: + Identity key: {} + Sphinx key: {} + Host: {} + Layer: {} + Location: [physical location of your node's server] + Version: {} + ", + identity_keypair.public_key().to_base58_string(), + sphinx_keypair.public_key().to_base58_string(), + config.get_announce_address(), + config.get_layer(), + config.get_version(), + ); +} + pub fn execute(matches: &ArgMatches) { // TODO: this should probably be made implicit by slapping `#[tokio::main]` on our main method // and then removing runtime from mixnode itself in `run` @@ -185,5 +227,7 @@ pub fn execute(matches: &ArgMatches) { .expect("Failed to save the config file"); println!("Saved configuration file to {:?}", config_save_location); println!("Mixnode configuration completed.\n\n\n"); + + show_bonding_info(&config) }) } From 42268e044cbd00f32d271acfb175d8f09ac262b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C4=99drzej=20Stuczy=C5=84ski?= Date: Thu, 1 Apr 2021 11:38:08 +0100 Subject: [PATCH 6/6] Added extra step for publishing validator client (#553) --- clients/validator/README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/clients/validator/README.md b/clients/validator/README.md index bff7270699..d54ebe7c77 100644 --- a/clients/validator/README.md +++ b/clients/validator/README.md @@ -36,4 +36,5 @@ If you're a Nym platform developer who's made changes to the client and wants to 1. Bump the version number (use SemVer) 1. `npm run build` -1. `npm login` (if you haven't already) \ No newline at end of file +1. `npm login` (if you haven't already) +1. `npm publish` \ No newline at end of file