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

Commit

Permalink
fix: handle message send to self
Browse files Browse the repository at this point in the history
  • Loading branch information
madadam authored and oetyng committed Dec 14, 2020
1 parent f742219 commit a1c26ff
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 6 deletions.
13 changes: 12 additions & 1 deletion src/routing/approved.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1793,7 +1793,18 @@ impl Approved {
SrcLocation::Node(_) => {
// If the source is a single node, we don't even need to vote, so let's cut this short.
let msg = Message::single_src(&self.node, dst, variant, None, None)?;
Ok(self.relay_message(&msg)?.into_iter().collect())
let mut commands = vec![];

if dst.contains(&self.node.name(), self.section.prefix()) {
commands.push(Command::HandleMessage {
sender: Some(self.node.addr),
message: msg.clone(),
});
}

commands.extend(self.relay_message(&msg)?);

Ok(commands)
}
SrcLocation::Section(_) => {
let vote = self.create_send_message_vote(dst, variant, None)?;
Expand Down
56 changes: 51 additions & 5 deletions src/routing/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,15 @@ use crate::{
consensus::{test_utils::*, Proven, Vote},
crypto,
event::Event,
location::DstLocation,
location::{DstLocation, SrcLocation},
majority,
messages::{
BootstrapResponse, JoinRequest, Message, PlainMessage, ResourceProofResponse, Variant,
},
network::Network,
node::Node,
peer::Peer,
relocation,
relocation::RelocateDetails,
relocation::RelocatePayload,
relocation::SignedRelocateDetails,
relocation::{self, RelocateDetails, RelocatePayload, SignedRelocateDetails},
section::{
test_utils::*, EldersInfo, MemberInfo, PeerState, Section, SectionKeyShare,
SectionProofChain, MIN_AGE,
Expand Down Expand Up @@ -1388,6 +1385,55 @@ async fn relocation(relocated_peer_role: RelocatedPeerRole) -> Result<()> {
Ok(())
}

#[tokio::test]
async fn node_message_to_self() -> Result<()> {
message_to_self(MessageDst::Node).await
}

#[tokio::test]
async fn section_message_to_self() -> Result<()> {
message_to_self(MessageDst::Section).await
}

enum MessageDst {
Node,
Section,
}

async fn message_to_self(dst: MessageDst) -> Result<()> {
let node = create_node();
let peer = node.peer();
let state = Approved::first_node(node, mpsc::unbounded_channel().0)?;
let stage = Stage::new(state, create_comm()?);

let src = SrcLocation::Node(*peer.name());
let dst = match dst {
MessageDst::Node => DstLocation::Node(*peer.name()),
MessageDst::Section => DstLocation::Section(rand::random()),
};
let content = Bytes::from_static(b"hello");

let commands = stage
.handle_command(Command::SendUserMessage {
src,
dst,
content: content.clone(),
})
.await?;

assert_matches!(&commands[..], [Command::HandleMessage { sender, message }] => {
assert_eq!(sender.as_ref(), Some(peer.addr()));
assert_eq!(message.src().src_location(), src);
assert_eq!(message.dst(), &dst);
assert_matches!(
message.variant(),
Variant::UserMessage(actual_content) if actual_content == &content
);
});

Ok(())
}

// TODO: add more tests here

fn create_peer() -> Peer {
Expand Down

0 comments on commit a1c26ff

Please sign in to comment.