From 17a9ee7c2ef4ea24d707a4390e2172e3ce0d5453 Mon Sep 17 00:00:00 2001 From: behzad nouri Date: Fri, 10 Jan 2025 12:28:07 -0600 Subject: [PATCH] defines alias for Fn(&ContactInfo) -> R trait https://github.com/anza-xyz/agave/pull/4391 adds type parameter to ContactInfo.cache but we don't want to expose that to outside of 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 gossip crate. --- core/src/banking_simulation.rs | 4 ++-- core/src/banking_stage.rs | 6 +++--- core/src/next_leader.rs | 11 ++++------- core/src/warm_quic_cache_service.rs | 4 ++-- gossip/src/cluster_info.rs | 6 +++--- gossip/src/contact_info.rs | 5 +++++ 6 files changed, 19 insertions(+), 17 deletions(-) diff --git a/core/src/banking_simulation.rs b/core/src/banking_simulation.rs index e811c9c6df9bd8..443c4ce3d66fbe 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}, @@ -250,7 +250,7 @@ impl LikeClusterInfo for Arc { fn lookup_contact_info(&self, _id: &Pubkey, _map: F) -> Option where - F: FnOnce(&ContactInfo) -> Y, + F: ContactInfoQuery, { None } diff --git a/core/src/banking_stage.rs b/core/src/banking_stage.rs index 845e2981d2fe6d..6413657353c855 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}, @@ -330,7 +330,7 @@ pub trait LikeClusterInfo: Send + Sync + 'static + Clone { fn lookup_contact_info(&self, id: &Pubkey, map: F) -> Option where - F: FnOnce(&ContactInfo) -> Y; + F: ContactInfoQuery; } impl LikeClusterInfo for Arc { @@ -340,7 +340,7 @@ impl LikeClusterInfo for Arc { fn lookup_contact_info(&self, id: &Pubkey, map: F) -> Option where - F: FnOnce(&ContactInfo) -> Y, + F: ContactInfoQuery, { self.deref().lookup_contact_info(id, map) } 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 c0a51f037b1c54..b24b1c548ce335 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 d8649a9150b37c..2ab6b6d3cf7ba8 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, @@ -466,7 +466,7 @@ impl ClusterInfo { pub fn lookup_contact_info(&self, id: &Pubkey, map: F) -> Option where - F: FnOnce(&ContactInfo) -> Y, + F: ContactInfoQuery, { let gossip_crds = self.gossip.crds.read().unwrap(); gossip_crds.get(*id).map(map) @@ -1112,7 +1112,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 e7a8015d8b95cb..8a5f055c14aeb1 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}")]