@@ -8,37 +8,24 @@ use smallvec::SmallVec;
8
8
use std:: collections:: HashMap ;
9
9
use std:: time:: Instant ;
10
10
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 ;
19
15
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 ;
39
25
40
26
pub struct Connectivity < N : NetworkGlobalsProvider > {
41
- config : ConnectivityConfig ,
27
+ target_peers : usize ,
28
+ discovery_enabled : bool ,
42
29
/// Peers queued to be dialed.
43
30
peers_to_dial : Vec < Enr > ,
44
31
network_globals_provider : N ,
@@ -48,12 +35,14 @@ pub struct Connectivity<N: NetworkGlobalsProvider> {
48
35
49
36
impl < N : NetworkGlobalsProvider > Connectivity < N > {
50
37
pub fn new (
51
- config : ConnectivityConfig ,
38
+ target_peers : usize ,
39
+ discovery_enabled : bool ,
52
40
network_globals_provider : N ,
53
41
log : & slog:: Logger ,
54
42
) -> Self {
55
43
Self {
56
- config,
44
+ target_peers,
45
+ discovery_enabled,
57
46
peers_to_dial : Default :: default ( ) ,
58
47
network_globals_provider,
59
48
log : log. clone ( ) ,
@@ -156,68 +145,65 @@ impl<N: NetworkGlobalsProvider> Connectivity<N> {
156
145
events : & mut SmallVec < [ PeerManagerEvent ; 16 ] > ,
157
146
) {
158
147
// Check if we need to do a discovery lookup
159
- if self . config . discovery_enabled {
148
+ if self . discovery_enabled {
160
149
let peer_count = self . network_globals_provider . connected_or_dialing_peers ( ) ;
161
150
let outbound_only_peer_count = self
162
151
. network_globals_provider
163
152
. connected_outbound_only_peers ( ) ;
164
153
// 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
+ } ;
178
166
if wanted_peers != 0 {
179
167
// 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) ;
181
169
events. push ( PeerManagerEvent :: DiscoverPeers ( wanted_peers) ) ;
182
170
}
183
171
}
184
172
}
185
173
186
174
/// The target number of peers we would like to connect to.
187
175
pub fn target_peers ( & self ) -> usize {
188
- self . config . target_peers
176
+ self . target_peers
189
177
}
190
178
191
179
/// The maximum number of peers we allow to connect to us. This is `target_peers` * (1 +
192
180
/// peer_excess_factor)
193
181
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
195
183
}
196
184
197
185
/// The maximum number of peers we allow when dialing a priority peer (i.e a peer that is
198
186
/// subscribed to subnets that our validator requires. This is `target_peers` * (1 +
199
187
/// PEER_EXCESS_FACTOR + PRIORITY_PEER_EXCESS)
200
188
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
204
191
}
205
192
206
193
/// The minimum number of outbound peers that we reach before we start another discovery query.
207
194
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
209
196
}
210
197
211
198
/// The minimum number of outbound peers that we reach before we start another discovery query.
212
199
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
214
201
}
215
202
216
203
/// The maximum number of peers that are connected or dialing before we refuse to do another
217
204
/// discovery search for more outbound peers. We can use up to half the priority peer excess allocation.
218
205
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
222
208
}
223
209
}
0 commit comments