Skip to content

Commit

Permalink
consolidates quic/udp dispatch macros (solana-labs#30249)
Browse files Browse the repository at this point in the history
  • Loading branch information
behzadnouri authored Feb 11, 2023
1 parent 6558c8f commit 6d56688
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 65 deletions.
21 changes: 17 additions & 4 deletions client/src/connection_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,17 +145,30 @@ impl Default for ConnectionCache {
}

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]
$vis fn $name(&self $(, $arg:$ty)?) $(-> $out)? {
$(#[$meta])*
$vis fn $name$(<$($t: $cons),*>)?(&self $(, $arg:$ty)*) $(-> $out)? {
match self {
Self::Quic(this) => this.$name($($arg, )?),
Self::Udp(this) => this.$name($($arg, )?),
Self::Quic(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 {
dispatch!(fn server_addr(&self) -> &SocketAddr);
dispatch!(fn send_data(&self, buffer: &[u8]) -> TransportResult<()>);
Expand Down
67 changes: 6 additions & 61 deletions client/src/thin_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//! unstable and may change in future releases.
use {
crate::connection_cache::ConnectionCache,
crate::connection_cache::{dispatch, ConnectionCache},
solana_quic_client::{QuicConfig, QuicConnectionManager, QuicPool},
solana_rpc_client::rpc_client::RpcClient,
solana_rpc_client_api::config::RpcProgramAccountsConfig,
Expand Down Expand Up @@ -37,61 +37,6 @@ pub enum ThinClient {
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 {
/// 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
Expand Down Expand Up @@ -266,19 +211,19 @@ impl SyncClient for ThinClient {

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,
commitment_config: CommitmentConfig
) -> TransportResult<(Hash, FeeCalculator, Slot)>);

dispatch_allow_deprecated!(fn get_fee_calculator_for_blockhash(
dispatch!(#[allow(deprecated)] fn get_fee_calculator_for_blockhash(
&self,
blockhash: &Hash
) -> 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(
&self,
Expand Down Expand Up @@ -315,7 +260,7 @@ impl SyncClient for ThinClient {

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>);

Expand Down

0 comments on commit 6d56688

Please sign in to comment.