From cb50877bbf89cf92e94249ea8f47ab35782cefdc Mon Sep 17 00:00:00 2001 From: Michael Vines Date: Sat, 9 May 2020 09:06:32 -0700 Subject: [PATCH] send_and_confirm_transaction() no longer needs a keypair (#9950) --- cli/src/cli.rs | 33 ++++++++---------------- cli/src/nonce.rs | 9 +++---- cli/src/stake.rs | 14 +++++------ cli/src/storage.rs | 4 +-- cli/src/validator_info.rs | 2 +- cli/src/vote.rs | 10 +++----- client/src/rpc_client.rs | 51 +++++++++++++++++--------------------- install/src/command.rs | 7 +++--- ramp-tps/src/voters.rs | 7 ++---- stake-accounts/src/main.rs | 2 +- 10 files changed, 58 insertions(+), 81 deletions(-) diff --git a/cli/src/cli.rs b/cli/src/cli.rs index f82c0aa738..9789fc102f 100644 --- a/cli/src/cli.rs +++ b/cli/src/cli.rs @@ -1343,8 +1343,7 @@ fn process_deploy( )?; trace!("Creating program account"); - let result = - rpc_client.send_and_confirm_transaction_with_spinner(&mut create_account_tx, &signers); + let result = rpc_client.send_and_confirm_transaction_with_spinner(&create_account_tx); log_instruction_custom_error::(result, &config).map_err(|_| { CliError::DynamicProgramError("Program account allocation failed".to_string()) })?; @@ -1358,7 +1357,7 @@ fn process_deploy( trace!("Finalizing program account"); rpc_client - .send_and_confirm_transaction_with_spinner(&mut finalize_tx, &signers) + .send_and_confirm_transaction_with_spinner(&finalize_tx) .map_err(|e| { CliError::DynamicProgramError(format!("Finalizing program account failed: {}", e)) })?; @@ -1423,8 +1422,7 @@ fn process_pay( &fee_calculator, &tx.message, )?; - let result = - rpc_client.send_and_confirm_transaction_with_spinner(&mut tx, &config.signers); + let result = rpc_client.send_and_confirm_transaction_with_spinner(&tx); log_instruction_custom_error::(result, &config) } } else if *witnesses == None { @@ -1459,10 +1457,7 @@ fn process_pay( &fee_calculator, &tx.message, )?; - let result = rpc_client.send_and_confirm_transaction_with_spinner( - &mut tx, - &[config.signers[0], &contract_state], - ); + let result = rpc_client.send_and_confirm_transaction_with_spinner(&tx); let signature = log_instruction_custom_error::(result, &config)?; Ok(json!({ "signature": signature, @@ -1498,10 +1493,7 @@ fn process_pay( return_signers(&tx, &config) } else { tx.try_sign(&[config.signers[0], &contract_state], blockhash)?; - let result = rpc_client.send_and_confirm_transaction_with_spinner( - &mut tx, - &[config.signers[0], &contract_state], - ); + let result = rpc_client.send_and_confirm_transaction_with_spinner(&tx); check_account_for_fee( rpc_client, &config.signers[0].pubkey(), @@ -1536,8 +1528,7 @@ fn process_cancel(rpc_client: &RpcClient, config: &CliConfig, pubkey: &Pubkey) - &fee_calculator, &tx.message, )?; - let result = - rpc_client.send_and_confirm_transaction_with_spinner(&mut tx, &[config.signers[0]]); + let result = rpc_client.send_and_confirm_transaction_with_spinner(&tx); log_instruction_custom_error::(result, &config) } @@ -1560,8 +1551,7 @@ fn process_time_elapsed( &fee_calculator, &tx.message, )?; - let result = - rpc_client.send_and_confirm_transaction_with_spinner(&mut tx, &[config.signers[0]]); + let result = rpc_client.send_and_confirm_transaction_with_spinner(&tx); log_instruction_custom_error::(result, &config) } @@ -1623,7 +1613,7 @@ fn process_transfer( let result = if no_wait { rpc_client.send_transaction(&tx) } else { - rpc_client.send_and_confirm_transaction_with_spinner(&mut tx, &config.signers) + rpc_client.send_and_confirm_transaction_with_spinner(&tx) }; log_instruction_custom_error::(result, &config) } @@ -1647,8 +1637,7 @@ fn process_witness( &fee_calculator, &tx.message, )?; - let result = - rpc_client.send_and_confirm_transaction_with_spinner(&mut tx, &[config.signers[0]]); + let result = rpc_client.send_and_confirm_transaction_with_spinner(&tx); log_instruction_custom_error::(result, &config) } @@ -2279,8 +2268,8 @@ pub fn request_and_confirm_airdrop( sleep(Duration::from_secs(1)); } }?; - let mut tx = keypair.airdrop_transaction(); - let result = rpc_client.send_and_confirm_transaction_with_spinner(&mut tx, &[&keypair]); + let tx = keypair.airdrop_transaction(); + let result = rpc_client.send_and_confirm_transaction_with_spinner(&tx); log_instruction_custom_error::(result, &config) } diff --git a/cli/src/nonce.rs b/cli/src/nonce.rs index 2181bde65a..ea66753125 100644 --- a/cli/src/nonce.rs +++ b/cli/src/nonce.rs @@ -446,7 +446,7 @@ pub fn process_authorize_nonce_account( &fee_calculator, &tx.message, )?; - let result = rpc_client.send_and_confirm_transaction_with_spinner(&mut tx, &config.signers); + let result = rpc_client.send_and_confirm_transaction_with_spinner(&tx); log_instruction_custom_error::(result, &config) } @@ -523,7 +523,7 @@ pub fn process_create_nonce_account( &fee_calculator, &tx.message, )?; - let result = rpc_client.send_and_confirm_transaction_with_spinner(&mut tx, &config.signers); + let result = rpc_client.send_and_confirm_transaction_with_spinner(&tx); log_instruction_custom_error::(result, &config) } @@ -564,8 +564,7 @@ pub fn process_new_nonce( &fee_calculator, &tx.message, )?; - let result = rpc_client - .send_and_confirm_transaction_with_spinner(&mut tx, &[config.signers[0], nonce_authority]); + let result = rpc_client.send_and_confirm_transaction_with_spinner(&tx); log_instruction_custom_error::(result, &config) } @@ -624,7 +623,7 @@ pub fn process_withdraw_from_nonce_account( &fee_calculator, &tx.message, )?; - let result = rpc_client.send_and_confirm_transaction_with_spinner(&mut tx, &config.signers); + let result = rpc_client.send_and_confirm_transaction_with_spinner(&tx); log_instruction_custom_error::(result, &config) } diff --git a/cli/src/stake.rs b/cli/src/stake.rs index a7d7626822..3f540e401b 100644 --- a/cli/src/stake.rs +++ b/cli/src/stake.rs @@ -869,7 +869,7 @@ pub fn process_create_stake_account( &fee_calculator, &tx.message, )?; - let result = rpc_client.send_and_confirm_transaction_with_spinner(&mut tx, &config.signers); + let result = rpc_client.send_and_confirm_transaction_with_spinner(&tx); log_instruction_custom_error::(result, &config) } } @@ -934,7 +934,7 @@ pub fn process_stake_authorize( &fee_calculator, &tx.message, )?; - let result = rpc_client.send_and_confirm_transaction_with_spinner(&mut tx, &config.signers); + let result = rpc_client.send_and_confirm_transaction_with_spinner(&tx); log_instruction_custom_error::(result, &config) } } @@ -988,7 +988,7 @@ pub fn process_deactivate_stake_account( &fee_calculator, &tx.message, )?; - let result = rpc_client.send_and_confirm_transaction_with_spinner(&mut tx, &config.signers); + let result = rpc_client.send_and_confirm_transaction_with_spinner(&tx); log_instruction_custom_error::(result, &config) } } @@ -1051,7 +1051,7 @@ pub fn process_withdraw_stake( &fee_calculator, &tx.message, )?; - let result = rpc_client.send_and_confirm_transaction_with_spinner(&mut tx, &config.signers); + let result = rpc_client.send_and_confirm_transaction_with_spinner(&tx); log_instruction_custom_error::(result, &config) } } @@ -1185,7 +1185,7 @@ pub fn process_split_stake( &fee_calculator, &tx.message, )?; - let result = rpc_client.send_and_confirm_transaction_with_spinner(&mut tx, &config.signers); + let result = rpc_client.send_and_confirm_transaction_with_spinner(&tx); log_instruction_custom_error::(result, &config) } } @@ -1242,7 +1242,7 @@ pub fn process_stake_set_lockup( &fee_calculator, &tx.message, )?; - let result = rpc_client.send_and_confirm_transaction_with_spinner(&mut tx, &config.signers); + let result = rpc_client.send_and_confirm_transaction_with_spinner(&tx); log_instruction_custom_error::(result, &config) } } @@ -1450,7 +1450,7 @@ pub fn process_delegate_stake( &fee_calculator, &tx.message, )?; - let result = rpc_client.send_and_confirm_transaction_with_spinner(&mut tx, &config.signers); + let result = rpc_client.send_and_confirm_transaction_with_spinner(&tx); log_instruction_custom_error::(result, &config) } } diff --git a/cli/src/storage.rs b/cli/src/storage.rs index 0866705d0d..377456f0b1 100644 --- a/cli/src/storage.rs +++ b/cli/src/storage.rs @@ -242,7 +242,7 @@ pub fn process_create_storage_account( &fee_calculator, &tx.message, )?; - let result = rpc_client.send_and_confirm_transaction_with_spinner(&mut tx, &config.signers); + let result = rpc_client.send_and_confirm_transaction_with_spinner(&tx); log_instruction_custom_error::(result, &config) } @@ -266,7 +266,7 @@ pub fn process_claim_storage_reward( &fee_calculator, &tx.message, )?; - let signature = rpc_client.send_and_confirm_transaction_with_spinner(&mut tx, &signers)?; + let signature = rpc_client.send_and_confirm_transaction_with_spinner(&tx)?; Ok(signature.to_string()) } diff --git a/cli/src/validator_info.rs b/cli/src/validator_info.rs index 0a7ce48813..4f2927e7d4 100644 --- a/cli/src/validator_info.rs +++ b/cli/src/validator_info.rs @@ -367,7 +367,7 @@ pub fn process_set_validator_info( &fee_calculator, &tx.message, )?; - let signature_str = rpc_client.send_and_confirm_transaction_with_spinner(&mut tx, &signers)?; + let signature_str = rpc_client.send_and_confirm_transaction_with_spinner(&tx)?; println!("Success! Validator info published at: {:?}", info_pubkey); println!("{}", signature_str); diff --git a/cli/src/vote.rs b/cli/src/vote.rs index e4e781bddb..07c8060ed2 100644 --- a/cli/src/vote.rs +++ b/cli/src/vote.rs @@ -437,7 +437,7 @@ pub fn process_create_vote_account( &fee_calculator, &tx.message, )?; - let result = rpc_client.send_and_confirm_transaction_with_spinner(&mut tx, &config.signers); + let result = rpc_client.send_and_confirm_transaction_with_spinner(&tx); log_instruction_custom_error::(result, &config) } @@ -477,8 +477,7 @@ pub fn process_vote_authorize( &fee_calculator, &tx.message, )?; - let result = - rpc_client.send_and_confirm_transaction_with_spinner(&mut tx, &[config.signers[0]]); + let result = rpc_client.send_and_confirm_transaction_with_spinner(&tx); log_instruction_custom_error::(result, &config) } @@ -511,7 +510,7 @@ pub fn process_vote_update_validator( &fee_calculator, &tx.message, )?; - let result = rpc_client.send_and_confirm_transaction_with_spinner(&mut tx, &config.signers); + let result = rpc_client.send_and_confirm_transaction_with_spinner(&tx); log_instruction_custom_error::(result, &config) } @@ -616,8 +615,7 @@ pub fn process_withdraw_from_vote_account( &fee_calculator, &transaction.message, )?; - let result = - rpc_client.send_and_confirm_transaction_with_spinner(&mut transaction, &config.signers); + let result = rpc_client.send_and_confirm_transaction_with_spinner(&transaction); log_instruction_custom_error::(result, &config) } diff --git a/client/src/rpc_client.rs b/client/src/rpc_client.rs index 6831a71b7c..39079c3682 100644 --- a/client/src/rpc_client.rs +++ b/client/src/rpc_client.rs @@ -24,7 +24,7 @@ use solana_sdk::{ pubkey::Pubkey, signature::Signature, signers::Signers, - transaction::{self, Transaction, TransactionError}, + transaction::{self, Transaction}, }; use solana_transaction_status::{ ConfirmedBlock, ConfirmedTransaction, TransactionEncoding, TransactionStatus, @@ -449,10 +449,9 @@ impl RpcClient { .map_err(|err| ClientError::new_with_command(err.into(), "MinimumLedgerSlot")) } - pub fn send_and_confirm_transaction( + pub fn send_and_confirm_transaction( &self, - transaction: &mut Transaction, - signer_keys: &T, + transaction: &Transaction, ) -> ClientResult { let mut send_retries = 20; loop { @@ -476,11 +475,6 @@ impl RpcClient { send_retries = if let Some(result) = status.clone() { match result { Ok(_) => return Ok(signature), - Err(TransactionError::AccountInUse) => { - // Fetch a new blockhash and re-sign the transaction before sending it again - self.resign_transaction(transaction, signer_keys)?; - send_retries - 1 - } Err(_) => 0, } } else { @@ -491,7 +485,9 @@ impl RpcClient { return Err(err.unwrap_err().into()); } else { return Err( - RpcError::ForUser("unable to confirm transaction. This can happen in situations such as transaction expiration and insufficient fee-payer funds".to_string()).into(), + RpcError::ForUser("unable to confirm transaction. \ + This can happen in situations such as transaction expiration \ + and insufficient fee-payer funds".to_string()).into(), ); } } @@ -1088,10 +1084,9 @@ impl RpcClient { Ok(confirmations) } - pub fn send_and_confirm_transaction_with_spinner( + pub fn send_and_confirm_transaction_with_spinner( &self, - transaction: &mut Transaction, - signer_keys: &T, + transaction: &Transaction, ) -> ClientResult { let mut confirmations = 0; @@ -1128,11 +1123,6 @@ impl RpcClient { send_retries = if let Some(result) = status.clone() { match result { Ok(_) => 0, - Err(TransactionError::AccountInUse) => { - // Fetch a new blockhash and re-sign the transaction before sending it again - self.resign_transaction(transaction, signer_keys)?; - send_retries - 1 - } // If transaction errors, return right away; no point in counting confirmations Err(_) => 0, } @@ -1150,9 +1140,13 @@ impl RpcClient { } } } else { - return Err( - RpcError::ForUser("unable to confirm transaction. This can happen in situations such as transaction expiration and insufficient fee-payer funds".to_string()).into(), - ); + return Err(RpcError::ForUser( + "unable to confirm transaction. \ + This can happen in situations such as transaction \ + expiration and insufficient fee-payer funds" + .to_string(), + ) + .into()); } } }; @@ -1177,7 +1171,9 @@ impl RpcClient { .unwrap_or(confirmations); if now.elapsed().as_secs() >= MAX_HASH_AGE_IN_SECONDS as u64 { return Err( - RpcError::ForUser("transaction not finalized. This can happen when a transaction lands in an abandoned fork. Please retry.".to_string()).into(), + RpcError::ForUser("transaction not finalized. \ + This can happen when a transaction lands in an abandoned fork. \ + Please retry.".to_string()).into(), ); } } @@ -1380,17 +1376,16 @@ mod tests { let key = Keypair::new(); let to = Pubkey::new_rand(); let blockhash = Hash::default(); - let mut tx = system_transaction::transfer(&key, &to, 50, blockhash); - - let result = rpc_client.send_and_confirm_transaction(&mut tx, &[&key]); + let tx = system_transaction::transfer(&key, &to, 50, blockhash); + let result = rpc_client.send_and_confirm_transaction(&tx); result.unwrap(); let rpc_client = RpcClient::new_mock("account_in_use".to_string()); - let result = rpc_client.send_and_confirm_transaction(&mut tx, &[&key]); + let result = rpc_client.send_and_confirm_transaction(&tx); assert!(result.is_err()); let rpc_client = RpcClient::new_mock("instruction_error".to_string()); - let result = rpc_client.send_and_confirm_transaction(&mut tx, &[&key]); + let result = rpc_client.send_and_confirm_transaction(&tx); assert_matches!( result.unwrap_err().kind(), ClientErrorKind::TransactionError(TransactionError::InstructionError( @@ -1400,7 +1395,7 @@ mod tests { ); let rpc_client = RpcClient::new_mock("sig_not_found".to_string()); - let result = rpc_client.send_and_confirm_transaction(&mut tx, &[&key]); + let result = rpc_client.send_and_confirm_transaction(&tx); if let ClientErrorKind::Io(err) = result.unwrap_err().kind() { assert_eq!(err.kind(), io::ErrorKind::Other); } diff --git a/install/src/command.rs b/install/src/command.rs index 080ee67bf2..a74ed828f0 100644 --- a/install/src/command.rs +++ b/install/src/command.rs @@ -221,8 +221,7 @@ fn new_update_manifest( let mut transaction = Transaction::new_unsigned_instructions(&instructions); let signers = [from_keypair, update_manifest_keypair]; transaction.sign(&signers, recent_blockhash); - - rpc_client.send_and_confirm_transaction(&mut transaction, &[from_keypair])?; + rpc_client.send_and_confirm_transaction(&transaction)?; } Ok(()) } @@ -245,8 +244,8 @@ fn store_update_manifest( ); let message = Message::new_with_payer(&[instruction], Some(&from_keypair.pubkey())); - let mut transaction = Transaction::new(&signers, message, recent_blockhash); - rpc_client.send_and_confirm_transaction(&mut transaction, &[from_keypair])?; + let transaction = Transaction::new(&signers, message, recent_blockhash); + rpc_client.send_and_confirm_transaction(&transaction)?; Ok(()) } diff --git a/ramp-tps/src/voters.rs b/ramp-tps/src/voters.rs index 6088bb871d..2eae40a822 100644 --- a/ramp-tps/src/voters.rs +++ b/ramp-tps/src/voters.rs @@ -138,7 +138,7 @@ fn delegate_stake( } }; - let mut transaction = Transaction::new_signed_instructions( + let transaction = Transaction::new_signed_instructions( &[faucet_keypair, &stake_account_keypair], &stake_instruction::create_account_and_delegate_stake( &faucet_keypair.pubkey(), @@ -160,10 +160,7 @@ fn delegate_stake( } } - if let Err(err) = rpc_client.send_and_confirm_transaction( - &mut transaction, - &[faucet_keypair, &stake_account_keypair], - ) { + if let Err(err) = rpc_client.send_and_confirm_transaction(&transaction) { error!( "Failed to delegate stake (retries: {}): {}", retry_count, err diff --git a/stake-accounts/src/main.rs b/stake-accounts/src/main.rs index 2e83ac7b94..a5590debcc 100644 --- a/stake-accounts/src/main.rs +++ b/stake-accounts/src/main.rs @@ -214,7 +214,7 @@ fn send_message( if no_wait { client.send_transaction(&transaction) } else { - client.send_and_confirm_transaction_with_spinner(&mut transaction, signers) + client.send_and_confirm_transaction_with_spinner(&transaction) } }