Skip to content

Commit

Permalink
cli: Use confirmed commitment level in commands (coral-xyz#2506)
Browse files Browse the repository at this point in the history
  • Loading branch information
acheroncrypto authored May 30, 2023
1 parent 70d9223 commit 2bf8afe
Showing 1 changed file with 30 additions and 67 deletions.
97 changes: 30 additions & 67 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1676,14 +1676,12 @@ fn cd_member(cfg_override: &ConfigOverride, program_name: &str) -> Result<()> {
}

pub fn verify_bin(program_id: Pubkey, bin_path: &Path, cluster: &str) -> Result<BinVerification> {
let client = RpcClient::new(cluster.to_string());
// Use `finalized` state for verify
let client = RpcClient::new_with_commitment(cluster, CommitmentConfig::finalized());

// Get the deployed build artifacts.
let (deployed_bin, state) = {
let account = client
.get_account_with_commitment(&program_id, CommitmentConfig::default())?
.value
.map_or(Err(anyhow!("Program account not found")), Ok)?;
let account = client.get_account(&program_id)?;
if account.owner == bpf_loader::id() || account.owner == bpf_loader_deprecated::id() {
let bin = account.data.to_vec();
let state = BinVerificationState::ProgramData {
Expand All @@ -1696,13 +1694,7 @@ pub fn verify_bin(program_id: Pubkey, bin_path: &Path, cluster: &str) -> Result<
UpgradeableLoaderState::Program {
programdata_address,
} => {
let account = client
.get_account_with_commitment(
&programdata_address,
CommitmentConfig::default(),
)?
.value
.map_or(Err(anyhow!("Program data account not found")), Ok)?;
let account = client.get_account(&programdata_address)?;
let bin = account.data
[UpgradeableLoaderState::size_of_programdata_metadata()..]
.to_vec();
Expand Down Expand Up @@ -1791,19 +1783,12 @@ fn fetch_idl(cfg_override: &ConfigOverride, idl_addr: Pubkey) -> Result<Idl> {
}
};

let client = RpcClient::new(url);

let mut account = client
.get_account_with_commitment(&idl_addr, CommitmentConfig::processed())?
.value
.map_or(Err(anyhow!("IDL account not found")), Ok)?;
let client = create_client(url);

let mut account = client.get_account(&idl_addr)?;
if account.executable {
let idl_addr = IdlAccount::address(&idl_addr);
account = client
.get_account_with_commitment(&idl_addr, CommitmentConfig::processed())?
.value
.map_or(Err(anyhow!("IDL account not found")), Ok)?;
account = client.get_account(&idl_addr)?;
}

// Cut off account discriminator.
Expand Down Expand Up @@ -1941,7 +1926,7 @@ fn idl_set_buffer(
let keypair = solana_sdk::signature::read_keypair_file(&cfg.provider.wallet.to_string())
.map_err(|_| anyhow!("Unable to read keypair file"))?;
let url = cluster_url(cfg, &cfg.test_validator);
let client = RpcClient::new(url);
let client = create_client(url);

let idl_address = IdlAccount::address(&program_id);
let idl_authority = if print_only {
Expand Down Expand Up @@ -1978,10 +1963,7 @@ fn idl_set_buffer(
);

// Send the transaction.
client.send_and_confirm_transaction_with_spinner_and_commitment(
&tx,
CommitmentConfig::confirmed(),
)?;
client.send_and_confirm_transaction_with_spinner(&tx)?;
}

Ok(())
Expand All @@ -2000,12 +1982,9 @@ fn idl_upgrade(
fn idl_authority(cfg_override: &ConfigOverride, program_id: Pubkey) -> Result<()> {
with_workspace(cfg_override, |cfg| {
let url = cluster_url(cfg, &cfg.test_validator);
let client = RpcClient::new(url);
let client = create_client(url);
let idl_address = {
let account = client
.get_account_with_commitment(&program_id, CommitmentConfig::processed())?
.value
.map_or(Err(anyhow!("Account not found")), Ok)?;
let account = client.get_account(&program_id)?;
if account.executable {
IdlAccount::address(&program_id)
} else {
Expand Down Expand Up @@ -2037,7 +2016,7 @@ fn idl_set_authority(
let keypair = solana_sdk::signature::read_keypair_file(&cfg.provider.wallet.to_string())
.map_err(|_| anyhow!("Unable to read keypair file"))?;
let url = cluster_url(cfg, &cfg.test_validator);
let client = RpcClient::new(url);
let client = create_client(url);

let idl_authority = if print_only {
get_idl_account(&client, &idl_address)?.authority
Expand Down Expand Up @@ -2073,10 +2052,7 @@ fn idl_set_authority(
&[&keypair],
latest_hash,
);
client.send_and_confirm_transaction_with_spinner_and_commitment(
&tx,
CommitmentConfig::confirmed(),
)?;
client.send_and_confirm_transaction_with_spinner(&tx)?;

println!("Authority update complete.");
}
Expand Down Expand Up @@ -2110,7 +2086,7 @@ fn idl_close_account(
let keypair = solana_sdk::signature::read_keypair_file(&cfg.provider.wallet.to_string())
.map_err(|_| anyhow!("Unable to read keypair file"))?;
let url = cluster_url(cfg, &cfg.test_validator);
let client = RpcClient::new(url);
let client = create_client(url);

let idl_authority = if print_only {
get_idl_account(&client, &idl_address)?.authority
Expand Down Expand Up @@ -2141,10 +2117,7 @@ fn idl_close_account(
&[&keypair],
latest_hash,
);
client.send_and_confirm_transaction_with_spinner_and_commitment(
&tx,
CommitmentConfig::confirmed(),
)?;
client.send_and_confirm_transaction_with_spinner(&tx)?;
}

Ok(())
Expand All @@ -2162,7 +2135,7 @@ fn idl_write(cfg: &Config, program_id: &Pubkey, idl: &Idl, idl_address: Pubkey)
let keypair = solana_sdk::signature::read_keypair_file(&cfg.provider.wallet.to_string())
.map_err(|_| anyhow!("Unable to read keypair file"))?;
let url = cluster_url(cfg, &cfg.test_validator);
let client = RpcClient::new(url);
let client = create_client(url);

// Serialize and compress the idl.
let idl_data = {
Expand Down Expand Up @@ -2202,10 +2175,7 @@ fn idl_write(cfg: &Config, program_id: &Pubkey, idl: &Idl, idl_address: Pubkey)
&[&keypair],
latest_hash,
);
client.send_and_confirm_transaction_with_spinner_and_commitment(
&tx,
CommitmentConfig::confirmed(),
)?;
client.send_and_confirm_transaction_with_spinner(&tx)?;
offset += MAX_WRITE_SIZE;
}
Ok(())
Expand Down Expand Up @@ -2330,7 +2300,7 @@ fn account(
.unwrap_or(Cluster::Localnet);
cluster = cfg_override.cluster.as_ref().unwrap_or(cluster);

let data = RpcClient::new(cluster.url()).get_account_data(&address)?;
let data = create_client(cluster.url()).get_account_data(&address)?;
if data.len() < 8 {
return Err(anyhow!(
"The account has less than 8 bytes and is not an Anchor account."
Expand Down Expand Up @@ -2794,7 +2764,7 @@ fn validator_flags(
} else if key == "clone" {
// Client for fetching accounts data
let client = if let Some(url) = entries["url"].as_str() {
RpcClient::new(url.to_string())
create_client(url)
} else {
return Err(anyhow!(
"Validator url for Solana's JSON RPC should be provided in order to clone accounts from it"
Expand All @@ -2813,12 +2783,7 @@ fn validator_flags(
.collect::<Result<HashSet<Pubkey>>>()?;

let accounts_keys = pubkeys.iter().cloned().collect::<Vec<_>>();
let accounts = client
.get_multiple_accounts_with_commitment(
&accounts_keys,
CommitmentConfig::default(),
)?
.value;
let accounts = client.get_multiple_accounts(&accounts_keys)?;

// Check if there are program accounts
for (account, acc_key) in accounts.iter().zip(accounts_keys) {
Expand Down Expand Up @@ -2926,7 +2891,6 @@ fn start_test_validator(
flags: Option<Vec<String>>,
test_log_stdout: bool,
) -> Result<Child> {
//
let (test_ledger_directory, test_ledger_log_filename) =
test_validator_file_paths(test_validator);

Expand Down Expand Up @@ -2978,7 +2942,7 @@ fn start_test_validator(
.map_err(|e| anyhow::format_err!("{}", e.to_string()))?;

// Wait for the validator to be ready.
let client = RpcClient::new(rpc_url);
let client = create_client(rpc_url);
let mut count = 0;
let ms_wait = test_validator
.as_ref()
Expand Down Expand Up @@ -3214,7 +3178,7 @@ fn create_idl_account(
let keypair = solana_sdk::signature::read_keypair_file(keypair_path)
.map_err(|_| anyhow!("Unable to read keypair file"))?;
let url = cluster_url(cfg, &cfg.test_validator);
let client = RpcClient::new(url);
let client = create_client(url);
let idl_data = serialize_idl(idl)?;

// Run `Create instruction.
Expand Down Expand Up @@ -3269,10 +3233,7 @@ fn create_idl_account(
&[&keypair],
latest_hash,
);
client.send_and_confirm_transaction_with_spinner_and_commitment(
&tx,
CommitmentConfig::finalized(),
)?;
client.send_and_confirm_transaction_with_spinner(&tx)?;
}

// Write directly to the IDL account buffer.
Expand All @@ -3290,7 +3251,7 @@ fn create_idl_buffer(
let keypair = solana_sdk::signature::read_keypair_file(keypair_path)
.map_err(|_| anyhow!("Unable to read keypair file"))?;
let url = cluster_url(cfg, &cfg.test_validator);
let client = RpcClient::new(url);
let client = create_client(url);

let buffer = Keypair::new();

Expand Down Expand Up @@ -3333,10 +3294,7 @@ fn create_idl_buffer(
);

// Send the transaction.
client.send_and_confirm_transaction_with_spinner_and_commitment(
&tx,
CommitmentConfig::confirmed(),
)?;
client.send_and_confirm_transaction_with_spinner(&tx)?;

Ok(buffer.pubkey())
}
Expand Down Expand Up @@ -3991,6 +3949,11 @@ fn strip_workspace_prefix(absolute_path: String) -> String {
.into()
}

/// Create a new [`RpcClient`] with `confirmed` commitment level instead of the default(finalized).
fn create_client<U: ToString>(url: U) -> RpcClient {
RpcClient::new_with_commitment(url, CommitmentConfig::confirmed())
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit 2bf8afe

Please sign in to comment.