From e837388d5fb1bef73f69cebaf4c6b391abd76e52 Mon Sep 17 00:00:00 2001 From: Bogdan Opanchuk Date: Sun, 20 Oct 2024 20:45:51 -0700 Subject: [PATCH] Move things around --- Cargo.toml | 2 +- {example => examples}/Cargo.toml | 0 {example => examples}/LICENSE | 0 {example => examples}/README.md | 0 {example => examples}/src/lib.rs | 0 {example => examples}/src/simple.rs | 0 {example => examples}/src/simple_malicious.rs | 0 {example => examples}/tests/async.rs | 0 manul/src/protocol.rs | 14 +- manul/src/protocol/error.rs | 26 --- manul/src/protocol/errors.rs | 209 ++++++++++++++++++ manul/src/protocol/object_safe.rs | 7 +- manul/src/protocol/round.rs | 180 +-------------- 13 files changed, 227 insertions(+), 211 deletions(-) rename {example => examples}/Cargo.toml (100%) rename {example => examples}/LICENSE (100%) rename {example => examples}/README.md (100%) rename {example => examples}/src/lib.rs (100%) rename {example => examples}/src/simple.rs (100%) rename {example => examples}/src/simple_malicious.rs (100%) rename {example => examples}/tests/async.rs (100%) delete mode 100644 manul/src/protocol/error.rs create mode 100644 manul/src/protocol/errors.rs diff --git a/Cargo.toml b/Cargo.toml index 1a43d60..88a27dc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [workspace] members = [ - "example", + "examples", "manul", ] resolver = "2" diff --git a/example/Cargo.toml b/examples/Cargo.toml similarity index 100% rename from example/Cargo.toml rename to examples/Cargo.toml diff --git a/example/LICENSE b/examples/LICENSE similarity index 100% rename from example/LICENSE rename to examples/LICENSE diff --git a/example/README.md b/examples/README.md similarity index 100% rename from example/README.md rename to examples/README.md diff --git a/example/src/lib.rs b/examples/src/lib.rs similarity index 100% rename from example/src/lib.rs rename to examples/src/lib.rs diff --git a/example/src/simple.rs b/examples/src/simple.rs similarity index 100% rename from example/src/simple.rs rename to examples/src/simple.rs diff --git a/example/src/simple_malicious.rs b/examples/src/simple_malicious.rs similarity index 100% rename from example/src/simple_malicious.rs rename to examples/src/simple_malicious.rs diff --git a/example/tests/async.rs b/examples/tests/async.rs similarity index 100% rename from example/tests/async.rs rename to examples/tests/async.rs diff --git a/manul/src/protocol.rs b/manul/src/protocol.rs index d98c256..ab12a48 100644 --- a/manul/src/protocol.rs +++ b/manul/src/protocol.rs @@ -11,19 +11,21 @@ to be executed by a [`Session`](`crate::session::Session`). For more details, see the documentation of the mentioned traits. */ -mod error; +mod errors; mod object_safe; mod round; pub use crate::session::SessionId; -pub use error::{LocalError, RemoteError}; +pub use errors::{ + DeserializationError, DirectMessageError, EchoBroadcastError, FinalizeError, LocalError, MessageValidationError, + ProtocolValidationError, ReceiveError, RemoteError, +}; pub use round::{ - AnotherRound, Artifact, DeserializationError, DirectMessage, DirectMessageError, EchoBroadcast, EchoBroadcastError, - FinalizeError, FinalizeOutcome, FirstRound, MessageValidationError, Payload, Protocol, ProtocolError, - ProtocolValidationError, ReceiveError, Round, RoundId, + AnotherRound, Artifact, DirectMessage, EchoBroadcast, FinalizeOutcome, FirstRound, Payload, Protocol, + ProtocolError, Round, RoundId, }; +pub(crate) use errors::ReceiveErrorType; pub(crate) use object_safe::{ObjectSafeRound, ObjectSafeRoundWrapper}; -pub(crate) use round::ReceiveErrorType; pub use digest; diff --git a/manul/src/protocol/error.rs b/manul/src/protocol/error.rs deleted file mode 100644 index 69fa537..0000000 --- a/manul/src/protocol/error.rs +++ /dev/null @@ -1,26 +0,0 @@ -use alloc::string::String; -use core::fmt::Debug; - -/// An error indicating a local problem, most likely a misuse of the API or a bug in the code. -#[derive(displaydoc::Display, Debug, Clone)] -#[displaydoc("Local error: {0}")] -pub struct LocalError(String); - -impl LocalError { - /// Creates a new error from anything castable to string. - pub fn new(message: impl Into) -> Self { - Self(message.into()) - } -} - -/// An error indicating a problem whose reason is another node sending invalid data. -#[derive(displaydoc::Display, Debug, Clone)] -#[displaydoc("Remote error: {0}")] -pub struct RemoteError(String); - -impl RemoteError { - /// Creates a new error from anything castable to string. - pub fn new(message: impl Into) -> Self { - Self(message.into()) - } -} diff --git a/manul/src/protocol/errors.rs b/manul/src/protocol/errors.rs new file mode 100644 index 0000000..837e54c --- /dev/null +++ b/manul/src/protocol/errors.rs @@ -0,0 +1,209 @@ +use alloc::{format, string::String}; +use core::fmt::Debug; + +use super::round::Protocol; +use crate::session::EchoRoundError; + +/// An error indicating a local problem, most likely a misuse of the API or a bug in the code. +#[derive(displaydoc::Display, Debug, Clone)] +#[displaydoc("Local error: {0}")] +pub struct LocalError(String); + +impl LocalError { + /// Creates a new error from anything castable to string. + pub fn new(message: impl Into) -> Self { + Self(message.into()) + } +} + +/// An error indicating a problem whose reason is another node sending invalid data. +#[derive(displaydoc::Display, Debug, Clone)] +#[displaydoc("Remote error: {0}")] +pub struct RemoteError(String); + +impl RemoteError { + /// Creates a new error from anything castable to string. + pub fn new(message: impl Into) -> Self { + Self(message.into()) + } +} + +/// An error that can be returned from [`Round::receive_message`]. +#[derive(Debug)] +pub struct ReceiveError(pub(crate) ReceiveErrorType); + +#[derive(Debug)] +pub(crate) enum ReceiveErrorType { + /// A local error, indicating an implemenation bug or a misuse by the upper layer. + Local(LocalError), + /// The given direct message cannot be deserialized. + InvalidDirectMessage(DirectMessageError), + /// The given echo broadcast cannot be deserialized. + InvalidEchoBroadcast(EchoBroadcastError), + /// A provable error occurred. + Protocol(P::ProtocolError), + /// An unprovable error occurred. + Unprovable(RemoteError), + // Note that this variant should not be instantiated by the user (a protocol author), + // so this whole enum is crate-private and the variants are created + // via constructors and From impls. + /// An echo round error occurred. + Echo(EchoRoundError), +} + +impl ReceiveError { + /// A local error, indicating an implemenation bug or a misuse by the upper layer. + pub fn local(message: impl Into) -> Self { + Self(ReceiveErrorType::Local(LocalError::new(message.into()))) + } + + /// An unprovable error occurred. + pub fn unprovable(message: impl Into) -> Self { + Self(ReceiveErrorType::Unprovable(RemoteError::new(message.into()))) + } + + /// A provable error occurred. + pub fn protocol(error: P::ProtocolError) -> Self { + Self(ReceiveErrorType::Protocol(error)) + } +} + +impl From for ReceiveError +where + P: Protocol, +{ + fn from(error: LocalError) -> Self { + Self(ReceiveErrorType::Local(error)) + } +} + +impl From for ReceiveError +where + P: Protocol, +{ + fn from(error: RemoteError) -> Self { + Self(ReceiveErrorType::Unprovable(error)) + } +} + +impl From> for ReceiveError +where + P: Protocol, +{ + fn from(error: EchoRoundError) -> Self { + Self(ReceiveErrorType::Echo(error)) + } +} + +impl From for ReceiveError +where + P: Protocol, +{ + fn from(error: DirectMessageError) -> Self { + Self(ReceiveErrorType::InvalidDirectMessage(error)) + } +} + +impl From for ReceiveError +where + P: Protocol, +{ + fn from(error: EchoBroadcastError) -> Self { + Self(ReceiveErrorType::InvalidEchoBroadcast(error)) + } +} + +/// An error that can occur during [`Round::finalize`]. +pub enum FinalizeError { + /// A local error, usually indicating a bug in the implementation. + Local(LocalError), + /// An unattributable error, with an attached proof that this node performed its duties correctly. + Unattributable(P::CorrectnessProof), +} + +impl From for FinalizeError

{ + fn from(error: LocalError) -> Self { + Self::Local(error) + } +} + +/// An error that can occur during the validation of an evidence of an invalid message. +#[derive(Debug, Clone)] +pub enum MessageValidationError { + /// Indicates a local problem, usually a bug in the library code. + Local(LocalError), + /// Indicates a problem with the evidence, for example the given round not sending such messages, + /// or the message actually deserializing successfully. + InvalidEvidence(String), +} + +/// An error that can be returned during deserialization error. +#[derive(displaydoc::Display, Debug, Clone)] +#[displaydoc("Deserialization error: {0}")] +pub struct DeserializationError(String); + +impl DeserializationError { + /// Creates a new deserialization error. + pub fn new(message: impl Into) -> Self { + Self(message.into()) + } +} + +impl From for MessageValidationError { + fn from(error: LocalError) -> Self { + Self::Local(error) + } +} + +/// An error that can occur during the validation of an evidence of a protocol error. +#[derive(Debug, Clone)] +pub enum ProtocolValidationError { + /// Indicates a local problem, usually a bug in the library code. + Local(LocalError), + /// Indicates a problem with the evidence, for example missing messages, + /// or messages that cannot be deserialized. + InvalidEvidence(String), +} + +// If fail to deserialize a message when validating the evidence +// it means that the evidence is invalid - a deserialization error would have been +// processed separately, generating its own evidence. +impl From for ProtocolValidationError { + fn from(error: DirectMessageError) -> Self { + Self::InvalidEvidence(format!("Failed to deserialize direct message: {error:?}")) + } +} + +impl From for ProtocolValidationError { + fn from(error: EchoBroadcastError) -> Self { + Self::InvalidEvidence(format!("Failed to deserialize echo broadcast: {error:?}")) + } +} + +impl From for ProtocolValidationError { + fn from(error: LocalError) -> Self { + Self::Local(error) + } +} + +/// An error during deserialization of a direct message. +#[derive(displaydoc::Display, Debug, Clone)] +#[displaydoc("Direct message error: {0}")] +pub struct DirectMessageError(DeserializationError); + +impl DirectMessageError { + pub(crate) fn new(error: DeserializationError) -> Self { + Self(error) + } +} + +/// An error during deserialization of an echo broadcast. +#[derive(displaydoc::Display, Debug, Clone)] +#[displaydoc("Echo broadcast error: {0}")] +pub struct EchoBroadcastError(DeserializationError); + +impl EchoBroadcastError { + pub(crate) fn new(error: DeserializationError) -> Self { + Self(error) + } +} diff --git a/manul/src/protocol/object_safe.rs b/manul/src/protocol/object_safe.rs index c0c4760..b5c8f48 100644 --- a/manul/src/protocol/object_safe.rs +++ b/manul/src/protocol/object_safe.rs @@ -8,11 +8,8 @@ use core::marker::PhantomData; use rand_core::{CryptoRng, CryptoRngCore, RngCore}; use super::{ - error::LocalError, - round::{ - Artifact, DirectMessage, EchoBroadcast, FinalizeError, FinalizeOutcome, Payload, Protocol, ReceiveError, Round, - RoundId, - }, + errors::{FinalizeError, LocalError, ReceiveError}, + round::{Artifact, DirectMessage, EchoBroadcast, FinalizeOutcome, Payload, Protocol, Round, RoundId}, }; /// Since object-safe trait methods cannot take `impl CryptoRngCore` arguments, diff --git a/manul/src/protocol/round.rs b/manul/src/protocol/round.rs index 0103b4d..37b752b 100644 --- a/manul/src/protocol/round.rs +++ b/manul/src/protocol/round.rs @@ -2,7 +2,6 @@ use alloc::{ boxed::Box, collections::{BTreeMap, BTreeSet}, format, - string::String, vec::Vec, }; use core::{any::Any, fmt::Debug}; @@ -12,95 +11,13 @@ use serde::{Deserialize, Serialize}; use serde_encoded_bytes::{Base64, SliceLike}; use super::{ - error::{LocalError, RemoteError}, + errors::{ + DeserializationError, DirectMessageError, EchoBroadcastError, FinalizeError, LocalError, + MessageValidationError, ProtocolValidationError, ReceiveError, + }, object_safe::{ObjectSafeRound, ObjectSafeRoundWrapper}, }; -use crate::session::{EchoRoundError, SessionId}; - -/// An error that can be returned from [`Round::receive_message`]. -#[derive(Debug)] -pub struct ReceiveError(pub(crate) ReceiveErrorType); - -#[derive(Debug)] -pub(crate) enum ReceiveErrorType { - /// A local error, indicating an implemenation bug or a misuse by the upper layer. - Local(LocalError), - /// The given direct message cannot be deserialized. - InvalidDirectMessage(DirectMessageError), - /// The given echo broadcast cannot be deserialized. - InvalidEchoBroadcast(EchoBroadcastError), - /// A provable error occurred. - Protocol(P::ProtocolError), - /// An unprovable error occurred. - Unprovable(RemoteError), - // Note that this variant should not be instantiated by the user (a protocol author), - // so this whole enum is crate-private and the variants are created - // via constructors and From impls. - /// An echo round error occurred. - Echo(EchoRoundError), -} - -impl ReceiveError { - /// A local error, indicating an implemenation bug or a misuse by the upper layer. - pub fn local(message: impl Into) -> Self { - Self(ReceiveErrorType::Local(LocalError::new(message.into()))) - } - - /// An unprovable error occurred. - pub fn unprovable(message: impl Into) -> Self { - Self(ReceiveErrorType::Unprovable(RemoteError::new(message.into()))) - } - - /// A provable error occurred. - pub fn protocol(error: P::ProtocolError) -> Self { - Self(ReceiveErrorType::Protocol(error)) - } -} - -impl From for ReceiveError -where - P: Protocol, -{ - fn from(error: LocalError) -> Self { - Self(ReceiveErrorType::Local(error)) - } -} - -impl From for ReceiveError -where - P: Protocol, -{ - fn from(error: RemoteError) -> Self { - Self(ReceiveErrorType::Unprovable(error)) - } -} - -impl From> for ReceiveError -where - P: Protocol, -{ - fn from(error: EchoRoundError) -> Self { - Self(ReceiveErrorType::Echo(error)) - } -} - -impl From for ReceiveError -where - P: Protocol, -{ - fn from(error: DirectMessageError) -> Self { - Self(ReceiveErrorType::InvalidDirectMessage(error)) - } -} - -impl From for ReceiveError -where - P: Protocol, -{ - fn from(error: EchoBroadcastError) -> Self { - Self(ReceiveErrorType::InvalidEchoBroadcast(error)) - } -} +use crate::session::SessionId; /// Possible successful outcomes of [`Round::finalize`]. pub enum FinalizeOutcome { @@ -152,20 +69,6 @@ where } } -/// An error that can occur during [`Round::finalize`]. -pub enum FinalizeError { - /// A local error, usually indicating a bug in the implementation. - Local(LocalError), - /// An unattributable error, with an attached proof that this node performed its duties correctly. - Unattributable(P::CorrectnessProof), -} - -impl From for FinalizeError

{ - fn from(error: LocalError) -> Self { - Self::Local(error) - } -} - /// A round identifier. #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] pub struct RoundId { @@ -213,34 +116,6 @@ impl RoundId { } } -/// An error that can occur during the validation of an evidence of an invalid message. -#[derive(Debug, Clone)] -pub enum MessageValidationError { - /// Indicates a local problem, usually a bug in the library code. - Local(LocalError), - /// Indicates a problem with the evidence, for example the given round not sending such messages, - /// or the message actually deserializing successfully. - InvalidEvidence(String), -} - -/// An error that can be returned during deserialization error. -#[derive(displaydoc::Display, Debug, Clone)] -#[displaydoc("Deserialization error: {0}")] -pub struct DeserializationError(String); - -impl DeserializationError { - /// Creates a new deserialization error. - pub fn new(message: impl Into) -> Self { - Self(message.into()) - } -} - -impl From for MessageValidationError { - fn from(error: LocalError) -> Self { - Self::Local(error) - } -} - /// A distributed protocol. pub trait Protocol: Debug + Sized { /// The successful result of an execution of this protocol. @@ -287,37 +162,6 @@ pub trait Protocol: Debug + Sized { } } -/// An error that can occur during the validation of an evidence of a protocol error. -#[derive(Debug, Clone)] -pub enum ProtocolValidationError { - /// Indicates a local problem, usually a bug in the library code. - Local(LocalError), - /// Indicates a problem with the evidence, for example missing messages, - /// or messages that cannot be deserialized. - InvalidEvidence(String), -} - -// If fail to deserialize a message when validating the evidence -// it means that the evidence is invalid - a deserialization error would have been -// processed separately, generating its own evidence. -impl From for ProtocolValidationError { - fn from(error: DirectMessageError) -> Self { - Self::InvalidEvidence(format!("Failed to deserialize direct message: {error:?}")) - } -} - -impl From for ProtocolValidationError { - fn from(error: EchoBroadcastError) -> Self { - Self::InvalidEvidence(format!("Failed to deserialize echo broadcast: {error:?}")) - } -} - -impl From for ProtocolValidationError { - fn from(error: LocalError) -> Self { - Self::Local(error) - } -} - /// Describes provable errors originating during protocol execution. /// /// Provable here means that we can create an evidence object entirely of messages signed by some party, @@ -369,16 +213,6 @@ pub trait ProtocolError: Debug + Clone + Send { ) -> Result<(), ProtocolValidationError>; } -/// An error during deserialization of a direct message. -#[derive(displaydoc::Display, Debug, Clone)] -#[displaydoc("Direct message error: {0}")] -pub struct DirectMessageError(DeserializationError); - -/// An error during deserialization of an echo broadcast. -#[derive(displaydoc::Display, Debug, Clone)] -#[displaydoc("Echo broadcast error: {0}")] -pub struct EchoBroadcastError(DeserializationError); - /// A serialized direct message. #[derive(Debug, Clone, Serialize, Deserialize)] pub struct DirectMessage(#[serde(with = "SliceLike::")] Box<[u8]>); @@ -404,7 +238,7 @@ impl DirectMessage { /// Deserializes the direct message. pub fn deserialize Deserialize<'de>>(&self) -> Result { - P::deserialize(&self.0).map_err(DirectMessageError) + P::deserialize(&self.0).map_err(DirectMessageError::new) } } @@ -433,7 +267,7 @@ impl EchoBroadcast { /// Deserializes the echo broadcast. pub fn deserialize Deserialize<'de>>(&self) -> Result { - P::deserialize(&self.0).map_err(EchoBroadcastError) + P::deserialize(&self.0).map_err(EchoBroadcastError::new) } }