Skip to content

Commit

Permalink
Merge Round::id, possible_next_rounds and may_produce_result
Browse files Browse the repository at this point in the history
  • Loading branch information
fjarri committed Feb 11, 2025
1 parent 9810d01 commit 87884b4
Show file tree
Hide file tree
Showing 11 changed files with 194 additions and 146 deletions.
22 changes: 5 additions & 17 deletions examples/src/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use manul::protocol::{
Artifact, BoxedRound, Deserializer, DirectMessage, EchoBroadcast, EntryPoint, FinalizeOutcome, LocalError,
MessageValidationError, NormalBroadcast, PartyId, Payload, Protocol, ProtocolError, ProtocolMessage,
ProtocolMessagePart, ProtocolValidationError, ReceiveError, RequiredMessageParts, RequiredMessages, Round, RoundId,
Serializer,
RoundTransition, Serializer,
};
use rand_core::CryptoRngCore;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -195,12 +195,8 @@ 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) -> RoundTransition {
RoundTransition::regular(1)
}

fn message_destinations(&self) -> &BTreeSet<Id> {
Expand Down Expand Up @@ -319,16 +315,8 @@ pub(crate) struct Round2Message {
impl<Id: PartyId> Round<Id> for Round2<Id> {
type Protocol = SimpleProtocol;

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

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

fn may_produce_result(&self) -> bool {
true
fn transition_info(&self) -> RoundTransition {
RoundTransition::result(2)
}

fn message_destinations(&self) -> &BTreeSet<Id> {
Expand Down
16 changes: 4 additions & 12 deletions manul/benches/async_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use manul::{
protocol::{
Artifact, BoxedRound, Deserializer, DirectMessage, EchoBroadcast, EntryPoint, FinalizeOutcome, LocalError,
MessageValidationError, NoProtocolErrors, NormalBroadcast, PartyId, Payload, Protocol, ProtocolMessage,
ProtocolMessagePart, ReceiveError, Round, RoundId, Serializer,
ProtocolMessagePart, ReceiveError, Round, RoundId, RoundTransition, Serializer,
},
signature::Keypair,
};
Expand Down Expand Up @@ -105,22 +105,14 @@ 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) -> RoundTransition {
if self.inputs.rounds_num == self.round_counter {
BTreeSet::new()
RoundTransition::result(self.round_counter)
} else {
[(self.round_counter + 1).into()].into()
RoundTransition::regular(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
}
Expand Down
16 changes: 4 additions & 12 deletions manul/benches/empty_rounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use manul::{
protocol::{
Artifact, BoxedRound, Deserializer, DirectMessage, EchoBroadcast, EntryPoint, FinalizeOutcome, LocalError,
MessageValidationError, NoProtocolErrors, NormalBroadcast, PartyId, Payload, Protocol, ProtocolMessage,
ProtocolMessagePart, ReceiveError, Round, RoundId, Serializer,
ProtocolMessagePart, ReceiveError, Round, RoundId, RoundTransition, Serializer,
},
signature::Keypair,
};
Expand Down Expand Up @@ -94,22 +94,14 @@ 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) -> RoundTransition {
if self.inputs.rounds_num == self.round_counter {
BTreeSet::new()
RoundTransition::result(self.round_counter)
} else {
[(self.round_counter + 1).into()].into()
RoundTransition::regular(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
}
Expand Down
43 changes: 9 additions & 34 deletions manul/src/combinators/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ use serde::{Deserialize, Serialize};
use crate::protocol::{
Artifact, BoxedRng, BoxedRound, Deserializer, DirectMessage, EchoBroadcast, EchoRoundParticipation, EntryPoint,
FinalizeOutcome, LocalError, MessageValidationError, NormalBroadcast, ObjectSafeRound, PartyId, Payload, Protocol,
ProtocolError, ProtocolMessage, ProtocolValidationError, ReceiveError, RequiredMessages, RoundId, Serializer,
ProtocolError, ProtocolMessage, ProtocolValidationError, ReceiveError, RequiredMessages, RoundId, RoundTransition,
Serializer,
};

/// A marker trait that is used to disambiguate blanket trait implementations for [`Protocol`] and [`EntryPoint`].
Expand Down Expand Up @@ -350,43 +351,17 @@ 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) -> RoundTransition {
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(),
ChainState::Protocol2(round) => round.as_ref().transition_info().group_under(2),
}
}

Expand Down
14 changes: 3 additions & 11 deletions manul/src/combinators/misbehave.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use rand_core::CryptoRngCore;
use crate::protocol::{
Artifact, BoxedRng, BoxedRound, Deserializer, DirectMessage, EchoBroadcast, EchoRoundParticipation, EntryPoint,
FinalizeOutcome, LocalError, NormalBroadcast, ObjectSafeRound, PartyId, Payload, Protocol, ProtocolMessage,
ReceiveError, RoundId, Serializer,
ReceiveError, RoundId, RoundTransition, Serializer,
};

/// A trait describing required properties for a behavior type.
Expand Down Expand Up @@ -233,16 +233,8 @@ 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 transition_info(&self) -> RoundTransition {
self.round.as_ref().transition_info()
}

fn message_destinations(&self) -> &BTreeSet<Id> {
Expand Down
2 changes: 1 addition & 1 deletion manul/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub use message::{DirectMessage, EchoBroadcast, NormalBroadcast, ProtocolMessage
pub use object_safe::BoxedRound;
pub use round::{
Artifact, EchoRoundParticipation, EntryPoint, FinalizeOutcome, NoProtocolErrors, PartyId, Payload, Protocol,
ProtocolError, RequiredMessageParts, RequiredMessages, Round, RoundId,
ProtocolError, RequiredMessageParts, RequiredMessages, Round, RoundId, RoundTransition,
};
pub use serialization::{Deserializer, Serializer};

Expand Down
28 changes: 11 additions & 17 deletions manul/src/protocol/object_safe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ use rand_core::{CryptoRng, CryptoRngCore, RngCore};
use super::{
errors::{LocalError, ReceiveError},
message::{DirectMessage, EchoBroadcast, NormalBroadcast, ProtocolMessage},
round::{Artifact, EchoRoundParticipation, FinalizeOutcome, PartyId, Payload, Protocol, Round, RoundId},
round::{
Artifact, EchoRoundParticipation, FinalizeOutcome, PartyId, Payload, Protocol, Round, RoundId, RoundTransition,
},
serialization::{Deserializer, Serializer},
};

Expand Down Expand Up @@ -42,11 +44,7 @@ impl RngCore for BoxedRng<'_> {
pub(crate) trait ObjectSafeRound<Id: PartyId>: 'static + Debug + Send + Sync {
type Protocol: Protocol<Id>;

fn id(&self) -> RoundId;

fn possible_next_rounds(&self) -> BTreeSet<RoundId>;

fn may_produce_result(&self) -> bool;
fn transition_info(&self) -> RoundTransition;

fn message_destinations(&self) -> &BTreeSet<Id>;

Expand Down Expand Up @@ -123,16 +121,8 @@ where
{
type Protocol = <R as Round<Id>>::Protocol;

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

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

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

fn message_destinations(&self) -> &BTreeSet<Id> {
Expand Down Expand Up @@ -276,6 +266,10 @@ impl<Id: PartyId, P: Protocol<Id>> BoxedRound<Id, P> {

/// Returns the round's ID.
pub fn id(&self) -> RoundId {
self.round.id()
self.round.transition_info().id()
}

pub(crate) fn transition_info(&self) -> RoundTransition {
self.round.transition_info()
}
}
Loading

0 comments on commit 87884b4

Please sign in to comment.