diff --git a/client/network/src/service.rs b/client/network/src/service.rs index 78197e68e7793..2297fe6a52f72 100644 --- a/client/network/src/service.rs +++ b/client/network/src/service.rs @@ -672,13 +672,14 @@ impl NetworkService { /// Adds a `PeerId` and its address as reserved. The string should encode the address /// and peer ID of the remote node. + /// + /// Returns an `Err` if the given string is not a valid multiaddress + /// or contains an invalid peer ID (which includes the local peer ID). pub fn add_reserved_peer(&self, peer: String) -> Result<(), String> { let (peer_id, addr) = parse_str_addr(&peer).map_err(|e| format!("{:?}", e))?; // Make sure the local peer ID is never added to the PSM. - // We don't treat this as an error, because the caller may - // not know the peer ID beforehand. - if peer == &self.local_peer_id { - return Ok(()) + if peer_id == self.local_peer_id { + return Err("Local peer ID cannot be added as a reserved peer.".to_string()) } self.peerset.add_reserved_peer(peer_id.clone()); let _ = self @@ -700,18 +701,21 @@ impl NetworkService { } /// Modify a peerset priority group. + /// + /// Returns an `Err` if one of the given addresses contains an invalid + /// peer ID (which includes the local peer ID). pub fn set_priority_group(&self, group_id: String, peers: HashSet) -> Result<(), String> { let peers = peers.into_iter() - .map(|p| parse_addr(p).map_err(|e| format!("{:?}", e))) - .filter(|r| - // Make sure the local peer ID is never added to the PSM - // or added as a "known address", even if given. - // We don't treat this as an error, because the caller may - // not know the peer ID beforehand. - if let Ok((peer, _)) = r { - peer != &self.local_peer_id - } else { - true // keep all errors + .map(|p| match parse_addr(p) { + Err(e) => Err(format!("{:?}", e)), + Ok((peer, addr)) => + // Make sure the local peer ID is never added to the PSM + // or added as a "known address", even if given. + if peer == self.local_peer_id { + Err("Local peer ID in priority group.".to_string()) + } else { + Ok((peer, addr)) + } }) .collect::, String>>()?;