From 3391c7f1d49e050ae2fe580816a10add68388d14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Cig=C3=A1nek?= Date: Thu, 14 Jan 2021 12:57:31 +0100 Subject: [PATCH] refactor!: remove Error::BadLocation BREAKING CHANGE: - remove `Error::BadLocation` (use the more specific `InvalidSrcLocation` / `InvalidDstLocation` instead) - rename `Error::InvalidSource` to `Error::InvalidSrcLocation` - rename `Error::InvalidDestination` to `Error::InvalidDstLocation` --- examples/stress.rs | 2 +- src/error.rs | 10 ++++------ src/location.rs | 4 ++-- src/messages/src_authority.rs | 8 ++++---- src/routing/approved.rs | 16 ++++++++-------- 5 files changed, 19 insertions(+), 21 deletions(-) diff --git a/examples/stress.rs b/examples/stress.rs index 0af63ed89d..b1f1bb713b 100644 --- a/examples/stress.rs +++ b/examples/stress.rs @@ -456,7 +456,7 @@ impl Network { .await { Ok(()) => Ok(true), - Err(RoutingError::InvalidSource) => Ok(false), // node name changed + Err(RoutingError::InvalidSrcLocation) => Ok(false), // node name changed Err(error) => Err(error.into()), } } diff --git a/src/error.rs b/src/error.rs index ad69a0241f..81ddd521fa 100644 --- a/src/error.rs +++ b/src/error.rs @@ -15,8 +15,6 @@ pub type Result = std::result::Result; #[derive(Debug, Error)] #[allow(missing_docs)] pub enum Error { - #[error("Invalid requester or handler locations.")] - BadLocation, #[error("Failed signature check.")] FailedSignature, #[error("Cannot route.")] @@ -27,10 +25,10 @@ pub enum Error { InvalidState, #[error("Bincode error: {}", .0)] Bincode(#[from] bincode::Error), - #[error("Invalid source.")] - InvalidSource, - #[error("Invalid destination.")] - InvalidDestination, + #[error("Invalid source location.")] + InvalidSrcLocation, + #[error("Invalid destination location.")] + InvalidDstLocation, #[error("Content of a received message is inconsistent.")] InvalidMessage, #[error("A signature share is invalid.")] diff --git a/src/location.rs b/src/location.rs index 491cd4d48e..ccc2bb34d0 100644 --- a/src/location.rs +++ b/src/location.rs @@ -70,7 +70,7 @@ impl DstLocation { pub(crate) fn as_node(&self) -> Result<&XorName> { match self { Self::Node(name) => Ok(name), - Self::Section(_) | Self::Direct => Err(Error::BadLocation), + Self::Section(_) | Self::Direct => Err(Error::InvalidDstLocation), } } @@ -78,7 +78,7 @@ impl DstLocation { pub(crate) fn check_is_section(&self) -> Result<()> { match self { Self::Section(_) => Ok(()), - Self::Node(_) | Self::Direct => Err(Error::BadLocation), + Self::Node(_) | Self::Direct => Err(Error::InvalidDstLocation), } } diff --git a/src/messages/src_authority.rs b/src/messages/src_authority.rs index 19b058975a..bd1e9f012d 100644 --- a/src/messages/src_authority.rs +++ b/src/messages/src_authority.rs @@ -54,7 +54,7 @@ impl SrcAuthority { if self.is_section() { Ok(()) } else { - Err(Error::BadLocation) + Err(Error::InvalidSrcLocation) } } @@ -65,14 +65,14 @@ impl SrcAuthority { pub(crate) fn to_node_name(&self) -> Result { match self { Self::Node { public_key, .. } => Ok(name(public_key)), - Self::Section { .. } => Err(Error::BadLocation), + Self::Section { .. } => Err(Error::InvalidSrcLocation), } } // If this location is `Node`, returns the corresponding `Peer` with `addr`. Otherwise error. pub(crate) fn to_node_peer(&self, addr: SocketAddr) -> Result { match self { - Self::Section { .. } => Err(Error::BadLocation), + Self::Section { .. } => Err(Error::InvalidSrcLocation), Self::Node { public_key, age, .. } => Ok(Peer::new(name(public_key), addr, *age)), @@ -83,7 +83,7 @@ impl SrcAuthority { pub(crate) fn as_section_prefix(&self) -> Result<&Prefix> { match self { Self::Section { prefix, .. } => Ok(prefix), - Self::Node { .. } => Err(Error::BadLocation), + Self::Node { .. } => Err(Error::InvalidSrcLocation), } } } diff --git a/src/routing/approved.rs b/src/routing/approved.rs index ac7d13b8b0..9706a4c64c 100644 --- a/src/routing/approved.rs +++ b/src/routing/approved.rs @@ -495,7 +495,7 @@ impl Approved { self.handle_relocate_promise(*promise, msg.to_bytes()) } Variant::BootstrapRequest(name) => { - let sender = sender.ok_or(Error::InvalidSource)?; + let sender = sender.ok_or(Error::InvalidSrcLocation)?; Ok(vec![self.handle_bootstrap_request( msg.src().to_node_peer(sender)?, *name, @@ -503,7 +503,7 @@ impl Approved { } Variant::JoinRequest(join_request) => { - let sender = sender.ok_or(Error::InvalidSource)?; + let sender = sender.ok_or(Error::InvalidSrcLocation)?; self.handle_join_request(msg.src().to_node_peer(sender)?, *join_request.clone()) } Variant::UserMessage(content) => { @@ -511,7 +511,7 @@ impl Approved { Ok(vec![]) } Variant::BouncedUntrustedMessage(message) => { - let sender = sender.ok_or(Error::InvalidSource)?; + let sender = sender.ok_or(Error::InvalidSrcLocation)?; Ok(self .handle_bounced_untrusted_message( msg.src().to_node_peer(sender)?, @@ -522,7 +522,7 @@ impl Approved { .collect()) } Variant::BouncedUnknownMessage { src_key, message } => { - let sender = sender.ok_or(Error::InvalidSource)?; + let sender = sender.ok_or(Error::InvalidSrcLocation)?; self.handle_bounced_unknown_message( msg.src().to_node_peer(sender)?, message.clone(), @@ -941,7 +941,7 @@ impl Approved { let conn_infos = section.peers().map(Peer::addr).copied().collect(); BootstrapResponse::Rebootstrap(conn_infos) } else { - return Err(Error::InvalidDestination); + return Err(Error::InvalidDstLocation); }; debug!("Sending BootstrapResponse {:?} to {}", response, peer); @@ -1137,7 +1137,7 @@ impl Approved { .section .members() .get(sender) - .ok_or(Error::InvalidSource)? + .ok_or(Error::InvalidSrcLocation)? .peer; if !proofs.verify(&elders_info) { @@ -1793,7 +1793,7 @@ impl Approved { "Not sending user message {:?} -> {:?}: not part of the source location", src, dst ); - return Err(Error::InvalidSource); + return Err(Error::InvalidSrcLocation); } if matches!(dst, DstLocation::Direct) { @@ -1801,7 +1801,7 @@ impl Approved { "Not sending user message {:?} -> {:?}: direct dst not supported", src, dst ); - return Err(Error::InvalidDestination); + return Err(Error::InvalidDstLocation); } let variant = Variant::UserMessage(content);