From 29b16ffed341606548d96249e38346587a0b9faf Mon Sep 17 00:00:00 2001 From: Bogdan Opanchuk Date: Sun, 5 Jan 2025 10:14:44 -0800 Subject: [PATCH 1/2] Add conversion and comparison impls for RoundId/u8 --- CHANGELOG.md | 2 ++ examples/src/simple.rs | 22 +++++++++++----------- examples/src/simple_malicious.rs | 6 +++--- manul/benches/empty_rounds.rs | 4 ++-- manul/src/protocol/round.rs | 12 ++++++++++++ manul/src/tests/partial_echo.rs | 2 +- 6 files changed, 31 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fcc9f3e..aa8dc71 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `impl From for ProtocolValidationError` (to match what already exists for other messages). ([#77]) - Exposed `dev::ExecutionResult`. ([#79]) - `NoProtocolErrors` stub type to indicate that the protocol does not generate any provable errors. ([#79]) +- Conversion from `u8` to `RoundId` and comparison of `RoundId` with `u8`. ([#84]) [#75]: https://github.com/entropyxyz/manul/pull/75 @@ -37,6 +38,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 [#77]: https://github.com/entropyxyz/manul/pull/77 [#79]: https://github.com/entropyxyz/manul/pull/79 [#83]: https://github.com/entropyxyz/manul/pull/83 +[#84]: https://github.com/entropyxyz/manul/pull/84 ## [0.1.0] - 2024-11-19 diff --git a/examples/src/simple.rs b/examples/src/simple.rs index 0f2254f..6976251 100644 --- a/examples/src/simple.rs +++ b/examples/src/simple.rs @@ -33,8 +33,8 @@ impl ProtocolError for SimpleProtocolError { } Self::Round2InvalidPosition => RequiredMessages::new( RequiredMessageParts::direct_message_only(), - Some([(RoundId::new(1), RequiredMessageParts::direct_message_only())].into()), - Some([RoundId::new(1)].into()), + Some([(1.into(), RequiredMessageParts::direct_message_only())].into()), + Some([1.into()].into()), ), } } @@ -58,7 +58,7 @@ impl ProtocolError for SimpleProtocolError { SimpleProtocolError::Round2InvalidPosition => { let _r1_message = message.direct_message.deserialize::(deserializer)?; let r1_echos_serialized = combined_echos - .get(&RoundId::new(1)) + .get(&1.into()) .ok_or_else(|| LocalError::new("Could not find combined echos for Round 1"))?; // Deserialize the echos @@ -84,8 +84,8 @@ impl Protocol for SimpleProtocol { message: &DirectMessage, ) -> Result<(), MessageValidationError> { match round_id { - r if r == &RoundId::new(1) => message.verify_is_not::(deserializer), - r if r == &RoundId::new(2) => message.verify_is_not::(deserializer), + r if r == &1 => message.verify_is_not::(deserializer), + r if r == &2 => message.verify_is_not::(deserializer), _ => Err(MessageValidationError::InvalidEvidence("Invalid round number".into())), } } @@ -96,8 +96,8 @@ impl Protocol for SimpleProtocol { message: &EchoBroadcast, ) -> Result<(), MessageValidationError> { match round_id { - r if r == &RoundId::new(1) => message.verify_is_some(), - r if r == &RoundId::new(2) => message.verify_is_not::(deserializer), + r if r == &1 => message.verify_is_some(), + r if r == &2 => message.verify_is_not::(deserializer), _ => Err(MessageValidationError::InvalidEvidence("Invalid round number".into())), } } @@ -107,7 +107,7 @@ impl Protocol for SimpleProtocol { round_id: &RoundId, message: &NormalBroadcast, ) -> Result<(), MessageValidationError> { - if round_id == &RoundId::new(1) || round_id == &RoundId::new(2) { + if round_id == &1 || round_id == &2 { message.verify_is_some() } else { Err(MessageValidationError::InvalidEvidence("Invalid round number".into())) @@ -193,11 +193,11 @@ impl Round for Round1 { type Protocol = SimpleProtocol; fn id(&self) -> RoundId { - RoundId::new(1) + 1.into() } fn possible_next_rounds(&self) -> BTreeSet { - [RoundId::new(2)].into() + [2.into()].into() } fn message_destinations(&self) -> &BTreeSet { @@ -317,7 +317,7 @@ impl Round for Round2 { type Protocol = SimpleProtocol; fn id(&self) -> RoundId { - RoundId::new(2) + 2.into() } fn possible_next_rounds(&self) -> BTreeSet { diff --git a/examples/src/simple_malicious.rs b/examples/src/simple_malicious.rs index bd27d72..a4bfbef 100644 --- a/examples/src/simple_malicious.rs +++ b/examples/src/simple_malicious.rs @@ -6,7 +6,7 @@ use manul::{ dev::{run_sync, BinaryFormat, TestSessionParams, TestSigner}, protocol::{ Artifact, BoxedRound, Deserializer, DirectMessage, EntryPoint, LocalError, PartyId, ProtocolMessagePart, - RoundId, Serializer, + Serializer, }, signature::Keypair, }; @@ -37,7 +37,7 @@ impl Misbehaving for MaliciousLogic { direct_message: DirectMessage, artifact: Option, ) -> Result<(DirectMessage, Option), LocalError> { - let dm = if round.id() == RoundId::new(1) { + let dm = if round.id() == 1 { match behavior { Behavior::SerializedGarbage => DirectMessage::new(serializer, [99u8])?, Behavior::AttributableFailure => { @@ -50,7 +50,7 @@ impl Misbehaving for MaliciousLogic { } _ => direct_message, } - } else if round.id() == RoundId::new(2) { + } else if round.id() == 2 { match behavior { Behavior::AttributableFailureRound2 => { let round2 = round.downcast_ref::>()?; diff --git a/manul/benches/empty_rounds.rs b/manul/benches/empty_rounds.rs index c121eb4..e64d962 100644 --- a/manul/benches/empty_rounds.rs +++ b/manul/benches/empty_rounds.rs @@ -90,14 +90,14 @@ impl Round for EmptyRound { type Protocol = EmptyProtocol; fn id(&self) -> RoundId { - RoundId::new(self.round_counter) + self.round_counter.into() } fn possible_next_rounds(&self) -> BTreeSet { if self.inputs.rounds_num == self.round_counter { BTreeSet::new() } else { - [RoundId::new(self.round_counter + 1)].into() + [(self.round_counter + 1).into()].into() } } diff --git a/manul/src/protocol/round.rs b/manul/src/protocol/round.rs index d08e936..38642cb 100644 --- a/manul/src/protocol/round.rs +++ b/manul/src/protocol/round.rs @@ -142,6 +142,18 @@ impl RoundId { } } +impl From for RoundId { + fn from(source: u8) -> Self { + Self::new(source) + } +} + +impl PartialEq for RoundId { + fn eq(&self, rhs: &u8) -> bool { + self == &RoundId::new(*rhs) + } +} + /// A distributed protocol. pub trait Protocol: 'static { /// The successful result of an execution of this protocol. diff --git a/manul/src/tests/partial_echo.rs b/manul/src/tests/partial_echo.rs index e5a1f9c..7692520 100644 --- a/manul/src/tests/partial_echo.rs +++ b/manul/src/tests/partial_echo.rs @@ -84,7 +84,7 @@ impl Deserialize<'de>> Round for Round1; fn id(&self) -> RoundId { - RoundId::new(1) + 1.into() } fn possible_next_rounds(&self) -> BTreeSet { From c18a3f5e91b752f4911121464a32fe5a5478386a Mon Sep 17 00:00:00 2001 From: Bogdan Opanchuk Date: Sun, 5 Jan 2025 10:05:48 -0800 Subject: [PATCH 2/2] Rename `Round::entry_round()` to `entry_round_id()` and make it mandatory to implement --- CHANGELOG.md | 1 + examples/src/simple.rs | 5 +++++ manul/benches/empty_rounds.rs | 5 +++++ manul/src/combinators/chain.rs | 8 ++++---- manul/src/combinators/misbehave.rs | 4 ++++ manul/src/protocol/round.rs | 4 +--- manul/src/tests/partial_echo.rs | 5 +++++ 7 files changed, 25 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index aa8dc71..602a4e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Using a single `ProtocolError::required_messages()` instead of multiple methods. ([#79]) - `Protocol::verify_*_is_invalid()` are now mandatory to implement. ([#79]) - Removed the RNG parameter from `Round::receive_message()` and `Session::process_message()`. ([#83]) +- Renamed `Round::entry_round()` to `entry_round_id()` and made it mandatory to implement. ([#84]) ### Added diff --git a/examples/src/simple.rs b/examples/src/simple.rs index 6976251..ad5be1f 100644 --- a/examples/src/simple.rs +++ b/examples/src/simple.rs @@ -161,6 +161,11 @@ impl SimpleProtocolEntryPoint { impl EntryPoint for SimpleProtocolEntryPoint { type Protocol = SimpleProtocol; + + fn entry_round_id() -> RoundId { + 1.into() + } + fn make_round( self, _rng: &mut impl CryptoRngCore, diff --git a/manul/benches/empty_rounds.rs b/manul/benches/empty_rounds.rs index e64d962..8b59377 100644 --- a/manul/benches/empty_rounds.rs +++ b/manul/benches/empty_rounds.rs @@ -73,6 +73,11 @@ struct Round1Artifact; impl EntryPoint for Inputs { type Protocol = EmptyProtocol; + + fn entry_round_id() -> RoundId { + 1.into() + } + fn make_round( self, _rng: &mut impl CryptoRngCore, diff --git a/manul/src/combinators/chain.rs b/manul/src/combinators/chain.rs index a44beac..41a0cc8 100644 --- a/manul/src/combinators/chain.rs +++ b/manul/src/combinators/chain.rs @@ -295,8 +295,8 @@ where { type Protocol = T::Protocol; - fn entry_round() -> RoundId { - >::EntryPoint::entry_round().group_under(1) + fn entry_round_id() -> RoundId { + >::EntryPoint::entry_round_id().group_under(1) } fn make_round( @@ -368,8 +368,8 @@ where .collect::>(); if round.as_ref().may_produce_result() { - tracing::debug!("Adding {}", T::EntryPoint::entry_round().group_under(2)); - next_rounds.insert(T::EntryPoint::entry_round().group_under(2)); + tracing::debug!("Adding {}", T::EntryPoint::entry_round_id().group_under(2)); + next_rounds.insert(T::EntryPoint::entry_round_id().group_under(2)); } next_rounds diff --git a/manul/src/combinators/misbehave.rs b/manul/src/combinators/misbehave.rs index 45c5a6f..fdcb16c 100644 --- a/manul/src/combinators/misbehave.rs +++ b/manul/src/combinators/misbehave.rs @@ -138,6 +138,10 @@ where { type Protocol = >::Protocol; + fn entry_round_id() -> RoundId { + M::EntryPoint::entry_round_id() + } + fn make_round( self, rng: &mut impl CryptoRngCore, diff --git a/manul/src/protocol/round.rs b/manul/src/protocol/round.rs index 38642cb..a5e5cf0 100644 --- a/manul/src/protocol/round.rs +++ b/manul/src/protocol/round.rs @@ -398,9 +398,7 @@ pub trait EntryPoint { type Protocol: Protocol; /// Returns the ID of the round returned by [`Self::make_round`]. - fn entry_round() -> RoundId { - RoundId::new(1) - } + fn entry_round_id() -> RoundId; /// Creates the round. /// diff --git a/manul/src/tests/partial_echo.rs b/manul/src/tests/partial_echo.rs index 7692520..fa9ad75 100644 --- a/manul/src/tests/partial_echo.rs +++ b/manul/src/tests/partial_echo.rs @@ -70,6 +70,11 @@ struct Round1Echo { impl Deserialize<'de>> EntryPoint for Inputs { type Protocol = PartialEchoProtocol; + + fn entry_round_id() -> RoundId { + 1.into() + } + fn make_round( self, _rng: &mut impl CryptoRngCore,