From 09700819cb9e8ed6532438ebd330e1e541f28872 Mon Sep 17 00:00:00 2001 From: Bogdan Opanchuk Date: Sat, 2 Nov 2024 13:41:59 -0700 Subject: [PATCH] Prepare for nested RoundId's; remove Copy --- manul/src/protocol/round.rs | 2 +- manul/src/session/evidence.rs | 38 ++++++++++++++++----------------- manul/src/session/message.rs | 4 ++-- manul/src/session/session.rs | 16 +++++++------- manul/src/session/transcript.rs | 10 ++++----- 5 files changed, 35 insertions(+), 35 deletions(-) diff --git a/manul/src/protocol/round.rs b/manul/src/protocol/round.rs index 56c1aa1..681fdb8 100644 --- a/manul/src/protocol/round.rs +++ b/manul/src/protocol/round.rs @@ -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, diff --git a/manul/src/session/evidence.rs b/manul/src/session/evidence.rs index c0fab32..3ff0ab9 100644 --- a/manul/src/session/evidence.rs +++ b/manul/src/session/evidence.rs @@ -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::, _>>()?; @@ -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::, _>>()?; @@ -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::, _>>()?; @@ -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::, _>>()?; @@ -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, )?) } @@ -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, )?) } @@ -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, )?) } @@ -470,12 +470,12 @@ where for (round_id, direct_message) in self.direct_messages.iter() { let verified_direct_message = direct_message.clone().verify::(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::(verifier)?.payload().clone(); @@ -500,31 +500,31 @@ where for (round_id, echo_broadcast) in self.echo_broadcasts.iter() { let verified_echo_broadcast = echo_broadcast.clone().verify::(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::(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::(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(), )); @@ -537,14 +537,14 @@ where for (other_verifier, echo_broadcast) in echo_set.echo_broadcasts.iter() { let verified_echo_broadcast = echo_broadcast.clone().verify::(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( diff --git a/manul/src/session/message.rs b/manul/src/session/message.rs index 51dbc55..0016e7c 100644 --- a/manul/src/session/message.rs +++ b/manul/src/session/message.rs @@ -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 } } diff --git a/manul/src/session/session.rs b/manul/src/session/session.rs index 6daec53..5368032 100644 --- a/manul/src/session/session.rs +++ b/manul/src/session/session.rs @@ -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"; @@ -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}"); @@ -408,7 +408,7 @@ where ) -> Result, 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, @@ -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, @@ -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 } @@ -747,9 +747,9 @@ where fn cache_message(&mut self, message: VerifiedMessage) -> 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 diff --git a/manul/src/session/transcript.rs b/manul/src/session/transcript.rs index 382448d..3f678e5 100644 --- a/manul/src/session/transcript.rs +++ b/manul/src/session/transcript.rs @@ -36,7 +36,7 @@ where #[allow(clippy::too_many_arguments)] pub fn update( self, - round_id: RoundId, + round_id: &RoundId, echo_broadcasts: BTreeMap>, normal_broadcasts: BTreeMap>, direct_messages: BTreeMap>, @@ -45,7 +45,7 @@ where missing_messages: BTreeSet, ) -> Result { 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!( @@ -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!( @@ -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!( @@ -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!(