From 214b561a286936b025deb27f00b9a942d352a3c3 Mon Sep 17 00:00:00 2001 From: Jon Cinque Date: Fri, 24 Dec 2021 15:44:19 +0100 Subject: [PATCH] banks-client: Update return type to `BanksClientError` (#22058) --- banks-client/src/lib.rs | 104 +++++++++++++++++++--------------------- 1 file changed, 48 insertions(+), 56 deletions(-) diff --git a/banks-client/src/lib.rs b/banks-client/src/lib.rs index 101a78422b4945..520ae0e09a3d1a 100644 --- a/banks-client/src/lib.rs +++ b/banks-client/src/lib.rs @@ -21,9 +21,7 @@ use { message::Message, signature::Signature, transaction::{self, Transaction}, - transport, }, - std::io, tarpc::{ client::{self, NewClient, RequestDispatch}, context::{self, Context}, @@ -60,10 +58,9 @@ impl BanksClient { &mut self, ctx: Context, transaction: Transaction, - ) -> impl Future> + '_ { + ) -> impl Future> + '_ { self.inner .send_transaction_with_context(ctx, transaction) - .map_err(BanksClientError::from) // Remove this when return Err type updated to BanksClientError .map_err(Into::into) } @@ -75,11 +72,10 @@ impl BanksClient { &mut self, ctx: Context, commitment: CommitmentLevel, - ) -> impl Future> + '_ { + ) -> impl Future> + '_ { #[allow(deprecated)] self.inner .get_fees_with_commitment_and_context(ctx, commitment) - .map_err(BanksClientError::from) // Remove this when return Err type updated to BanksClientError .map_err(Into::into) } @@ -87,10 +83,9 @@ impl BanksClient { &mut self, ctx: Context, signature: Signature, - ) -> impl Future>> + '_ { + ) -> impl Future, BanksClientError>> + '_ { self.inner .get_transaction_status_with_context(ctx, signature) - .map_err(BanksClientError::from) // Remove this when return Err type updated to BanksClientError .map_err(Into::into) } @@ -98,10 +93,9 @@ impl BanksClient { &mut self, ctx: Context, commitment: CommitmentLevel, - ) -> impl Future> + '_ { + ) -> impl Future> + '_ { self.inner .get_slot_with_context(ctx, commitment) - .map_err(BanksClientError::from) // Remove this when return Err type updated to BanksClientError .map_err(Into::into) } @@ -109,10 +103,9 @@ impl BanksClient { &mut self, ctx: Context, commitment: CommitmentLevel, - ) -> impl Future> + '_ { + ) -> impl Future> + '_ { self.inner .get_block_height_with_context(ctx, commitment) - .map_err(BanksClientError::from) // Remove this when return Err type updated to BanksClientError .map_err(Into::into) } @@ -121,10 +114,9 @@ impl BanksClient { ctx: Context, transaction: Transaction, commitment: CommitmentLevel, - ) -> impl Future>>> + '_ { + ) -> impl Future>, BanksClientError>> + '_ { self.inner .process_transaction_with_commitment_and_context(ctx, transaction, commitment) - .map_err(BanksClientError::from) // Remove this when return Err type updated to BanksClientError .map_err(Into::into) } @@ -133,10 +125,9 @@ impl BanksClient { ctx: Context, address: Pubkey, commitment: CommitmentLevel, - ) -> impl Future>> + '_ { + ) -> impl Future, BanksClientError>> + '_ { self.inner .get_account_with_commitment_and_context(ctx, address, commitment) - .map_err(BanksClientError::from) // Remove this when return Err type updated to BanksClientError .map_err(Into::into) } @@ -146,7 +137,7 @@ impl BanksClient { pub fn send_transaction( &mut self, transaction: Transaction, - ) -> impl Future> + '_ { + ) -> impl Future> + '_ { self.send_transaction_with_context(context::current(), transaction) } @@ -159,27 +150,25 @@ impl BanksClient { )] pub fn get_fees( &mut self, - ) -> impl Future> + '_ { + ) -> impl Future> + '_ { #[allow(deprecated)] self.get_fees_with_commitment_and_context(context::current(), CommitmentLevel::default()) } /// Return the cluster Sysvar - pub fn get_sysvar(&mut self) -> impl Future> + '_ { + pub fn get_sysvar( + &mut self, + ) -> impl Future> + '_ { self.get_account(T::id()).map(|result| { - let sysvar = result? - .ok_or(BanksClientError::ClientError("Sysvar not present")) - .map_err(io::Error::from)?; // Remove this map when return Err type updated to BanksClientError - from_account::(&sysvar) - .ok_or(BanksClientError::ClientError( - "Failed to deserialize sysvar", - )) - .map_err(Into::into) // Remove this when return Err type updated to BanksClientError + let sysvar = result?.ok_or(BanksClientError::ClientError("Sysvar not present"))?; + from_account::(&sysvar).ok_or(BanksClientError::ClientError( + "Failed to deserialize sysvar", + )) }) } /// Return the cluster rent - pub fn get_rent(&mut self) -> impl Future> + '_ { + pub fn get_rent(&mut self) -> impl Future> + '_ { self.get_sysvar::() } @@ -187,7 +176,9 @@ impl BanksClient { /// transactions with a blockhash that has not yet expired. Use the `get_fees` /// method to get both a blockhash and the blockhash's last valid slot. #[deprecated(since = "1.9.0", note = "Please use `get_latest_blockhash` instead")] - pub fn get_recent_blockhash(&mut self) -> impl Future> + '_ { + pub fn get_recent_blockhash( + &mut self, + ) -> impl Future> + '_ { #[allow(deprecated)] self.get_fees().map(|result| Ok(result?.1)) } @@ -198,7 +189,7 @@ impl BanksClient { &mut self, transaction: Transaction, commitment: CommitmentLevel, - ) -> impl Future> + '_ { + ) -> impl Future> + '_ { let mut ctx = context::current(); ctx.deadline += Duration::from_secs(50); self.process_transaction_with_commitment_and_context(ctx, transaction, commitment) @@ -208,14 +199,13 @@ impl BanksClient { )), Some(transaction_result) => Ok(transaction_result?), }) - .map_err(Into::into) // Remove this when return Err type updated to BanksClientError } /// Send a transaction and return until the transaction has been finalized or rejected. pub fn process_transaction( &mut self, transaction: Transaction, - ) -> impl Future> + '_ { + ) -> impl Future> + '_ { self.process_transaction_with_commitment(transaction, CommitmentLevel::default()) } @@ -223,7 +213,7 @@ impl BanksClient { &mut self, transactions: Vec, commitment: CommitmentLevel, - ) -> transport::Result<()> { + ) -> Result<(), BanksClientError> { let mut clients: Vec<_> = transactions.iter().map(|_| self.clone()).collect(); let futures = clients .iter_mut() @@ -239,19 +229,21 @@ impl BanksClient { pub fn process_transactions( &mut self, transactions: Vec, - ) -> impl Future> + '_ { + ) -> impl Future> + '_ { self.process_transactions_with_commitment(transactions, CommitmentLevel::default()) } /// Return the most recent rooted slot. All transactions at or below this slot /// are said to be finalized. The cluster will not fork to a higher slot. - pub fn get_root_slot(&mut self) -> impl Future> + '_ { + pub fn get_root_slot(&mut self) -> impl Future> + '_ { self.get_slot_with_context(context::current(), CommitmentLevel::default()) } /// Return the most recent rooted block height. All transactions at or below this height /// are said to be finalized. The cluster will not fork to a higher block height. - pub fn get_root_block_height(&mut self) -> impl Future> + '_ { + pub fn get_root_block_height( + &mut self, + ) -> impl Future> + '_ { self.get_block_height_with_context(context::current(), CommitmentLevel::default()) } @@ -261,7 +253,7 @@ impl BanksClient { &mut self, address: Pubkey, commitment: CommitmentLevel, - ) -> impl Future>> + '_ { + ) -> impl Future, BanksClientError>> + '_ { self.get_account_with_commitment_and_context(context::current(), address, commitment) } @@ -270,7 +262,7 @@ impl BanksClient { pub fn get_account( &mut self, address: Pubkey, - ) -> impl Future>> + '_ { + ) -> impl Future, BanksClientError>> + '_ { self.get_account_with_commitment(address, CommitmentLevel::default()) } @@ -279,14 +271,11 @@ impl BanksClient { pub fn get_packed_account_data( &mut self, address: Pubkey, - ) -> impl Future> + '_ { + ) -> impl Future> + '_ { self.get_account(address).map(|result| { - let account = result? - .ok_or(BanksClientError::ClientError("Account not found")) - .map_err(io::Error::from)?; // Remove this map when return Err type updated to BanksClientError + let account = result?.ok_or(BanksClientError::ClientError("Account not found"))?; T::unpack_from_slice(&account.data) .map_err(|_| BanksClientError::ClientError("Failed to deserialize account")) - .map_err(Into::into) // Remove this when return Err type updated to BanksClientError }) } @@ -295,7 +284,7 @@ impl BanksClient { pub fn get_account_data_with_borsh( &mut self, address: Pubkey, - ) -> impl Future> + '_ { + ) -> impl Future> + '_ { self.get_account(address).map(|result| { let account = result?.ok_or(BanksClientError::ClientError("Account not found"))?; T::try_from_slice(&account.data).map_err(Into::into) @@ -308,14 +297,17 @@ impl BanksClient { &mut self, address: Pubkey, commitment: CommitmentLevel, - ) -> impl Future> + '_ { + ) -> impl Future> + '_ { self.get_account_with_commitment_and_context(context::current(), address, commitment) .map(|result| Ok(result?.map(|x| x.lamports).unwrap_or(0))) } /// Return the balance in lamports of an account at the given address at the time /// of the most recent root slot. - pub fn get_balance(&mut self, address: Pubkey) -> impl Future> + '_ { + pub fn get_balance( + &mut self, + address: Pubkey, + ) -> impl Future> + '_ { self.get_balance_with_commitment(address, CommitmentLevel::default()) } @@ -327,7 +319,7 @@ impl BanksClient { pub fn get_transaction_status( &mut self, signature: Signature, - ) -> impl Future>> + '_ { + ) -> impl Future, BanksClientError>> + '_ { self.get_transaction_status_with_context(context::current(), signature) } @@ -335,7 +327,7 @@ impl BanksClient { pub async fn get_transaction_statuses( &mut self, signatures: Vec, - ) -> io::Result>> { + ) -> Result>, BanksClientError> { // tarpc futures oddly hold a mutable reference back to the client so clone the client upfront let mut clients_and_signatures: Vec<_> = signatures .into_iter() @@ -352,7 +344,9 @@ impl BanksClient { statuses.into_iter().collect() } - pub fn get_latest_blockhash(&mut self) -> impl Future> + '_ { + pub fn get_latest_blockhash( + &mut self, + ) -> impl Future> + '_ { self.get_latest_blockhash_with_commitment(CommitmentLevel::default()) .map(|result| { result? @@ -365,7 +359,7 @@ impl BanksClient { pub fn get_latest_blockhash_with_commitment( &mut self, commitment: CommitmentLevel, - ) -> impl Future>> + '_ { + ) -> impl Future, BanksClientError>> + '_ { self.get_latest_blockhash_with_commitment_and_context(context::current(), commitment) } @@ -373,10 +367,9 @@ impl BanksClient { &mut self, ctx: Context, commitment: CommitmentLevel, - ) -> impl Future>> + '_ { + ) -> impl Future, BanksClientError>> + '_ { self.inner .get_latest_blockhash_with_commitment_and_context(ctx, commitment) - .map_err(BanksClientError::from) // Remove this when return Err type updated to BanksClientError .map_err(Into::into) } @@ -385,15 +378,14 @@ impl BanksClient { ctx: Context, commitment: CommitmentLevel, message: Message, - ) -> impl Future>> + '_ { + ) -> impl Future, BanksClientError>> + '_ { self.inner .get_fee_for_message_with_commitment_and_context(ctx, commitment, message) - .map_err(BanksClientError::from) // Remove this when return Err type updated to BanksClientError .map_err(Into::into) } } -pub async fn start_client(transport: C) -> io::Result +pub async fn start_client(transport: C) -> Result where C: Transport, Response> + Send + 'static, { @@ -402,7 +394,7 @@ where }) } -pub async fn start_tcp_client(addr: T) -> io::Result { +pub async fn start_tcp_client(addr: T) -> Result { let transport = tcp::connect(addr, Bincode::default).await?; Ok(BanksClient { inner: TarpcClient::new(client::Config::default(), transport).spawn(),