diff --git a/p2p/src/peers.rs b/p2p/src/peers.rs index 36d411be2..fed122c9d 100644 --- a/p2p/src/peers.rs +++ b/p2p/src/peers.rs @@ -416,7 +416,12 @@ impl Peers { /// Iterate over the peer list and prune all peers we have /// lost connection to or have been deemed problematic. /// Also avoid connected peer count getting too high. - pub fn clean_peers(&self, max_inbound_count: usize, max_outbound_count: usize) { + pub fn clean_peers( + &self, + max_inbound_count: usize, + max_outbound_count: usize, + preferred_peers: &[PeerAddr], + ) { let mut rm = vec![]; // build a list of peers to be cleaned up @@ -464,12 +469,13 @@ impl Peers { let excess_outgoing_count = (self.peer_outbound_count() as usize).saturating_sub(max_outbound_count); if excess_outgoing_count > 0 { - let mut addrs = self + let mut addrs: Vec<_> = self .outgoing_connected_peers() .iter() + .filter(|x| !preferred_peers.contains(&x.info.addr)) .take(excess_outgoing_count) .map(|x| x.info.addr) - .collect::>(); + .collect(); rm.append(&mut addrs); } @@ -477,12 +483,13 @@ impl Peers { let excess_incoming_count = (self.peer_inbound_count() as usize).saturating_sub(max_inbound_count); if excess_incoming_count > 0 { - let mut addrs = self + let mut addrs: Vec<_> = self .incoming_connected_peers() .iter() + .filter(|x| !preferred_peers.contains(&x.info.addr)) .take(excess_incoming_count) .map(|x| x.info.addr) - .collect::>(); + .collect(); rm.append(&mut addrs); } diff --git a/servers/src/grin/seed.rs b/servers/src/grin/seed.rs index 3dc1fcc0f..8327df507 100644 --- a/servers/src/grin/seed.rs +++ b/servers/src/grin/seed.rs @@ -52,9 +52,11 @@ pub fn connect_and_monitor( p2p_server: Arc, capabilities: p2p::Capabilities, seed_list: Box Vec + Send>, - preferred_peers: Option>, + preferred_peers: &[PeerAddr], stop_state: Arc, ) -> std::io::Result> { + let preferred_peers = preferred_peers.to_vec(); + thread::Builder::new() .name("seed".to_string()) .spawn(move || { @@ -69,7 +71,7 @@ pub fn connect_and_monitor( peers.clone(), tx.clone(), seed_list, - preferred_peers.clone(), + &preferred_peers, ); let mut prev = MIN_DATE.and_hms(0, 0, 0); @@ -113,7 +115,7 @@ pub fn connect_and_monitor( peers.clone(), p2p_server.config.clone(), tx.clone(), - preferred_peers.clone(), + &preferred_peers, ); prev = Utc::now(); @@ -141,7 +143,7 @@ fn monitor_peers( peers: Arc, config: p2p::P2PConfig, tx: mpsc::Sender, - preferred_peers_list: Option>, + preferred_peers: &[PeerAddr], ) { // regularly check if we need to acquire more peers and if so, gets // them from db @@ -189,6 +191,7 @@ fn monitor_peers( peers.clean_peers( config.peer_max_inbound_count() as usize, config.peer_max_outbound_count() as usize, + preferred_peers, ); if peers.enough_outbound_peers() { @@ -209,16 +212,14 @@ fn monitor_peers( connected_peers.push(p.info.addr) } - // Attempt to connect to preferred peers if there is some - if let Some(preferred_peers) = preferred_peers_list { - for p in preferred_peers { - if !connected_peers.is_empty() { - if !connected_peers.contains(&p) { - tx.send(p).unwrap(); - } - } else { - tx.send(p).unwrap(); + // Attempt to connect to any preferred peers. + for p in preferred_peers { + if !connected_peers.is_empty() { + if !connected_peers.contains(p) { + tx.send(*p).unwrap(); } + } else { + tx.send(*p).unwrap(); } } @@ -257,7 +258,7 @@ fn connect_to_seeds_and_preferred_peers( peers: Arc, tx: mpsc::Sender, seed_list: Box Vec>, - peers_preferred_list: Option>, + peers_preferred: &[PeerAddr], ) { // check if we have some peers in db // look for peers that are able to give us other peers (via PEER_LIST capability) @@ -270,11 +271,8 @@ fn connect_to_seeds_and_preferred_peers( seed_list() }; - // If we have preferred peers add them to the connection - match peers_preferred_list { - Some(mut peers_preferred) => peer_addrs.append(&mut peers_preferred), - None => trace!("No preferred peers"), - }; + // If we have preferred peers add them to the initial list + peer_addrs.extend_from_slice(peers_preferred); if peer_addrs.is_empty() { warn!("No seeds were retrieved."); diff --git a/servers/src/grin/server.rs b/servers/src/grin/server.rs index 4101e05e3..e27e588b7 100644 --- a/servers/src/grin/server.rs +++ b/servers/src/grin/server.rs @@ -248,13 +248,16 @@ impl Server { _ => unreachable!(), }; - let preferred_peers = config.p2p_config.peers_preferred.clone().map(|p| p.peers); + let preferred_peers = match &config.p2p_config.peers_preferred { + Some(addrs) => addrs.peers.clone(), + None => vec![], + }; connect_thread = Some(seed::connect_and_monitor( p2p_server.clone(), config.p2p_config.capabilities, seeder, - preferred_peers, + &preferred_peers, stop_state.clone(), )?); }