From 696f6c399cb53a2b588d3c0edface6d52421ea3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C4=99drzej=20Stuczy=C5=84ski?= Date: Thu, 30 Mar 2023 16:07:22 +0100 Subject: [PATCH] feature cleanup + added the capability to few nym-cli commands --- Cargo.lock | 1 + clients/native/Cargo.toml | 2 +- clients/socks5/Cargo.toml | 2 +- common/bin-common/Cargo.toml | 1 + common/bin-common/src/output_format/mod.rs | 5 ++-- common/commands/Cargo.toml | 1 + common/commands/src/utils.rs | 23 ++++++++++++++++++- .../create_family_join_permit_sign_payload.rs | 9 ++++++-- .../mixnode/mixnode_bonding_sign_payload.rs | 9 ++++++-- gateway/Cargo.toml | 2 +- mixnode/Cargo.toml | 2 +- mixnode/src/commands/mod.rs | 2 +- mixnode/src/commands/run.rs | 2 +- .../network-requester/Cargo.toml | 2 +- 14 files changed, 48 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0b95bb44ab..8502cbea2d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3216,6 +3216,7 @@ dependencies = [ "humantime-serde", "k256", "log", + "nym-bin-common", "nym-coconut-bandwidth-contract-common", "nym-coconut-dkg-common", "nym-contracts-common", diff --git a/clients/native/Cargo.toml b/clients/native/Cargo.toml index 1adefd9679..36061ee954 100644 --- a/clients/native/Cargo.toml +++ b/clients/native/Cargo.toml @@ -34,7 +34,7 @@ tokio = { version = "1.24.1", features = ["rt-multi-thread", "net", "signal"] } tokio-tungstenite = "0.14" # websocket ## internal -nym-bin-common = { path = "../../common/bin-common", features = ["serde_json"] } +nym-bin-common = { path = "../../common/bin-common", features = ["output_format"] } client-core = { path = "../../common/client-core", features = ["fs-surb-storage"] } nym-coconut-interface = { path = "../../common/coconut-interface" } nym-config = { path = "../../common/config" } diff --git a/clients/socks5/Cargo.toml b/clients/socks5/Cargo.toml index b350b5cae9..66d8a99aeb 100644 --- a/clients/socks5/Cargo.toml +++ b/clients/socks5/Cargo.toml @@ -19,7 +19,7 @@ tokio = { version = "1.24.1", features = ["rt-multi-thread", "net", "signal"] } url = "2.2" # internal -nym-bin-common = { path = "../../common/bin-common", features = ["serde_json"] } +nym-bin-common = { path = "../../common/bin-common", features = ["output_format"] } client-core = { path = "../../common/client-core", features = ["fs-surb-storage"] } nym-coconut-interface = { path = "../../common/coconut-interface" } nym-config = { path = "../../common/config" } diff --git a/common/bin-common/Cargo.toml b/common/bin-common/Cargo.toml index 6c0750e3c6..40867d90d6 100644 --- a/common/bin-common/Cargo.toml +++ b/common/bin-common/Cargo.toml @@ -22,3 +22,4 @@ vergen = { version = "=7.4.3", default-features = false, features = ["build", "g [features] default = [] +output_format = ["serde", "serde_json"] diff --git a/common/bin-common/src/output_format/mod.rs b/common/bin-common/src/output_format/mod.rs index dfde754237..386cc267a1 100644 --- a/common/bin-common/src/output_format/mod.rs +++ b/common/bin-common/src/output_format/mod.rs @@ -2,7 +2,6 @@ // SPDX-License-Identifier: Apache-2.0 use clap::ValueEnum; -use serde::Serialize; use std::fmt::{Display, Formatter}; #[derive(Default, Copy, Debug, Clone, ValueEnum)] @@ -26,8 +25,8 @@ impl OutputFormat { matches!(self, OutputFormat::Text) } - #[cfg(feature = "serde_json")] - pub fn format(&self, data: &T) -> String { + #[cfg(feature = "output_format")] + pub fn format(&self, data: &T) -> String { match self { OutputFormat::Text => data.to_string(), OutputFormat::Json => serde_json::to_string(data).unwrap(), diff --git a/common/commands/Cargo.toml b/common/commands/Cargo.toml index d4cdfbe91d..9bddc24e50 100644 --- a/common/commands/Cargo.toml +++ b/common/commands/Cargo.toml @@ -29,6 +29,7 @@ cosmrs = { git = "https://github.com/neacsu/cosmos-rust", branch = "neacsu/feegr cosmwasm-std = { workspace = true } validator-client = { path = "../client-libs/validator-client", features = ["nyxd-client"] } +nym-bin-common = { path = "../../common/bin-common", features = ["output_format"] } nym-crypto = { path = "../../common/crypto", features = ["asymmetric"] } nym-network-defaults = { path = "../network-defaults" } nym-contracts-common = { path = "../cosmwasm-smart-contracts/contracts-common" } diff --git a/common/commands/src/utils.rs b/common/commands/src/utils.rs index 05c395924d..a3db404198 100644 --- a/common/commands/src/utils.rs +++ b/common/commands/src/utils.rs @@ -4,8 +4,9 @@ use cosmrs::AccountId; use cosmwasm_std::{Addr, Coin as CosmWasmCoin, Decimal}; use log::error; +use serde::Serialize; use std::error::Error; -use std::fmt::Display; +use std::fmt::{Display, Formatter}; use validator_client::nyxd::Coin; // TODO: perhaps it should be moved to some global common crate? @@ -54,3 +55,23 @@ where error!("{}", e); e } + +#[derive(Serialize)] +pub(crate) struct DataWrapper { + data: T, +} + +impl Display for DataWrapper +where + T: Display, +{ + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.data) + } +} + +impl DataWrapper { + pub(crate) fn new(data: T) -> Self { + DataWrapper { data } + } +} diff --git a/common/commands/src/validator/mixnet/operators/mixnode/families/create_family_join_permit_sign_payload.rs b/common/commands/src/validator/mixnet/operators/mixnode/families/create_family_join_permit_sign_payload.rs index 2654a337e4..2e80ded330 100644 --- a/common/commands/src/validator/mixnet/operators/mixnode/families/create_family_join_permit_sign_payload.rs +++ b/common/commands/src/validator/mixnet/operators/mixnode/families/create_family_join_permit_sign_payload.rs @@ -2,10 +2,11 @@ // SPDX-License-Identifier: Apache-2.0 use crate::context::QueryClient; -use crate::utils::account_id_to_cw_addr; +use crate::utils::{account_id_to_cw_addr, DataWrapper}; use clap::Parser; use cosmrs::AccountId; use log::info; +use nym_bin_common::output_format::OutputFormat; use nym_crypto::asymmetric::identity; use nym_mixnet_contract_common::construct_family_join_permit; use nym_mixnet_contract_common::families::FamilyHead; @@ -25,6 +26,9 @@ pub struct Args { /// Identity of the member for whom we're issuing the permit #[arg(long)] pub member: identity::PublicKey, + + #[clap(short, long, default_value_t = OutputFormat::default())] + output: OutputFormat, } pub async fn create_family_join_permit_sign_payload(args: Args, client: QueryClient) { @@ -68,5 +72,6 @@ pub async fn create_family_join_permit_sign_payload(args: Args, client: QueryCli let head = FamilyHead::new(mixnode.bond_information.identity()); let payload = construct_family_join_permit(nonce, head, proxy, args.member.to_base58_string()); - println!("{}", payload.to_base58_string().unwrap()) + let wrapper = DataWrapper::new(payload.to_base58_string().unwrap()); + println!("{}", args.output.format(&wrapper)) } diff --git a/common/commands/src/validator/mixnet/operators/mixnode/mixnode_bonding_sign_payload.rs b/common/commands/src/validator/mixnet/operators/mixnode/mixnode_bonding_sign_payload.rs index a4b41b03f2..c674b942f6 100644 --- a/common/commands/src/validator/mixnet/operators/mixnode/mixnode_bonding_sign_payload.rs +++ b/common/commands/src/validator/mixnet/operators/mixnode/mixnode_bonding_sign_payload.rs @@ -2,9 +2,10 @@ // SPDX-License-Identifier: Apache-2.0 use crate::context::SigningClient; -use crate::utils::account_id_to_cw_addr; +use crate::utils::{account_id_to_cw_addr, DataWrapper}; use clap::Parser; use cosmwasm_std::{Coin, Uint128}; +use nym_bin_common::output_format::OutputFormat; use nym_contracts_common::Percent; use nym_mixnet_contract_common::{construct_mixnode_bonding_sign_payload, MixNodeCostParams}; use nym_network_defaults::{ @@ -54,6 +55,9 @@ pub struct Args { /// Indicates whether the mixnode is going to get bonded via a vesting account #[arg(long)] pub with_vesting_account: bool, + + #[clap(short, long, default_value_t = OutputFormat::default())] + output: OutputFormat, } pub async fn create_payload(args: Args, client: SigningClient) { @@ -104,5 +108,6 @@ pub async fn create_payload(args: Args, client: SigningClient) { let payload = construct_mixnode_bonding_sign_payload(nonce, address, proxy, coin, mixnode, cost_params); - println!("{}", payload.to_base58_string().unwrap()) + let wrapper = DataWrapper::new(payload.to_base58_string().unwrap()); + println!("{}", args.output.format(&wrapper)) } diff --git a/gateway/Cargo.toml b/gateway/Cargo.toml index 317a5e1f81..6e803e461f 100644 --- a/gateway/Cargo.toml +++ b/gateway/Cargo.toml @@ -56,7 +56,7 @@ nym-coconut-interface = { path = "../common/coconut-interface" } nym-credentials = { path = "../common/credentials" } nym-config = { path = "../common/config" } nym-crypto = { path = "../common/crypto" } -nym-bin-common = { path = "../common/bin-common", features = ["serde_json"] } +nym-bin-common = { path = "../common/bin-common", features = ["output_format"] } gateway-requests = { path = "gateway-requests" } mixnet-client = { path = "../common/client-libs/mixnet-client" } mixnode-common = { path = "../common/mixnode-common" } diff --git a/mixnode/Cargo.toml b/mixnode/Cargo.toml index fce6af86e9..5bfce1a43b 100644 --- a/mixnode/Cargo.toml +++ b/mixnode/Cargo.toml @@ -51,7 +51,7 @@ nym-task = { path = "../common/task" } nym-types = { path = "../common/types" } nym-topology = { path = "../common/topology" } validator-client = { path = "../common/client-libs/validator-client" } -nym-bin-common = { path = "../common/bin-common", features = ["serde_json"] } +nym-bin-common = { path = "../common/bin-common", features = ["output_format"] } cpu-cycles = { path = "../cpu-cycles", optional = true } [dev-dependencies] diff --git a/mixnode/src/commands/mod.rs b/mixnode/src/commands/mod.rs index 26c663bab0..6172776c72 100644 --- a/mixnode/src/commands/mod.rs +++ b/mixnode/src/commands/mod.rs @@ -62,7 +62,7 @@ struct OverrideConfig { pub(crate) async fn execute(args: Cli) { let bin_name = "nym-mixnode"; - + match args.command { Commands::Describe(m) => describe::execute(m), Commands::Init(m) => init::execute(&m), diff --git a/mixnode/src/commands/run.rs b/mixnode/src/commands/run.rs index b0595d38d8..c8d40721d9 100644 --- a/mixnode/src/commands/run.rs +++ b/mixnode/src/commands/run.rs @@ -6,9 +6,9 @@ use crate::commands::{override_config, version_check}; use crate::config::Config; use crate::node::MixNode; use clap::Args; +use nym_bin_common::output_format::OutputFormat; use nym_config::NymConfig; use std::net::IpAddr; -use nym_bin_common::output_format::OutputFormat; use validator_client::nyxd; #[derive(Args, Clone)] diff --git a/service-providers/network-requester/Cargo.toml b/service-providers/network-requester/Cargo.toml index af2f8cecb9..9283495517 100644 --- a/service-providers/network-requester/Cargo.toml +++ b/service-providers/network-requester/Cargo.toml @@ -34,7 +34,7 @@ url = { workspace = true } client-core = { path = "../../common/client-core" } nym-config = { path = "../../common/config" } nym-crypto = { path = "../../common/crypto" } -nym-bin-common = { path = "../../common/bin-common", features = ["serde_json"] } +nym-bin-common = { path = "../../common/bin-common", features = ["output_format"] } nym-network-defaults = { path = "../../common/network-defaults" } nym-sdk = { path = "../../sdk/rust/nym-sdk" } nym-sphinx = { path = "../../common/nymsphinx" }