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

Commit d9a986e

Browse files
oetyngmaqi
authored andcommitted
fix: issues pointed out in review comments
1 parent a79d2d0 commit d9a986e

8 files changed

Lines changed: 84 additions & 79 deletions

File tree

examples/stress.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -457,13 +457,13 @@ impl Network {
457457
},
458458
};
459459
let bytes = bincode::serialize(&message)?.into();
460-
let itry = Itinerary {
460+
let itinerary = Itinerary {
461461
src: SrcLocation::Node(src),
462462
dst: DstLocation::Section(dst),
463463
aggregation: Aggregation::None,
464464
};
465465

466-
match node.send_message(itry, bytes).await {
466+
match node.send_message(itinerary, bytes).await {
467467
Ok(()) => Ok(true),
468468
Err(RoutingError::InvalidSrcLocation) => Ok(false), // node name changed
469469
Err(error) => Err(error.into()),

src/messages/mod.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub(crate) struct Message {
4040
src: SrcAuthority,
4141
/// Destination location.
4242
dst: DstLocation,
43-
///
43+
/// The aggregation scheme to be used.
4444
aggregation: Aggregation,
4545
/// The body of the message.
4646
variant: Variant,
@@ -311,15 +311,13 @@ impl Message {
311311
}
312312

313313
/// Elders will aggregate a group sig before
314-
/// they all all send one copy of it each to dst.
315-
pub fn aggregate_at_src(&self) -> bool {
316-
matches!(self.src, SrcAuthority::Section { .. })
317-
}
318-
319-
/// Elders will send their signed message, which
320-
/// recipients aggregate.
321-
pub fn aggregate_at_dst(&self) -> bool {
322-
matches!(self.src, SrcAuthority::BlsShare { .. })
314+
/// they all send one copy of it each to dst.
315+
pub fn aggregation(&self) -> Aggregation {
316+
match self.src {
317+
SrcAuthority::Section { .. } => Aggregation::AtSource,
318+
SrcAuthority::BlsShare { .. } => Aggregation::AtDestination,
319+
SrcAuthority::Node { .. } => Aggregation::None,
320+
}
323321
}
324322

325323
/// Returns the attached proof chain, if any.

src/routing/approved.rs

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ use sn_messaging::{
4949
section_info::{
5050
Error as TargetSectionError, GetSectionResponse, Message as SectionInfoMsg, SectionInfo,
5151
},
52-
DstLocation, EndUser, Itinerary, MessageType, SrcLocation,
52+
Aggregation, DstLocation, EndUser, Itinerary, MessageType, SrcLocation,
5353
};
5454
use std::{cmp, net::SocketAddr, slice};
5555
use tokio::sync::mpsc;
@@ -820,7 +820,7 @@ impl Approved {
820820
};
821821

822822
let bounce_dst_key = *self.section_key_by_name(&src_name);
823-
let bounce_dst = if msg.aggregate_at_src() {
823+
let bounce_dst = if matches!(msg.aggregation(), Aggregation::AtSource) {
824824
DstLocation::Section(src_name)
825825
} else {
826826
DstLocation::Node(src_name)
@@ -1002,46 +1002,39 @@ impl Approved {
10021002
message: MessageType::ClientMessage(ClientMessage::from(content)?),
10031003
}]);
10041004
}
1005-
if msg.aggregate_at_dst() {
1006-
if !matches!(dst, DstLocation::Node(_)) {
1007-
return Err(Error::InvalidDstLocation);
1008-
}
1009-
if let SrcAuthority::BlsShare {
1010-
proof_share,
1011-
src_section,
1012-
..
1013-
} = &src
1005+
if let SrcAuthority::BlsShare {
1006+
proof_share,
1007+
src_section,
1008+
..
1009+
} = &src
1010+
{
1011+
let signed_bytes = bincode::serialize(&msg.signable_view())?;
1012+
match self
1013+
.message_accumulator
1014+
.add(&signed_bytes, proof_share.clone())
10141015
{
1015-
let signed_bytes = bincode::serialize(&msg.signable_view())?;
1016-
match self
1017-
.message_accumulator
1018-
.add(&signed_bytes, proof_share.clone())
1019-
{
1020-
Ok(proof) => {
1021-
trace!("Successfully aggregated signatures for message: {:?}", msg);
1022-
let key = msg.proof_chain_last_key()?;
1023-
if key.verify(&proof.signature, signed_bytes) {
1024-
self.send_event(Event::MessageReceived {
1025-
content,
1026-
src: SrcLocation::Section(*src_section),
1027-
dst,
1028-
});
1029-
} else {
1030-
trace!(
1031-
"Aggregated signature is invalid. Handling message {:?} skipped",
1032-
msg
1033-
);
1034-
}
1035-
}
1036-
Err(AggregatorError::NotEnoughShares) => {}
1037-
Err(err) => {
1038-
trace!("Error accumulating message at destination: {:?}", err);
1016+
Ok(proof) => {
1017+
trace!("Successfully aggregated signatures for message: {:?}", msg);
1018+
let key = msg.proof_chain_last_key()?;
1019+
if key.verify(&proof.signature, signed_bytes) {
1020+
self.send_event(Event::MessageReceived {
1021+
content,
1022+
src: SrcLocation::Section(*src_section),
1023+
dst,
1024+
});
1025+
} else {
1026+
trace!(
1027+
"Aggregated signature is invalid. Handling message {:?} skipped",
1028+
msg
1029+
);
10391030
}
10401031
}
1041-
return Ok(vec![]);
1042-
} else {
1043-
return Err(Error::InvalidSrcLocation);
1032+
Err(AggregatorError::NotEnoughShares) => {}
1033+
Err(err) => {
1034+
trace!("Error accumulating message at destination: {:?}", err);
1035+
}
10441036
}
1037+
return Ok(vec![]);
10451038
}
10461039

10471040
self.send_event(Event::MessageReceived {
@@ -2036,53 +2029,60 @@ impl Approved {
20362029
Ok(commands)
20372030
}
20382031

2039-
pub fn send_user_message(&mut self, itry: Itinerary, content: Bytes) -> Result<Vec<Command>> {
2040-
let are_we_src =
2041-
matches!(itry.src, SrcLocation::Node(_)) && itry.src.name() == self.node.name();
2032+
pub fn send_user_message(
2033+
&mut self,
2034+
itinerary: Itinerary,
2035+
content: Bytes,
2036+
) -> Result<Vec<Command>> {
2037+
let are_we_src = itinerary.src.equals(&self.node.name())
2038+
|| itinerary.src.equals(&self.section().prefix().name());
20422039
if !are_we_src {
20432040
error!(
20442041
"Not sending user message {:?} -> {:?}: we are not the source location",
2045-
itry.src, itry.dst
2042+
itinerary.src, itinerary.dst
20462043
);
20472044
return Err(Error::InvalidSrcLocation);
20482045
}
2049-
if matches!(itry.src, SrcLocation::EndUser(_)) {
2046+
if matches!(itinerary.src, SrcLocation::EndUser(_)) {
20502047
return Err(Error::InvalidSrcLocation);
20512048
}
2052-
if matches!(itry.dst, DstLocation::Direct) {
2049+
if matches!(itinerary.dst, DstLocation::Direct) {
20532050
error!(
20542051
"Not sending user message {:?} -> {:?}: direct dst not supported",
2055-
itry.src, itry.dst
2052+
itinerary.src, itinerary.dst
20562053
);
20572054
return Err(Error::InvalidDstLocation);
20582055
}
20592056

2060-
// If the source is a single node, we don't even need to vote, so let's cut this short.
2061-
let msg = if itry.aggregate_at_dst() {
2057+
// If the msg is to be aggregated at dst, we don't vote among our peers, wemsimply send the msg as our vote to the dst.
2058+
let msg = if itinerary.aggregate_at_dst() {
20622059
Message::for_dst_accumulation(
20632060
&self.node,
20642061
self.section_keys_provider.key_share()?,
2065-
itry.dst,
2062+
itinerary.dst,
20662063
content,
20672064
self.section().create_proof_chain_for_our_info(None),
20682065
None,
20692066
self.section().prefix().name(),
20702067
)?
2071-
} else if itry.aggregate_at_src() {
2068+
} else if itinerary.aggregate_at_src() {
20722069
let variant = Variant::UserMessage(content);
2073-
let vote = self.create_send_message_vote(itry.dst, variant, None)?;
2070+
let vote = self.create_send_message_vote(itinerary.dst, variant, None)?;
20742071
let recipients = delivery_group::signature_targets(
2075-
&itry.dst,
2072+
&itinerary.dst,
20762073
self.section.elders_info().peers().copied(),
20772074
);
20782075
return self.send_vote(&recipients, vote);
20792076
} else {
20802077
let variant = Variant::UserMessage(content);
2081-
Message::single_src(&self.node, itry.dst, variant, None, None)?
2078+
Message::single_src(&self.node, itinerary.dst, variant, None, None)?
20822079
};
20832080
let mut commands = vec![];
20842081

2085-
if itry.dst.contains(&self.node.name(), self.section.prefix()) {
2082+
if itinerary
2083+
.dst
2084+
.contains(&self.node.name(), self.section.prefix())
2085+
{
20862086
commands.push(Command::HandleMessage {
20872087
sender: Some(self.node.addr),
20882088
message: msg.clone(),

src/routing/command.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@ pub(crate) enum Command {
7070
message: MessageType,
7171
},
7272
/// Send `UserMessage` with the given source and destination.
73-
SendUserMessage { itry: Itinerary, content: Bytes },
73+
SendUserMessage {
74+
itinerary: Itinerary,
75+
content: Bytes,
76+
},
7477
/// Schedule a timeout after the given duration. When the timeout expires, a `HandleTimeout`
7578
/// command is raised. The token is used to identify the timeout.
7679
ScheduleTimeout { duration: Duration, token: u64 },
@@ -162,9 +165,9 @@ impl Debug for Command {
162165
.field("delivery_group_size", delivery_group_size)
163166
.field("message", message)
164167
.finish(),
165-
Self::SendUserMessage { itry, content } => f
168+
Self::SendUserMessage { itinerary, content } => f
166169
.debug_struct("SendUserMessage")
167-
.field("itry", itry)
170+
.field("itinerary", itinerary)
168171
.field("content", &format_args!("{:10}", HexFmt(content)))
169172
.finish(),
170173
Self::ScheduleTimeout { duration, token } => f

src/routing/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,11 +312,11 @@ impl Routing {
312312
/// Send a message.
313313
/// Messages sent here, either section to section or node to node are signed
314314
/// and validated upon receipt by routing itself.
315-
pub async fn send_message(&self, itry: Itinerary, content: Bytes) -> Result<()> {
315+
pub async fn send_message(&self, itinerary: Itinerary, content: Bytes) -> Result<()> {
316316
if let DstLocation::EndUser(EndUser::Client {
317317
socket_id,
318318
public_key,
319-
}) = itry.dst
319+
}) = itinerary.dst
320320
{
321321
let socket_addr = self
322322
.stage
@@ -338,7 +338,7 @@ impl Routing {
338338
debug!("Sending user message instead.. (Command::SendUserMessage)");
339339
}
340340
}
341-
let command = Command::SendUserMessage { itry, content };
341+
let command = Command::SendUserMessage { itinerary, content };
342342
self.stage.clone().handle_commands(command).await
343343
}
344344

src/routing/stage.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,11 @@ impl Stage {
147147
self.send_message(&recipients, delivery_group_size, message)
148148
.await
149149
}
150-
Command::SendUserMessage { itry, content } => {
151-
self.state.lock().await.send_user_message(itry, content)
152-
}
150+
Command::SendUserMessage { itinerary, content } => self
151+
.state
152+
.lock()
153+
.await
154+
.send_user_message(itinerary, content),
153155
Command::ScheduleTimeout { duration, token } => Ok(self
154156
.handle_schedule_timeout(duration, token)
155157
.await

src/routing/tests/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1459,7 +1459,7 @@ async fn message_to_self(dst: MessageDst) -> Result<()> {
14591459

14601460
let commands = stage
14611461
.handle_command(Command::SendUserMessage {
1462-
itry: Itinerary {
1462+
itinerary: Itinerary {
14631463
src,
14641464
dst,
14651465
aggregation: Aggregation::None,

tests/messages.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,15 @@ async fn test_messages_between_nodes() -> Result<()> {
153153

154154
println!("sending msg..");
155155

156-
let itry = Itinerary {
156+
let itinerary = Itinerary {
157157
src: SrcLocation::Node(node2_name),
158158
dst: DstLocation::Node(node1_name),
159159
aggregation: Aggregation::None,
160160
};
161161

162-
node2.send_message(itry, Bytes::from_static(msg)).await?;
162+
node2
163+
.send_message(itinerary, Bytes::from_static(msg))
164+
.await?;
163165

164166
println!("msg sent");
165167

@@ -168,15 +170,15 @@ async fn test_messages_between_nodes() -> Result<()> {
168170
println!("Got dst: {:?} (expecting: {}", dst.name(), node2_name);
169171
println!("sending response from {:?}..", node1_name);
170172

171-
let itry = Itinerary {
173+
let itinerary = Itinerary {
172174
src: SrcLocation::Node(node1_name),
173175
dst,
174176
aggregation: Aggregation::None,
175177
};
176178

177179
// send response from node1 to node2
178180
node1
179-
.send_message(itry, Bytes::from_static(response))
181+
.send_message(itinerary, Bytes::from_static(response))
180182
.await?;
181183

182184
println!("checking response received..");

0 commit comments

Comments
 (0)