Skip to content

Commit 9f32985

Browse files
authored
feat: add select_random_peers to census (#1850)
1 parent 7051e56 commit 9f32985

File tree

4 files changed

+39
-16
lines changed

4 files changed

+39
-16
lines changed

bin/portal-bridge/src/census/mod.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{collections::HashSet, time::Duration};
33
use discv5::enr::NodeId;
44
use ethportal_api::{
55
types::{network::Subnetwork, portal_wire::OfferTrace},
6-
OverlayContentKey,
6+
BeaconContentKey, HistoryContentKey, OverlayContentKey, StateContentKey,
77
};
88
use network::{Network, NetworkAction, NetworkInitializationConfig, NetworkManager};
99
use peer::PeerInfo;
@@ -75,9 +75,22 @@ impl Census {
7575
content_key: &impl OverlayContentKey,
7676
) -> Result<Vec<PeerInfo>, CensusError> {
7777
match subnetwork {
78-
Subnetwork::History => self.history.select_peers(content_key),
79-
Subnetwork::State => self.state.select_peers(content_key),
80-
Subnetwork::Beacon => self.beacon.select_peers(content_key),
78+
Subnetwork::History => self.history.select_peers(Some(content_key)),
79+
Subnetwork::State => self.state.select_peers(Some(content_key)),
80+
Subnetwork::Beacon => self.beacon.select_peers(Some(content_key)),
81+
_ => Err(CensusError::UnsupportedSubnetwork(subnetwork)),
82+
}
83+
}
84+
85+
/// Selects random peers to receive content.
86+
pub fn select_random_peers(
87+
&self,
88+
subnetwork: Subnetwork,
89+
) -> Result<Vec<PeerInfo>, CensusError> {
90+
match subnetwork {
91+
Subnetwork::History => self.history.select_peers(None::<&HistoryContentKey>),
92+
Subnetwork::State => self.state.select_peers(None::<&StateContentKey>),
93+
Subnetwork::Beacon => self.beacon.select_peers(None::<&BeaconContentKey>),
8194
_ => Err(CensusError::UnsupportedSubnetwork(subnetwork)),
8295
}
8396
}

bin/portal-bridge/src/census/network.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,12 @@ impl Network {
106106
}
107107

108108
/// Selects peers to receive content.
109+
///
110+
/// If content key is present, only peers interested in content will be returned.
111+
/// Otherwise, all peers are eligible.
109112
pub fn select_peers(
110113
&self,
111-
content_key: &impl OverlayContentKey,
114+
content_key: Option<&impl OverlayContentKey>,
112115
) -> Result<Vec<PeerInfo>, CensusError> {
113116
if self.peers.is_empty() {
114117
error!(

bin/portal-bridge/src/census/peers.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,10 @@ impl<W: Weight> Peers<W> {
123123
}
124124

125125
/// Selects peers to receive content.
126-
pub fn select_peers(&self, content_key: &impl OverlayContentKey) -> Vec<PeerInfo> {
126+
///
127+
/// If content key is present, only peers interested in content will be returned.
128+
/// Otherwise, all peers are eligible.
129+
pub fn select_peers(&self, content_key: Option<&impl OverlayContentKey>) -> Vec<PeerInfo> {
127130
self.selector
128131
.select_peers(content_key, self.read().peers.values())
129132
}

bin/portal-bridge/src/census/scoring.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,25 @@ use super::peer::{Peer, PeerInfo};
1111

1212
/// A trait for calculating peer's weight.
1313
pub trait Weight: Send + Sync {
14+
/// Calculates peer's weight based on
15+
/// - content key and id
16+
/// - OfferResult's
17+
/// - LivenessCheck's
1418
fn weight(
1519
&self,
16-
content_key: &impl OverlayContentKey,
17-
content_id: &[u8; 32],
20+
content_key_and_id: &Option<(&impl OverlayContentKey, [u8; 32])>,
1821
peer: &Peer,
1922
) -> u32;
2023

2124
fn weight_all<'a>(
2225
&self,
23-
content_key: &impl OverlayContentKey,
26+
content_key: Option<&impl OverlayContentKey>,
2427
peers: impl IntoIterator<Item = &'a Peer>,
2528
) -> impl Iterator<Item = (&'a Peer, u32)> {
26-
let content_id = content_key.content_id();
29+
let content_key_and_id = content_key.map(|key| (key, key.content_id()));
2730
peers
2831
.into_iter()
29-
.map(move |peer| (peer, self.weight(content_key, &content_id, peer)))
32+
.map(move |peer| (peer, self.weight(&content_key_and_id, peer)))
3033
}
3134
}
3235

@@ -83,12 +86,13 @@ impl Default for AdditiveWeight {
8386
impl Weight for AdditiveWeight {
8487
fn weight(
8588
&self,
86-
content_key: &impl OverlayContentKey,
87-
content_id: &[u8; 32],
89+
content_key_and_id: &Option<(&impl OverlayContentKey, [u8; 32])>,
8890
peer: &Peer,
8991
) -> u32 {
90-
if !peer.is_interested_in_content(content_key, content_id) {
91-
return 0;
92+
if let Some((content_key, content_id)) = content_key_and_id {
93+
if !peer.is_interested_in_content(*content_key, content_id) {
94+
return 0;
95+
}
9296
}
9397

9498
let liveness_weight = peer
@@ -145,7 +149,7 @@ impl<W: Weight> PeerSelector<W> {
145149
/// Selects up to `self.limit` peers based on their weights.
146150
pub fn select_peers<'a>(
147151
&self,
148-
content_key: &impl OverlayContentKey,
152+
content_key: Option<&impl OverlayContentKey>,
149153
peers: impl IntoIterator<Item = &'a Peer>,
150154
) -> Vec<PeerInfo> {
151155
let weighted_peers = self

0 commit comments

Comments
 (0)