Skip to content

Commit

Permalink
Cli: promote commitment to a global arg + config.yml (solana-labs#14684)
Browse files Browse the repository at this point in the history
* Make commitment a global arg

* Add commitment to solana/cli/config.yml

* Fixup a couple Display/Verbose bugs
  • Loading branch information
CriesofCarrots authored Jan 20, 2021
1 parent ed90ef7 commit a7086a0
Show file tree
Hide file tree
Showing 11 changed files with 153 additions and 102 deletions.
22 changes: 0 additions & 22 deletions clap-utils/src/commitment.rs

This file was deleted.

11 changes: 3 additions & 8 deletions clap-utils/src/input_parsers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,9 @@ pub fn cluster_type_of(matches: &ArgMatches<'_>, name: &str) -> Option<ClusterTy
}

pub fn commitment_of(matches: &ArgMatches<'_>, name: &str) -> Option<CommitmentConfig> {
matches.value_of(name).map(|value| match value {
"max" => CommitmentConfig::max(),
"recent" => CommitmentConfig::recent(),
"root" => CommitmentConfig::root(),
"single" => CommitmentConfig::single(),
"singleGossip" => CommitmentConfig::single_gossip(),
_ => CommitmentConfig::default(),
})
matches
.value_of(name)
.map(|value| CommitmentConfig::from_str(value).unwrap_or_default())
}

#[cfg(test)]
Expand Down
1 change: 0 additions & 1 deletion clap-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ impl std::fmt::Debug for DisplayError {
}
}

pub mod commitment;
pub mod fee_payer;
pub mod input_parsers;
pub mod input_validators;
Expand Down
6 changes: 5 additions & 1 deletion cli-config/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ pub struct Config {
pub json_rpc_url: String,
pub websocket_url: String,
pub keypair_path: String,

#[serde(default)]
pub address_labels: HashMap<String, String>,
#[serde(default)]
pub commitment: String,
}

impl Default for Config {
Expand All @@ -41,11 +42,14 @@ impl Default for Config {
"System Program".to_string(),
);

let commitment = "singleGossip".to_string();

Self {
json_rpc_url,
websocket_url,
keypair_path,
address_labels,
commitment,
}
}
}
Expand Down
45 changes: 38 additions & 7 deletions cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use serde_json::{self, Value};
use solana_account_decoder::{UiAccount, UiAccountEncoding};
use solana_clap_utils::{
self,
commitment::commitment_arg_with_default,
fee_payer::{fee_payer_arg, FEE_PAYER_ARG},
input_parsers::*,
input_validators::*,
Expand Down Expand Up @@ -431,6 +430,10 @@ impl CliConfig<'_> {
solana_cli_config::Config::default().websocket_url
}

fn default_commitment() -> CommitmentConfig {
CommitmentConfig::single_gossip()
}

fn first_nonempty_setting(
settings: std::vec::Vec<(SettingType, String)>,
) -> (SettingType, String) {
Expand All @@ -440,6 +443,16 @@ impl CliConfig<'_> {
.expect("no nonempty setting")
}

fn first_setting_is_some<T>(
settings: std::vec::Vec<(SettingType, Option<T>)>,
) -> (SettingType, T) {
let (setting_type, setting_option) = settings
.into_iter()
.find(|(_, value)| value.is_some())
.expect("all settings none");
(setting_type, setting_option.unwrap())
}

pub fn compute_websocket_url_setting(
websocket_cmd_url: &str,
websocket_cfg_url: &str,
Expand Down Expand Up @@ -484,6 +497,23 @@ impl CliConfig<'_> {
])
}

pub fn compute_commitment_config(
commitment_cmd: &str,
commitment_cfg: &str,
) -> (SettingType, CommitmentConfig) {
Self::first_setting_is_some(vec![
(
SettingType::Explicit,
CommitmentConfig::from_str(commitment_cmd).ok(),
),
(
SettingType::Explicit,
CommitmentConfig::from_str(commitment_cfg).ok(),
),
(SettingType::SystemDefault, Some(Self::default_commitment())),
])
}

pub(crate) fn pubkey(&self) -> Result<Pubkey, SignerError> {
if !self.signers.is_empty() {
self.signers[0].try_pubkey()
Expand Down Expand Up @@ -1019,7 +1049,9 @@ fn process_show_account(

let mut account_string = config.output_format.formatted_string(&cli_account);

if config.output_format == OutputFormat::Display {
if config.output_format == OutputFormat::Display
|| config.output_format == OutputFormat::DisplayVerbose
{
if let Some(output_file) = output_file {
let mut f = File::create(output_file)?;
f.write_all(&data)?;
Expand Down Expand Up @@ -1107,12 +1139,13 @@ fn process_transfer(
}

pub fn process_command(config: &CliConfig) -> ProcessResult {
if config.verbose && config.output_format == OutputFormat::Display {
if config.verbose && config.output_format == OutputFormat::DisplayVerbose {
println_name_value("RPC URL:", &config.json_rpc_url);
println_name_value("Default Signer Path:", &config.keypair_path);
if config.keypair_path.starts_with("usb://") {
println_name_value("Pubkey:", &format!("{:?}", config.pubkey()?));
}
println_name_value("Commitment:", &config.commitment.commitment.to_string());
}

let mut _rpc_client;
Expand Down Expand Up @@ -1851,8 +1884,7 @@ pub fn app<'ab, 'v>(name: &str, about: &'ab str, version: &'v str) -> App<'ab, '
.long("lamports")
.takes_value(false)
.help("Display balance in lamports instead of SOL"),
)
.arg(commitment_arg_with_default("singleGossip")),
),
)
.subcommand(
SubCommand::with_name("confirm")
Expand Down Expand Up @@ -1949,8 +1981,7 @@ pub fn app<'ab, 'v>(name: &str, about: &'ab str, version: &'v str) -> App<'ab, '
.long("allow-excessive-deploy-account-balance")
.takes_value(false)
.help("Use the designated program id, even if the account already holds a large balance of SOL")
)
.arg(commitment_arg_with_default("singleGossip")),
),
)
.subcommand(
SubCommand::with_name("pay")
Expand Down
45 changes: 14 additions & 31 deletions cli/src/cluster_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use chrono::{Local, TimeZone};
use clap::{value_t, value_t_or_exit, App, AppSettings, Arg, ArgMatches, SubCommand};
use console::{style, Emoji};
use solana_clap_utils::{
commitment::{commitment_arg, commitment_arg_with_default},
input_parsers::*,
input_validators::*,
keypair::DefaultSigner,
Expand Down Expand Up @@ -121,20 +120,17 @@ impl ClusterQuerySubCommands for App<'_, '_> {
.long("log")
.takes_value(false)
.help("Don't update the progress inplace; instead show updates with its own new lines"),
)
.arg(commitment_arg()),
),
)
.subcommand(
SubCommand::with_name("cluster-date")
.about("Get current cluster date, computed from genesis creation time and network time")
.arg(commitment_arg()),
.about("Get current cluster date, computed from genesis creation time and network time"),
)
.subcommand(
SubCommand::with_name("cluster-version")
.about("Get the version of the cluster entrypoint"),
)
.subcommand(SubCommand::with_name("fees").about("Display current cluster fees")
.arg(commitment_arg()),
.subcommand(SubCommand::with_name("fees").about("Display current cluster fees"),
)
.subcommand(
SubCommand::with_name("first-available-block")
Expand Down Expand Up @@ -165,8 +161,7 @@ impl ClusterQuerySubCommands for App<'_, '_> {
.subcommand(
SubCommand::with_name("epoch-info")
.about("Get information about the current epoch")
.alias("get-epoch-info")
.arg(commitment_arg()),
.alias("get-epoch-info"),
)
.subcommand(
SubCommand::with_name("genesis-hash")
Expand All @@ -175,16 +170,13 @@ impl ClusterQuerySubCommands for App<'_, '_> {
)
.subcommand(
SubCommand::with_name("slot").about("Get current slot")
.alias("get-slot")
.arg(commitment_arg()),
.alias("get-slot"),
)
.subcommand(
SubCommand::with_name("block-height").about("Get current block height")
.arg(commitment_arg()),
SubCommand::with_name("block-height").about("Get current block height"),
)
.subcommand(
SubCommand::with_name("epoch").about("Get current epoch")
.arg(commitment_arg()),
SubCommand::with_name("epoch").about("Get current epoch"),
)
.subcommand(
SubCommand::with_name("largest-accounts").about("Get addresses of largest cluster accounts")
Expand All @@ -200,8 +192,7 @@ impl ClusterQuerySubCommands for App<'_, '_> {
.takes_value(false)
.conflicts_with("circulating")
.help("Filter address list to only non-circulating accounts")
)
.arg(commitment_arg()),
),
)
.subcommand(
SubCommand::with_name("supply").about("Get information about the cluster supply of SOL")
Expand All @@ -210,18 +201,15 @@ impl ClusterQuerySubCommands for App<'_, '_> {
.long("print-accounts")
.takes_value(false)
.help("Print list of non-circualting account addresses")
)
.arg(commitment_arg()),
),
)
.subcommand(
SubCommand::with_name("total-supply").about("Get total number of SOL")
.setting(AppSettings::Hidden)
.arg(commitment_arg()),
.setting(AppSettings::Hidden),
)
.subcommand(
SubCommand::with_name("transaction-count").about("Get current transaction count")
.alias("get-transaction-count")
.arg(commitment_arg()),
.alias("get-transaction-count"),
)
.subcommand(
SubCommand::with_name("ping")
Expand Down Expand Up @@ -268,8 +256,7 @@ impl ClusterQuerySubCommands for App<'_, '_> {
.default_value("15")
.help("Wait up to timeout seconds for transaction confirmation"),
)
.arg(blockhash_arg())
.arg(commitment_arg()),
.arg(blockhash_arg()),
)
.subcommand(
SubCommand::with_name("live-slots")
Expand All @@ -292,8 +279,7 @@ impl ClusterQuerySubCommands for App<'_, '_> {
.takes_value(false)
.conflicts_with("address")
.help("Include vote transactions when monitoring all transactions")
)
.arg(commitment_arg_with_default("singleGossip")),
),
)
.subcommand(
SubCommand::with_name("block-production")
Expand Down Expand Up @@ -343,8 +329,7 @@ impl ClusterQuerySubCommands for App<'_, '_> {
.long("lamports")
.takes_value(false)
.help("Display balance in lamports instead of SOL"),
)
.arg(commitment_arg()),
),
)
.subcommand(
SubCommand::with_name("transaction-history")
Expand Down Expand Up @@ -1984,8 +1969,6 @@ mod tests {
"-t",
"3",
"-D",
"--commitment",
"max",
"--blockhash",
"4CCNp28j6AhGq7PkjPDP4wbQWBS8LLbQin2xV5n8frKX",
]);
Expand Down
Loading

0 comments on commit a7086a0

Please sign in to comment.