Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge Round methods #93

Merged
merged 2 commits into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Rework `RequiredMessageParts` API. ([#85])
- Removed `Send + Sync` bound on `WireFormat`. ([#92])
- Removed `Send` bound on `ProtocolError`. ([#92])
- Merged `Round::id()`, `possible_next_rounds()` and `may_produce_result()` into `transition_info()`. ([#93])
- Merged `Round::message_destinations()`, `expecting_messages_from()` and `echo_round_participation()` into `communication_info()`. ([#93])


### Added
Expand Down Expand Up @@ -61,6 +63,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[#90]: https://github.com/entropyxyz/manul/pull/90
[#91]: https://github.com/entropyxyz/manul/pull/91
[#92]: https://github.com/entropyxyz/manul/pull/92
[#93]: https://github.com/entropyxyz/manul/pull/93


## [0.1.0] - 2024-11-19
Expand Down
42 changes: 11 additions & 31 deletions examples/src/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use alloc::collections::{BTreeMap, BTreeSet};
use core::fmt::Debug;

use manul::protocol::{
Artifact, BoxedRound, Deserializer, DirectMessage, EchoBroadcast, EntryPoint, FinalizeOutcome, LocalError,
MessageValidationError, NormalBroadcast, PartyId, Payload, Protocol, ProtocolError, ProtocolMessage,
Artifact, BoxedRound, CommunicationInfo, Deserializer, DirectMessage, EchoBroadcast, EntryPoint, FinalizeOutcome,
LocalError, MessageValidationError, NormalBroadcast, PartyId, Payload, Protocol, ProtocolError, ProtocolMessage,
ProtocolMessagePart, ProtocolValidationError, ReceiveError, RequiredMessageParts, RequiredMessages, Round, RoundId,
Serializer,
Serializer, TransitionInfo,
};
use rand_core::CryptoRngCore;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -195,16 +195,12 @@ impl<Id: PartyId> EntryPoint<Id> for SimpleProtocolEntryPoint<Id> {
impl<Id: PartyId> Round<Id> for Round1<Id> {
type Protocol = SimpleProtocol;

fn id(&self) -> RoundId {
1.into()
}

fn possible_next_rounds(&self) -> BTreeSet<RoundId> {
[2.into()].into()
fn transition_info(&self) -> TransitionInfo {
TransitionInfo::new_linear(1)
}

fn message_destinations(&self) -> &BTreeSet<Id> {
&self.context.other_ids
fn communication_info(&self) -> CommunicationInfo<Id> {
CommunicationInfo::regular(&self.context.other_ids)
}

fn make_normal_broadcast(
Expand Down Expand Up @@ -298,10 +294,6 @@ impl<Id: PartyId> Round<Id> for Round1<Id> {
});
Ok(FinalizeOutcome::AnotherRound(round2))
}

fn expecting_messages_from(&self) -> &BTreeSet<Id> {
&self.context.other_ids
}
}

#[derive(Debug)]
Expand All @@ -319,20 +311,12 @@ pub(crate) struct Round2Message {
impl<Id: PartyId> Round<Id> for Round2<Id> {
type Protocol = SimpleProtocol;

fn id(&self) -> RoundId {
2.into()
fn transition_info(&self) -> TransitionInfo {
TransitionInfo::new_linear_terminating(2)
}

fn possible_next_rounds(&self) -> BTreeSet<RoundId> {
BTreeSet::new()
}

fn may_produce_result(&self) -> bool {
true
}

fn message_destinations(&self) -> &BTreeSet<Id> {
&self.context.other_ids
fn communication_info(&self) -> CommunicationInfo<Id> {
CommunicationInfo::regular(&self.context.other_ids)
}

fn make_direct_message(
Expand Down Expand Up @@ -394,10 +378,6 @@ impl<Id: PartyId> Round<Id> for Round2<Id> {

Ok(FinalizeOutcome::Result(sum + self.round1_sum))
}

fn expecting_messages_from(&self) -> &BTreeSet<Id> {
&self.context.other_ids
}
}

#[cfg(test)]
Expand Down
28 changes: 8 additions & 20 deletions manul/benches/async_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use criterion::{criterion_group, criterion_main, Criterion};
use manul::{
dev::{tokio::run_async, BinaryFormat, TestSessionParams, TestSigner},
protocol::{
Artifact, BoxedRound, Deserializer, DirectMessage, EchoBroadcast, EntryPoint, FinalizeOutcome, LocalError,
MessageValidationError, NoProtocolErrors, NormalBroadcast, PartyId, Payload, Protocol, ProtocolMessage,
ProtocolMessagePart, ReceiveError, Round, RoundId, Serializer,
Artifact, BoxedRound, CommunicationInfo, Deserializer, DirectMessage, EchoBroadcast, EntryPoint,
FinalizeOutcome, LocalError, MessageValidationError, NoProtocolErrors, NormalBroadcast, PartyId, Payload,
Protocol, ProtocolMessage, ProtocolMessagePart, ReceiveError, Round, RoundId, Serializer, TransitionInfo,
},
signature::Keypair,
};
Expand Down Expand Up @@ -105,24 +105,16 @@ impl<Id: PartyId> EntryPoint<Id> for Inputs<Id> {
impl<Id: PartyId> Round<Id> for EmptyRound<Id> {
type Protocol = EmptyProtocol;

fn id(&self) -> RoundId {
self.round_counter.into()
}

fn possible_next_rounds(&self) -> BTreeSet<RoundId> {
fn transition_info(&self) -> TransitionInfo {
if self.inputs.rounds_num == self.round_counter {
BTreeSet::new()
TransitionInfo::new_linear_terminating(self.round_counter)
} else {
[(self.round_counter + 1).into()].into()
TransitionInfo::new_linear(self.round_counter)
}
}

fn may_produce_result(&self) -> bool {
self.inputs.rounds_num == self.round_counter
}

fn message_destinations(&self) -> &BTreeSet<Id> {
&self.inputs.other_ids
fn communication_info(&self) -> CommunicationInfo<Id> {
CommunicationInfo::regular(&self.inputs.other_ids)
}

fn make_echo_broadcast(
Expand Down Expand Up @@ -193,10 +185,6 @@ impl<Id: PartyId> Round<Id> for EmptyRound<Id> {
Ok(FinalizeOutcome::AnotherRound(round))
}
}

fn expecting_messages_from(&self) -> &BTreeSet<Id> {
&self.inputs.other_ids
}
}

fn bench_async_session(c: &mut Criterion) {
Expand Down
28 changes: 8 additions & 20 deletions manul/benches/empty_rounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use criterion::{criterion_group, criterion_main, Criterion};
use manul::{
dev::{run_sync, BinaryFormat, TestSessionParams, TestSigner},
protocol::{
Artifact, BoxedRound, Deserializer, DirectMessage, EchoBroadcast, EntryPoint, FinalizeOutcome, LocalError,
MessageValidationError, NoProtocolErrors, NormalBroadcast, PartyId, Payload, Protocol, ProtocolMessage,
ProtocolMessagePart, ReceiveError, Round, RoundId, Serializer,
Artifact, BoxedRound, CommunicationInfo, Deserializer, DirectMessage, EchoBroadcast, EntryPoint,
FinalizeOutcome, LocalError, MessageValidationError, NoProtocolErrors, NormalBroadcast, PartyId, Payload,
Protocol, ProtocolMessage, ProtocolMessagePart, ReceiveError, Round, RoundId, Serializer, TransitionInfo,
},
signature::Keypair,
};
Expand Down Expand Up @@ -94,24 +94,16 @@ impl<Id: PartyId> EntryPoint<Id> for Inputs<Id> {
impl<Id: PartyId> Round<Id> for EmptyRound<Id> {
type Protocol = EmptyProtocol;

fn id(&self) -> RoundId {
self.round_counter.into()
}

fn possible_next_rounds(&self) -> BTreeSet<RoundId> {
fn transition_info(&self) -> TransitionInfo {
if self.inputs.rounds_num == self.round_counter {
BTreeSet::new()
TransitionInfo::new_linear_terminating(self.round_counter)
} else {
[(self.round_counter + 1).into()].into()
TransitionInfo::new_linear(self.round_counter)
}
}

fn may_produce_result(&self) -> bool {
self.inputs.rounds_num == self.round_counter
}

fn message_destinations(&self) -> &BTreeSet<Id> {
&self.inputs.other_ids
fn communication_info(&self) -> CommunicationInfo<Id> {
CommunicationInfo::regular(&self.inputs.other_ids)
}

fn make_echo_broadcast(
Expand Down Expand Up @@ -180,10 +172,6 @@ impl<Id: PartyId> Round<Id> for EmptyRound<Id> {
Ok(FinalizeOutcome::AnotherRound(round))
}
}

fn expecting_messages_from(&self) -> &BTreeSet<Id> {
&self.inputs.other_ids
}
}

fn bench_empty_rounds(c: &mut Criterion) {
Expand Down
72 changes: 15 additions & 57 deletions manul/src/combinators/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,17 @@ Usage:
when verifying evidence from the chained protocol.
*/

use alloc::{
boxed::Box,
collections::{BTreeMap, BTreeSet},
};
use alloc::{boxed::Box, collections::BTreeMap};
use core::fmt::{self, Debug};

use rand_core::CryptoRngCore;
use serde::{Deserialize, Serialize};

use crate::protocol::{
Artifact, BoxedRng, BoxedRound, Deserializer, DirectMessage, EchoBroadcast, EchoRoundParticipation, EntryPoint,
Artifact, BoxedRng, BoxedRound, CommunicationInfo, Deserializer, DirectMessage, EchoBroadcast, EntryPoint,
FinalizeOutcome, LocalError, MessageValidationError, NormalBroadcast, ObjectSafeRound, PartyId, Payload, Protocol,
ProtocolError, ProtocolMessage, ProtocolValidationError, ReceiveError, RequiredMessages, RoundId, Serializer,
TransitionInfo,
};

/// A marker trait that is used to disambiguate blanket trait implementations for [`Protocol`] and [`EntryPoint`].
Expand Down Expand Up @@ -180,11 +178,11 @@ where
) -> Result<(), ProtocolValidationError> {
let previous_messages = previous_messages
.into_iter()
.map(|(round_id, message)| round_id.ungroup().map(|round_id| (round_id, message)))
.map(|(round_id, message)| round_id.split_group().map(|(_group_num, round_id)| (round_id, message)))
.collect::<Result<BTreeMap<_, _>, _>>()?;
let combined_echos = combined_echos
.into_iter()
.map(|(round_id, message)| round_id.ungroup().map(|round_id| (round_id, message)))
.map(|(round_id, message)| round_id.split_group().map(|(_group_num, round_id)| (round_id, message)))
.collect::<Result<BTreeMap<_, _>, _>>()?;

match self {
Expand Down Expand Up @@ -350,64 +348,24 @@ where
{
type Protocol = T::Protocol;

fn id(&self) -> RoundId {
match &self.state {
ChainState::Protocol1 { round, .. } => round.as_ref().id().group_under(1),
ChainState::Protocol2(round) => round.as_ref().id().group_under(2),
}
}

fn possible_next_rounds(&self) -> BTreeSet<RoundId> {
fn transition_info(&self) -> TransitionInfo {
match &self.state {
ChainState::Protocol1 { round, .. } => {
let mut next_rounds = round
.as_ref()
.possible_next_rounds()
.into_iter()
.map(|round_id| round_id.group_under(1))
.collect::<BTreeSet<_>>();

if round.as_ref().may_produce_result() {
tracing::debug!("Adding {}", T::EntryPoint::entry_round_id().group_under(2));
next_rounds.insert(T::EntryPoint::entry_round_id().group_under(2));
let mut tinfo = round.as_ref().transition_info().group_under(1);
if tinfo.may_produce_result {
tinfo.may_produce_result = false;
tinfo.children.insert(T::EntryPoint::entry_round_id().group_under(2));
}

next_rounds
tinfo
}
ChainState::Protocol2(round) => round
.as_ref()
.possible_next_rounds()
.into_iter()
.map(|round_id| round_id.group_under(2))
.collect(),
}
}

fn may_produce_result(&self) -> bool {
match &self.state {
ChainState::Protocol1 { .. } => false,
ChainState::Protocol2(round) => round.as_ref().may_produce_result(),
}
}

fn message_destinations(&self) -> &BTreeSet<Id> {
match &self.state {
ChainState::Protocol1 { round, .. } => round.as_ref().message_destinations(),
ChainState::Protocol2(round) => round.as_ref().message_destinations(),
}
}

fn expecting_messages_from(&self) -> &BTreeSet<Id> {
match &self.state {
ChainState::Protocol1 { round, .. } => round.as_ref().expecting_messages_from(),
ChainState::Protocol2(round) => round.as_ref().expecting_messages_from(),
ChainState::Protocol2(round) => round.as_ref().transition_info().group_under(2),
}
}

fn echo_round_participation(&self) -> EchoRoundParticipation<Id> {
fn communication_info(&self) -> CommunicationInfo<Id> {
match &self.state {
ChainState::Protocol1 { round, .. } => round.as_ref().echo_round_participation(),
ChainState::Protocol2(round) => round.as_ref().echo_round_participation(),
ChainState::Protocol1 { round, .. } => round.as_ref().communication_info(),
ChainState::Protocol2(round) => round.as_ref().communication_info(),
}
}

Expand Down
33 changes: 7 additions & 26 deletions manul/src/combinators/misbehave.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,15 @@ Usage:
as the entry point of the new protocol.
*/

use alloc::{
boxed::Box,
collections::{BTreeMap, BTreeSet},
};
use alloc::{boxed::Box, collections::BTreeMap};
use core::fmt::Debug;

use rand_core::CryptoRngCore;

use crate::protocol::{
Artifact, BoxedRng, BoxedRound, Deserializer, DirectMessage, EchoBroadcast, EchoRoundParticipation, EntryPoint,
Artifact, BoxedRng, BoxedRound, CommunicationInfo, Deserializer, DirectMessage, EchoBroadcast, EntryPoint,
FinalizeOutcome, LocalError, NormalBroadcast, ObjectSafeRound, PartyId, Payload, Protocol, ProtocolMessage,
ReceiveError, RoundId, Serializer,
ReceiveError, RoundId, Serializer, TransitionInfo,
};

/// A trait describing required properties for a behavior type.
Expand Down Expand Up @@ -233,28 +230,12 @@ where
{
type Protocol = <M::EntryPoint as EntryPoint<Id>>::Protocol;

fn id(&self) -> RoundId {
self.round.as_ref().id()
}

fn possible_next_rounds(&self) -> BTreeSet<RoundId> {
self.round.as_ref().possible_next_rounds()
}

fn may_produce_result(&self) -> bool {
self.round.as_ref().may_produce_result()
}

fn message_destinations(&self) -> &BTreeSet<Id> {
self.round.as_ref().message_destinations()
}

fn expecting_messages_from(&self) -> &BTreeSet<Id> {
self.round.as_ref().expecting_messages_from()
fn transition_info(&self) -> TransitionInfo {
self.round.as_ref().transition_info()
}

fn echo_round_participation(&self) -> EchoRoundParticipation<Id> {
self.round.as_ref().echo_round_participation()
fn communication_info(&self) -> CommunicationInfo<Id> {
self.round.as_ref().communication_info()
}

fn make_direct_message(
Expand Down
Loading
Loading