cli: Update `simulate_and_update_compute_unit_limit` to take `Message` (#839)

Update `simulate_and_update_compute_unit_limit` to take `Message`
This commit is contained in:
Jon C 2024-04-17 12:36:58 +02:00 committed by GitHub
parent 6b52ca29c1
commit 3bb7ab7360
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 38 additions and 40 deletions

View File

@ -6,6 +6,7 @@ use {
borsh1::try_from_slice_unchecked,
compute_budget::{self, ComputeBudgetInstruction},
instruction::Instruction,
message::Message,
transaction::Transaction,
},
};
@ -21,32 +22,33 @@ pub(crate) enum UpdateComputeUnitLimitResult {
// Returns the index of the compute unit limit instruction
pub(crate) fn simulate_and_update_compute_unit_limit(
rpc_client: &RpcClient,
transaction: &mut Transaction,
message: &mut Message,
) -> Result<UpdateComputeUnitLimitResult, Box<dyn std::error::Error>> {
let Some(compute_unit_limit_ix_index) = transaction
.message
.instructions
.iter()
.enumerate()
.find_map(|(ix_index, instruction)| {
let ix_program_id = transaction.message.program_id(ix_index)?;
if ix_program_id != &compute_budget::id() {
return None;
}
let Some(compute_unit_limit_ix_index) =
message
.instructions
.iter()
.enumerate()
.find_map(|(ix_index, instruction)| {
let ix_program_id = message.program_id(ix_index)?;
if ix_program_id != &compute_budget::id() {
return None;
}
matches!(
try_from_slice_unchecked(&instruction.data),
Ok(ComputeBudgetInstruction::SetComputeUnitLimit(_))
)
.then_some(ix_index)
})
matches!(
try_from_slice_unchecked(&instruction.data),
Ok(ComputeBudgetInstruction::SetComputeUnitLimit(_))
)
.then_some(ix_index)
})
else {
return Ok(UpdateComputeUnitLimitResult::NoInstructionFound);
};
let transaction = Transaction::new_unsigned(message.clone());
let simulate_result = rpc_client
.simulate_transaction_with_config(
transaction,
&transaction,
RpcSimulateTransactionConfig {
replace_recent_blockhash: true,
commitment: Some(rpc_client.commitment()),
@ -66,7 +68,7 @@ pub(crate) fn simulate_and_update_compute_unit_limit(
// Overwrite the compute unit limit instruction with the actual units consumed
let compute_unit_limit = u32::try_from(units_consumed)?;
transaction.message.instructions[compute_unit_limit_ix_index].data =
message.instructions[compute_unit_limit_ix_index].data =
ComputeBudgetInstruction::set_compute_unit_limit(compute_unit_limit).data;
Ok(UpdateComputeUnitLimitResult::UpdatedInstructionIndex(

View File

@ -2416,9 +2416,9 @@ fn do_process_program_write_and_deploy(
let final_tx_sig = send_deploy_messages(
rpc_client,
config,
&initial_message,
&write_messages,
&final_message,
initial_message,
write_messages,
final_message,
fee_payer_signer,
buffer_signer,
Some(buffer_authority_signer),
@ -2565,9 +2565,9 @@ fn do_process_program_upgrade(
let final_tx_sig = send_deploy_messages(
rpc_client,
config,
&initial_message,
&write_messages,
&final_message,
initial_message,
write_messages,
final_message,
fee_payer_signer,
buffer_signer,
Some(upgrade_authority),
@ -2696,22 +2696,20 @@ fn check_payer(
fn send_deploy_messages(
rpc_client: Arc<RpcClient>,
config: &CliConfig,
initial_message: &Option<Message>,
write_messages: &[Message],
final_message: &Option<Message>,
initial_message: Option<Message>,
mut write_messages: Vec<Message>,
final_message: Option<Message>,
fee_payer_signer: &dyn Signer,
initial_signer: Option<&dyn Signer>,
write_signer: Option<&dyn Signer>,
final_signers: Option<&[&dyn Signer]>,
max_sign_attempts: usize,
) -> Result<Option<Signature>, Box<dyn std::error::Error>> {
if let Some(message) = initial_message {
if let Some(mut message) = initial_message {
if let Some(initial_signer) = initial_signer {
trace!("Preparing the required accounts");
simulate_and_update_compute_unit_limit(&rpc_client, &mut message)?;
let mut initial_transaction = Transaction::new_unsigned(message.clone());
simulate_and_update_compute_unit_limit(&rpc_client, &mut initial_transaction)?;
let blockhash = rpc_client.get_latest_blockhash()?;
// Most of the initial_transaction combinations require both the fee-payer and new program
@ -2738,11 +2736,10 @@ fn send_deploy_messages(
// Simulate the first write message to get the number of compute units
// consumed and then reuse that value as the compute unit limit for all
// write messages.
let mut write_messages = write_messages.to_vec();
{
let mut transaction = Transaction::new_unsigned(write_messages[0].clone());
let mut message = write_messages[0].clone();
if let UpdateComputeUnitLimitResult::UpdatedInstructionIndex(ix_index) =
simulate_and_update_compute_unit_limit(&rpc_client, &mut transaction)?
simulate_and_update_compute_unit_limit(&rpc_client, &mut message)?
{
for msg in &mut write_messages {
// Write messages are all assumed to be identical except
@ -2752,7 +2749,7 @@ fn send_deploy_messages(
// instruction.
assert_eq!(msg.program_id(ix_index), Some(&compute_budget::id()));
msg.instructions[ix_index].data =
transaction.message.instructions[ix_index].data.clone();
message.instructions[ix_index].data.clone();
}
}
}
@ -2813,13 +2810,12 @@ fn send_deploy_messages(
}
}
if let Some(message) = final_message {
if let Some(mut message) = final_message {
if let Some(final_signers) = final_signers {
trace!("Deploying program");
let mut final_tx = Transaction::new_unsigned(message.clone());
simulate_and_update_compute_unit_limit(&rpc_client, &mut final_tx)?;
simulate_and_update_compute_unit_limit(&rpc_client, &mut message)?;
let mut final_tx = Transaction::new_unsigned(message);
let blockhash = rpc_client.get_latest_blockhash()?;
let mut signers = final_signers.to_vec();
signers.push(fee_payer_signer);