cli: Fix IDL write getting corrupted from retries (#2964)

This commit is contained in:
Andrei Hrs 2024-05-27 22:46:12 +02:00 committed by GitHub
parent c614f108bb
commit bcf3862ef7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 44 additions and 18 deletions

View File

@ -34,6 +34,7 @@ The minor version will be incremented upon a breaking change and the patch versi
- lang: Fix `ProgramError::ArithmeticOverflow` not found error ([#2975](https://github.com/coral-xyz/anchor/pull/2975)). - lang: Fix `ProgramError::ArithmeticOverflow` not found error ([#2975](https://github.com/coral-xyz/anchor/pull/2975)).
- lang: Fix using optional accounts with `declare_program!` ([#2967](https://github.com/coral-xyz/anchor/pull/2967)). - lang: Fix using optional accounts with `declare_program!` ([#2967](https://github.com/coral-xyz/anchor/pull/2967)).
- lang: Fix instruction return type generation with `declare_program!` ([#2977](https://github.com/coral-xyz/anchor/pull/2977)). - lang: Fix instruction return type generation with `declare_program!` ([#2977](https://github.com/coral-xyz/anchor/pull/2977)).
- cli: Fix IDL write getting corrupted from retries ([#2964](https://github.com/coral-xyz/anchor/pull/2964)).
### Breaking ### Breaking

View File

@ -2331,8 +2331,11 @@ fn idl_set_buffer(
let instructions = prepend_compute_unit_ix(vec![ix], &client, priority_fee)?; let instructions = prepend_compute_unit_ix(vec![ix], &client, priority_fee)?;
// Send the transaction. // Send the transaction.
for retry_transactions in 0..20 { let mut latest_hash = client.get_latest_blockhash()?;
let latest_hash = client.get_latest_blockhash()?; for retries in 0..20 {
if !client.is_blockhash_valid(&latest_hash, client.commitment())? {
latest_hash = client.get_latest_blockhash()?;
}
let tx = Transaction::new_signed_with_payer( let tx = Transaction::new_signed_with_payer(
&instructions, &instructions,
Some(&keypair.pubkey()), Some(&keypair.pubkey()),
@ -2343,7 +2346,7 @@ fn idl_set_buffer(
match client.send_and_confirm_transaction_with_spinner(&tx) { match client.send_and_confirm_transaction_with_spinner(&tx) {
Ok(_) => break, Ok(_) => break,
Err(e) => { Err(e) => {
if retry_transactions == 19 { if retries == 19 {
return Err(anyhow!("Error: {e}. Failed to send transaction.")); return Err(anyhow!("Error: {e}. Failed to send transaction."));
} }
println!("Error: {e}. Retrying transaction."); println!("Error: {e}. Retrying transaction.");
@ -2591,8 +2594,11 @@ fn idl_write(
// Send transaction. // Send transaction.
let instructions = prepend_compute_unit_ix(vec![ix], &client, priority_fee)?; let instructions = prepend_compute_unit_ix(vec![ix], &client, priority_fee)?;
for retry_transactions in 0..20 { let mut latest_hash = client.get_latest_blockhash()?;
let latest_hash = client.get_latest_blockhash()?; for retries in 0..20 {
if !client.is_blockhash_valid(&latest_hash, client.commitment())? {
latest_hash = client.get_latest_blockhash()?;
}
let tx = Transaction::new_signed_with_payer( let tx = Transaction::new_signed_with_payer(
&instructions, &instructions,
Some(&keypair.pubkey()), Some(&keypair.pubkey()),
@ -2603,7 +2609,7 @@ fn idl_write(
match client.send_and_confirm_transaction_with_spinner(&tx) { match client.send_and_confirm_transaction_with_spinner(&tx) {
Ok(_) => break, Ok(_) => break,
Err(e) => { Err(e) => {
if retry_transactions == 19 { if retries == 19 {
return Err(anyhow!("Error: {e}. Failed to send transaction.")); return Err(anyhow!("Error: {e}. Failed to send transaction."));
} }
println!("Error: {e}. Retrying transaction."); println!("Error: {e}. Retrying transaction.");
@ -3779,16 +3785,31 @@ fn create_idl_account(
data, data,
}); });
} }
let latest_hash = client.get_latest_blockhash()?;
instructions = prepend_compute_unit_ix(instructions, &client, priority_fee)?; instructions = prepend_compute_unit_ix(instructions, &client, priority_fee)?;
let tx = Transaction::new_signed_with_payer( let mut latest_hash = client.get_latest_blockhash()?;
&instructions, for retries in 0..20 {
Some(&keypair.pubkey()), if !client.is_blockhash_valid(&latest_hash, client.commitment())? {
&[&keypair], latest_hash = client.get_latest_blockhash()?;
latest_hash, }
);
client.send_and_confirm_transaction_with_spinner(&tx)?; let tx = Transaction::new_signed_with_payer(
&instructions,
Some(&keypair.pubkey()),
&[&keypair],
latest_hash,
);
match client.send_and_confirm_transaction_with_spinner(&tx) {
Ok(_) => break,
Err(err) => {
if retries == 19 {
return Err(anyhow!("Error creating IDL account: {}", err));
}
println!("Error creating IDL account: {}. Retrying...", err);
}
}
}
} }
// Write directly to the IDL account buffer. // Write directly to the IDL account buffer.
@ -3851,8 +3872,11 @@ fn create_idl_buffer(
priority_fee, priority_fee,
)?; )?;
for retries in 0..5 { let mut latest_hash = client.get_latest_blockhash()?;
let latest_hash = client.get_latest_blockhash()?; for retries in 0..20 {
if !client.is_blockhash_valid(&latest_hash, client.commitment())? {
latest_hash = client.get_latest_blockhash()?;
}
let tx = Transaction::new_signed_with_payer( let tx = Transaction::new_signed_with_payer(
&instructions, &instructions,
Some(&keypair.pubkey()), Some(&keypair.pubkey()),
@ -3862,7 +3886,7 @@ fn create_idl_buffer(
match client.send_and_confirm_transaction_with_spinner(&tx) { match client.send_and_confirm_transaction_with_spinner(&tx) {
Ok(_) => break, Ok(_) => break,
Err(err) => { Err(err) => {
if retries == 4 { if retries == 19 {
return Err(anyhow!("Error creating buffer account: {}", err)); return Err(anyhow!("Error creating buffer account: {}", err));
} }
println!("Error creating buffer account: {}. Retrying...", err); println!("Error creating buffer account: {}. Retrying...", err);
@ -4517,7 +4541,8 @@ fn get_recommended_micro_lamport_fee(client: &RpcClient, priority_fee: Option<u6
Ok(median_priority_fee) Ok(median_priority_fee)
} }
/// Prepend a compute unit ix, if the priority fee is greater than 0.
/// This helps to improve the chances that the transaction will land.
fn prepend_compute_unit_ix( fn prepend_compute_unit_ix(
instructions: Vec<Instruction>, instructions: Vec<Instruction>,
client: &RpcClient, client: &RpcClient,