@@ -36,7 +36,7 @@ const BANNED_THRESHOLD: i32 = 82 * (i32::min_value() / 100);
3636/// Reputation change for a node when we get disconnected from it.
3737const DISCONNECT_REPUTATION_CHANGE : i32 = -256 ;
3838/// Reserved peers group ID
39- const RESERVED_NODES : & ' static str = "reserved" ;
39+ const RESERVED_NODES : & str = "reserved" ;
4040/// Amount of time between the moment we disconnect from a node and the moment we remove it from
4141/// the list.
4242const FORGET_AFTER : Duration = Duration :: from_secs ( 3600 ) ;
@@ -87,7 +87,7 @@ impl PeersetHandle {
8787 /// Has no effect if the node was already a reserved peer.
8888 ///
8989 /// > **Note**: Keep in mind that the networking has to know an address for this node,
90- /// > otherwise it will not be able to connect to it.
90+ /// > otherwise it will not be able to connect to it.
9191 pub fn add_reserved_peer ( & self , peer_id : PeerId ) {
9292 let _ = self . tx . unbounded_send ( Action :: AddReservedPeer ( peer_id) ) ;
9393 }
@@ -169,7 +169,7 @@ pub struct PeersetConfig {
169169 /// List of bootstrap nodes to initialize the peer with.
170170 ///
171171 /// > **Note**: Keep in mind that the networking has to know an address for these nodes,
172- /// > otherwise it will not be able to connect to them.
172+ /// > otherwise it will not be able to connect to them.
173173 pub bootnodes : Vec < PeerId > ,
174174
175175 /// If true, we only accept nodes in [`PeersetConfig::priority_groups`].
@@ -178,7 +178,7 @@ pub struct PeersetConfig {
178178 /// Lists of nodes we should always be connected to.
179179 ///
180180 /// > **Note**: Keep in mind that the networking has to know an address for these nodes,
181- /// > otherwise it will not be able to connect to them.
181+ /// > otherwise it will not be able to connect to them.
182182 pub priority_groups : Vec < ( String , HashSet < PeerId > ) > ,
183183}
184184
@@ -430,10 +430,9 @@ impl Peerset {
430430 . get ( RESERVED_NODES )
431431 . into_iter ( )
432432 . flatten ( )
433- . filter ( move |n| {
433+ . find ( move |n| {
434434 data. peer ( n) . into_connected ( ) . is_none ( )
435435 } )
436- . next ( )
437436 . cloned ( )
438437 } ;
439438
@@ -469,10 +468,9 @@ impl Peerset {
469468 self . priority_groups
470469 . values ( )
471470 . flatten ( )
472- . filter ( move |n| {
471+ . find ( move |n| {
473472 data. peer ( n) . into_connected ( ) . is_none ( )
474473 } )
475- . next ( )
476474 . cloned ( )
477475 } ;
478476
@@ -497,21 +495,17 @@ impl Peerset {
497495 }
498496
499497 // Now, we try to connect to non-priority nodes.
500- loop {
501- // Try to grab the next node to attempt to connect to.
502- let next = match self . data . highest_not_connected_peer ( ) {
503- Some ( p) => p,
504- None => break , // No known node to add.
505- } ;
506-
498+ while let Some ( next) = self . data . highest_not_connected_peer ( ) {
507499 // Don't connect to nodes with an abysmal reputation.
508500 if next. reputation ( ) < BANNED_THRESHOLD {
509501 break ;
510502 }
511503
512504 match next. try_outgoing ( ) {
513- Ok ( conn) => self . message_queue . push_back ( Message :: Connect ( conn. into_peer_id ( ) ) ) ,
514- Err ( _) => break , // No more slots available.
505+ Ok ( conn) => self
506+ . message_queue
507+ . push_back ( Message :: Connect ( conn. into_peer_id ( ) ) ) ,
508+ Err ( _) => break , // No more slots available.
515509 }
516510 }
517511 }
@@ -530,11 +524,9 @@ impl Peerset {
530524 trace ! ( target: "peerset" , "Incoming {:?}" , peer_id) ;
531525 self . update_time ( ) ;
532526
533- if self . reserved_only {
534- if !self . priority_groups . get ( RESERVED_NODES ) . map_or ( false , |n| n. contains ( & peer_id) ) {
535- self . message_queue . push_back ( Message :: Reject ( index) ) ;
536- return ;
537- }
527+ if self . reserved_only && !self . priority_groups . get ( RESERVED_NODES ) . map_or ( false , |n| n. contains ( & peer_id) ) {
528+ self . message_queue . push_back ( Message :: Reject ( index) ) ;
529+ return ;
538530 }
539531
540532 let not_connected = match self . data . peer ( & peer_id) {
@@ -584,7 +576,7 @@ impl Peerset {
584576 /// Adds discovered peer ids to the PSM.
585577 ///
586578 /// > **Note**: There is no equivalent "expired" message, meaning that it is the responsibility
587- /// > of the PSM to remove `PeerId`s that fail to dial too often.
579+ /// > of the PSM to remove `PeerId`s that fail to dial too often.
588580 pub fn discovered < I : IntoIterator < Item = PeerId > > ( & mut self , peer_ids : I ) {
589581 let mut discovered_any = false ;
590582
@@ -747,12 +739,12 @@ mod tests {
747739
748740 let ( mut peerset, _handle) = Peerset :: from_config ( config) ;
749741 peerset. incoming ( incoming. clone ( ) , ii) ;
750- peerset. incoming ( incoming. clone ( ) , ii4) ;
751- peerset. incoming ( incoming2. clone ( ) , ii2) ;
752- peerset. incoming ( incoming3. clone ( ) , ii3) ;
742+ peerset. incoming ( incoming, ii4) ;
743+ peerset. incoming ( incoming2, ii2) ;
744+ peerset. incoming ( incoming3, ii3) ;
753745
754746 assert_messages ( peerset, vec ! [
755- Message :: Connect ( bootnode. clone ( ) ) ,
747+ Message :: Connect ( bootnode) ,
756748 Message :: Accept ( ii) ,
757749 Message :: Accept ( ii2) ,
758750 Message :: Reject ( ii3) ,
@@ -772,7 +764,7 @@ mod tests {
772764 } ;
773765
774766 let ( mut peerset, _) = Peerset :: from_config ( config) ;
775- peerset. incoming ( incoming. clone ( ) , ii) ;
767+ peerset. incoming ( incoming, ii) ;
776768
777769 assert_messages ( peerset, vec ! [
778770 Message :: Reject ( ii) ,
0 commit comments