Skip to content

Commit

Permalink
Remove the make_direct_message() shortcut
Browse files Browse the repository at this point in the history
  • Loading branch information
fjarri committed Nov 1, 2024
1 parent c98e50c commit aed78d0
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 62 deletions.
1 change: 0 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `SessionId::new()` renamed to `from_seed()`. ([#41])
- `FirstRound::new()` takes a `&[u8]` instead of a `SessionId` object. ([#41])
- The signatures of `Round::make_echo_broadcast()`, `Round::make_direct_message()`, and `Round::receive_message()`, take messages without `Option`s. ([#46])
- `Round::make_direct_message_with_artifact()` is the method returning an artifact now; `Round::make_direct_message()` is a shortcut for cases where no artifact is returned. ([#46])
- `Artifact::empty()` removed, the user should return `None` instead. ([#46])
- `EchoBroadcast` and `DirectMessage` now use `ProtocolMessagePart` trait for their methods. ([#47])
- Added normal broadcasts support in addition to echo ones; signatures of `Round` methods changed accordingly; added `Round::make_normal_broadcast()`. ([#47])
Expand Down
10 changes: 6 additions & 4 deletions examples/src/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,14 +228,15 @@ impl<Id: PartyId> Round<Id> for Round1<Id> {
_rng: &mut impl CryptoRngCore,
serializer: &Serializer,
destination: &Id,
) -> Result<DirectMessage, LocalError> {
) -> Result<(DirectMessage, Option<Artifact>), LocalError> {
debug!("{:?}: making direct message for {:?}", self.context.id, destination);

let message = Round1Message {
my_position: self.context.ids_to_positions[&self.context.id],
your_position: self.context.ids_to_positions[destination],
};
DirectMessage::new(serializer, message)
let dm = DirectMessage::new(serializer, message)?;
Ok((dm, None))
}

fn receive_message(
Expand Down Expand Up @@ -325,14 +326,15 @@ impl<Id: PartyId> Round<Id> for Round2<Id> {
_rng: &mut impl CryptoRngCore,
serializer: &Serializer,
destination: &Id,
) -> Result<DirectMessage, LocalError> {
) -> Result<(DirectMessage, Option<Artifact>), LocalError> {
debug!("{:?}: making direct message for {:?}", self.context.id, destination);

let message = Round2Message {
my_position: self.context.ids_to_positions[&self.context.id],
your_position: self.context.ids_to_positions[destination],
};
DirectMessage::new(serializer, message)
let dm = DirectMessage::new(serializer, message)?;
Ok((dm, None))
}

fn receive_message(
Expand Down
10 changes: 5 additions & 5 deletions examples/src/simple_malicious.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ impl<Id: PartyId> RoundOverride<Id> for MaliciousRound1<Id> {
rng: &mut impl CryptoRngCore,
serializer: &Serializer,
destination: &Id,
) -> Result<DirectMessage, LocalError> {
) -> Result<(DirectMessage, Option<Artifact>), LocalError> {
if matches!(self.behavior, Behavior::SerializedGarbage) {
DirectMessage::new(serializer, [99u8])
Ok((DirectMessage::new(serializer, [99u8])?, None))
} else if matches!(self.behavior, Behavior::AttributableFailure) {
let message = Round1Message {
my_position: self.round.context.ids_to_positions[&self.round.context.id],
your_position: self.round.context.ids_to_positions[&self.round.context.id],
};
DirectMessage::new(serializer, message)
Ok((DirectMessage::new(serializer, message)?, None))
} else {
self.inner_round_ref().make_direct_message(rng, serializer, destination)
}
Expand Down Expand Up @@ -131,13 +131,13 @@ impl<Id: PartyId> RoundOverride<Id> for MaliciousRound2<Id> {
rng: &mut impl CryptoRngCore,
serializer: &Serializer,
destination: &Id,
) -> Result<DirectMessage, LocalError> {
) -> Result<(DirectMessage, Option<Artifact>), LocalError> {
if matches!(self.behavior, Behavior::AttributableFailureRound2) {
let message = Round2Message {
my_position: self.round.context.ids_to_positions[&self.round.context.id],
your_position: self.round.context.ids_to_positions[&self.round.context.id],
};
DirectMessage::new(serializer, message)
Ok((DirectMessage::new(serializer, message)?, None))
} else {
self.inner_round_ref().make_direct_message(rng, serializer, destination)
}
Expand Down
2 changes: 1 addition & 1 deletion manul/benches/empty_rounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ impl<Id: PartyId> Round<Id> for EmptyRound<Id> {
}
}

fn make_direct_message_with_artifact(
fn make_direct_message(
&self,
_rng: &mut impl CryptoRngCore,
serializer: &Serializer,
Expand Down
7 changes: 3 additions & 4 deletions manul/src/protocol/object_safe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub(crate) trait ObjectSafeRound<Id: PartyId>: 'static + Debug + Send + Sync {

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

fn make_direct_message_with_artifact(
fn make_direct_message(
&self,
rng: &mut dyn CryptoRngCore,
serializer: &Serializer,
Expand Down Expand Up @@ -130,15 +130,14 @@ where
self.round.message_destinations()
}

fn make_direct_message_with_artifact(
fn make_direct_message(
&self,
rng: &mut dyn CryptoRngCore,
serializer: &Serializer,
destination: &Id,
) -> Result<(DirectMessage, Option<Artifact>), LocalError> {
let mut boxed_rng = BoxedRng(rng);
self.round
.make_direct_message_with_artifact(&mut boxed_rng, serializer, destination)
self.round.make_direct_message(&mut boxed_rng, serializer, destination)
}

fn make_echo_broadcast(
Expand Down
25 changes: 3 additions & 22 deletions manul/src/protocol/round.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,35 +358,16 @@ pub trait Round<Id: PartyId>: 'static + Debug + Send + Sync {
///
/// Return [`DirectMessage::none`] if this round does not send direct messages.
///
/// Falls back to [`make_direct_message`](`Self::make_direct_message`) if not implemented.
/// This is the method that will be called by the upper layer when creating direct messages.
///
/// In some protocols, when a message to another node is created, there is some associated information
/// that needs to be retained for later (randomness, proofs of knowledge, and so on).
/// These should be put in an [`Artifact`] and will be available at the time of [`finalize`](`Self::finalize`).
fn make_direct_message_with_artifact(
&self,
rng: &mut impl CryptoRngCore,
serializer: &Serializer,
destination: &Id,
) -> Result<(DirectMessage, Option<Artifact>), LocalError> {
Ok((self.make_direct_message(rng, serializer, destination)?, None))
}

/// Returns the direct message to the given destination.
///
/// This method will not be called by the upper layer directly,
/// only via [`make_direct_message_with_artifact`](`Self::make_direct_message_with_artifact`).
///
/// Return [`DirectMessage::none`] if this round does not send direct messages.
/// This is also the blanket implementation.
fn make_direct_message(
&self,
#[allow(unused_variables)] rng: &mut impl CryptoRngCore,
#[allow(unused_variables)] serializer: &Serializer,
#[allow(unused_variables)] destination: &Id,
) -> Result<DirectMessage, LocalError> {
Ok(DirectMessage::none())
) -> Result<(DirectMessage, Option<Artifact>), LocalError> {
Ok((DirectMessage::none(), None))
}

/// Returns the echo broadcast for this round.
Expand Down Expand Up @@ -438,7 +419,7 @@ pub trait Round<Id: PartyId>: 'static + Debug + Send + Sync {
///
/// `payloads` here are the ones previously generated by [`receive_message`](`Self::receive_message`),
/// and `artifacts` are the ones previously generated by
/// [`make_direct_message_with_artifact`](`Self::make_direct_message_with_artifact`).
/// [`make_direct_message`](`Self::make_direct_message`).
fn finalize(
self,
rng: &mut impl CryptoRngCore,
Expand Down
4 changes: 1 addition & 3 deletions manul/src/session/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,7 @@ where
rng: &mut impl CryptoRngCore,
destination: &SP::Verifier,
) -> Result<(Message<SP::Verifier>, ProcessedArtifact<SP>), LocalError> {
let (direct_message, artifact) =
self.round
.make_direct_message_with_artifact(rng, &self.serializer, destination)?;
let (direct_message, artifact) = self.round.make_direct_message(rng, &self.serializer, destination)?;

let message = Message::new::<SP>(
rng,
Expand Down
24 changes: 2 additions & 22 deletions manul/src/testing/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,13 @@ pub trait RoundWrapper<Id: PartyId>: 'static + Sized + Send + Sync {
///
/// The blanket implementations delegate to the methods of the wrapped round.
pub trait RoundOverride<Id: PartyId>: RoundWrapper<Id> {
/// An override for [`Round::make_direct_message_with_artifact`].
fn make_direct_message_with_artifact(
&self,
rng: &mut impl CryptoRngCore,
serializer: &Serializer,
destination: &Id,
) -> Result<(DirectMessage, Option<Artifact>), LocalError> {
let dm = self.make_direct_message(rng, serializer, destination)?;
Ok((dm, None))
}

/// An override for [`Round::make_direct_message`].
fn make_direct_message(
&self,
rng: &mut impl CryptoRngCore,
serializer: &Serializer,
destination: &Id,
) -> Result<DirectMessage, LocalError> {
) -> Result<(DirectMessage, Option<Artifact>), LocalError> {
self.inner_round_ref().make_direct_message(rng, serializer, destination)
}

Expand Down Expand Up @@ -113,17 +102,8 @@ macro_rules! round_override {
rng: &mut impl CryptoRngCore,
serializer: &$crate::protocol::Serializer,
destination: &Id,
) -> Result<$crate::protocol::DirectMessage, $crate::protocol::LocalError> {
<Self as $crate::testing::RoundOverride<Id>>::make_direct_message(self, rng, serializer, destination)
}

fn make_direct_message_with_artifact(
&self,
rng: &mut impl CryptoRngCore,
serializer: &$crate::protocol::Serializer,
destination: &Id,
) -> Result<($crate::protocol::DirectMessage, Option<$crate::protocol::Artifact>), $crate::protocol::LocalError> {
<Self as $crate::testing::RoundOverride<Id>>::make_direct_message_with_artifact(self, rng, serializer, destination)
<Self as $crate::testing::RoundOverride<Id>>::make_direct_message(self, rng, serializer, destination)
}

fn make_echo_broadcast(
Expand Down

0 comments on commit aed78d0

Please sign in to comment.