From f22f11022e06f652332ee1afb4e77faa70a54c73 Mon Sep 17 00:00:00 2001 From: Bogdan Opanchuk Date: Tue, 29 Oct 2024 22:17:59 -0700 Subject: [PATCH 1/2] Use derive-where, remove some Debug requirements on generics --- Cargo.lock | 12 ++++++++++++ examples/src/simple.rs | 1 - examples/tests/async_runner.rs | 8 +++----- manul/Cargo.toml | 1 + manul/src/protocol/object_safe.rs | 2 +- manul/src/protocol/round.rs | 4 ++-- manul/src/session/echo.rs | 6 +++--- manul/src/session/evidence.rs | 21 ++++++++++++++------- manul/src/session/message.rs | 3 ++- manul/src/session/session.rs | 5 ++--- manul/src/testing/run_sync.rs | 6 ++---- 11 files changed, 42 insertions(+), 27 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0555796..c31e3f0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -263,6 +263,17 @@ dependencies = [ "typenum", ] +[[package]] +name = "derive-where" +version = "1.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62d671cc41a825ebabc75757b62d3d168c577f9149b2d49ece1dad1f72119d25" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "digest" version = "0.10.7" @@ -467,6 +478,7 @@ name = "manul" version = "0.0.2-dev" dependencies = [ "criterion", + "derive-where", "digest", "displaydoc", "erased-serde", diff --git a/examples/src/simple.rs b/examples/src/simple.rs index a152086..da0d325 100644 --- a/examples/src/simple.rs +++ b/examples/src/simple.rs @@ -6,7 +6,6 @@ use rand_core::CryptoRngCore; use serde::{Deserialize, Serialize}; use tracing::debug; -#[derive(Debug)] pub struct SimpleProtocol; #[derive(Debug, Clone, Serialize, Deserialize)] diff --git a/examples/tests/async_runner.rs b/examples/tests/async_runner.rs index 20bccc2..53a1dcc 100644 --- a/examples/tests/async_runner.rs +++ b/examples/tests/async_runner.rs @@ -1,7 +1,5 @@ extern crate alloc; -use std::fmt::Debug; - use alloc::collections::{BTreeMap, BTreeSet}; use manul::{ @@ -41,7 +39,7 @@ async fn run_session( ) -> Result, LocalError> where P: Protocol, - SP: SessionParameters + Debug, + SP: SessionParameters, { let rng = &mut OsRng; @@ -155,7 +153,7 @@ async fn message_dispatcher( txs: BTreeMap>>, rx: mpsc::Receiver>, ) where - SP: SessionParameters + Debug, + SP: SessionParameters, { let mut rx = rx; let mut messages = Vec::>::new(); @@ -197,7 +195,7 @@ async fn message_dispatcher( async fn run_nodes(sessions: Vec>) -> Vec> where P: Protocol + Send, - SP: SessionParameters + Debug, + SP: SessionParameters, P::Result: Send, SP::Signer: Send, { diff --git a/manul/Cargo.toml b/manul/Cargo.toml index 3ecc86d..893157b 100644 --- a/manul/Cargo.toml +++ b/manul/Cargo.toml @@ -19,6 +19,7 @@ signature = { version = "2", default-features = false, features = ["digest", "ra rand_core = { version = "0.6.4", default-features = false } tracing = { version = "0.1", default-features = false } displaydoc = { version = "0.2", default-features = false } +derive-where = "1" rand = { version = "0.8", default-features = false, optional = true } serde-persistent-deserializer = { version = "0.3", optional = true } diff --git a/manul/src/protocol/object_safe.rs b/manul/src/protocol/object_safe.rs index 4eacf02..ff73917 100644 --- a/manul/src/protocol/object_safe.rs +++ b/manul/src/protocol/object_safe.rs @@ -39,7 +39,7 @@ impl RngCore for BoxedRng<'_> { // Since we want `Round` methods to take `&mut impl CryptoRngCore` arguments // (which is what all cryptographic libraries generally take), it cannot be object-safe. // Thus we have to add this crate-private object-safe layer on top of `Round`. -pub(crate) trait ObjectSafeRound: 'static + Send + Sync + Debug { +pub(crate) trait ObjectSafeRound: 'static + Debug + Send + Sync { type Protocol: Protocol; fn id(&self) -> RoundId; diff --git a/manul/src/protocol/round.rs b/manul/src/protocol/round.rs index a0d6846..00ad5d4 100644 --- a/manul/src/protocol/round.rs +++ b/manul/src/protocol/round.rs @@ -116,7 +116,7 @@ impl RoundId { } /// A distributed protocol. -pub trait Protocol: 'static + Debug + Sized { +pub trait Protocol: 'static + Sized { /// The successful result of an execution of this protocol. type Result: Debug; @@ -314,7 +314,7 @@ The way a round will be used by an external caller: - process received messages from other nodes (by calling [`receive_message`](`Self::receive_message`)); - attempt to finalize (by calling [`finalize`](`Self::finalize`)) to produce the next round, or return a result. */ -pub trait Round: 'static + Send + Sync + Debug { +pub trait Round: 'static + Debug + Send + Sync { /// The protocol this round is a part of. type Protocol: Protocol; diff --git a/manul/src/session/echo.rs b/manul/src/session/echo.rs index 05049d6..3649a56 100644 --- a/manul/src/session/echo.rs +++ b/manul/src/session/echo.rs @@ -61,7 +61,7 @@ pub(crate) struct EchoRoundMessage { /// Each protocol round can contain one `EchoRound` with "echo messages" that are sent to all /// participants. The execution layer of the protocol guarantees that all participants have received /// the messages. -#[derive(Debug)] +#[derive_where::derive_where(Debug)] pub struct EchoRound { verifier: SP::Verifier, echo_broadcasts: BTreeMap>, @@ -75,7 +75,7 @@ pub struct EchoRound { impl EchoRound where P: Protocol, - SP: SessionParameters + Debug, + SP: SessionParameters, { pub fn new( verifier: SP::Verifier, @@ -110,7 +110,7 @@ where impl Round for EchoRound where P: 'static + Protocol, - SP: 'static + SessionParameters + Debug, + SP: 'static + SessionParameters, { type Protocol = P; diff --git a/manul/src/session/evidence.rs b/manul/src/session/evidence.rs index 3912623..54a78f3 100644 --- a/manul/src/session/evidence.rs +++ b/manul/src/session/evidence.rs @@ -70,7 +70,8 @@ impl From for EvidenceError { } } -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive_where::derive_where(Debug)] +#[derive(Clone, Serialize, Deserialize)] pub struct Evidence { guilty_party: SP::Verifier, description: String, @@ -246,7 +247,8 @@ where } } -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive_where::derive_where(Debug)] +#[derive(Clone, Serialize, Deserialize)] enum EvidenceEnum { Protocol(ProtocolEvidence

), InvalidDirectMessage(InvalidDirectMessageEvidence

), @@ -256,7 +258,8 @@ enum EvidenceEnum { MismatchedBroadcasts(MismatchedBroadcastsEvidence), } -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive_where::derive_where(Debug)] +#[derive(Clone, Serialize, Deserialize)] pub struct InvalidEchoPackEvidence { normal_broadcast: SignedMessage, invalid_echo_sender: SP::Verifier, @@ -324,7 +327,8 @@ impl MismatchedBroadcastsEvidence { } } -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive_where::derive_where(Debug)] +#[derive(Clone, Serialize, Deserialize)] pub struct InvalidDirectMessageEvidence { direct_message: SignedMessage, phantom: core::marker::PhantomData

, @@ -347,7 +351,8 @@ where } } -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive_where::derive_where(Debug)] +#[derive(Clone, Serialize, Deserialize)] pub struct InvalidEchoBroadcastEvidence { echo_broadcast: SignedMessage, phantom: core::marker::PhantomData

, @@ -370,7 +375,8 @@ where } } -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive_where::derive_where(Debug)] +#[derive(Clone, Serialize, Deserialize)] pub struct InvalidNormalBroadcastEvidence { normal_broadcast: SignedMessage, phantom: core::marker::PhantomData

, @@ -392,7 +398,8 @@ where } } -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive_where::derive_where(Debug)] +#[derive(Clone, Serialize, Deserialize)] struct ProtocolEvidence { error: P::ProtocolError, direct_message: SignedMessage, diff --git a/manul/src/session/message.rs b/manul/src/session/message.rs index 3e25e39..3a1c35f 100644 --- a/manul/src/session/message.rs +++ b/manul/src/session/message.rs @@ -250,7 +250,8 @@ impl CheckedMessageBundle { /// A `VerifiedMessageBundle` is the final evolution of a [`MessageBundle`]. At this point in the /// process, the [`DirectMessage`] and an eventual [`EchoBroadcast`] have been fully checked and the /// signature on the [`SignedMessage`] from the original [`MessageBundle`] successfully verified. -#[derive(Clone, Debug)] +#[derive_where::derive_where(Debug)] +#[derive(Clone)] pub struct VerifiedMessageBundle { from: SP::Verifier, metadata: MessageMetadata, diff --git a/manul/src/session/session.rs b/manul/src/session/session.rs index 8928502..49f538d 100644 --- a/manul/src/session/session.rs +++ b/manul/src/session/session.rs @@ -137,7 +137,7 @@ pub enum RoundOutcome { impl Session where P: Protocol, - SP: SessionParameters + Debug, + SP: SessionParameters, { /// Initializes a new session. pub fn new( @@ -538,7 +538,7 @@ pub enum CanFinalize { } /// A mutable accumulator for collecting the results and errors from processing messages for a single round. -#[derive(Debug)] +#[derive_where::derive_where(Debug)] pub struct RoundAccumulator { still_have_not_sent_messages: BTreeSet, expecting_messages_from: BTreeSet, @@ -795,7 +795,6 @@ mod tests { // Send/Sync. But we want to make sure that if the generic parameters are // Send/Sync, our types are too. - #[derive(Debug)] struct DummyProtocol; #[derive(Debug, Clone, Serialize, Deserialize)] diff --git a/manul/src/testing/run_sync.rs b/manul/src/testing/run_sync.rs index c338f8c..4a45f69 100644 --- a/manul/src/testing/run_sync.rs +++ b/manul/src/testing/run_sync.rs @@ -1,5 +1,3 @@ -use core::fmt::Debug; - use alloc::{collections::BTreeMap, vec::Vec}; use rand::Rng; @@ -37,7 +35,7 @@ fn propagate( ) -> Result<(State, Vec>), LocalError> where P: 'static + Protocol, - SP: 'static + SessionParameters + Debug, + SP: 'static + SessionParameters, { let mut messages = Vec::new(); @@ -97,7 +95,7 @@ pub fn run_sync( ) -> Result>, LocalError> where R: 'static + FirstRound, - SP: 'static + SessionParameters + Debug, + SP: 'static + SessionParameters, { let session_id = SessionId::random::(rng); From dbe3a001cbd4376749fc541f09b5cc6ce8f8568b Mon Sep 17 00:00:00 2001 From: Bogdan Opanchuk Date: Tue, 29 Oct 2024 22:37:10 -0700 Subject: [PATCH 2/2] Remove some unused `'static` --- manul/src/protocol/object_safe.rs | 2 +- manul/src/protocol/round.rs | 2 +- manul/src/session/echo.rs | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/manul/src/protocol/object_safe.rs b/manul/src/protocol/object_safe.rs index ff73917..de07c8a 100644 --- a/manul/src/protocol/object_safe.rs +++ b/manul/src/protocol/object_safe.rs @@ -204,7 +204,7 @@ where impl dyn ObjectSafeRound where Id: 'static, - P: 'static + Protocol, + P: Protocol, { pub fn try_downcast>(self: Box) -> Result> { if core::any::TypeId::of::>() == self.get_type_id() { diff --git a/manul/src/protocol/round.rs b/manul/src/protocol/round.rs index 00ad5d4..27bc785 100644 --- a/manul/src/protocol/round.rs +++ b/manul/src/protocol/round.rs @@ -289,7 +289,7 @@ impl Artifact { /// /// This is a round that can be created directly; /// all the others are only reachable throud [`Round::finalize`] by the execution layer. -pub trait FirstRound: Round + Sized { +pub trait FirstRound: Round + Sized { /// Additional inputs for the protocol (besides the mandatory ones in [`new`](`Self::new`)). type Inputs; diff --git a/manul/src/session/echo.rs b/manul/src/session/echo.rs index 3649a56..ad8a92d 100644 --- a/manul/src/session/echo.rs +++ b/manul/src/session/echo.rs @@ -109,8 +109,8 @@ where impl Round for EchoRound where - P: 'static + Protocol, - SP: 'static + SessionParameters, + P: Protocol, + SP: SessionParameters, { type Protocol = P;