cli: Fix deploy program length calculation when using buffers (#25132)

This commit is contained in:
Justin Starry 2022-05-12 09:05:41 +08:00 committed by GitHub
parent 0514e83fa2
commit 05de0e3155
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 45 additions and 17 deletions

View File

@ -924,25 +924,53 @@ fn process_program_deploy(
.get_account_with_commitment(&buffer_pubkey, config.commitment)? .get_account_with_commitment(&buffer_pubkey, config.commitment)?
.value .value
{ {
if let Ok(UpgradeableLoaderState::Buffer { if !bpf_loader_upgradeable::check_id(&account.owner) {
authority_address: _, return Err(format!(
}) = account.state() "Buffer account {buffer_pubkey} is not owned by the BPF Upgradeable Loader",
{ )
} else { .into());
return Err(format!("Buffer account {} is not initialized", buffer_pubkey).into());
} }
(vec![], account.data.len())
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 { } else {
return Err(format!( return Err(format!(
"Buffer account {} not found, was it already consumed?", "Buffer account {buffer_pubkey} not found, was it already consumed?",
buffer_pubkey
) )
.into()); .into());
} }
} else { } else {
return Err("Program location required if buffer not supplied".into()); return Err("Program location required if buffer not supplied".into());
}; };
let buffer_data_len = program_len;
let programdata_len = if let Some(len) = max_len { let programdata_len = if let Some(len) = max_len {
if program_len > len { if program_len > len {
return Err("Max length specified not large enough".into()); return Err("Max length specified not large enough".into());
@ -954,7 +982,7 @@ fn process_program_deploy(
program_len * 2 program_len * 2
}; };
let minimum_balance = rpc_client.get_minimum_balance_for_rent_exemption( let minimum_balance = rpc_client.get_minimum_balance_for_rent_exemption(
UpgradeableLoaderState::size_of_programdata(buffer_data_len), UpgradeableLoaderState::size_of_programdata(program_len),
)?; )?;
let result = if do_deploy { let result = if do_deploy {
@ -967,7 +995,7 @@ fn process_program_deploy(
rpc_client.clone(), rpc_client.clone(),
config, config,
&program_data, &program_data,
buffer_data_len, program_len,
programdata_len, programdata_len,
minimum_balance, minimum_balance,
&bpf_loader_upgradeable::id(), &bpf_loader_upgradeable::id(),
@ -1729,7 +1757,7 @@ fn do_process_program_write_and_deploy(
rpc_client: Arc<RpcClient>, rpc_client: Arc<RpcClient>,
config: &CliConfig, config: &CliConfig,
program_data: &[u8], program_data: &[u8],
buffer_data_len: usize, program_len: usize,
programdata_len: usize, programdata_len: usize,
minimum_balance: u64, minimum_balance: u64,
loader_id: &Pubkey, loader_id: &Pubkey,
@ -1757,9 +1785,9 @@ fn do_process_program_write_and_deploy(
buffer_pubkey, buffer_pubkey,
&account, &account,
if loader_id == &bpf_loader_upgradeable::id() { if loader_id == &bpf_loader_upgradeable::id() {
UpgradeableLoaderState::size_of_buffer(buffer_data_len) UpgradeableLoaderState::size_of_buffer(program_len)
} else { } else {
buffer_data_len program_len
}, },
minimum_balance, minimum_balance,
allow_excessive_balance, allow_excessive_balance,
@ -1771,7 +1799,7 @@ fn do_process_program_write_and_deploy(
buffer_pubkey, buffer_pubkey,
&buffer_authority_signer.pubkey(), &buffer_authority_signer.pubkey(),
minimum_balance, minimum_balance,
buffer_data_len, program_len,
)?, )?,
minimum_balance, minimum_balance,
) )
@ -1781,7 +1809,7 @@ fn do_process_program_write_and_deploy(
&config.signers[0].pubkey(), &config.signers[0].pubkey(),
buffer_pubkey, buffer_pubkey,
minimum_balance, minimum_balance,
buffer_data_len as u64, program_len as u64,
loader_id, loader_id,
)], )],
minimum_balance, minimum_balance,