Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
See if returning errors is ok.
Browse files Browse the repository at this point in the history
  • Loading branch information
Roman S. Borschel committed Jun 10, 2020
1 parent 216cf7b commit dfa12b6
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions client/network/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -672,13 +672,14 @@ impl<B: BlockT + 'static, H: ExHashT> NetworkService<B, H> {

/// 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
Expand All @@ -700,18 +701,21 @@ impl<B: BlockT + 'static, H: ExHashT> NetworkService<B, H> {
}

/// 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<Multiaddr>) -> 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::<Result<Vec<(PeerId, Multiaddr)>, String>>()?;

Expand Down

0 comments on commit dfa12b6

Please sign in to comment.