Skip to content

Commit

Permalink
Merge communication info methods of Round into a single method
Browse files Browse the repository at this point in the history
  • Loading branch information
fjarri committed Feb 16, 2025
1 parent bc66749 commit 49296fa
Show file tree
Hide file tree
Showing 12 changed files with 112 additions and 153 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- 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
20 changes: 6 additions & 14 deletions examples/src/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ 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, TransitionInfo,
};
Expand Down Expand Up @@ -199,8 +199,8 @@ impl<Id: PartyId> Round<Id> for Round1<Id> {
TransitionInfo::regular(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 @@ -294,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,8 +315,8 @@ impl<Id: PartyId> Round<Id> for Round2<Id> {
TransitionInfo::result(2)
}

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 @@ -382,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
14 changes: 5 additions & 9 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, TransitionInfo,
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 @@ -113,8 +113,8 @@ impl<Id: PartyId> Round<Id> for EmptyRound<Id> {
}
}

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 @@ -185,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
14 changes: 5 additions & 9 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, TransitionInfo,
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 @@ -102,8 +102,8 @@ impl<Id: PartyId> Round<Id> for EmptyRound<Id> {
}
}

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 @@ -172,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
27 changes: 5 additions & 22 deletions manul/src/combinators/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,14 @@ 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,
Expand Down Expand Up @@ -365,24 +362,10 @@ where
}
}

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(),
}
}

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
19 changes: 4 additions & 15 deletions manul/src/combinators/misbehave.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,13 @@ 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, TransitionInfo,
};
Expand Down Expand Up @@ -237,16 +234,8 @@ where
self.round.as_ref().transition_info()
}

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 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
4 changes: 2 additions & 2 deletions manul/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ pub use errors::{
pub use message::{DirectMessage, EchoBroadcast, NormalBroadcast, ProtocolMessage, ProtocolMessagePart};
pub use object_safe::BoxedRound;
pub use round::{
Artifact, EchoRoundParticipation, EntryPoint, FinalizeOutcome, NoProtocolErrors, PartyId, Payload, Protocol,
ProtocolError, RequiredMessageParts, RequiredMessages, Round,
Artifact, CommunicationInfo, EchoRoundParticipation, EntryPoint, FinalizeOutcome, NoProtocolErrors, PartyId,
Payload, Protocol, ProtocolError, RequiredMessageParts, RequiredMessages, Round,
};
pub use round_id::{RoundId, TransitionInfo};
pub use serialization::{Deserializer, Serializer};
Expand Down
26 changes: 5 additions & 21 deletions manul/src/protocol/object_safe.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
use alloc::{
boxed::Box,
collections::{BTreeMap, BTreeSet},
format,
};
use alloc::{boxed::Box, collections::BTreeMap, format};
use core::{fmt::Debug, marker::PhantomData};

use rand_core::{CryptoRng, CryptoRngCore, RngCore};

use super::{
errors::{LocalError, ReceiveError},
message::{DirectMessage, EchoBroadcast, NormalBroadcast, ProtocolMessage},
round::{Artifact, EchoRoundParticipation, FinalizeOutcome, PartyId, Payload, Protocol, Round},
round::{Artifact, CommunicationInfo, FinalizeOutcome, PartyId, Payload, Protocol, Round},
round_id::{RoundId, TransitionInfo},
serialization::{Deserializer, Serializer},
};
Expand Down Expand Up @@ -45,11 +41,7 @@ pub(crate) trait ObjectSafeRound<Id: PartyId>: 'static + Debug + Send + Sync {

fn transition_info(&self) -> TransitionInfo;

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

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

fn echo_round_participation(&self) -> EchoRoundParticipation<Id>;
fn communication_info(&self) -> CommunicationInfo<Id>;

fn make_direct_message(
&self,
Expand Down Expand Up @@ -124,16 +116,8 @@ where
self.round.transition_info()
}

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

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

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

fn make_direct_message(
Expand Down
64 changes: 41 additions & 23 deletions manul/src/protocol/round.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,43 @@ use super::{
serialization::{Deserializer, Serializer},
};

/// Describes what other parties this rounds sends messages to, and what other parties it expects messages from.
#[derive(Debug, Clone)]
pub struct CommunicationInfo<Id> {
/// The destinations of the messages to be sent out by this round.
///
/// The way it is interpreted by the execution layer is
/// - An echo broadcast (if any) is sent to all of these destinations;
/// - A direct message is sent to each of these destinations,
/// which means [`make_direct_message`](`Round::make_direct_message`) may be called
/// for each element of the returned set.
pub message_destinations: BTreeSet<Id>,

/// Returns the set of node IDs from which this round expects messages.
///
/// The execution layer will not call [`finalize`](`Round::finalize`) until all these nodes have responded
/// (and the corresponding [`receive_message`](`Round::receive_message`) finished successfully).
pub expecting_messages_from: BTreeSet<Id>,

/// Returns the specific way the node participates in the echo round following this round.
///
/// Returns [`EchoRoundParticipation::Default`] by default; this works fine when every node
/// sends messages to every other one, or do not send or receive any echo broadcasts.
/// Otherwise, review the options in [`EchoRoundParticipation`] and pick the appropriate one.
pub echo_round_participation: EchoRoundParticipation<Id>,
}

impl<Id: PartyId> CommunicationInfo<Id> {
/// A regular round that sends messages to all `other_parties`, and expects messages back from them.
pub fn regular(other_parties: &BTreeSet<Id>) -> Self {
Self {
message_destinations: other_parties.clone(),
expecting_messages_from: other_parties.clone(),
echo_round_participation: EchoRoundParticipation::Default,
}
}
}

/// Possible successful outcomes of [`Round::finalize`].
#[derive(Debug)]
pub enum FinalizeOutcome<Id: PartyId, P: Protocol<Id>> {
Expand Down Expand Up @@ -338,29 +375,10 @@ pub trait Round<Id: PartyId>: 'static + Debug + Send + Sync {
/// See [`TransitionInfo`] documentation for more details.
fn transition_info(&self) -> TransitionInfo;

/// The destinations of the messages to be sent out by this round.
/// Returns the information about the communication this rounds engages in with other nodes.
///
/// The way it is interpreted by the execution layer is
/// - An echo broadcast (if any) is sent to all of these destinations;
/// - A direct message is sent to each of these destinations,
/// which means [`make_direct_message`](`Self::make_direct_message`) may be called
/// for each element of the returned set.
fn message_destinations(&self) -> &BTreeSet<Id>;

/// Returns the set of node IDs from which this round expects messages.
///
/// The execution layer will not call [`finalize`](`Self::finalize`) until all these nodes have responded
/// (and the corresponding [`receive_message`](`Self::receive_message`) finished successfully).
fn expecting_messages_from(&self) -> &BTreeSet<Id>;

/// Returns the specific way the node participates in the echo round following this round.
///
/// Returns [`EchoRoundParticipation::Default`] by default; this works fine when every node
/// sends messages to every other one, or do not send or receive any echo broadcasts.
/// Otherwise, review the options in [`EchoRoundParticipation`] and pick the appropriate one.
fn echo_round_participation(&self) -> EchoRoundParticipation<Id> {
EchoRoundParticipation::Default
}
/// See [`CommunicationInfo`] documentation for more details.
fn communication_info(&self) -> CommunicationInfo<Id>;

/// Returns the direct message to the given destination and (maybe) an accompanying artifact.
///
Expand Down Expand Up @@ -399,7 +417,7 @@ pub trait Round<Id: PartyId>: 'static + Debug + Send + Sync {
/// Return [`NormalBroadcast::none`] if this round does not send normal broadcast messages.
/// This is also the blanket implementation.
///
/// Unlike the echo broadcasts, these will be just sent to every node from [`Self::message_destinations`]
/// Unlike the echo broadcasts, these will be just sent to every node defined in [`Self::communication_info`]
/// without any confirmation required.
fn make_normal_broadcast(
&self,
Expand Down
Loading

0 comments on commit 49296fa

Please sign in to comment.