Skip to content

Commit

Permalink
Use core::error::Error trait
Browse files Browse the repository at this point in the history
Signed-off-by: Denis Varlakov <denis@dfns.co>
  • Loading branch information
survived committed Dec 3, 2024
1 parent 57375b0 commit 5d53357
Show file tree
Hide file tree
Showing 13 changed files with 94 additions and 149 deletions.
41 changes: 14 additions & 27 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions examples/random-generation-protocol/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ rand_core = { version = "0.6", default-features = false }
sha2 = { version = "0.10", default-features = false }
serde = { version = "1", default-features = false, features = ["derive"] }

displaydoc = { version = "0.2", default-features = false }
thiserror = { version = "1", optional = true }
thiserror = { version = "2", default-features = false }

# We don't use it directy, but we need to enable `serde` feature
generic-array = { version = "0.14", features = ["serde"] }
Expand All @@ -26,5 +25,3 @@ hex = "0.4"
rand_dev = "0.1"
rand = "0.8"

[features]
std = ["thiserror"]
29 changes: 12 additions & 17 deletions examples/random-generation-protocol/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
#![no_std]
#![forbid(unused_crate_dependencies, missing_docs)]

#[cfg(any(feature = "std", test))]
#[cfg(test)]
extern crate std;

extern crate alloc;

mod _unused_deps {
// We don't use it directy, but we need to enable `serde` feature
// We don't use it directly, but we need to enable `serde` feature
use generic_array as _;
}

Expand Down Expand Up @@ -132,28 +132,23 @@ where
}

/// Protocol error
#[derive(Debug, displaydoc::Display)]
#[cfg_attr(feature = "std", derive(thiserror::Error))]
#[derive(Debug, thiserror::Error)]
pub enum Error<RecvErr, SendErr> {
/// Couldn't send a message in the first round
#[displaydoc("send a message at round 1")]
Round1Send(#[cfg_attr(feature = "std", source)] SendErr),
#[error("send a message at round 1")]
Round1Send(#[source] SendErr),
/// Couldn't receive a message in the first round
#[displaydoc("receive messages at round 1")]
Round1Receive(
#[cfg_attr(feature = "std", source)] CompleteRoundError<RoundInputError, RecvErr>,
),
#[error("receive messages at round 1")]
Round1Receive(#[source] CompleteRoundError<RoundInputError, RecvErr>),
/// Couldn't send a message in the second round
#[displaydoc("send a message at round 2")]
Round2Send(#[cfg_attr(feature = "std", source)] SendErr),
#[error("send a message at round 2")]
Round2Send(#[source] SendErr),
/// Couldn't receive a message in the second round
#[displaydoc("receive messages at round 2")]
Round2Receive(
#[cfg_attr(feature = "std", source)] CompleteRoundError<RoundInputError, RecvErr>,
),
#[error("receive messages at round 2")]
Round2Receive(#[source] CompleteRoundError<RoundInputError, RecvErr>),

/// Some of the parties cheated
#[displaydoc("malicious parties: {guilty_parties:?}")]
#[error("malicious parties: {guilty_parties:?}")]
PartiesOpenedRandomnessDoesntMatchCommitment {
/// List of cheated parties
guilty_parties: Vec<Blame>,
Expand Down
5 changes: 4 additions & 1 deletion round-based/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## v0.4.0
* Improve ergonomics of protocol simulation, which is used for writing tests [#14]
* BREAKING: Improve ergonomics of protocol simulation, which is used for writing tests [#14]
* Remove `dev` feature, it's replaced with `sim` and `sim-async`
* `round_based::simulation` module is renamed into `round_based::sim`
* `round_based::simulation::Simulation` is renamed and moved to `round_based::sim::async_env::Network`
Expand All @@ -12,6 +12,9 @@
* When `sim-async` feature is enabled, you can use `round_based::sim::async_env::{run, run_with_setup, ...}`,
but typically you don't want to use them
* `round_based::simulation::SimulationSync` has been renamed to `round_based::sim::Simulation`
* Use `core::error::Error` trait which is now always implemented for all errors regardless whether `std` feature
is enabled or not [#14]
* Update `thiserror` dependency to v2

Migration guidelines:
* Replace `dev` feature with `sim`
Expand Down
9 changes: 4 additions & 5 deletions round-based/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ rustdoc-args = ["--cfg", "docsrs"]
futures-util = { version = "0.3", default-features = false, features = ["sink"] }
phantom-type = { version = "0.3", default-features = false }
tracing = { version = "0.1", default-features = false }
thiserror = { version = "1", optional = true }
displaydoc = { version = "0.2", default-features = false }
thiserror = { version = "2", default-features = false }

round-based-derive = { version = "0.2", optional = true, path = "../round-based-derive" }

Expand All @@ -40,13 +39,13 @@ rand_dev = "0.1"
anyhow = "1"

[features]
default = ["std"]
default = []
state-machine = []
sim = ["std", "state-machine"]
sim = ["state-machine"]
sim-async = ["sim", "tokio/sync", "tokio-stream", "futures-util/alloc"]
derive = ["round-based-derive"]
runtime-tokio = ["tokio"]
std = ["thiserror"]
std = []

[[test]]
name = "derive"
Expand Down
10 changes: 4 additions & 6 deletions round-based/src/delivery.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use futures_util::{Sink, Stream};

use crate::StdError;

/// Networking abstraction
///
/// Basically, it's pair of channels: [`Stream`] for receiving messages, and [`Sink`] for sending
Expand All @@ -12,9 +10,9 @@ pub trait Delivery<M> {
/// Incoming delivery channel
type Receive: Stream<Item = Result<Incoming<M>, Self::ReceiveError>> + Unpin;
/// Error of outgoing delivery channel
type SendError: StdError + Send + Sync + 'static;
type SendError: core::error::Error + Send + Sync + 'static;
/// Error of incoming delivery channel
type ReceiveError: StdError + Send + Sync + 'static;
type ReceiveError: core::error::Error + Send + Sync + 'static;
/// Returns a pair of incoming and outgoing delivery channels
fn split(self) -> (Self::Receive, Self::Send);
}
Expand All @@ -23,8 +21,8 @@ impl<M, I, O, IErr, OErr> Delivery<M> for (I, O)
where
I: Stream<Item = Result<Incoming<M>, IErr>> + Unpin,
O: Sink<Outgoing<M>, Error = OErr> + Unpin,
IErr: StdError + Send + Sync + 'static,
OErr: StdError + Send + Sync + 'static,
IErr: core::error::Error + Send + Sync + 'static,
OErr: core::error::Error + Send + Sync + 'static,
{
type Send = O;
type Receive = I;
Expand Down
12 changes: 0 additions & 12 deletions round-based/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,3 @@ pub mod _docs;
/// See [`ProtocolMessage`] docs for more details
#[cfg(feature = "derive")]
pub use round_based_derive::ProtocolMessage;

mod std_error {
#[cfg(feature = "std")]
pub use std::error::Error as StdError;

#[cfg(not(feature = "std"))]
pub trait StdError: core::fmt::Display + core::fmt::Debug {}
#[cfg(not(feature = "std"))]
impl<E: core::fmt::Display + core::fmt::Debug> StdError for E {}
}

use std_error::StdError;
9 changes: 4 additions & 5 deletions round-based/src/party.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ use phantom_type::PhantomType;

use crate::delivery::Delivery;
use crate::runtime::{self, AsyncRuntime};
use crate::StdError;

/// Party of MPC protocol (trait)
///
Expand Down Expand Up @@ -82,9 +81,9 @@ pub trait Mpc: internal::Sealed {
type Runtime: AsyncRuntime;

/// Sending message error
type SendError: StdError + Send + Sync + 'static;
type SendError: core::error::Error + Send + Sync + 'static;
/// Receiving message error
type ReceiveError: StdError + Send + Sync + 'static;
type ReceiveError: core::error::Error + Send + Sync + 'static;

/// Converts into [`MpcParty`]
fn into_party(self) -> MpcParty<Self::ProtocolMessage, Self::Delivery, Self::Runtime>;
Expand Down Expand Up @@ -142,8 +141,8 @@ impl<M, D, B> internal::Sealed for MpcParty<M, D, B> {}
impl<M, D, R> Mpc for MpcParty<M, D, R>
where
D: Delivery<M>,
D::SendError: StdError + Send + Sync + 'static,
D::ReceiveError: StdError + Send + Sync + 'static,
D::SendError: core::error::Error + Send + Sync + 'static,
D::ReceiveError: core::error::Error + Send + Sync + 'static,
R: AsyncRuntime,
{
type ProtocolMessage = M;
Expand Down
Loading

0 comments on commit 5d53357

Please sign in to comment.