From c13ab9f14ec136f94e89778e456150f956c8c864 Mon Sep 17 00:00:00 2001 From: Trent Nelson Date: Sun, 26 Jan 2020 01:27:24 -0700 Subject: [PATCH] CLI: Consolidate offline arg declarations (#7979) automerge --- cli/src/cli.rs | 33 +++---------- cli/src/lib.rs | 1 + cli/src/nonce.rs | 3 +- cli/src/offline.rs | 61 ++++++++++++++++++++++++ cli/src/stake.rs | 115 ++++++--------------------------------------- 5 files changed, 84 insertions(+), 129 deletions(-) create mode 100644 cli/src/offline.rs diff --git a/cli/src/cli.rs b/cli/src/cli.rs index 9cf7d8bb6..b7598d1b9 100644 --- a/cli/src/cli.rs +++ b/cli/src/cli.rs @@ -2,6 +2,7 @@ use crate::{ cluster_query::*, display::{println_name_value, println_signers}, nonce::{self, *}, + offline::*, stake::*, storage::*, validator_info::*, @@ -599,9 +600,9 @@ pub fn parse_command(matches: &ArgMatches<'_>) -> Result(name: &str, about: &'ab str, version: &'v str) -> App<'ab, ' .long("cancelable") .takes_value(false), ) - .arg( - Arg::with_name("sign_only") - .long("sign-only") - .takes_value(false) - .help("Sign the transaction offline"), - ) + .offline_args() .arg(nonce_arg()) - .arg(nonce_authority_arg()) - .arg( - Arg::with_name("signer") - .long("signer") - .value_name("PUBKEY=BASE58_SIG") - .takes_value(true) - .validator(is_pubkey_sig) - .multiple(true) - .help("Provide a public-key/signature pair for the transaction"), - ) - .arg( - Arg::with_name("blockhash") - .long("blockhash") - .value_name("BLOCKHASH") - .takes_value(true) - .validator(is_hash) - .help("Use the supplied blockhash"), - ), + .arg(nonce_authority_arg()), ) .subcommand( SubCommand::with_name("send-signature") diff --git a/cli/src/lib.rs b/cli/src/lib.rs index 18d68fada..113409647 100644 --- a/cli/src/lib.rs +++ b/cli/src/lib.rs @@ -6,6 +6,7 @@ pub mod cluster_query; pub mod config; pub mod display; pub mod nonce; +pub mod offline; pub mod stake; pub mod storage; pub mod validator_info; diff --git a/cli/src/nonce.rs b/cli/src/nonce.rs index 42b4cdba2..b8df353fa 100644 --- a/cli/src/nonce.rs +++ b/cli/src/nonce.rs @@ -3,6 +3,7 @@ use crate::cli::{ log_instruction_custom_error, required_lamports_from, CliCommand, CliCommandInfo, CliConfig, CliError, ProcessResult, SigningAuthority, }; +use crate::offline::BLOCKHASH_ARG; use clap::{App, Arg, ArgMatches, SubCommand}; use solana_clap_utils::{input_parsers::*, input_validators::*, ArgConstant}; use solana_client::rpc_client::RpcClient; @@ -55,7 +56,7 @@ pub fn nonce_arg<'a, 'b>() -> Arg<'a, 'b> { .long(NONCE_ARG.long) .takes_value(true) .value_name("PUBKEY") - .requires("blockhash") + .requires(BLOCKHASH_ARG.name) .validator(is_pubkey) .help(NONCE_ARG.help) } diff --git a/cli/src/offline.rs b/cli/src/offline.rs new file mode 100644 index 000000000..937f646c3 --- /dev/null +++ b/cli/src/offline.rs @@ -0,0 +1,61 @@ +use clap::{App, Arg}; +use solana_clap_utils::{ + input_validators::{is_hash, is_pubkey_sig}, + ArgConstant, +}; + +pub const BLOCKHASH_ARG: ArgConstant<'static> = ArgConstant { + name: "blockhash", + long: "blockhash", + help: "Use the supplied blockhash", +}; + +pub const SIGN_ONLY_ARG: ArgConstant<'static> = ArgConstant { + name: "sign_only", + long: "sign-only", + help: "Sign the transaction offline", +}; + +pub const SIGNER_ARG: ArgConstant<'static> = ArgConstant { + name: "signer", + long: "signer", + help: "Provid a public-key/signature pair for the transaction", +}; + +fn blockhash_arg<'a, 'b>() -> Arg<'a, 'b> { + Arg::with_name(BLOCKHASH_ARG.name) + .long(BLOCKHASH_ARG.long) + .takes_value(true) + .value_name("BLOCKHASH") + .validator(is_hash) + .help(BLOCKHASH_ARG.help) +} + +fn sign_only_arg<'a, 'b>() -> Arg<'a, 'b> { + Arg::with_name(SIGN_ONLY_ARG.name) + .long(SIGN_ONLY_ARG.long) + .takes_value(false) + .help(SIGN_ONLY_ARG.help) +} + +fn signer_arg<'a, 'b>() -> Arg<'a, 'b> { + Arg::with_name(SIGNER_ARG.name) + .long(SIGNER_ARG.long) + .takes_value(true) + .value_name("BASE58_PUBKEY=BASE58_SIG") + .validator(is_pubkey_sig) + .multiple(true) + .help(SIGNER_ARG.help) +} + +pub trait OfflineArgs { + fn offline_args(self) -> Self; +} + +impl OfflineArgs for App<'_, '_> { + fn offline_args(self) -> Self { + self.arg(blockhash_arg()) + .arg(sign_only_arg()) + .arg(signer_arg()) + } +} diff --git a/cli/src/stake.rs b/cli/src/stake.rs index ed7fe091c..7d8da8fb9 100644 --- a/cli/src/stake.rs +++ b/cli/src/stake.rs @@ -6,6 +6,7 @@ use crate::{ CliConfig, CliError, ProcessResult, SigningAuthority, }, nonce::{check_nonce_account, nonce_arg, NONCE_ARG, NONCE_AUTHORITY_ARG}, + offline::*, }; use clap::{App, Arg, ArgMatches, SubCommand}; use console::style; @@ -172,29 +173,7 @@ impl StakeSubCommands for App<'_, '_> { .help("The vote account to which the stake will be delegated") ) .arg(stake_authority_arg()) - .arg( - Arg::with_name("sign_only") - .long("sign-only") - .takes_value(false) - .help("Sign the transaction offline"), - ) - .arg( - Arg::with_name("signer") - .long("signer") - .value_name("PUBKEY=BASE58_SIG") - .takes_value(true) - .validator(is_pubkey_sig) - .multiple(true) - .help("Provide a public-key/signature pair for the transaction"), - ) - .arg( - Arg::with_name("blockhash") - .long("blockhash") - .value_name("BLOCKHASH") - .takes_value(true) - .validator(is_hash) - .help("Use the supplied blockhash"), - ) + .offline_args() .arg(nonce_arg()) .arg(nonce_authority_arg()) ) @@ -220,29 +199,7 @@ impl StakeSubCommands for App<'_, '_> { .help("New authorized staker") ) .arg(stake_authority_arg()) - .arg( - Arg::with_name("sign_only") - .long("sign-only") - .takes_value(false) - .help("Sign the transaction offline"), - ) - .arg( - Arg::with_name("signer") - .long("signer") - .value_name("PUBKEY=BASE58_SIG") - .takes_value(true) - .validator(is_pubkey_sig) - .multiple(true) - .help("Provide a public-key/signature pair for the transaction"), - ) - .arg( - Arg::with_name("blockhash") - .long("blockhash") - .value_name("BLOCKHASH") - .takes_value(true) - .validator(is_hash) - .help("Use the supplied blockhash"), - ) + .offline_args() .arg(nonce_arg()) .arg(nonce_authority_arg()) ) @@ -268,29 +225,7 @@ impl StakeSubCommands for App<'_, '_> { .help("New authorized withdrawer") ) .arg(withdraw_authority_arg()) - .arg( - Arg::with_name("sign_only") - .long("sign-only") - .takes_value(false) - .help("Sign the transaction offline"), - ) - .arg( - Arg::with_name("signer") - .long("signer") - .value_name("PUBKEY=BASE58_SIG") - .takes_value(true) - .validator(is_pubkey_sig) - .multiple(true) - .help("Provide a public-key/signature pair for the transaction"), - ) - .arg( - Arg::with_name("blockhash") - .long("blockhash") - .value_name("BLOCKHASH") - .takes_value(true) - .validator(is_hash) - .help("Use the supplied blockhash"), - ) + .offline_args() .arg(nonce_arg()) .arg(nonce_authority_arg()) ) @@ -306,29 +241,7 @@ impl StakeSubCommands for App<'_, '_> { .help("Stake account to be deactivated.") ) .arg(stake_authority_arg()) - .arg( - Arg::with_name("sign_only") - .long("sign-only") - .takes_value(false) - .help("Sign the transaction offline"), - ) - .arg( - Arg::with_name("signer") - .long("signer") - .value_name("PUBKEY=BASE58_SIG") - .takes_value(true) - .validator(is_pubkey_sig) - .multiple(true) - .help("Provide a public-key/signature pair for the transaction"), - ) - .arg( - Arg::with_name("blockhash") - .long("blockhash") - .value_name("BLOCKHASH") - .takes_value(true) - .validator(is_hash) - .help("Use the supplied blockhash"), - ) + .offline_args() .arg(nonce_arg()) .arg(nonce_authority_arg()) ) @@ -437,9 +350,9 @@ pub fn parse_stake_delegate_stake(matches: &ArgMatches<'_>) -> Result STAKE_AUTHORITY_ARG.name, StakeAuthorize::Withdrawer => WITHDRAW_AUTHORITY_ARG.name, }; - let sign_only = matches.is_present("sign_only"); - let signers = pubkeys_sigs_of(&matches, "signer"); + let sign_only = matches.is_present(SIGN_ONLY_ARG.name); + let signers = pubkeys_sigs_of(&matches, SIGNER_ARG.name); let authority = if matches.is_present(authority_flag) { Some(SigningAuthority::new_from_matches( &matches, @@ -498,7 +411,7 @@ pub fn parse_stake_authorize( } else { None }; - let blockhash = value_of(matches, "blockhash"); + let blockhash = value_of(matches, BLOCKHASH_ARG.name); let nonce_account = pubkey_of(&matches, NONCE_ARG.name); let nonce_authority = if matches.is_present(NONCE_AUTHORITY_ARG.name) { Some(SigningAuthority::new_from_matches( @@ -528,9 +441,9 @@ pub fn parse_stake_authorize( pub fn parse_stake_deactivate_stake(matches: &ArgMatches<'_>) -> Result { let stake_account_pubkey = pubkey_of(matches, "stake_account_pubkey").unwrap(); - let sign_only = matches.is_present("sign_only"); - let signers = pubkeys_sigs_of(&matches, "signer"); - let blockhash = value_of(matches, "blockhash"); + let sign_only = matches.is_present(SIGN_ONLY_ARG.name); + let signers = pubkeys_sigs_of(&matches, SIGNER_ARG.name); + let blockhash = value_of(matches, BLOCKHASH_ARG.name); let require_keypair = signers.is_none(); let nonce_account = pubkey_of(&matches, NONCE_ARG.name); let stake_authority = if matches.is_present(STAKE_AUTHORITY_ARG.name) {