Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor wallet CLI args #1262

Merged
merged 2 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion test/functional/test_framework/wallet_cli_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def __init__(self, node, config, log, wallet_args: List[str] = [], chain_config_
async def __aenter__(self):
wallet_cli = os.path.join(self.config["environment"]["BUILDDIR"], "test_wallet"+self.config["environment"]["EXEEXT"] )
cookie_file = os.path.join(self.node.datadir, ".cookie")
wallet_args = ["--rpc-address", self.node.url.split("@")[1], "--rpc-cookie-file", cookie_file] + self.wallet_args + ["regtest"] + self.chain_config_args
wallet_args = ["regtest", "--rpc-address", self.node.url.split("@")[1], "--rpc-cookie-file", cookie_file] + self.wallet_args + self.chain_config_args
self.wallet_log_file = NamedTemporaryFile(prefix="wallet_stderr_", dir=os.path.dirname(self.node.datadir), delete=False)
self.wallet_commands_file = NamedTemporaryFile(prefix="wallet_commands_responses_", dir=os.path.dirname(self.node.datadir), delete=False)

Expand Down
42 changes: 33 additions & 9 deletions wallet/wallet-cli-lib/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,48 @@

use std::path::PathBuf;

use clap::{Parser, Subcommand};
use clap::{Args, Parser, Subcommand};
use common::chain::config::{regtest_options::ChainConfigOptions, ChainType};

#[derive(Subcommand, Clone, Debug)]
pub enum Network {
Mainnet,
Testnet,
Regtest(Box<ChainConfigOptions>),
Signet,
Mainnet(CliArgs),
Testnet(CliArgs),
Regtest(Box<RegtestOptions>),
Signet(CliArgs),
}

#[derive(Args, Clone, Debug)]
pub struct RegtestOptions {
#[clap(flatten)]
pub run_options: CliArgs,
#[clap(flatten)]
pub chain_config: ChainConfigOptions,
}

#[derive(Parser, Debug)]
#[clap(version)]
#[command(args_conflicts_with_subcommands = true)]
pub struct WalletCliArgs {
/// Network
#[clap(subcommand)]
pub network: Network,
pub network: Option<Network>,

#[clap(flatten)]
pub run_options: CliArgs,
}

impl WalletCliArgs {
pub fn cli_args(self) -> CliArgs {
self.network.map_or(self.run_options, |network| match network {
Network::Mainnet(args) | Network::Signet(args) | Network::Testnet(args) => args,
Network::Regtest(args) => args.run_options,
})
}
}

#[derive(Args, Clone, Debug)]
pub struct CliArgs {
/// Optional path to the wallet file
#[clap(long)]
pub wallet_file: Option<PathBuf>,
Expand Down Expand Up @@ -88,10 +112,10 @@ pub struct WalletCliArgs {
impl From<&Network> for ChainType {
fn from(value: &Network) -> Self {
match value {
Network::Mainnet => ChainType::Mainnet,
Network::Testnet => ChainType::Testnet,
Network::Mainnet(_) => ChainType::Mainnet,
Network::Testnet(_) => ChainType::Testnet,
Network::Regtest(_) => ChainType::Regtest,
Network::Signet => ChainType::Signet,
Network::Signet(_) => ChainType::Signet,
}
}
}
43 changes: 21 additions & 22 deletions wallet/wallet-cli-lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use common::chain::{
config::{regtest_options::regtest_chain_config, ChainType},
ChainConfig,
};
use config::{Network, WalletCliArgs};
use config::{CliArgs, Network};
use console::{ConsoleInput, ConsoleOutput};
use errors::WalletCliError;
use rpc::RpcAuthData;
Expand All @@ -53,8 +53,25 @@ pub async fn run(
args: config::WalletCliArgs,
chain_config: Option<Arc<ChainConfig>>,
) -> Result<(), WalletCliError> {
let WalletCliArgs {
network,
let chain_type = args.network.as_ref().map_or(ChainType::Testnet, |network| network.into());
let chain_config = match chain_config {
Some(chain_config) => chain_config,
None => match &args.network {
Some(Network::Regtest(regtest_options)) => {
eprintln!("\n\nRegtest config {}", chain_type.name());
Arc::new(
regtest_chain_config(&regtest_options.chain_config)
.map_err(|err| WalletCliError::InvalidConfig(err.to_string()))?,
)
}
_ => {
eprintln!("\n\nNot regtest {}", chain_type.name());
Arc::new(common::chain::config::Builder::new(chain_type).build())
}
},
};

let CliArgs {
wallet_file,
wallet_password,
start_staking,
Expand All @@ -67,7 +84,7 @@ pub async fn run(
exit_on_error,
vi_mode,
in_top_x_mb,
} = args;
} = args.cli_args();

let mode = if let Some(file_path) = commands_file {
repl::non_interactive::log::init();
Expand All @@ -81,24 +98,6 @@ pub async fn run(
Mode::NonInteractive
};

let chain_type: ChainType = (&network).into();
let chain_config = match chain_config {
Some(chain_config) => chain_config,
None => match network {
Network::Regtest(regtest_options) => {
eprintln!("\n\nRegtest config {}", chain_type.name());
Arc::new(
regtest_chain_config(&regtest_options)
.map_err(|err| WalletCliError::InvalidConfig(err.to_string()))?,
)
}
_ => {
eprintln!("\n\nNot regtest {}", chain_type.name());
Arc::new(common::chain::config::Builder::new(chain_type).build())
}
},
};

// TODO: Use the constant with the node
let default_http_rpc_addr = || "127.0.0.1:3030".to_owned();
let rpc_address = rpc_address.unwrap_or_else(default_http_rpc_addr);
Expand Down
76 changes: 47 additions & 29 deletions wallet/wallet-cli-lib/tests/cli_test_framework.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ use rpc::{rpc_creds::RpcCreds, RpcConfig};
use subsystem::{ManagerJoinHandle, ShutdownTrigger};
use test_utils::test_dir::TestRoot;
use wallet_cli_lib::{
config::{Network, WalletCliArgs},
config::{Network, RegtestOptions, WalletCliArgs},
console::{ConsoleInput, ConsoleOutput},
errors::WalletCliError,
};
Expand Down Expand Up @@ -293,34 +293,52 @@ impl CliTestFramework {
let manager_task = manager.main_in_task();

let wallet_options = WalletCliArgs {
network: Network::Regtest(Box::new(config::regtest_options::ChainConfigOptions {
chain_magic_bytes: None,
chain_max_future_block_time_offset: None,
software_version: None,
chain_target_block_spacing: None,
chain_coin_decimals: None,
chain_emission_schedule: None,
chain_max_block_header_size: None,
chain_max_block_size_with_standard_txs: None,
chain_max_block_size_with_smart_contracts: None,
chain_initial_difficulty: None,
chain_pos_netupgrades: None,
chain_pos_netupgrades_v0_to_v1: None,
chain_genesis_block_timestamp: None,
chain_genesis_staking_settings: GenesisStakingSettings::default(),
})),
wallet_file: None,
wallet_password: None,
start_staking: false,
rpc_address: Some(rpc_address.to_string()),
rpc_cookie_file: None,
rpc_username: Some(RPC_USERNAME.to_owned()),
rpc_password: Some(RPC_PASSWORD.to_owned()),
commands_file: None,
history_file: None,
exit_on_error: None,
vi_mode: false,
in_top_x_mb: 5,
network: Some(Network::Regtest(Box::new(RegtestOptions {
chain_config: config::regtest_options::ChainConfigOptions {
chain_magic_bytes: None,
chain_max_future_block_time_offset: None,
software_version: None,
chain_target_block_spacing: None,
chain_coin_decimals: None,
chain_emission_schedule: None,
chain_max_block_header_size: None,
chain_max_block_size_with_standard_txs: None,
chain_max_block_size_with_smart_contracts: None,
chain_initial_difficulty: None,
chain_pos_netupgrades: None,
chain_pos_netupgrades_v0_to_v1: None,
chain_genesis_block_timestamp: None,
chain_genesis_staking_settings: GenesisStakingSettings::default(),
},
run_options: wallet_cli_lib::config::CliArgs {
wallet_file: None,
wallet_password: None,
start_staking: false,
rpc_address: Some(rpc_address.to_string()),
rpc_cookie_file: None,
rpc_username: Some(RPC_USERNAME.to_owned()),
rpc_password: Some(RPC_PASSWORD.to_owned()),
commands_file: None,
history_file: None,
exit_on_error: None,
vi_mode: false,
in_top_x_mb: 5,
},
}))),
run_options: wallet_cli_lib::config::CliArgs {
wallet_file: None,
wallet_password: None,
start_staking: false,
rpc_address: Some(rpc_address.to_string()),
rpc_cookie_file: None,
rpc_username: Some(RPC_USERNAME.to_owned()),
rpc_password: Some(RPC_PASSWORD.to_owned()),
commands_file: None,
history_file: None,
exit_on_error: None,
vi_mode: false,
in_top_x_mb: 5,
},
};

let (output_tx, output_rx) = std::sync::mpsc::channel();
Expand Down
Loading