Skip to content

Commit

Permalink
defines alias for Fn(&ContactInfo) -> R trait (#4403)
Browse files Browse the repository at this point in the history
#4391
adds type parameter to ContactInfo.cache but we don't want to expose
that implementation detail to outside of the gossip crate.

In order to encapsulate that change in gossip crate, this commit adds an
alias for:
    Fn(&ContactInfo) -> R
which can be used outside of the gossip crate.
  • Loading branch information
behzadnouri authored Jan 13, 2025
1 parent 6b88a9c commit 18dd59b
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 30 deletions.
7 changes: 2 additions & 5 deletions core/src/banking_simulation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use {
solana_client::connection_cache::ConnectionCache,
solana_gossip::{
cluster_info::{ClusterInfo, Node},
contact_info::ContactInfo,
contact_info::ContactInfoQuery,
},
solana_ledger::{
blockstore::{Blockstore, PurgeType},
Expand Down Expand Up @@ -248,10 +248,7 @@ impl LikeClusterInfo for Arc<DummyClusterInfo> {
*self.id.read().unwrap()
}

fn lookup_contact_info<F, Y>(&self, _id: &Pubkey, _map: F) -> Option<Y>
where
F: FnOnce(&ContactInfo) -> Y,
{
fn lookup_contact_info<R>(&self, _: &Pubkey, _: impl ContactInfoQuery<R>) -> Option<R> {
None
}
}
Expand Down
13 changes: 4 additions & 9 deletions core/src/banking_stage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use {
crossbeam_channel::{unbounded, Receiver, RecvTimeoutError, Sender},
histogram::Histogram,
solana_client::connection_cache::ConnectionCache,
solana_gossip::{cluster_info::ClusterInfo, contact_info::ContactInfo},
solana_gossip::{cluster_info::ClusterInfo, contact_info::ContactInfoQuery},
solana_ledger::blockstore_processor::TransactionStatusSender,
solana_measure::measure_us,
solana_perf::{data_budget::DataBudget, packet::PACKETS_PER_BATCH},
Expand Down Expand Up @@ -328,21 +328,16 @@ pub struct FilterForwardingResults {
pub trait LikeClusterInfo: Send + Sync + 'static + Clone {
fn id(&self) -> Pubkey;

fn lookup_contact_info<F, Y>(&self, id: &Pubkey, map: F) -> Option<Y>
where
F: FnOnce(&ContactInfo) -> Y;
fn lookup_contact_info<R>(&self, id: &Pubkey, query: impl ContactInfoQuery<R>) -> Option<R>;
}

impl LikeClusterInfo for Arc<ClusterInfo> {
fn id(&self) -> Pubkey {
self.deref().id()
}

fn lookup_contact_info<F, Y>(&self, id: &Pubkey, map: F) -> Option<Y>
where
F: FnOnce(&ContactInfo) -> Y,
{
self.deref().lookup_contact_info(id, map)
fn lookup_contact_info<R>(&self, id: &Pubkey, query: impl ContactInfoQuery<R>) -> Option<R> {
self.deref().lookup_contact_info(id, query)
}
}

Expand Down
11 changes: 4 additions & 7 deletions core/src/next_leader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use {
itertools::Itertools,
solana_gossip::{
cluster_info::ClusterInfo,
contact_info::{ContactInfo, Protocol},
contact_info::{ContactInfoQuery, Protocol},
},
solana_poh::poh_recorder::PohRecorder,
solana_sdk::{clock::FORWARD_TRANSACTIONS_TO_LEADER_AT_SLOT_OFFSET, pubkey::Pubkey},
Expand Down Expand Up @@ -45,14 +45,11 @@ pub(crate) fn next_leader_tpu_vote(
})
}

pub(crate) fn next_leader<F>(
pub(crate) fn next_leader(
cluster_info: &impl LikeClusterInfo,
poh_recorder: &RwLock<PohRecorder>,
port_selector: F,
) -> Option<(Pubkey, SocketAddr)>
where
F: FnOnce(&ContactInfo) -> Option<SocketAddr>,
{
port_selector: impl ContactInfoQuery<Option<SocketAddr>>,
) -> Option<(Pubkey, SocketAddr)> {
let leader_pubkey = poh_recorder
.read()
.unwrap()
Expand Down
4 changes: 2 additions & 2 deletions core/src/warm_quic_cache_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use {
rand::{thread_rng, Rng},
solana_client::connection_cache::{ConnectionCache, Protocol},
solana_connection_cache::client_connection::ClientConnection as TpuConnection,
solana_gossip::{cluster_info::ClusterInfo, contact_info::ContactInfo},
solana_gossip::{cluster_info::ClusterInfo, contact_info::ContactInfoQuery},
solana_poh::poh_recorder::PohRecorder,
solana_pubkey::Pubkey,
std::{
Expand All @@ -32,7 +32,7 @@ impl WarmQuicCacheService {
cache: Option<&ConnectionCache>,
cluster_info: &ClusterInfo,
leader_pubkey: &Pubkey,
contact_info_selector: impl Fn(&ContactInfo) -> Option<SocketAddr>,
contact_info_selector: impl ContactInfoQuery<Option<SocketAddr>>,
log_context: &str,
) {
if let Some(connection_cache) = cache {
Expand Down
15 changes: 8 additions & 7 deletions gossip/src/cluster_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use {
cluster_info_metrics::{
submit_gossip_stats, Counter, GossipStats, ScopedTimer, TimedGuard,
},
contact_info::{self, ContactInfo, Error as ContactInfoError},
contact_info::{self, ContactInfo, ContactInfoQuery, Error as ContactInfoError},
crds::{Crds, Cursor, GossipRoute},
crds_data::{
self, CrdsData, EpochSlotsIndex, LowestSlot, NodeInstance, SnapshotHashes, Version,
Expand Down Expand Up @@ -457,12 +457,13 @@ impl ClusterInfo {
Ok(())
}

pub fn lookup_contact_info<F, Y>(&self, id: &Pubkey, map: F) -> Option<Y>
where
F: FnOnce(&ContactInfo) -> Y,
{
pub fn lookup_contact_info<R>(
&self,
id: &Pubkey,
query: impl ContactInfoQuery<R>,
) -> Option<R> {
let gossip_crds = self.gossip.crds.read().unwrap();
gossip_crds.get(*id).map(map)
gossip_crds.get(*id).map(query)
}

pub fn lookup_contact_info_by_gossip_addr(
Expand Down Expand Up @@ -1105,7 +1106,7 @@ impl ClusterInfo {
}

/// all validators that have a valid tvu port and are on the same `shred_version`.
pub fn tvu_peers<R>(&self, query: impl Fn(&ContactInfo) -> R) -> Vec<R> {
pub fn tvu_peers<R>(&self, query: impl ContactInfoQuery<R>) -> Vec<R> {
let self_pubkey = self.id();
let self_shred_version = self.my_shred_version();
self.time_gossip_read_lock("tvu_peers", &self.stats.tvu_peers)
Expand Down
5 changes: 5 additions & 0 deletions gossip/src/contact_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ const SOCKET_TAG_TVU_QUIC: u8 = 11;
const_assert_eq!(SOCKET_CACHE_SIZE, 13);
const SOCKET_CACHE_SIZE: usize = SOCKET_TAG_TPU_VOTE_QUIC as usize + 1usize;

// An alias for a function that reads data from a ContactInfo entry stored in
// the gossip CRDS table.
pub trait ContactInfoQuery<R>: Fn(&ContactInfo) -> R {}
impl<R, F: Fn(&ContactInfo) -> R> ContactInfoQuery<R> for F {}

#[derive(Copy, Clone, Debug, Eq, Error, PartialEq)]
pub enum Error {
#[error("Duplicate IP address: {0}")]
Expand Down

0 comments on commit 18dd59b

Please sign in to comment.