Skip to content

Commit

Permalink
Prepare for nested RoundId's; remove Copy
Browse files Browse the repository at this point in the history
  • Loading branch information
fjarri committed Nov 3, 2024
1 parent 104d625 commit 0970081
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 35 deletions.
2 changes: 1 addition & 1 deletion manul/src/protocol/round.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ where
}

/// A round identifier.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct RoundId {
round_num: u8,
is_echo: bool,
Expand Down
38 changes: 19 additions & 19 deletions manul/src/session/evidence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ where
.iter()
.map(|round_id| {
transcript
.get_echo_broadcast(*round_id, verifier)
.map(|echo| (*round_id, echo))
.get_echo_broadcast(round_id.clone(), verifier)
.map(|echo| (round_id.clone(), echo))
})
.collect::<Result<BTreeMap<_, _>, _>>()?;

Expand All @@ -110,8 +110,8 @@ where
.iter()
.map(|round_id| {
transcript
.get_normal_broadcast(*round_id, verifier)
.map(|bc| (*round_id, bc))
.get_normal_broadcast(round_id.clone(), verifier)
.map(|bc| (round_id.clone(), bc))
})
.collect::<Result<BTreeMap<_, _>, _>>()?;

Expand All @@ -120,8 +120,8 @@ where
.iter()
.map(|round_id| {
transcript
.get_direct_message(*round_id, verifier)
.map(|dm| (*round_id, dm))
.get_direct_message(round_id.clone(), verifier)
.map(|dm| (round_id.clone(), dm))
})
.collect::<Result<BTreeMap<_, _>, _>>()?;

Expand All @@ -131,7 +131,7 @@ where
.map(|round_id| {
transcript
.get_normal_broadcast(round_id.echo(), verifier)
.map(|dm| (*round_id, dm))
.map(|dm| (round_id.clone(), dm))
})
.collect::<Result<BTreeMap<_, _>, _>>()?;

Expand Down Expand Up @@ -371,7 +371,7 @@ where
} else {
Ok(P::verify_direct_message_is_invalid(
deserializer,
self.direct_message.metadata().round_id(),
self.direct_message.metadata().round_id().clone(),
payload,
)?)
}
Expand Down Expand Up @@ -401,7 +401,7 @@ where
} else {
Ok(P::verify_echo_broadcast_is_invalid(
deserializer,
self.echo_broadcast.metadata().round_id(),
self.echo_broadcast.metadata().round_id().clone(),
payload,
)?)
}
Expand Down Expand Up @@ -434,7 +434,7 @@ where
} else {
Ok(P::verify_normal_broadcast_is_invalid(
deserializer,
self.normal_broadcast.metadata().round_id(),
self.normal_broadcast.metadata().round_id().clone(),
payload,
)?)
}
Expand Down Expand Up @@ -470,12 +470,12 @@ where
for (round_id, direct_message) in self.direct_messages.iter() {
let verified_direct_message = direct_message.clone().verify::<SP>(verifier)?;
let metadata = verified_direct_message.metadata();
if metadata.session_id() != session_id || metadata.round_id() != *round_id {
if metadata.session_id() != session_id || metadata.round_id() != round_id {
return Err(EvidenceError::InvalidEvidence(
"Invalid attached message metadata".into(),
));
}
verified_direct_messages.insert(*round_id, verified_direct_message.payload().clone());
verified_direct_messages.insert(round_id.clone(), verified_direct_message.payload().clone());
}

let verified_echo_broadcast = self.echo_broadcast.clone().verify::<SP>(verifier)?.payload().clone();
Expand All @@ -500,31 +500,31 @@ where
for (round_id, echo_broadcast) in self.echo_broadcasts.iter() {
let verified_echo_broadcast = echo_broadcast.clone().verify::<SP>(verifier)?;
let metadata = verified_echo_broadcast.metadata();
if metadata.session_id() != session_id || metadata.round_id() != *round_id {
if metadata.session_id() != session_id || metadata.round_id() != round_id {
return Err(EvidenceError::InvalidEvidence(
"Invalid attached message metadata".into(),
));
}
verified_echo_broadcasts.insert(*round_id, verified_echo_broadcast.payload().clone());
verified_echo_broadcasts.insert(round_id.clone(), verified_echo_broadcast.payload().clone());
}

let mut verified_normal_broadcasts = BTreeMap::new();
for (round_id, normal_broadcast) in self.normal_broadcasts.iter() {
let verified_normal_broadcast = normal_broadcast.clone().verify::<SP>(verifier)?;
let metadata = verified_normal_broadcast.metadata();
if metadata.session_id() != session_id || metadata.round_id() != *round_id {
if metadata.session_id() != session_id || metadata.round_id() != round_id {
return Err(EvidenceError::InvalidEvidence(
"Invalid attached message metadata".into(),
));
}
verified_normal_broadcasts.insert(*round_id, verified_normal_broadcast.payload().clone());
verified_normal_broadcasts.insert(round_id.clone(), verified_normal_broadcast.payload().clone());
}

let mut combined_echos = BTreeMap::new();
for (round_id, combined_echo) in self.combined_echos.iter() {
let verified_combined_echo = combined_echo.clone().verify::<SP>(verifier)?;
let metadata = verified_combined_echo.metadata();
if metadata.session_id() != session_id || metadata.round_id().non_echo() != *round_id {
if metadata.session_id() != session_id || &metadata.round_id().non_echo() != round_id {
return Err(EvidenceError::InvalidEvidence(
"Invalid attached message metadata".into(),
));
Expand All @@ -537,14 +537,14 @@ where
for (other_verifier, echo_broadcast) in echo_set.echo_broadcasts.iter() {
let verified_echo_broadcast = echo_broadcast.clone().verify::<SP>(other_verifier)?;
let metadata = verified_echo_broadcast.metadata();
if metadata.session_id() != session_id || metadata.round_id() != *round_id {
if metadata.session_id() != session_id || metadata.round_id() != round_id {
return Err(EvidenceError::InvalidEvidence(
"Invalid attached message metadata".into(),
));
}
verified_echo_set.push(verified_echo_broadcast.payload().clone());
}
combined_echos.insert(*round_id, verified_echo_set);
combined_echos.insert(round_id.clone(), verified_echo_set);
}

Ok(self.error.verify_messages_constitute_error(
Expand Down
4 changes: 2 additions & 2 deletions manul/src/session/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ impl MessageMetadata {
&self.session_id
}

pub fn round_id(&self) -> RoundId {
self.round_id
pub fn round_id(&self) -> &RoundId {
&self.round_id
}
}

Expand Down
16 changes: 8 additions & 8 deletions manul/src/session/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ where
return Ok(PreprocessOutcome::remote_error(err));
}
};
let message_round_id = checked_message.metadata().round_id();
let message_round_id = checked_message.metadata().round_id().clone();

if checked_message.metadata().session_id() != &self.session_id {
let err = "The received message has an incorrect session ID";
Expand All @@ -319,7 +319,7 @@ where
}
MessageFor::ThisRound
} else if self.possible_next_rounds.contains(&message_round_id) {
if accum.message_is_cached(from, message_round_id) {
if accum.message_is_cached(from, &message_round_id) {
let err = format!("Message for {:?} is already cached", message_round_id);
accum.register_unprovable_error(from, RemoteError::new(&err))?;
trace!("{key:?} {err}");
Expand Down Expand Up @@ -408,7 +408,7 @@ where
) -> Result<SessionReport<P, SP>, LocalError> {
let round_id = self.round_id();
let transcript = self.transcript.update(
round_id,
&round_id,
accum.echo_broadcasts,
accum.normal_broadcasts,
accum.direct_messages,
Expand Down Expand Up @@ -448,7 +448,7 @@ where
let round_id = self.round_id();

let transcript = self.transcript.update(
round_id,
&round_id,
accum.echo_broadcasts,
accum.normal_broadcasts,
accum.direct_messages,
Expand Down Expand Up @@ -608,9 +608,9 @@ where
self.processing.contains(from)
}

fn message_is_cached(&self, from: &SP::Verifier, round_id: RoundId) -> bool {
fn message_is_cached(&self, from: &SP::Verifier, round_id: &RoundId) -> bool {
if let Some(entry) = self.cached.get(from) {
entry.contains_key(&round_id)
entry.contains_key(round_id)
} else {
false
}
Expand Down Expand Up @@ -747,9 +747,9 @@ where

fn cache_message(&mut self, message: VerifiedMessage<SP::Verifier>) -> Result<(), LocalError> {
let from = message.from().clone();
let round_id = message.metadata().round_id();
let round_id = message.metadata().round_id().clone();
let cached = self.cached.entry(from.clone()).or_default();
if cached.insert(round_id, message).is_some() {
if cached.insert(round_id.clone(), message).is_some() {
return Err(LocalError::new(format!(
"A message from for {:?} has already been cached",
round_id
Expand Down
10 changes: 5 additions & 5 deletions manul/src/session/transcript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ where
#[allow(clippy::too_many_arguments)]
pub fn update(
self,
round_id: RoundId,
round_id: &RoundId,
echo_broadcasts: BTreeMap<SP::Verifier, SignedMessagePart<EchoBroadcast>>,
normal_broadcasts: BTreeMap<SP::Verifier, SignedMessagePart<NormalBroadcast>>,
direct_messages: BTreeMap<SP::Verifier, SignedMessagePart<DirectMessage>>,
Expand All @@ -45,7 +45,7 @@ where
missing_messages: BTreeSet<SP::Verifier>,
) -> Result<Self, LocalError> {
let mut all_echo_broadcasts = self.echo_broadcasts;
match all_echo_broadcasts.entry(round_id) {
match all_echo_broadcasts.entry(round_id.clone()) {
Entry::Vacant(entry) => entry.insert(echo_broadcasts),
Entry::Occupied(_) => {
return Err(LocalError::new(format!(
Expand All @@ -55,7 +55,7 @@ where
};

let mut all_normal_broadcasts = self.normal_broadcasts;
match all_normal_broadcasts.entry(round_id) {
match all_normal_broadcasts.entry(round_id.clone()) {
Entry::Vacant(entry) => entry.insert(normal_broadcasts),
Entry::Occupied(_) => {
return Err(LocalError::new(format!(
Expand All @@ -65,7 +65,7 @@ where
};

let mut all_direct_messages = self.direct_messages;
match all_direct_messages.entry(round_id) {
match all_direct_messages.entry(round_id.clone()) {
Entry::Vacant(entry) => entry.insert(direct_messages),
Entry::Occupied(_) => {
return Err(LocalError::new(format!(
Expand Down Expand Up @@ -93,7 +93,7 @@ where
}

let mut all_missing_messages = self.missing_messages;
match all_missing_messages.entry(round_id) {
match all_missing_messages.entry(round_id.clone()) {
Entry::Vacant(entry) => entry.insert(missing_messages),
Entry::Occupied(_) => {
return Err(LocalError::new(format!(
Expand Down

0 comments on commit 0970081

Please sign in to comment.