Skip to content

Commit e3e4d4f

Browse files
committed
Move constants, remove config and simplify code
1 parent 71efc87 commit e3e4d4f

File tree

3 files changed

+65
-84
lines changed

3 files changed

+65
-84
lines changed

beacon_node/lighthouse_network/src/peer_manager/connectivity.rs

Lines changed: 41 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,24 @@ use smallvec::SmallVec;
88
use std::collections::HashMap;
99
use std::time::Instant;
1010

11-
pub struct ConnectivityConfig {
12-
target_peers: usize,
13-
peer_excess_factor: f32,
14-
priority_peer_excess: f32,
15-
min_outbound_only_factor: f32,
16-
target_outbound_only_factor: f32,
17-
discovery_enabled: bool,
18-
}
11+
/// A fraction of `PeerManager::target_peers` that we allow to connect to us in excess of
12+
/// `PeerManager::target_peers`. For clarity, if `PeerManager::target_peers` is 50 and
13+
/// PEER_EXCESS_FACTOR = 0.1 we allow 10% more nodes, i.e 55.
14+
pub const PEER_EXCESS_FACTOR: f32 = 0.1;
1915

20-
impl ConnectivityConfig {
21-
pub fn new(
22-
target_peers: usize,
23-
peer_excess_factor: f32,
24-
priority_peer_excess: f32,
25-
min_outbound_only_factor: f32,
26-
target_outbound_only_factor: f32,
27-
discovery_enabled: bool,
28-
) -> Self {
29-
Self {
30-
target_peers,
31-
peer_excess_factor,
32-
priority_peer_excess,
33-
min_outbound_only_factor,
34-
target_outbound_only_factor,
35-
discovery_enabled,
36-
}
37-
}
38-
}
16+
/// The fraction of extra peers beyond the PEER_EXCESS_FACTOR that we allow us to dial for when
17+
/// requiring subnet peers. More specifically, if our target peer limit is 50, and our excess peer
18+
/// limit is 55, and we are at 55 peers, the following parameter provisions a few more slots of
19+
/// dialing priority peers we need for validator duties.
20+
const PRIORITY_PEER_EXCESS: f32 = 0.2;
21+
22+
/// A fraction of `PeerManager::target_peers` that if we get below, we start a discovery query to
23+
/// reach our target.
24+
pub const MIN_OUTBOUND_ONLY_FACTOR: f32 = 0.2;
3925

4026
pub struct Connectivity<N: NetworkGlobalsProvider> {
41-
config: ConnectivityConfig,
27+
target_peers: usize,
28+
discovery_enabled: bool,
4229
/// Peers queued to be dialed.
4330
peers_to_dial: Vec<Enr>,
4431
network_globals_provider: N,
@@ -48,12 +35,14 @@ pub struct Connectivity<N: NetworkGlobalsProvider> {
4835

4936
impl<N: NetworkGlobalsProvider> Connectivity<N> {
5037
pub fn new(
51-
config: ConnectivityConfig,
38+
target_peers: usize,
39+
discovery_enabled: bool,
5240
network_globals_provider: N,
5341
log: &slog::Logger,
5442
) -> Self {
5543
Self {
56-
config,
44+
target_peers,
45+
discovery_enabled,
5746
peers_to_dial: Default::default(),
5847
network_globals_provider,
5948
log: log.clone(),
@@ -156,68 +145,65 @@ impl<N: NetworkGlobalsProvider> Connectivity<N> {
156145
events: &mut SmallVec<[PeerManagerEvent; 16]>,
157146
) {
158147
// Check if we need to do a discovery lookup
159-
if self.config.discovery_enabled {
148+
if self.discovery_enabled {
160149
let peer_count = self.network_globals_provider.connected_or_dialing_peers();
161150
let outbound_only_peer_count = self
162151
.network_globals_provider
163152
.connected_outbound_only_peers();
164153
// return wanted number of peers
165-
let wanted_peers =
166-
if peer_count < self.config.target_peers.saturating_sub(dialing_peers) {
167-
// We need more peers in general.
168-
self.max_peers().saturating_sub(dialing_peers) - peer_count
169-
} else if outbound_only_peer_count < self.min_outbound_only_peers()
170-
&& peer_count < self.max_outbound_dialing_peers()
171-
{
172-
self.max_outbound_dialing_peers()
173-
.saturating_sub(dialing_peers)
174-
.saturating_sub(peer_count)
175-
} else {
176-
0
177-
};
154+
let wanted_peers = if peer_count < self.target_peers.saturating_sub(dialing_peers) {
155+
// We need more peers in general.
156+
self.max_peers().saturating_sub(dialing_peers) - peer_count
157+
} else if outbound_only_peer_count < self.min_outbound_only_peers()
158+
&& peer_count < self.max_outbound_dialing_peers()
159+
{
160+
self.max_outbound_dialing_peers()
161+
.saturating_sub(dialing_peers)
162+
.saturating_sub(peer_count)
163+
} else {
164+
0
165+
};
178166
if wanted_peers != 0 {
179167
// We need more peers, re-queue a discovery lookup.
180-
debug!(self.log, "Starting a new peer discovery query"; "connected" => peer_count, "target" => self.config.target_peers, "outbound" => outbound_only_peer_count, "wanted" => wanted_peers);
168+
debug!(self.log, "Starting a new peer discovery query"; "connected" => peer_count, "target" => self.target_peers, "outbound" => outbound_only_peer_count, "wanted" => wanted_peers);
181169
events.push(PeerManagerEvent::DiscoverPeers(wanted_peers));
182170
}
183171
}
184172
}
185173

186174
/// The target number of peers we would like to connect to.
187175
pub fn target_peers(&self) -> usize {
188-
self.config.target_peers
176+
self.target_peers
189177
}
190178

191179
/// The maximum number of peers we allow to connect to us. This is `target_peers` * (1 +
192180
/// peer_excess_factor)
193181
pub fn max_peers(&self) -> usize {
194-
(self.config.target_peers as f32 * (1.0 + self.config.peer_excess_factor)).ceil() as usize
182+
(self.target_peers as f32 * (1.0 + PEER_EXCESS_FACTOR)).ceil() as usize
195183
}
196184

197185
/// The maximum number of peers we allow when dialing a priority peer (i.e a peer that is
198186
/// subscribed to subnets that our validator requires. This is `target_peers` * (1 +
199187
/// PEER_EXCESS_FACTOR + PRIORITY_PEER_EXCESS)
200188
pub fn max_priority_peers(&self) -> usize {
201-
(self.config.target_peers as f32
202-
* (1.0 + self.config.peer_excess_factor + self.config.priority_peer_excess))
203-
.ceil() as usize
189+
(self.target_peers as f32 * (1.0 + PEER_EXCESS_FACTOR + PRIORITY_PEER_EXCESS)).ceil()
190+
as usize
204191
}
205192

206193
/// The minimum number of outbound peers that we reach before we start another discovery query.
207194
pub fn min_outbound_only_peers(&self) -> usize {
208-
(self.config.target_peers as f32 * self.config.min_outbound_only_factor).ceil() as usize
195+
(self.target_peers as f32 * MIN_OUTBOUND_ONLY_FACTOR).ceil() as usize
209196
}
210197

211198
/// The minimum number of outbound peers that we reach before we start another discovery query.
212199
pub fn target_outbound_peers(&self) -> usize {
213-
(self.config.target_peers as f32 * self.config.target_outbound_only_factor).ceil() as usize
200+
(self.target_peers as f32 * MIN_OUTBOUND_ONLY_FACTOR).ceil() as usize
214201
}
215202

216203
/// The maximum number of peers that are connected or dialing before we refuse to do another
217204
/// discovery search for more outbound peers. We can use up to half the priority peer excess allocation.
218205
pub fn max_outbound_dialing_peers(&self) -> usize {
219-
(self.config.target_peers as f32
220-
* (1.0 + self.config.peer_excess_factor + self.config.priority_peer_excess / 2.0))
221-
.ceil() as usize
206+
(self.target_peers as f32 * (1.0 + PEER_EXCESS_FACTOR + PRIORITY_PEER_EXCESS / 2.0)).ceil()
207+
as usize
222208
}
223209
}

beacon_node/lighthouse_network/src/peer_manager/mod.rs

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ pub use libp2p::identity::Keypair;
2323

2424
pub mod peerdb;
2525

26-
use crate::peer_manager::connectivity::{Connectivity, ConnectivityConfig};
26+
use crate::peer_manager::connectivity::{
27+
Connectivity, MIN_OUTBOUND_ONLY_FACTOR, PEER_EXCESS_FACTOR,
28+
};
2729
use crate::peer_manager::peerdb::client::ClientKind;
2830
use libp2p::multiaddr;
2931
pub use peerdb::peer_info::{
@@ -54,20 +56,7 @@ pub const PEER_RECONNECTION_TIMEOUT: Duration = Duration::from_secs(600);
5456
/// lower our peer count below this number. Instead we favour a non-uniform distribution of subnet
5557
/// peers.
5658
pub const MIN_SYNC_COMMITTEE_PEERS: u64 = 2;
57-
/// A fraction of `PeerManager::target_peers` that we allow to connect to us in excess of
58-
/// `PeerManager::target_peers`. For clarity, if `PeerManager::target_peers` is 50 and
59-
/// PEER_EXCESS_FACTOR = 0.1 we allow 10% more nodes, i.e 55.
60-
pub const PEER_EXCESS_FACTOR: f32 = 0.1;
61-
/// A fraction of `PeerManager::target_peers` that we want to be outbound-only connections.
62-
pub const TARGET_OUTBOUND_ONLY_FACTOR: f32 = 0.3;
63-
/// A fraction of `PeerManager::target_peers` that if we get below, we start a discovery query to
64-
/// reach our target. MIN_OUTBOUND_ONLY_FACTOR must be < TARGET_OUTBOUND_ONLY_FACTOR.
65-
pub const MIN_OUTBOUND_ONLY_FACTOR: f32 = 0.2;
66-
/// The fraction of extra peers beyond the PEER_EXCESS_FACTOR that we allow us to dial for when
67-
/// requiring subnet peers. More specifically, if our target peer limit is 50, and our excess peer
68-
/// limit is 55, and we are at 55 peers, the following parameter provisions a few more slots of
69-
/// dialing priority peers we need for validator duties.
70-
pub const PRIORITY_PEER_EXCESS: f32 = 0.2;
59+
7160
/// The numbre of inbound libp2p peers we have seen before we consider our NAT to be open.
7261
pub const LIBP2P_NAT_OPEN_THRESHOLD: usize = 3;
7362

@@ -192,14 +181,8 @@ impl<E: EthSpec> PeerManager<E> {
192181
quic_enabled,
193182
log: log.clone(),
194183
connectivity: Connectivity::new(
195-
ConnectivityConfig::new(
196-
target_peer_count,
197-
PEER_EXCESS_FACTOR,
198-
PRIORITY_PEER_EXCESS,
199-
MIN_OUTBOUND_ONLY_FACTOR,
200-
TARGET_OUTBOUND_ONLY_FACTOR,
201-
discovery_enabled,
202-
),
184+
target_peer_count,
185+
discovery_enabled,
203186
network_globals,
204187
log,
205188
),
@@ -718,6 +701,14 @@ impl<E: EthSpec> PeerManager<E> {
718701
}
719702
}
720703

704+
pub fn peer_excess_factor(&self) -> f32 {
705+
PEER_EXCESS_FACTOR
706+
}
707+
708+
pub fn min_outbound_only_factor(&self) -> f32 {
709+
MIN_OUTBOUND_ONLY_FACTOR
710+
}
711+
721712
/* Internal functions */
722713

723714
/// Sets a peer as connected as long as their reputation allows it

beacon_node/lighthouse_network/src/service/mod.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use crate::peer_manager::{
77
config::Config as PeerManagerCfg, peerdb::score::PeerAction, peerdb::score::ReportSource,
88
ConnectionDirection, PeerManager, PeerManagerEvent,
99
};
10-
use crate::peer_manager::{MIN_OUTBOUND_ONLY_FACTOR, PEER_EXCESS_FACTOR, PRIORITY_PEER_EXCESS};
1110
use crate::rpc::methods::MetadataRequest;
1211
use crate::rpc::{
1312
self, GoodbyeReason, HandlerErr, NetworkParams, Protocol, RPCError, RPCMessage, RPCReceived,
@@ -411,15 +410,20 @@ impl<E: EthSpec> Network<E> {
411410
.with_max_pending_outgoing(Some(16))
412411
.with_max_established_incoming(Some(
413412
(config.target_peers as f32
414-
* (1.0 + PEER_EXCESS_FACTOR - MIN_OUTBOUND_ONLY_FACTOR))
415-
.ceil() as u32,
413+
* (1.0 + peer_manager.peer_excess_factor()
414+
- peer_manager.min_outbound_only_factor()))
415+
.ceil() as u32,
416416
))
417417
.with_max_established_outgoing(Some(
418-
(config.target_peers as f32 * (1.0 + PEER_EXCESS_FACTOR)).ceil() as u32,
418+
(config.target_peers as f32 * (1.0 + peer_manager.peer_excess_factor())).ceil()
419+
as u32,
419420
))
420421
.with_max_established(Some(
421-
(config.target_peers as f32 * (1.0 + PEER_EXCESS_FACTOR + PRIORITY_PEER_EXCESS))
422-
.ceil() as u32,
422+
(config.target_peers as f32
423+
* (1.0
424+
+ peer_manager.peer_excess_factor()
425+
+ peer_manager.min_outbound_only_factor()))
426+
.ceil() as u32,
423427
))
424428
.with_max_established_per_peer(Some(1));
425429

0 commit comments

Comments
 (0)