banks-client: Update return type to `BanksClientError` (#22058)

This commit is contained in:
Jon Cinque 2021-12-24 15:44:19 +01:00 committed by GitHub
parent ec7536faf6
commit 214b561a28
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 48 additions and 56 deletions

View File

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