cli: add program show for non-upgradeable programs (bp #15707) (#15709)

* cli: add program show for non-upgradeable programs (#15707)

(cherry picked from commit 2177e0aff8)

# Conflicts:
#	cli/src/program.rs

* fix conflicts

Co-authored-by: Jack May <jack@solana.com>
This commit is contained in:
mergify[bot]
2021-03-04 21:27:48 +00:00
committed by GitHub
parent e9d04b5517
commit 5f7258640b
3 changed files with 83 additions and 41 deletions

View File

@ -1570,10 +1570,34 @@ impl fmt::Display for CliProgramAuthority {
} }
} }
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CliProgram {
pub program_id: String,
pub owner: String,
pub data_len: usize,
}
impl QuietDisplay for CliProgram {}
impl VerboseDisplay for CliProgram {}
impl fmt::Display for CliProgram {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
writeln!(f)?;
writeln_name_value(f, "Program Id:", &self.program_id)?;
writeln_name_value(f, "Owner:", &self.owner)?;
writeln_name_value(
f,
"Data Length:",
&format!("{:?} ({:#x?}) bytes", self.data_len, self.data_len),
)?;
Ok(())
}
}
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct CliUpgradeableProgram { pub struct CliUpgradeableProgram {
pub program_id: String, pub program_id: String,
pub owner: String,
pub programdata_address: String, pub programdata_address: String,
pub authority: String, pub authority: String,
pub last_deploy_slot: u64, pub last_deploy_slot: u64,
@ -1585,6 +1609,7 @@ impl fmt::Display for CliUpgradeableProgram {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
writeln!(f)?; writeln!(f)?;
writeln_name_value(f, "Program Id:", &self.program_id)?; writeln_name_value(f, "Program Id:", &self.program_id)?;
writeln_name_value(f, "Owner:", &self.owner)?;
writeln_name_value(f, "ProgramData Address:", &self.programdata_address)?; writeln_name_value(f, "ProgramData Address:", &self.programdata_address)?;
writeln_name_value(f, "Authority:", &self.authority)?; writeln_name_value(f, "Authority:", &self.authority)?;
writeln_name_value( writeln_name_value(

View File

@ -14,7 +14,7 @@ use serde_json::{self, json, Value};
use solana_bpf_loader_program::{bpf_verifier, BpfError, ThisInstructionMeter}; use solana_bpf_loader_program::{bpf_verifier, BpfError, ThisInstructionMeter};
use solana_clap_utils::{self, input_parsers::*, input_validators::*, keypair::*}; use solana_clap_utils::{self, input_parsers::*, input_validators::*, keypair::*};
use solana_cli_output::{ use solana_cli_output::{
display::new_spinner_progress_bar, CliUpgradeableBuffer, CliUpgradeableProgram, display::new_spinner_progress_bar, CliProgram, CliUpgradeableBuffer, CliUpgradeableProgram,
}; };
use solana_client::{ use solana_client::{
rpc_client::RpcClient, rpc_config::RpcSendTransactionConfig, rpc_client::RpcClient, rpc_config::RpcSendTransactionConfig,
@ -923,6 +923,13 @@ fn process_show(
.get_account_with_commitment(&account_pubkey, config.commitment)? .get_account_with_commitment(&account_pubkey, config.commitment)?
.value .value
{ {
if account.owner == bpf_loader::id() || account.owner == bpf_loader_deprecated::id() {
Ok(config.output_format.formatted_string(&CliProgram {
program_id: account_pubkey.to_string(),
owner: account.owner.to_string(),
data_len: account.data.len(),
}))
} else if account.owner == bpf_loader_upgradeable::id() {
if let Ok(UpgradeableLoaderState::Program { if let Ok(UpgradeableLoaderState::Program {
programdata_address, programdata_address,
}) = account.state() }) = account.state()
@ -940,6 +947,7 @@ fn process_show(
.output_format .output_format
.formatted_string(&CliUpgradeableProgram { .formatted_string(&CliUpgradeableProgram {
program_id: account_pubkey.to_string(), program_id: account_pubkey.to_string(),
owner: account.owner.to_string(),
programdata_address: programdata_address.to_string(), programdata_address: programdata_address.to_string(),
authority: upgrade_authority_address authority: upgrade_authority_address
.map(|pubkey| pubkey.to_string()) .map(|pubkey| pubkey.to_string())
@ -949,7 +957,10 @@ fn process_show(
- UpgradeableLoaderState::programdata_data_offset()?, - UpgradeableLoaderState::programdata_data_offset()?,
})) }))
} else { } else {
Err("Invalid associated ProgramData account found for the program".into()) Err(
"Invalid associated ProgramData account found for the program"
.into(),
)
} }
} else { } else {
Err( Err(
@ -957,7 +968,8 @@ fn process_show(
.into(), .into(),
) )
} }
} else if let Ok(UpgradeableLoaderState::Buffer { authority_address }) = account.state() } else if let Ok(UpgradeableLoaderState::Buffer { authority_address }) =
account.state()
{ {
Ok(config Ok(config
.output_format .output_format
@ -972,6 +984,9 @@ fn process_show(
} else { } else {
Err("Not a buffer or program account".into()) Err("Not a buffer or program account".into())
} }
} else {
Err("Accont is not a BPF program".into())
}
} else { } else {
Err("Unable to find the account".into()) Err("Unable to find the account".into())
} }

View File

@ -69,6 +69,7 @@ An example output looks like:
```bash ```bash
Program Id: 3KS2k14CmtnuVv2fvYcvdrNgC94Y11WETBpMUGgXyWZL Program Id: 3KS2k14CmtnuVv2fvYcvdrNgC94Y11WETBpMUGgXyWZL
Owner: BPFLoaderUpgradeab1e11111111111111111111111
ProgramData Address: EHsACWBhgmw8iq5dmUZzTA1esRqcTognhKNHUkPi4q4g ProgramData Address: EHsACWBhgmw8iq5dmUZzTA1esRqcTognhKNHUkPi4q4g
Authority: FwoGJNUaJN2zfVEex9BB11Dqb3NJKy3e9oY3KTh9XzCU Authority: FwoGJNUaJN2zfVEex9BB11Dqb3NJKy3e9oY3KTh9XzCU
Last Deployed In Slot: 63890568 Last Deployed In Slot: 63890568
@ -77,6 +78,7 @@ Data Length: 5216 (0x1460) bytes
- `Program Id` is the address that can be referenced in an instruction's - `Program Id` is the address that can be referenced in an instruction's
`program_id` field when invoking a program. `program_id` field when invoking a program.
- `Owner`: The loader this program was deployed with.
- `ProgramData Address` is the account associated with the program account that - `ProgramData Address` is the account associated with the program account that
holds the program's data (shared object). holds the program's data (shared object).
- `Authority` is the program's upgrade authority. - `Authority` is the program's upgrade authority.