From 18dd59b637c20c11282f0966b05d7b2a74b67490 Mon Sep 17 00:00:00 2001 From: behzad nouri Date: Mon, 13 Jan 2025 14:39:55 +0000 Subject: [PATCH] defines alias for Fn(&ContactInfo) -> R trait (#4403) https://github.com/anza-xyz/agave/pull/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. --- core/src/banking_simulation.rs | 7 ++----- core/src/banking_stage.rs | 13 ++++--------- core/src/next_leader.rs | 11 ++++------- core/src/warm_quic_cache_service.rs | 4 ++-- gossip/src/cluster_info.rs | 15 ++++++++------- gossip/src/contact_info.rs | 5 +++++ 6 files changed, 25 insertions(+), 30 deletions(-) diff --git a/core/src/banking_simulation.rs b/core/src/banking_simulation.rs index e811c9c6df9bd8..eca67bfde9e838 100644 --- a/core/src/banking_simulation.rs +++ b/core/src/banking_simulation.rs @@ -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}, @@ -248,10 +248,7 @@ impl LikeClusterInfo for Arc { *self.id.read().unwrap() } - fn lookup_contact_info(&self, _id: &Pubkey, _map: F) -> Option - where - F: FnOnce(&ContactInfo) -> Y, - { + fn lookup_contact_info(&self, _: &Pubkey, _: impl ContactInfoQuery) -> Option { None } } diff --git a/core/src/banking_stage.rs b/core/src/banking_stage.rs index 845e2981d2fe6d..d209e68f754d1a 100644 --- a/core/src/banking_stage.rs +++ b/core/src/banking_stage.rs @@ -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}, @@ -328,9 +328,7 @@ pub struct FilterForwardingResults { pub trait LikeClusterInfo: Send + Sync + 'static + Clone { fn id(&self) -> Pubkey; - fn lookup_contact_info(&self, id: &Pubkey, map: F) -> Option - where - F: FnOnce(&ContactInfo) -> Y; + fn lookup_contact_info(&self, id: &Pubkey, query: impl ContactInfoQuery) -> Option; } impl LikeClusterInfo for Arc { @@ -338,11 +336,8 @@ impl LikeClusterInfo for Arc { self.deref().id() } - fn lookup_contact_info(&self, id: &Pubkey, map: F) -> Option - where - F: FnOnce(&ContactInfo) -> Y, - { - self.deref().lookup_contact_info(id, map) + fn lookup_contact_info(&self, id: &Pubkey, query: impl ContactInfoQuery) -> Option { + self.deref().lookup_contact_info(id, query) } } diff --git a/core/src/next_leader.rs b/core/src/next_leader.rs index 547d0c9588a839..f2dc9c628595df 100644 --- a/core/src/next_leader.rs +++ b/core/src/next_leader.rs @@ -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}, @@ -45,14 +45,11 @@ pub(crate) fn next_leader_tpu_vote( }) } -pub(crate) fn next_leader( +pub(crate) fn next_leader( cluster_info: &impl LikeClusterInfo, poh_recorder: &RwLock, - port_selector: F, -) -> Option<(Pubkey, SocketAddr)> -where - F: FnOnce(&ContactInfo) -> Option, -{ + port_selector: impl ContactInfoQuery>, +) -> Option<(Pubkey, SocketAddr)> { let leader_pubkey = poh_recorder .read() .unwrap() diff --git a/core/src/warm_quic_cache_service.rs b/core/src/warm_quic_cache_service.rs index 8833b4d525c91b..5197077e460a8a 100644 --- a/core/src/warm_quic_cache_service.rs +++ b/core/src/warm_quic_cache_service.rs @@ -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::{ @@ -32,7 +32,7 @@ impl WarmQuicCacheService { cache: Option<&ConnectionCache>, cluster_info: &ClusterInfo, leader_pubkey: &Pubkey, - contact_info_selector: impl Fn(&ContactInfo) -> Option, + contact_info_selector: impl ContactInfoQuery>, log_context: &str, ) { if let Some(connection_cache) = cache { diff --git a/gossip/src/cluster_info.rs b/gossip/src/cluster_info.rs index e821ece4bb08be..e17c81c852928e 100644 --- a/gossip/src/cluster_info.rs +++ b/gossip/src/cluster_info.rs @@ -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, @@ -457,12 +457,13 @@ impl ClusterInfo { Ok(()) } - pub fn lookup_contact_info(&self, id: &Pubkey, map: F) -> Option - where - F: FnOnce(&ContactInfo) -> Y, - { + pub fn lookup_contact_info( + &self, + id: &Pubkey, + query: impl ContactInfoQuery, + ) -> Option { 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( @@ -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(&self, query: impl Fn(&ContactInfo) -> R) -> Vec { + pub fn tvu_peers(&self, query: impl ContactInfoQuery) -> Vec { let self_pubkey = self.id(); let self_shred_version = self.my_shred_version(); self.time_gossip_read_lock("tvu_peers", &self.stats.tvu_peers) diff --git a/gossip/src/contact_info.rs b/gossip/src/contact_info.rs index 26f11a771b6136..3e0d66f9a2f1ba 100644 --- a/gossip/src/contact_info.rs +++ b/gossip/src/contact_info.rs @@ -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: Fn(&ContactInfo) -> R {} +impl R> ContactInfoQuery for F {} + #[derive(Copy, Clone, Debug, Eq, Error, PartialEq)] pub enum Error { #[error("Duplicate IP address: {0}")]