Skip to content

Commit 18dd59b

Browse files
authored
defines alias for Fn(&ContactInfo) -> R trait (#4403)
#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.
1 parent 6b88a9c commit 18dd59b

File tree

6 files changed

+25
-30
lines changed

6 files changed

+25
-30
lines changed

core/src/banking_simulation.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use {
1919
solana_client::connection_cache::ConnectionCache,
2020
solana_gossip::{
2121
cluster_info::{ClusterInfo, Node},
22-
contact_info::ContactInfo,
22+
contact_info::ContactInfoQuery,
2323
},
2424
solana_ledger::{
2525
blockstore::{Blockstore, PurgeType},
@@ -248,10 +248,7 @@ impl LikeClusterInfo for Arc<DummyClusterInfo> {
248248
*self.id.read().unwrap()
249249
}
250250

251-
fn lookup_contact_info<F, Y>(&self, _id: &Pubkey, _map: F) -> Option<Y>
252-
where
253-
F: FnOnce(&ContactInfo) -> Y,
254-
{
251+
fn lookup_contact_info<R>(&self, _: &Pubkey, _: impl ContactInfoQuery<R>) -> Option<R> {
255252
None
256253
}
257254
}

core/src/banking_stage.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use {
3131
crossbeam_channel::{unbounded, Receiver, RecvTimeoutError, Sender},
3232
histogram::Histogram,
3333
solana_client::connection_cache::ConnectionCache,
34-
solana_gossip::{cluster_info::ClusterInfo, contact_info::ContactInfo},
34+
solana_gossip::{cluster_info::ClusterInfo, contact_info::ContactInfoQuery},
3535
solana_ledger::blockstore_processor::TransactionStatusSender,
3636
solana_measure::measure_us,
3737
solana_perf::{data_budget::DataBudget, packet::PACKETS_PER_BATCH},
@@ -328,21 +328,16 @@ pub struct FilterForwardingResults {
328328
pub trait LikeClusterInfo: Send + Sync + 'static + Clone {
329329
fn id(&self) -> Pubkey;
330330

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

336334
impl LikeClusterInfo for Arc<ClusterInfo> {
337335
fn id(&self) -> Pubkey {
338336
self.deref().id()
339337
}
340338

341-
fn lookup_contact_info<F, Y>(&self, id: &Pubkey, map: F) -> Option<Y>
342-
where
343-
F: FnOnce(&ContactInfo) -> Y,
344-
{
345-
self.deref().lookup_contact_info(id, map)
339+
fn lookup_contact_info<R>(&self, id: &Pubkey, query: impl ContactInfoQuery<R>) -> Option<R> {
340+
self.deref().lookup_contact_info(id, query)
346341
}
347342
}
348343

core/src/next_leader.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use {
33
itertools::Itertools,
44
solana_gossip::{
55
cluster_info::ClusterInfo,
6-
contact_info::{ContactInfo, Protocol},
6+
contact_info::{ContactInfoQuery, Protocol},
77
},
88
solana_poh::poh_recorder::PohRecorder,
99
solana_sdk::{clock::FORWARD_TRANSACTIONS_TO_LEADER_AT_SLOT_OFFSET, pubkey::Pubkey},
@@ -45,14 +45,11 @@ pub(crate) fn next_leader_tpu_vote(
4545
})
4646
}
4747

48-
pub(crate) fn next_leader<F>(
48+
pub(crate) fn next_leader(
4949
cluster_info: &impl LikeClusterInfo,
5050
poh_recorder: &RwLock<PohRecorder>,
51-
port_selector: F,
52-
) -> Option<(Pubkey, SocketAddr)>
53-
where
54-
F: FnOnce(&ContactInfo) -> Option<SocketAddr>,
55-
{
51+
port_selector: impl ContactInfoQuery<Option<SocketAddr>>,
52+
) -> Option<(Pubkey, SocketAddr)> {
5653
let leader_pubkey = poh_recorder
5754
.read()
5855
.unwrap()

core/src/warm_quic_cache_service.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use {
55
rand::{thread_rng, Rng},
66
solana_client::connection_cache::{ConnectionCache, Protocol},
77
solana_connection_cache::client_connection::ClientConnection as TpuConnection,
8-
solana_gossip::{cluster_info::ClusterInfo, contact_info::ContactInfo},
8+
solana_gossip::{cluster_info::ClusterInfo, contact_info::ContactInfoQuery},
99
solana_poh::poh_recorder::PohRecorder,
1010
solana_pubkey::Pubkey,
1111
std::{
@@ -32,7 +32,7 @@ impl WarmQuicCacheService {
3232
cache: Option<&ConnectionCache>,
3333
cluster_info: &ClusterInfo,
3434
leader_pubkey: &Pubkey,
35-
contact_info_selector: impl Fn(&ContactInfo) -> Option<SocketAddr>,
35+
contact_info_selector: impl ContactInfoQuery<Option<SocketAddr>>,
3636
log_context: &str,
3737
) {
3838
if let Some(connection_cache) = cache {

gossip/src/cluster_info.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use {
1818
cluster_info_metrics::{
1919
submit_gossip_stats, Counter, GossipStats, ScopedTimer, TimedGuard,
2020
},
21-
contact_info::{self, ContactInfo, Error as ContactInfoError},
21+
contact_info::{self, ContactInfo, ContactInfoQuery, Error as ContactInfoError},
2222
crds::{Crds, Cursor, GossipRoute},
2323
crds_data::{
2424
self, CrdsData, EpochSlotsIndex, LowestSlot, NodeInstance, SnapshotHashes, Version,
@@ -457,12 +457,13 @@ impl ClusterInfo {
457457
Ok(())
458458
}
459459

460-
pub fn lookup_contact_info<F, Y>(&self, id: &Pubkey, map: F) -> Option<Y>
461-
where
462-
F: FnOnce(&ContactInfo) -> Y,
463-
{
460+
pub fn lookup_contact_info<R>(
461+
&self,
462+
id: &Pubkey,
463+
query: impl ContactInfoQuery<R>,
464+
) -> Option<R> {
464465
let gossip_crds = self.gossip.crds.read().unwrap();
465-
gossip_crds.get(*id).map(map)
466+
gossip_crds.get(*id).map(query)
466467
}
467468

468469
pub fn lookup_contact_info_by_gossip_addr(
@@ -1105,7 +1106,7 @@ impl ClusterInfo {
11051106
}
11061107

11071108
/// all validators that have a valid tvu port and are on the same `shred_version`.
1108-
pub fn tvu_peers<R>(&self, query: impl Fn(&ContactInfo) -> R) -> Vec<R> {
1109+
pub fn tvu_peers<R>(&self, query: impl ContactInfoQuery<R>) -> Vec<R> {
11091110
let self_pubkey = self.id();
11101111
let self_shred_version = self.my_shred_version();
11111112
self.time_gossip_read_lock("tvu_peers", &self.stats.tvu_peers)

gossip/src/contact_info.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ const SOCKET_TAG_TVU_QUIC: u8 = 11;
4242
const_assert_eq!(SOCKET_CACHE_SIZE, 13);
4343
const SOCKET_CACHE_SIZE: usize = SOCKET_TAG_TPU_VOTE_QUIC as usize + 1usize;
4444

45+
// An alias for a function that reads data from a ContactInfo entry stored in
46+
// the gossip CRDS table.
47+
pub trait ContactInfoQuery<R>: Fn(&ContactInfo) -> R {}
48+
impl<R, F: Fn(&ContactInfo) -> R> ContactInfoQuery<R> for F {}
49+
4550
#[derive(Copy, Clone, Debug, Eq, Error, PartialEq)]
4651
pub enum Error {
4752
#[error("Duplicate IP address: {0}")]

0 commit comments

Comments
 (0)