consolidates quic/udp dispatch macros (#30249)

This commit is contained in:
behzad nouri 2023-02-11 12:24:27 +00:00 committed by GitHub
parent 6558c8fdc9
commit 6d566889ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 65 deletions

View File

@ -145,17 +145,30 @@ impl Default for ConnectionCache {
} }
macro_rules! dispatch { macro_rules! dispatch {
($vis:vis fn $name:ident(&self $(, $arg:ident : $ty:ty)?) $(-> $out:ty)?) => { ($(#[$meta:meta])* $vis:vis fn $name:ident$(<$($t:ident: $cons:ident),*>)?(&self $(, $arg:ident: $ty:ty)*) $(-> $out:ty)?) => {
#[inline] #[inline]
$vis fn $name(&self $(, $arg:$ty)?) $(-> $out)? { $(#[$meta])*
$vis fn $name$(<$($t: $cons),*>)?(&self $(, $arg:$ty)*) $(-> $out)? {
match self { match self {
Self::Quic(this) => this.$name($($arg, )?), Self::Quic(this) => this.$name($($arg, )*),
Self::Udp(this) => this.$name($($arg, )?), Self::Udp(this) => this.$name($($arg, )*),
}
}
};
($(#[$meta:meta])* $vis:vis fn $name:ident$(<$($t:ident: $cons:ident),*>)?(&mut self $(, $arg:ident: $ty:ty)*) $(-> $out:ty)?) => {
#[inline]
$(#[$meta])*
$vis fn $name$(<$($t: $cons),*>)?(&mut self $(, $arg:$ty)*) $(-> $out)? {
match self {
Self::Quic(this) => this.$name($($arg, )*),
Self::Udp(this) => this.$name($($arg, )*),
} }
} }
}; };
} }
pub(crate) use dispatch;
impl ClientConnection for BlockingClientConnection { impl ClientConnection for BlockingClientConnection {
dispatch!(fn server_addr(&self) -> &SocketAddr); dispatch!(fn server_addr(&self) -> &SocketAddr);
dispatch!(fn send_data(&self, buffer: &[u8]) -> TransportResult<()>); dispatch!(fn send_data(&self, buffer: &[u8]) -> TransportResult<()>);

View File

@ -4,7 +4,7 @@
//! unstable and may change in future releases. //! unstable and may change in future releases.
use { use {
crate::connection_cache::ConnectionCache, crate::connection_cache::{dispatch, ConnectionCache},
solana_quic_client::{QuicConfig, QuicConnectionManager, QuicPool}, solana_quic_client::{QuicConfig, QuicConnectionManager, QuicPool},
solana_rpc_client::rpc_client::RpcClient, solana_rpc_client::rpc_client::RpcClient,
solana_rpc_client_api::config::RpcProgramAccountsConfig, solana_rpc_client_api::config::RpcProgramAccountsConfig,
@ -37,61 +37,6 @@ pub enum ThinClient {
Udp(BackendThinClient<UdpPool, UdpConnectionManager, UdpConfig>), Udp(BackendThinClient<UdpPool, UdpConnectionManager, UdpConfig>),
} }
/// Macros easing the forwarding calls to the BackendThinClient
macro_rules! dispatch {
/* Regular version */
($vis:vis fn $name:ident(&self $(, $arg:ident : $ty:ty)*) $(-> $out:ty)?) => {
#[inline]
$vis fn $name(&self $(, $arg:$ty)*) $(-> $out)? {
match self {
Self::Quic(this) => this.$name($($arg),*),
Self::Udp(this) => this.$name($($arg),*),
}
}
};
/* The self is a mut */
($vis:vis fn $name:ident(&mut self $(, $arg:ident : $ty:ty)*) $(-> $out:ty)?) => {
#[inline]
$vis fn $name(&mut self $(, $arg:$ty)*) $(-> $out)? {
match self {
Self::Quic(this) => this.$name($($arg),*),
Self::Udp(this) => this.$name($($arg),*),
}
}
};
/* There is a type parameter */
($vis:vis fn $name:ident<T: Signers>(&self $(, $arg:ident : $ty:ty)*) $(-> $out:ty)?) => {
#[inline]
$vis fn $name<T: Signers>(&self $(, $arg:$ty)*) $(-> $out)? {
match self {
Self::Quic(this) => this.$name($($arg),*),
Self::Udp(this) => this.$name($($arg),*),
}
}
};
}
/// Macro forwarding calls to BackendThinClient with deprecated functions
macro_rules! dispatch_allow_deprecated {
($vis:vis fn $name:ident(&self $(, $arg:ident : $ty:ty)*) $(-> $out:ty)?) => {
#[inline]
$vis fn $name(&self $(, $arg:$ty)*) $(-> $out)? {
match self {
Self::Quic(this) => {
#[allow(deprecated)]
this.$name($($arg),*)
}
Self::Udp(this) => {
#[allow(deprecated)]
this.$name($($arg),*)
}
}
}
};
}
impl ThinClient { impl ThinClient {
/// Create a new ThinClient that will interface with the Rpc at `rpc_addr` using TCP /// Create a new ThinClient that will interface with the Rpc at `rpc_addr` using TCP
/// and the Tpu at `tpu_addr` over `transactions_socket` using Quic or UDP /// and the Tpu at `tpu_addr` over `transactions_socket` using Quic or UDP
@ -266,19 +211,19 @@ impl SyncClient for ThinClient {
dispatch!(fn get_minimum_balance_for_rent_exemption(&self, data_len: usize) -> TransportResult<u64>); dispatch!(fn get_minimum_balance_for_rent_exemption(&self, data_len: usize) -> TransportResult<u64>);
dispatch_allow_deprecated!(fn get_recent_blockhash(&self) -> TransportResult<(Hash, FeeCalculator)>); dispatch!(#[allow(deprecated)] fn get_recent_blockhash(&self) -> TransportResult<(Hash, FeeCalculator)>);
dispatch_allow_deprecated!(fn get_recent_blockhash_with_commitment( dispatch!(#[allow(deprecated)] fn get_recent_blockhash_with_commitment(
&self, &self,
commitment_config: CommitmentConfig commitment_config: CommitmentConfig
) -> TransportResult<(Hash, FeeCalculator, Slot)>); ) -> TransportResult<(Hash, FeeCalculator, Slot)>);
dispatch_allow_deprecated!(fn get_fee_calculator_for_blockhash( dispatch!(#[allow(deprecated)] fn get_fee_calculator_for_blockhash(
&self, &self,
blockhash: &Hash blockhash: &Hash
) -> TransportResult<Option<FeeCalculator>>); ) -> TransportResult<Option<FeeCalculator>>);
dispatch_allow_deprecated!(fn get_fee_rate_governor(&self) -> TransportResult<FeeRateGovernor>); dispatch!(#[allow(deprecated)] fn get_fee_rate_governor(&self) -> TransportResult<FeeRateGovernor>);
dispatch!(fn get_signature_status( dispatch!(fn get_signature_status(
&self, &self,
@ -315,7 +260,7 @@ impl SyncClient for ThinClient {
dispatch!(fn poll_for_signature(&self, signature: &Signature) -> TransportResult<()>); dispatch!(fn poll_for_signature(&self, signature: &Signature) -> TransportResult<()>);
dispatch_allow_deprecated!(fn get_new_blockhash(&self, blockhash: &Hash) -> TransportResult<(Hash, FeeCalculator)>); dispatch!(#[allow(deprecated)] fn get_new_blockhash(&self, blockhash: &Hash) -> TransportResult<(Hash, FeeCalculator)>);
dispatch!(fn get_latest_blockhash(&self) -> TransportResult<Hash>); dispatch!(fn get_latest_blockhash(&self) -> TransportResult<Hash>);