Skip to content
This repository has been archived by the owner on Jun 25, 2021. It is now read-only.

Commit

Permalink
refactor!: remove Error::BadLocation
Browse files Browse the repository at this point in the history
BREAKING CHANGE:
    - remove `Error::BadLocation` (use the more specific `InvalidSrcLocation` / `InvalidDstLocation` instead)
    - rename `Error::InvalidSource` to `Error::InvalidSrcLocation`
    - rename `Error::InvalidDestination` to `Error::InvalidDstLocation`
  • Loading branch information
madadam committed Jan 18, 2021
1 parent adabf82 commit 3391c7f
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 21 deletions.
2 changes: 1 addition & 1 deletion examples/stress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
}
}
Expand Down
10 changes: 4 additions & 6 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ pub type Result<T, E = Error> = std::result::Result<T, E>;
#[derive(Debug, Error)]
#[allow(missing_docs)]
pub enum Error {
#[error("Invalid requester or handler locations.")]
BadLocation,
#[error("Failed signature check.")]
FailedSignature,
#[error("Cannot route.")]
Expand All @@ -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.")]
Expand Down
4 changes: 2 additions & 2 deletions src/location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ 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),
}
}

/// Returns `Ok` if this location is section, `Err(BadLocation)` otherwise.
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),
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/messages/src_authority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl SrcAuthority {
if self.is_section() {
Ok(())
} else {
Err(Error::BadLocation)
Err(Error::InvalidSrcLocation)
}
}

Expand All @@ -65,14 +65,14 @@ impl SrcAuthority {
pub(crate) fn to_node_name(&self) -> Result<XorName> {
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<Peer> {
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)),
Expand All @@ -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),
}
}
}
16 changes: 8 additions & 8 deletions src/routing/approved.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,23 +495,23 @@ 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,
)?])
}

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) => {
self.handle_user_message(msg.src().src_location(), *msg.dst(), content.clone());
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)?,
Expand All @@ -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(),
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -1137,7 +1137,7 @@ impl Approved {
.section
.members()
.get(sender)
.ok_or(Error::InvalidSource)?
.ok_or(Error::InvalidSrcLocation)?
.peer;

if !proofs.verify(&elders_info) {
Expand Down Expand Up @@ -1793,15 +1793,15 @@ 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) {
error!(
"Not sending user message {:?} -> {:?}: direct dst not supported",
src, dst
);
return Err(Error::InvalidDestination);
return Err(Error::InvalidDstLocation);
}

let variant = Variant::UserMessage(content);
Expand Down

0 comments on commit 3391c7f

Please sign in to comment.