cli: Add --confirmed option to a couple commands, also add --no-header (#7112)

* Add --confirmed option to get-slot, get-epoch-info, get-transaction-count

* Add --no-header option
This commit is contained in:
Michael Vines 2019-11-24 17:34:18 -07:00 committed by GitHub
parent 702f7cc51d
commit d5beb8a9e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 151 additions and 42 deletions

View File

@ -74,10 +74,16 @@ pub enum CliCommand {
},
ClusterVersion,
Fees,
GetEpochInfo,
GetEpochInfo {
commitment_config: CommitmentConfig,
},
GetGenesisHash,
GetSlot,
GetTransactionCount,
GetSlot {
commitment_config: CommitmentConfig,
},
GetTransactionCount {
commitment_config: CommitmentConfig,
},
Ping {
lamports: u64,
interval: Duration,
@ -217,6 +223,7 @@ pub struct CliConfig {
pub keypair: Keypair,
pub keypair_path: Option<String>,
pub rpc_client: Option<RpcClient>,
pub print_header: bool,
}
impl CliConfig {
@ -242,6 +249,7 @@ impl Default for CliConfig {
keypair: Keypair::new(),
keypair_path: Some(Self::default_keypair_path()),
rpc_client: None,
print_header: true,
}
}
}
@ -258,22 +266,13 @@ pub fn parse_command(matches: &ArgMatches<'_>) -> Result<CliCommandInfo, Box<dyn
command: CliCommand::Fees,
require_keypair: false,
}),
("get-epoch-info", Some(_matches)) => Ok(CliCommandInfo {
command: CliCommand::GetEpochInfo,
require_keypair: false,
}),
("get-epoch-info", Some(matches)) => parse_get_epoch_info(matches),
("get-genesis-hash", Some(_matches)) => Ok(CliCommandInfo {
command: CliCommand::GetGenesisHash,
require_keypair: false,
}),
("get-slot", Some(_matches)) => Ok(CliCommandInfo {
command: CliCommand::GetSlot,
require_keypair: false,
}),
("get-transaction-count", Some(_matches)) => Ok(CliCommandInfo {
command: CliCommand::GetTransactionCount,
require_keypair: false,
}),
("get-slot", Some(matches)) => parse_get_slot(matches),
("get-transaction-count", Some(matches)) => parse_get_transaction_count(matches),
("ping", Some(matches)) => parse_cluster_ping(matches),
("show-gossip", Some(_matches)) => Ok(CliCommandInfo {
command: CliCommand::ShowGossip,
@ -844,14 +843,16 @@ fn process_witness(
}
pub fn process_command(config: &CliConfig) -> ProcessResult {
if let Some(keypair_path) = &config.keypair_path {
println_name_value("Keypair:", keypair_path);
if config.print_header {
if let Some(keypair_path) = &config.keypair_path {
println_name_value("Keypair:", keypair_path);
}
if let CliCommand::Address = config.command {
// Get address of this client
return Ok(format!("{}", config.keypair.pubkey()));
}
println_name_value("RPC Endpoint:", &config.json_rpc_url);
}
if let CliCommand::Address = config.command {
// Get address of this client
return Ok(format!("{}", config.keypair.pubkey()));
}
println_name_value("RPC Endpoint:", &config.json_rpc_url);
let mut _rpc_client;
let rpc_client = if config.rpc_client.is_none() {
@ -870,9 +871,15 @@ pub fn process_command(config: &CliConfig) -> ProcessResult {
CliCommand::ClusterVersion => process_cluster_version(&rpc_client),
CliCommand::Fees => process_fees(&rpc_client),
CliCommand::GetGenesisHash => process_get_genesis_hash(&rpc_client),
CliCommand::GetSlot => process_get_slot(&rpc_client),
CliCommand::GetEpochInfo => process_get_epoch_info(&rpc_client),
CliCommand::GetTransactionCount => process_get_transaction_count(&rpc_client),
CliCommand::GetEpochInfo { commitment_config } => {
process_get_epoch_info(&rpc_client, commitment_config)
}
CliCommand::GetSlot { commitment_config } => {
process_get_slot(&rpc_client, commitment_config)
}
CliCommand::GetTransactionCount { commitment_config } => {
process_get_transaction_count(&rpc_client, commitment_config)
}
CliCommand::Ping {
lamports,
interval,
@ -1888,10 +1895,14 @@ mod tests {
let signature = process_command(&config);
assert_eq!(signature.unwrap(), SIGNATURE.to_string());
config.command = CliCommand::GetSlot;
config.command = CliCommand::GetSlot {
commitment_config: CommitmentConfig::default(),
};
assert_eq!(process_command(&config).unwrap(), "0");
config.command = CliCommand::GetTransactionCount;
config.command = CliCommand::GetTransactionCount {
commitment_config: CommitmentConfig::default(),
};
assert_eq!(process_command(&config).unwrap(), "1234");
config.command = CliCommand::Pay {
@ -2025,10 +2036,14 @@ mod tests {
config.command = CliCommand::VoteAuthorize(bob_pubkey, bob_pubkey, VoteAuthorize::Voter);
assert!(process_command(&config).is_err());
config.command = CliCommand::GetSlot;
config.command = CliCommand::GetSlot {
commitment_config: CommitmentConfig::default(),
};
assert!(process_command(&config).is_err());
config.command = CliCommand::GetTransactionCount;
config.command = CliCommand::GetTransactionCount {
commitment_config: CommitmentConfig::default(),
};
assert!(process_command(&config).is_err());
config.command = CliCommand::Pay {

View File

@ -55,14 +55,40 @@ impl ClusterQuerySubCommands for App<'_, '_> {
.subcommand(SubCommand::with_name("fees").about("Display current cluster fees"))
.subcommand(
SubCommand::with_name("get-epoch-info")
.about("Get information about the current epoch"),
.about("Get information about the current epoch")
.arg(
Arg::with_name("confirmed")
.long("confirmed")
.takes_value(false)
.help(
"Return information at maximum-lockout commitment level",
),
),
)
.subcommand(
SubCommand::with_name("get-genesis-hash").about("Get the genesis hash"),
)
.subcommand(SubCommand::with_name("get-slot").about("Get current slot"))
.subcommand(
SubCommand::with_name("get-transaction-count").about("Get current transaction count"),
SubCommand::with_name("get-slot").about("Get current slot")
.arg(
Arg::with_name("confirmed")
.long("confirmed")
.takes_value(false)
.help(
"Return slot at maximum-lockout commitment level",
),
),
)
.subcommand(
SubCommand::with_name("get-transaction-count").about("Get current transaction count")
.arg(
Arg::with_name("confirmed")
.long("confirmed")
.takes_value(false)
.help(
"Return count at maximum-lockout commitment level",
),
),
)
.subcommand(
SubCommand::with_name("ping")
@ -161,6 +187,42 @@ pub fn parse_cluster_ping(matches: &ArgMatches<'_>) -> Result<CliCommandInfo, Cl
})
}
pub fn parse_get_epoch_info(matches: &ArgMatches<'_>) -> Result<CliCommandInfo, CliError> {
let commitment_config = if matches.is_present("confirmed") {
CommitmentConfig::default()
} else {
CommitmentConfig::recent()
};
Ok(CliCommandInfo {
command: CliCommand::GetEpochInfo { commitment_config },
require_keypair: false,
})
}
pub fn parse_get_slot(matches: &ArgMatches<'_>) -> Result<CliCommandInfo, CliError> {
let commitment_config = if matches.is_present("confirmed") {
CommitmentConfig::default()
} else {
CommitmentConfig::recent()
};
Ok(CliCommandInfo {
command: CliCommand::GetSlot { commitment_config },
require_keypair: false,
})
}
pub fn parse_get_transaction_count(matches: &ArgMatches<'_>) -> Result<CliCommandInfo, CliError> {
let commitment_config = if matches.is_present("confirmed") {
CommitmentConfig::default()
} else {
CommitmentConfig::recent()
};
Ok(CliCommandInfo {
command: CliCommand::GetTransactionCount { commitment_config },
require_keypair: false,
})
}
pub fn parse_show_validators(matches: &ArgMatches<'_>) -> Result<CliCommandInfo, CliError> {
let use_lamports_unit = matches.is_present("lamports");
@ -251,8 +313,11 @@ pub fn process_fees(rpc_client: &RpcClient) -> ProcessResult {
))
}
pub fn process_get_epoch_info(rpc_client: &RpcClient) -> ProcessResult {
let epoch_info = rpc_client.get_epoch_info()?;
pub fn process_get_epoch_info(
rpc_client: &RpcClient,
commitment_config: &CommitmentConfig,
) -> ProcessResult {
let epoch_info = rpc_client.get_epoch_info_with_commitment(commitment_config.clone())?;
println!();
println_name_value("Current epoch:", &epoch_info.epoch.to_string());
println_name_value("Current slot:", &epoch_info.absolute_slot.to_string());
@ -285,13 +350,20 @@ pub fn process_get_genesis_hash(rpc_client: &RpcClient) -> ProcessResult {
Ok(genesis_hash.to_string())
}
pub fn process_get_slot(rpc_client: &RpcClient) -> ProcessResult {
let slot = rpc_client.get_slot()?;
pub fn process_get_slot(
rpc_client: &RpcClient,
commitment_config: &CommitmentConfig,
) -> ProcessResult {
let slot = rpc_client.get_slot_with_commitment(commitment_config.clone())?;
Ok(slot.to_string())
}
pub fn process_get_transaction_count(rpc_client: &RpcClient) -> ProcessResult {
let transaction_count = rpc_client.get_transaction_count()?;
pub fn process_get_transaction_count(
rpc_client: &RpcClient,
commitment_config: &CommitmentConfig,
) -> ProcessResult {
let transaction_count =
rpc_client.get_transaction_count_with_commitment(commitment_config.clone())?;
Ok(transaction_count.to_string())
}
@ -590,7 +662,9 @@ mod tests {
assert_eq!(
parse_command(&test_get_epoch_info).unwrap(),
CliCommandInfo {
command: CliCommand::GetEpochInfo,
command: CliCommand::GetEpochInfo {
commitment_config: CommitmentConfig::recent(),
},
require_keypair: false
}
);
@ -612,7 +686,9 @@ mod tests {
assert_eq!(
parse_command(&test_get_slot).unwrap(),
CliCommandInfo {
command: CliCommand::GetSlot,
command: CliCommand::GetSlot {
commitment_config: CommitmentConfig::recent(),
},
require_keypair: false
}
);
@ -623,7 +699,9 @@ mod tests {
assert_eq!(
parse_command(&test_transaction_count).unwrap(),
CliCommandInfo {
command: CliCommand::GetTransactionCount,
command: CliCommand::GetTransactionCount {
commitment_config: CommitmentConfig::recent(),
},
require_keypair: false
}
);

View File

@ -133,12 +133,15 @@ pub fn parse_args(matches: &ArgMatches<'_>) -> Result<CliConfig, Box<dyn error::
(default.keypair, None)
};
let print_header = !matches.is_present("no_header");
Ok(CliConfig {
command,
json_rpc_url,
keypair,
keypair_path,
rpc_client: None,
print_header,
})
}
@ -182,6 +185,12 @@ fn main() -> Result<(), Box<dyn error::Error>> {
.takes_value(true)
.help("/path/to/id.json"),
)
.arg(
Arg::with_name("no_header")
.long("no-header")
.global(true)
.help("Disable information header"),
)
.arg(
Arg::with_name(ASK_SEED_PHRASE_ARG.name)
.long(ASK_SEED_PHRASE_ARG.long)

View File

@ -197,9 +197,16 @@ impl RpcClient {
}
pub fn get_epoch_info(&self) -> io::Result<RpcEpochInfo> {
self.get_epoch_info_with_commitment(CommitmentConfig::default())
}
pub fn get_epoch_info_with_commitment(
&self,
commitment_config: CommitmentConfig,
) -> io::Result<RpcEpochInfo> {
let response = self
.client
.send(&RpcRequest::GetEpochInfo, None, 0, None)
.send(&RpcRequest::GetEpochInfo, None, 0, commitment_config.ok())
.map_err(|err| {
io::Error::new(
io::ErrorKind::Other,