From be9e7075e50e2aaffab003f802b3bff73fb60c6a Mon Sep 17 00:00:00 2001 From: Tyera Eulberg Date: Mon, 20 Dec 2021 21:09:51 -0700 Subject: [PATCH] Revert client Error type to io::Error; easy transition to BanksClientError --- banks-client/src/lib.rs | 104 +++++++++++++++++++++------------------- 1 file changed, 56 insertions(+), 48 deletions(-) diff --git a/banks-client/src/lib.rs b/banks-client/src/lib.rs index c92231b7277e4f..0ea769283c209c 100644 --- a/banks-client/src/lib.rs +++ b/banks-client/src/lib.rs @@ -22,6 +22,7 @@ use { signature::Signature, transaction::{self, Transaction}, }, + std::io, tarpc::{ client::{self, NewClient, RequestDispatch}, context::{self, Context}, @@ -58,9 +59,10 @@ 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) } @@ -72,10 +74,11 @@ 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) } @@ -83,9 +86,10 @@ impl BanksClient { &mut self, ctx: Context, signature: Signature, - ) -> impl Future, BanksClientError>> + '_ { + ) -> impl Future>> + '_ { 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) } @@ -93,9 +97,10 @@ 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) } @@ -103,9 +108,10 @@ 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) } @@ -114,9 +120,10 @@ impl BanksClient { ctx: Context, transaction: Transaction, commitment: CommitmentLevel, - ) -> impl Future>, BanksClientError>> + '_ { + ) -> impl Future>>> + '_ { 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) } @@ -125,9 +132,10 @@ impl BanksClient { ctx: Context, address: Pubkey, commitment: CommitmentLevel, - ) -> impl Future, BanksClientError>> + '_ { + ) -> impl Future>> + '_ { 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) } @@ -137,7 +145,7 @@ impl BanksClient { pub fn send_transaction( &mut self, transaction: Transaction, - ) -> impl Future> + '_ { + ) -> impl Future> + '_ { self.send_transaction_with_context(context::current(), transaction) } @@ -150,25 +158,27 @@ 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"))?; - from_account::(&sysvar).ok_or(BanksClientError::ClientError( - "Failed to deserialize sysvar", - )) + 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 }) } /// Return the cluster rent - pub fn get_rent(&mut self) -> impl Future> + '_ { + pub fn get_rent(&mut self) -> impl Future> + '_ { self.get_sysvar::() } @@ -176,9 +186,7 @@ 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)) } @@ -189,7 +197,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) @@ -199,13 +207,14 @@ 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()) } @@ -213,7 +222,7 @@ impl BanksClient { &mut self, transactions: Vec, commitment: CommitmentLevel, - ) -> Result<(), BanksClientError> { + ) -> io::Result<()> { let mut clients: Vec<_> = transactions.iter().map(|_| self.clone()).collect(); let futures = clients .iter_mut() @@ -229,21 +238,19 @@ 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()) } @@ -253,7 +260,7 @@ impl BanksClient { &mut self, address: Pubkey, commitment: CommitmentLevel, - ) -> impl Future, BanksClientError>> + '_ { + ) -> impl Future>> + '_ { self.get_account_with_commitment_and_context(context::current(), address, commitment) } @@ -262,7 +269,7 @@ impl BanksClient { pub fn get_account( &mut self, address: Pubkey, - ) -> impl Future, BanksClientError>> + '_ { + ) -> impl Future>> + '_ { self.get_account_with_commitment(address, CommitmentLevel::default()) } @@ -271,11 +278,14 @@ 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"))?; + 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 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 }) } @@ -284,7 +294,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) @@ -297,17 +307,14 @@ 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()) } @@ -319,7 +326,7 @@ impl BanksClient { pub fn get_transaction_status( &mut self, signature: Signature, - ) -> impl Future, BanksClientError>> + '_ { + ) -> impl Future>> + '_ { self.get_transaction_status_with_context(context::current(), signature) } @@ -327,7 +334,7 @@ impl BanksClient { pub async fn get_transaction_statuses( &mut self, signatures: Vec, - ) -> Result>, BanksClientError> { + ) -> io::Result>> { // 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() @@ -344,21 +351,20 @@ 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? .map(|x| x.0) .ok_or(BanksClientError::ClientError("valid blockhash not found")) + .map_err(Into::into) }) } pub fn get_latest_blockhash_with_commitment( &mut self, commitment: CommitmentLevel, - ) -> impl Future, BanksClientError>> + '_ { + ) -> impl Future>> + '_ { self.get_latest_blockhash_with_commitment_and_context(context::current(), commitment) } @@ -366,9 +372,10 @@ impl BanksClient { &mut self, ctx: Context, commitment: CommitmentLevel, - ) -> impl Future, BanksClientError>> + '_ { + ) -> impl Future>> + '_ { 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) } @@ -377,14 +384,15 @@ impl BanksClient { ctx: Context, commitment: CommitmentLevel, message: Message, - ) -> impl Future, BanksClientError>> + '_ { + ) -> impl Future>> + '_ { 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) -> Result +pub async fn start_client(transport: C) -> io::Result where C: Transport, Response> + Send + 'static, { @@ -393,7 +401,7 @@ where }) } -pub async fn start_tcp_client(addr: T) -> Result { +pub async fn start_tcp_client(addr: T) -> io::Result { let transport = tcp::connect(addr, Bincode::default).await?; Ok(BanksClient { inner: TarpcClient::new(client::Config::default(), transport).spawn(),