Add block command

This commit is contained in:
Michael Vines 2020-09-06 11:10:19 -07:00
parent 6677996369
commit 27752c4e4d
3 changed files with 65 additions and 3 deletions

View File

@ -178,6 +178,9 @@ pub enum CliCommand {
},
Fees,
FirstAvailableBlock,
GetBlock {
slot: Slot,
},
GetBlockTime {
slot: Option<Slot>,
},
@ -624,6 +627,7 @@ pub fn parse_command(
command: CliCommand::FirstAvailableBlock,
signers: vec![],
}),
("block", Some(matches)) => parse_get_block(matches),
("block-time", Some(matches)) => parse_get_block_time(matches),
("epoch-info", Some(matches)) => parse_get_epoch_info(matches),
("genesis-hash", Some(_matches)) => Ok(CliCommandInfo {
@ -1493,6 +1497,7 @@ pub fn process_command(config: &CliConfig) -> ProcessResult {
} => process_create_address_with_seed(config, from_pubkey.as_ref(), &seed, &program_id),
CliCommand::Fees => process_fees(&rpc_client, config),
CliCommand::FirstAvailableBlock => process_first_available_block(&rpc_client),
CliCommand::GetBlock { slot } => process_get_block(&rpc_client, config, *slot),
CliCommand::GetBlockTime { slot } => process_get_block_time(&rpc_client, config, *slot),
CliCommand::GetEpoch => process_get_epoch(&rpc_client, config),
CliCommand::GetEpochInfo => process_get_epoch_info(&rpc_client, config),

View File

@ -57,6 +57,19 @@ pub trait ClusterQuerySubCommands {
impl ClusterQuerySubCommands for App<'_, '_> {
fn cluster_query_subcommands(self) -> Self {
self.subcommand(
SubCommand::with_name("block")
.about("Get a confirmed block")
.arg(
Arg::with_name("slot")
.long("slot")
.validator(is_slot)
.value_name("SLOT")
.takes_value(true)
.index(1)
.required(true),
),
)
.subcommand(
SubCommand::with_name("catchup")
.about("Wait for a validator to catch up to the cluster")
.arg(
@ -346,6 +359,14 @@ pub fn parse_cluster_ping(
})
}
pub fn parse_get_block(matches: &ArgMatches<'_>) -> Result<CliCommandInfo, CliError> {
let slot = value_t_or_exit!(matches, "slot", Slot);
Ok(CliCommandInfo {
command: CliCommand::GetBlock { slot },
signers: vec![],
})
}
pub fn parse_get_block_time(matches: &ArgMatches<'_>) -> Result<CliCommandInfo, CliError> {
let slot = value_of(matches, "slot");
Ok(CliCommandInfo {
@ -658,6 +679,42 @@ pub fn process_leader_schedule(rpc_client: &RpcClient) -> ProcessResult {
Ok("".to_string())
}
pub fn process_get_block(rpc_client: &RpcClient, _config: &CliConfig, slot: Slot) -> ProcessResult {
let block =
rpc_client.get_confirmed_block_with_encoding(slot, UiTransactionEncoding::Base64)?;
println!("Slot: {}", slot);
println!("Parent Slot: {}", block.parent_slot);
println!("Blockhash: {}", block.blockhash);
println!("Previous Blockhash: {}", block.previous_blockhash);
if block.block_time.is_some() {
println!("Block Time: {:?}", block.block_time);
}
if !block.rewards.is_empty() {
println!("Rewards:",);
for reward in block.rewards {
println!(
" {:<44}: {}",
reward.pubkey,
if reward.lamports > 0 {
format!("{}", lamports_to_sol(reward.lamports as u64))
} else {
format!("◎-{}", lamports_to_sol(reward.lamports.abs() as u64))
}
);
}
}
for (index, transaction_with_meta) in block.transactions.iter().enumerate() {
println!("Transaction {}:", index);
println_transaction(
&transaction_with_meta.transaction.decode().unwrap(),
&transaction_with_meta.meta,
" ",
);
}
Ok("".to_string())
}
pub fn process_get_block_time(
rpc_client: &RpcClient,
config: &CliConfig,

View File

@ -162,7 +162,7 @@ pub fn write_transaction<W: io::Write>(
)?;
writeln!(
w,
"{} Fee: {} SOL",
"{} Fee: {}",
prefix,
lamports_to_sol(transaction_status.fee)
)?;
@ -179,7 +179,7 @@ pub fn write_transaction<W: io::Write>(
if pre == post {
writeln!(
w,
"{} Account {} balance: {} SOL",
"{} Account {} balance: {}",
prefix,
i,
lamports_to_sol(*pre)
@ -187,7 +187,7 @@ pub fn write_transaction<W: io::Write>(
} else {
writeln!(
w,
"{} Account {} balance: {} SOL -> {} SOL",
"{} Account {} balance: {} -> {}",
prefix,
i,
lamports_to_sol(*pre),