Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor - program v4 CLI #3428

Merged
merged 9 commits into from
Nov 18, 2024
38 changes: 14 additions & 24 deletions cargo-registry/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@ use {
input_validators::is_url_or_moniker,
keypair::{DefaultSigner, SignerIndex},
},
solana_cli::{
cli::{DEFAULT_CONFIRM_TX_TIMEOUT_SECONDS, DEFAULT_RPC_TIMEOUT_SECONDS},
program_v4::ProgramV4CommandConfig,
},
solana_cli::cli::{CliConfig, DEFAULT_CONFIRM_TX_TIMEOUT_SECONDS, DEFAULT_RPC_TIMEOUT_SECONDS},
solana_cli_config::{Config, ConfigInput},
solana_cli_output::OutputFormat,
solana_rpc_client::rpc_client::RpcClient,
solana_rpc_client_api::config::RpcSendTransactionConfig,
solana_sdk::{
Expand All @@ -20,34 +16,28 @@ use {
std::{error, sync::Arc, time::Duration},
};

pub(crate) struct RPCCommandConfig<'a>(pub ProgramV4CommandConfig<'a>);

impl<'a> RPCCommandConfig<'a> {
pub fn new(client: &'a Client) -> Self {
Self(ProgramV4CommandConfig {
websocket_url: &client.websocket_url,
commitment: client.commitment,
payer: &client.cli_signers[0],
authority: &client.cli_signers[client.authority_signer_index],
output_format: &OutputFormat::Display,
use_quic: true,
rpc_send_transaction_config: client.rpc_transaction_config,
})
}
}

pub(crate) struct Client {
pub rpc_client: Arc<RpcClient>,
pub port: u16,
pub server_url: String,
websocket_url: String,
commitment: commitment_config::CommitmentConfig,
cli_signers: Vec<Keypair>,
authority_signer_index: SignerIndex,
rpc_transaction_config: RpcSendTransactionConfig,
pub authority_signer_index: SignerIndex,
send_transaction_config: RpcSendTransactionConfig,
}

impl Client {
pub fn get_cli_config(&'_ self) -> CliConfig<'_> {
CliConfig {
websocket_url: self.websocket_url.clone(),
commitment: self.commitment,
signers: vec![&self.cli_signers[0], &self.cli_signers[1]],
send_transaction_config: self.send_transaction_config,
..CliConfig::default()
}
}

fn get_keypair(
matches: &ArgMatches<'_>,
config_path: &str,
Expand Down Expand Up @@ -233,7 +223,7 @@ impl Client {
commitment,
cli_signers: vec![payer_keypair, authority_keypair],
authority_signer_index: 1,
rpc_transaction_config: RpcSendTransactionConfig {
send_transaction_config: RpcSendTransactionConfig {
skip_preflight,
preflight_commitment: Some(commitment.commitment),
..RpcSendTransactionConfig::default()
Expand Down
23 changes: 13 additions & 10 deletions cargo-registry/src/crate_handler.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use {
crate::{
client::{Client, RPCCommandConfig},
client::Client,
sparse_index::{IndexEntry, RegistryIndex},
},
flate2::{
Expand All @@ -12,7 +12,7 @@ use {
serde_derive::{Deserialize, Serialize},
serde_json::from_slice,
sha2::{Digest, Sha256},
solana_cli::program_v4::{process_deploy_program, process_dump, read_and_verify_elf},
solana_cli::program_v4::{process_deploy_program, process_dump},
solana_sdk::{
pubkey::Pubkey,
signature::{Keypair, Signer},
Expand Down Expand Up @@ -110,8 +110,11 @@ impl Program {
return Err("Signer doesn't match program ID".into());
}

let mut program_data = read_and_verify_elf(self.path.as_ref(), &client.rpc_client)
pgarg66 marked this conversation as resolved.
Show resolved Hide resolved
.map_err(|e| format!("failed to read the program: {}", e))?;
let mut file = fs::File::open(&self.path)
.map_err(|err| format!("Unable to open program file: {err}"))?;
let mut program_data = Vec::new();
file.read_to_end(&mut program_data)
.map_err(|err| format!("Unable to read program file: {err}"))?;

if APPEND_CRATE_TO_ELF {
let program_id_str = Program::program_id_to_crate_name(self.id);
Expand All @@ -121,15 +124,16 @@ impl Program {
program_data.extend_from_slice(&crate_tar_gz.0);
program_data.extend_from_slice(&crate_len);
}
let command_config = RPCCommandConfig::new(client.as_ref());

process_deploy_program(
client.rpc_client.clone(),
&command_config.0,
&program_data,
program_data.len() as u32,
&client.get_cli_config(),
&client.authority_signer_index,
&signer.pubkey(),
&program_data,
None..None,
Some(signer),
false,
)
.map_err(|e| {
error!("Failed to deploy the program: {}", e);
Expand All @@ -141,11 +145,10 @@ impl Program {

fn dump(&mut self, client: Arc<Client>) -> Result<(), Error> {
info!("Fetching program {:?}", self.id);
let command_config = RPCCommandConfig::new(client.as_ref());

process_dump(
client.rpc_client.clone(),
command_config.0.commitment,
&client.get_cli_config(),
Some(self.id),
&self.path,
)
Expand Down
Loading
Loading