Skip to content

Commit

Permalink
Refactor - program v4 CLI (#3428)
Browse files Browse the repository at this point in the history
* Removes ProgramV4CommandConfig wrapper.

* Adds use_rpc option for deployment.

* Moves program_location parameter inside process_deploy_program().

* Combines deploy and redeploy commands into one.

* Combines the underlying ProgramV4CliCommand as well.

* Adds optional upload range parameters.

* Inlines build_create_buffer_message(), build_retract_and_truncate_messages() and build_retract_and_deploy_messages().

* Fixes cargo registry.

* Make cargo clippy happy.
  • Loading branch information
Lichtso authored Nov 18, 2024
1 parent 7fbaa56 commit c2880bb
Show file tree
Hide file tree
Showing 3 changed files with 527 additions and 587 deletions.
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)
.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

0 comments on commit c2880bb

Please sign in to comment.