Skip to content

Commit

Permalink
Add an impl of ProtocolError for ()
Browse files Browse the repository at this point in the history
  • Loading branch information
fjarri committed Nov 3, 2024
1 parent d0be605 commit 7738979
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 60 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Re-export `digest` from the `session` module. ([#56])
- Added `Message::destination()`. ([#56])
- `PartyId` trait alias for the combination of bounds needed for a party identifier. ([#59])
- An impl of `ProtocolError` for `()` for protocols that don't use errors. ([#60])


[#32]: https://github.com/entropyxyz/manul/pull/32
Expand Down
33 changes: 4 additions & 29 deletions manul/benches/empty_rounds.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
extern crate alloc;

use alloc::{
collections::{BTreeMap, BTreeSet},
string::String,
};
use alloc::collections::{BTreeMap, BTreeSet};
use core::fmt::Debug;

use criterion::{criterion_group, criterion_main, Criterion};
use manul::{
protocol::{
Artifact, BoxedRound, Deserializer, DirectMessage, EchoBroadcast, EntryPoint, FinalizeError, FinalizeOutcome,
LocalError, NormalBroadcast, PartyId, Payload, Protocol, ProtocolError, ProtocolMessagePart,
ProtocolValidationError, ReceiveError, Round, RoundId, Serializer,
LocalError, NormalBroadcast, PartyId, Payload, Protocol, ProtocolMessagePart, ReceiveError, Round, RoundId,
Serializer,
},
session::{signature::Keypair, SessionOutcome},
testing::{run_sync, BinaryFormat, TestSessionParams, TestSigner, TestVerifier},
Expand All @@ -22,31 +19,9 @@ use serde::{Deserialize, Serialize};
#[derive(Debug)]
pub struct EmptyProtocol;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EmptyProtocolError;

impl ProtocolError for EmptyProtocolError {
fn description(&self) -> String {
unimplemented!()
}
fn verify_messages_constitute_error(
&self,
_deserializer: &Deserializer,
_echo_broadcast: &EchoBroadcast,
_normal_broadcast: &NormalBroadcast,
_direct_message: &DirectMessage,
_echo_broadcasts: &BTreeMap<RoundId, EchoBroadcast>,
_normal_broadcasts: &BTreeMap<RoundId, NormalBroadcast>,
_direct_messages: &BTreeMap<RoundId, DirectMessage>,
_combined_echos: &BTreeMap<RoundId, Vec<EchoBroadcast>>,
) -> Result<(), ProtocolValidationError> {
unimplemented!()
}
}

impl Protocol for EmptyProtocol {
type Result = ();
type ProtocolError = EmptyProtocolError;
type ProtocolError = ();
type CorrectnessProof = ();
}

Expand Down
22 changes: 22 additions & 0 deletions manul/src/protocol/round.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,28 @@ pub trait ProtocolError: Debug + Clone + Send {
) -> Result<(), ProtocolValidationError>;
}

// A convenience implementation for protocols that don't define any errors.
// Have to do it for `()`, since `!` is unstable.
impl ProtocolError for () {
fn description(&self) -> String {
panic!("Attempt to use an empty error type in an evidence. This is a bug in the protocol implementation.")
}

fn verify_messages_constitute_error(
&self,
_deserializer: &Deserializer,
_echo_broadcast: &EchoBroadcast,
_normal_broadcast: &NormalBroadcast,
_direct_message: &DirectMessage,
_echo_broadcasts: &BTreeMap<RoundId, EchoBroadcast>,
_normal_broadcasts: &BTreeMap<RoundId, NormalBroadcast>,
_direct_messages: &BTreeMap<RoundId, DirectMessage>,
_combined_echos: &BTreeMap<RoundId, Vec<EchoBroadcast>>,
) -> Result<(), ProtocolValidationError> {
panic!("Attempt to use an empty error type in an evidence. This is a bug in the protocol implementation.")
}
}

/// Message payload created in [`Round::receive_message`].
#[derive(Debug)]
pub struct Payload(pub Box<dyn Any + Send + Sync>);
Expand Down
33 changes: 2 additions & 31 deletions manul/src/session/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -816,17 +816,11 @@ fn filter_messages<Verifier>(

#[cfg(test)]
mod tests {
use alloc::{collections::BTreeMap, string::String, vec::Vec};

use impls::impls;
use serde::{Deserialize, Serialize};

use super::{Message, ProcessedArtifact, ProcessedMessage, Session, VerifiedMessage};
use crate::{
protocol::{
Deserializer, DirectMessage, EchoBroadcast, NormalBroadcast, Protocol, ProtocolError,
ProtocolValidationError, RoundId,
},
protocol::Protocol,
testing::{BinaryFormat, TestSessionParams, TestVerifier},
};

Expand All @@ -842,32 +836,9 @@ mod tests {

struct DummyProtocol;

#[derive(Debug, Clone, Serialize, Deserialize)]
struct DummyProtocolError;

impl ProtocolError for DummyProtocolError {
fn description(&self) -> String {
unimplemented!()
}

fn verify_messages_constitute_error(
&self,
_deserializer: &Deserializer,
_echo_broadcast: &EchoBroadcast,
_normal_broadcast: &NormalBroadcast,
_direct_message: &DirectMessage,
_echo_broadcasts: &BTreeMap<RoundId, EchoBroadcast>,
_normal_broadcasts: &BTreeMap<RoundId, NormalBroadcast>,
_direct_messages: &BTreeMap<RoundId, DirectMessage>,
_combined_echos: &BTreeMap<RoundId, Vec<EchoBroadcast>>,
) -> Result<(), ProtocolValidationError> {
unimplemented!()
}
}

impl Protocol for DummyProtocol {
type Result = ();
type ProtocolError = DummyProtocolError;
type ProtocolError = ();
type CorrectnessProof = ();
}

Expand Down

0 comments on commit 7738979

Please sign in to comment.