Skip to content

Commit

Permalink
confirm successful tx for local cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
bw-solana committed Nov 5, 2024
1 parent 4e7f7f7 commit b53051a
Showing 1 changed file with 57 additions and 4 deletions.
61 changes: 57 additions & 4 deletions local-cluster/src/local_cluster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,61 @@ impl LocalCluster {
.into())
}

pub fn send_transaction_and_confirm_with_retries<T: Signers + ?Sized>(
client: &QuicTpuClient,
keypairs: &T,
transaction: &mut Transaction,
attempts: usize,
) -> std::result::Result<Signature, TransportError> {
for attempt in 0..attempts {
// Send the transaction to the upcoming leaders
client.send_transaction_to_upcoming_leaders(transaction)?;

// Check for signature confirmation
let signature = &transaction.signatures[0];
let now = Instant::now();
loop {
let Ok(signature_status) = client.rpc_client().get_signature_status(signature)
else {
error!("failed to get tx signature status");
continue;
};

let tx_status = match signature_status {
Some(tx_status) => tx_status,
None => {
trace!("tx signature not found");
if now.elapsed().as_secs() > MAX_PROCESSING_AGE as u64 {
error!("attempt {attempt} timed out waiting for tx signature");
break;
}
continue;
}
};

match tx_status {
Ok(_) => {
info!("tx signature confirmed");
return Ok(*signature);
}
Err(err) => {
error!("tx signature error: {err}");
break;
}
}
}

// Resend the transaction with updated blockhash
let blockhash = client.rpc_client().get_latest_blockhash()?;
transaction.sign(keypairs, blockhash);
}
Err(std::io::Error::new(
std::io::ErrorKind::Other,
"failed to confirm transaction".to_string(),
)
.into())
}

fn transfer_with_client(
client: &QuicTpuClient,
source_keypair: &Keypair,
Expand Down Expand Up @@ -802,12 +857,11 @@ impl LocalCluster {
.unwrap()
.0,
);
LocalCluster::send_transaction_with_retries(
LocalCluster::send_transaction_and_confirm_with_retries(
client,
&[from_account],
&mut transaction,
10,
0,
)
.expect("should fund vote");
client
Expand Down Expand Up @@ -838,12 +892,11 @@ impl LocalCluster {
.0,
);

LocalCluster::send_transaction_with_retries(
LocalCluster::send_transaction_and_confirm_with_retries(
client,
&[from_account.as_ref(), &stake_account_keypair],
&mut transaction,
5,
0,
)
.expect("should delegate stake");
client
Expand Down

0 comments on commit b53051a

Please sign in to comment.