Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

Commit

Permalink
cli: (program) move buffer fetching/validation into separate fn (#34317)
Browse files Browse the repository at this point in the history
* cli: (program) move buffer fetching/validation into separate fn

* error gracefully (don't panic)

---------

Co-authored-by: norwnd <norwnd>
  • Loading branch information
norwnd authored Dec 5, 2023
1 parent 8d7e63d commit f41badf
Showing 1 changed file with 54 additions and 49 deletions.
103 changes: 54 additions & 49 deletions cli/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1020,55 +1020,10 @@ fn process_program_deploy(
let program_len = program_data.len();
(program_data, program_len)
} else if buffer_provided {
// Check supplied buffer account
if let Some(account) = rpc_client
.get_account_with_commitment(&buffer_pubkey, config.commitment)?
.value
{
if !bpf_loader_upgradeable::check_id(&account.owner) {
return Err(format!(
"Buffer account {buffer_pubkey} is not owned by the BPF Upgradeable Loader",
)
.into());
}

match account.state() {
Ok(UpgradeableLoaderState::Buffer { .. }) => {
// continue if buffer is initialized
}
Ok(UpgradeableLoaderState::Program { .. }) => {
return Err(
format!("Cannot use program account {buffer_pubkey} as buffer").into(),
);
}
Ok(UpgradeableLoaderState::ProgramData { .. }) => {
return Err(format!(
"Cannot use program data account {buffer_pubkey} as buffer",
)
.into())
}
Ok(UpgradeableLoaderState::Uninitialized) => {
return Err(format!("Buffer account {buffer_pubkey} is not initialized").into());
}
Err(_) => {
return Err(
format!("Buffer account {buffer_pubkey} could not be deserialized").into(),
)
}
};

let program_len = account
.data
.len()
.saturating_sub(UpgradeableLoaderState::size_of_buffer_metadata());

(vec![], program_len)
} else {
return Err(format!(
"Buffer account {buffer_pubkey} not found, was it already consumed?",
)
.into());
}
(
vec![],
fetch_buffer_len(&rpc_client, config, buffer_pubkey)?,
)
} else {
return Err("Program location required if buffer not supplied".into());
};
Expand Down Expand Up @@ -1135,6 +1090,56 @@ fn process_program_deploy(
result
}

fn fetch_buffer_len(
rpc_client: &RpcClient,
config: &CliConfig,
buffer_pubkey: Pubkey,
) -> Result<usize, Box<dyn std::error::Error>> {
// Check supplied buffer account
if let Some(account) = rpc_client
.get_account_with_commitment(&buffer_pubkey, config.commitment)?
.value
{
if !bpf_loader_upgradeable::check_id(&account.owner) {
return Err(format!(
"Buffer account {buffer_pubkey} is not owned by the BPF Upgradeable Loader",
)
.into());
}

match account.state() {
Ok(UpgradeableLoaderState::Buffer { .. }) => {
// continue if buffer is initialized
}
Ok(UpgradeableLoaderState::Program { .. }) => {
return Err(format!("Cannot use program account {buffer_pubkey} as buffer").into());
}
Ok(UpgradeableLoaderState::ProgramData { .. }) => {
return Err(
format!("Cannot use program data account {buffer_pubkey} as buffer",).into(),
)
}
Ok(UpgradeableLoaderState::Uninitialized) => {
return Err(format!("Buffer account {buffer_pubkey} is not initialized").into());
}
Err(_) => {
return Err(
format!("Buffer account {buffer_pubkey} could not be deserialized").into(),
)
}
};

let program_len = account
.data
.len()
.saturating_sub(UpgradeableLoaderState::size_of_buffer_metadata());

Ok(program_len)
} else {
Err(format!("Buffer account {buffer_pubkey} not found, was it already consumed?",).into())
}
}

fn process_write_buffer(
rpc_client: Arc<RpcClient>,
config: &CliConfig,
Expand Down

0 comments on commit f41badf

Please sign in to comment.