Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify public-facing message API #56

Merged
merged 4 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `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])
- Serialization format is a part of `SessionParameters` now; `Round` and `Protocol` methods receive dynamic serializers/deserializers. ([#33])
- Renamed `(Verified)MessageBundle` to `(Verified)Message`. Both are now generic over `Verifier`. ([#56])


### Added

- `SerializableMap` wrapper for `BTreeMap` supporting more formats and providing some safety features. (#[32])
- `DirectMessage::assert_is_none()` and `verify_is_some()`, same for `EchoBroadcast`. Users can now check that a part of the round message (echo or direct) is `None` as expected, and make a verifiable evidence if it is not. ([#46])
- Re-export `digest` from the `session` module. ([#56])
- Added `Message::destination()`. ([#56])


[#32]: https://github.com/entropyxyz/manul/pull/32
Expand All @@ -35,6 +38,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[#41]: https://github.com/entropyxyz/manul/pull/41
[#46]: https://github.com/entropyxyz/manul/pull/46
[#47]: https://github.com/entropyxyz/manul/pull/47
[#56]: https://github.com/entropyxyz/manul/pull/56


## [0.0.1] - 2024-10-12
Expand Down
8 changes: 4 additions & 4 deletions examples/tests/async_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use alloc::collections::{BTreeMap, BTreeSet};
use manul::{
protocol::Protocol,
session::{
signature::Keypair, CanFinalize, LocalError, MessageBundle, RoundOutcome, Session, SessionId,
SessionParameters, SessionReport,
signature::Keypair, CanFinalize, LocalError, Message, RoundOutcome, Session, SessionId, SessionParameters,
SessionReport,
},
testing::{BinaryFormat, TestSessionParams, TestSigner},
};
Expand All @@ -23,12 +23,12 @@ use tracing_subscriber::{util::SubscriberInitExt, EnvFilter};
struct MessageOut<SP: SessionParameters> {
from: SP::Verifier,
to: SP::Verifier,
message: MessageBundle,
message: Message<SP::Verifier>,
}

struct MessageIn<SP: SessionParameters> {
from: SP::Verifier,
message: MessageBundle,
message: Message<SP::Verifier>,
}

/// Runs a session. Simulates what each participating party would run as the protocol progresses.
Expand Down
3 changes: 2 additions & 1 deletion manul/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ mod wire_format;

pub use crate::protocol::{LocalError, RemoteError};
pub use evidence::{Evidence, EvidenceError};
pub use message::MessageBundle;
pub use message::{Message, VerifiedMessage};
pub use session::{CanFinalize, RoundAccumulator, RoundOutcome, Session, SessionId, SessionParameters};
pub use transcript::{SessionOutcome, SessionReport};
pub use wire_format::WireFormat;

pub(crate) use echo::EchoRoundError;

pub use digest;
pub use signature;
14 changes: 7 additions & 7 deletions manul/src/session/echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use serde::{Deserialize, Serialize};
use tracing::debug;

use super::{
message::{MessageVerificationError, SignedMessage},
message::{MessageVerificationError, SignedMessagePart},
session::SessionParameters,
LocalError,
};
Expand Down Expand Up @@ -42,8 +42,8 @@ pub(crate) enum EchoRoundError<Id> {
MismatchedBroadcasts {
guilty_party: Id,
error: MismatchedBroadcastsError,
we_received: SignedMessage<EchoBroadcast>,
echoed_to_us: SignedMessage<EchoBroadcast>,
we_received: SignedMessagePart<EchoBroadcast>,
echoed_to_us: SignedMessagePart<EchoBroadcast>,
},
}

Expand All @@ -68,7 +68,7 @@ pub(crate) enum MismatchedBroadcastsError {

#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct EchoRoundMessage<SP: SessionParameters> {
pub(super) echo_broadcasts: SerializableMap<SP::Verifier, SignedMessage<EchoBroadcast>>,
pub(super) echo_broadcasts: SerializableMap<SP::Verifier, SignedMessagePart<EchoBroadcast>>,
}

/// Each protocol round can contain one `EchoRound` with "echo messages" that are sent to all
Expand All @@ -77,7 +77,7 @@ pub(crate) struct EchoRoundMessage<SP: SessionParameters> {
#[derive_where::derive_where(Debug)]
pub struct EchoRound<P, SP: SessionParameters> {
verifier: SP::Verifier,
echo_broadcasts: BTreeMap<SP::Verifier, SignedMessage<EchoBroadcast>>,
echo_broadcasts: BTreeMap<SP::Verifier, SignedMessagePart<EchoBroadcast>>,
destinations: BTreeSet<SP::Verifier>,
expected_echos: BTreeSet<SP::Verifier>,
main_round: Box<dyn ObjectSafeRound<SP::Verifier, Protocol = P>>,
Expand All @@ -92,8 +92,8 @@ where
{
pub fn new(
verifier: SP::Verifier,
my_echo_broadcast: SignedMessage<EchoBroadcast>,
echo_broadcasts: BTreeMap<SP::Verifier, SignedMessage<EchoBroadcast>>,
my_echo_broadcast: SignedMessagePart<EchoBroadcast>,
echo_broadcasts: BTreeMap<SP::Verifier, SignedMessagePart<EchoBroadcast>>,
main_round: Box<dyn ObjectSafeRound<SP::Verifier, Protocol = P>>,
payloads: BTreeMap<SP::Verifier, Payload>,
artifacts: BTreeMap<SP::Verifier, Artifact>,
Expand Down
51 changes: 21 additions & 30 deletions manul/src/session/evidence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use serde::{Deserialize, Serialize};

use super::{
echo::{EchoRound, EchoRoundError, EchoRoundMessage, MismatchedBroadcastsError},
message::{MessageVerificationError, MissingMessage, SignedMessage},
message::{MessageVerificationError, SignedMessagePart},
session::SessionParameters,
transcript::Transcript,
LocalError,
Expand All @@ -37,15 +37,6 @@ pub enum EvidenceError {
InvalidEvidence(String),
}

// Other nodes would send a signed message with the payload being either Some(...) or None.
// We expect the messages in the evidence only be the Some(...) ones, so if it's not the case, it's invalid evidence.
// It's hard to enforce statically since we have to keep the signed messages as they were created by remote nodes.
impl From<MissingMessage> for EvidenceError {
fn from(_error: MissingMessage) -> Self {
Self::InvalidEvidence("The signed message is missing the expected payload".into())
}
}

impl From<MessageVerificationError> for EvidenceError {
fn from(error: MessageVerificationError) -> Self {
match error {
Expand Down Expand Up @@ -98,9 +89,9 @@ where
{
pub(crate) fn new_protocol_error(
verifier: &SP::Verifier,
echo_broadcast: SignedMessage<EchoBroadcast>,
normal_broadcast: SignedMessage<NormalBroadcast>,
direct_message: SignedMessage<DirectMessage>,
echo_broadcast: SignedMessagePart<EchoBroadcast>,
normal_broadcast: SignedMessagePart<NormalBroadcast>,
direct_message: SignedMessagePart<DirectMessage>,
error: P::ProtocolError,
transcript: &Transcript<P, SP>,
) -> Result<Self, LocalError> {
Expand Down Expand Up @@ -164,7 +155,7 @@ where

pub(crate) fn new_echo_round_error(
verifier: &SP::Verifier,
normal_broadcast: SignedMessage<NormalBroadcast>,
normal_broadcast: SignedMessagePart<NormalBroadcast>,
error: EchoRoundError<SP::Verifier>,
) -> Result<Self, LocalError> {
let description = format!("Echo round error: {}", error.description());
Expand Down Expand Up @@ -196,7 +187,7 @@ where

pub(crate) fn new_invalid_direct_message(
verifier: &SP::Verifier,
direct_message: SignedMessage<DirectMessage>,
direct_message: SignedMessagePart<DirectMessage>,
error: DirectMessageError,
) -> Self {
Self {
Expand All @@ -211,7 +202,7 @@ where

pub(crate) fn new_invalid_echo_broadcast(
verifier: &SP::Verifier,
echo_broadcast: SignedMessage<EchoBroadcast>,
echo_broadcast: SignedMessagePart<EchoBroadcast>,
error: EchoBroadcastError,
) -> Self {
Self {
Expand All @@ -226,7 +217,7 @@ where

pub(crate) fn new_invalid_normal_broadcast(
verifier: &SP::Verifier,
normal_broadcast: SignedMessage<NormalBroadcast>,
normal_broadcast: SignedMessagePart<NormalBroadcast>,
error: NormalBroadcastError,
) -> Self {
Self {
Expand Down Expand Up @@ -280,7 +271,7 @@ enum EvidenceEnum<P: Protocol, SP: SessionParameters> {
#[derive_where::derive_where(Debug)]
#[derive(Clone, Serialize, Deserialize)]
pub struct InvalidEchoPackEvidence<SP: SessionParameters> {
normal_broadcast: SignedMessage<NormalBroadcast>,
normal_broadcast: SignedMessagePart<NormalBroadcast>,
invalid_echo_sender: SP::Verifier,
}

Expand Down Expand Up @@ -324,8 +315,8 @@ where
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MismatchedBroadcastsEvidence {
error: MismatchedBroadcastsError,
we_received: SignedMessage<EchoBroadcast>,
echoed_to_us: SignedMessage<EchoBroadcast>,
we_received: SignedMessagePart<EchoBroadcast>,
echoed_to_us: SignedMessagePart<EchoBroadcast>,
}

impl MismatchedBroadcastsEvidence {
Expand Down Expand Up @@ -360,7 +351,7 @@ impl MismatchedBroadcastsEvidence {
#[derive_where::derive_where(Debug)]
#[derive(Clone, Serialize, Deserialize)]
pub struct InvalidDirectMessageEvidence<P: Protocol> {
direct_message: SignedMessage<DirectMessage>,
direct_message: SignedMessagePart<DirectMessage>,
phantom: core::marker::PhantomData<P>,
}

Expand Down Expand Up @@ -390,7 +381,7 @@ where
#[derive_where::derive_where(Debug)]
#[derive(Clone, Serialize, Deserialize)]
pub struct InvalidEchoBroadcastEvidence<P: Protocol> {
echo_broadcast: SignedMessage<EchoBroadcast>,
echo_broadcast: SignedMessagePart<EchoBroadcast>,
phantom: core::marker::PhantomData<P>,
}

Expand Down Expand Up @@ -420,7 +411,7 @@ where
#[derive_where::derive_where(Debug)]
#[derive(Clone, Serialize, Deserialize)]
pub struct InvalidNormalBroadcastEvidence<P: Protocol> {
normal_broadcast: SignedMessage<NormalBroadcast>,
normal_broadcast: SignedMessagePart<NormalBroadcast>,
phantom: core::marker::PhantomData<P>,
}

Expand Down Expand Up @@ -454,13 +445,13 @@ where
#[derive(Clone, Serialize, Deserialize)]
struct ProtocolEvidence<P: Protocol> {
error: P::ProtocolError,
direct_message: SignedMessage<DirectMessage>,
echo_broadcast: SignedMessage<EchoBroadcast>,
normal_broadcast: SignedMessage<NormalBroadcast>,
direct_messages: SerializableMap<RoundId, SignedMessage<DirectMessage>>,
echo_broadcasts: SerializableMap<RoundId, SignedMessage<EchoBroadcast>>,
normal_broadcasts: SerializableMap<RoundId, SignedMessage<NormalBroadcast>>,
combined_echos: SerializableMap<RoundId, SignedMessage<NormalBroadcast>>,
direct_message: SignedMessagePart<DirectMessage>,
echo_broadcast: SignedMessagePart<EchoBroadcast>,
normal_broadcast: SignedMessagePart<NormalBroadcast>,
direct_messages: SerializableMap<RoundId, SignedMessagePart<DirectMessage>>,
echo_broadcasts: SerializableMap<RoundId, SignedMessagePart<EchoBroadcast>>,
normal_broadcasts: SerializableMap<RoundId, SignedMessagePart<NormalBroadcast>>,
combined_echos: SerializableMap<RoundId, SignedMessagePart<NormalBroadcast>>,
}

impl<P> ProtocolEvidence<P>
Expand Down
Loading
Loading