Skip to content

Commit

Permalink
Merge pull request #23 from alexandrainst/network-robustness
Browse files Browse the repository at this point in the history
Refactor network code
  • Loading branch information
quackzar authored Jun 14, 2024
2 parents 7091377 + 58e5078 commit 81ef369
Show file tree
Hide file tree
Showing 13 changed files with 721 additions and 470 deletions.
2 changes: 1 addition & 1 deletion rust-toolchain
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# vim: ft=toml
[toolchain]
channel = "nightly-2024-04-17"
channel = "nightly-2024-05-15"
2 changes: 1 addition & 1 deletion src/algebra/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ overload!((a: &mut Mod11) *= (b: ?Mod11) {

impl ConstantTimeEq for Mod11 {
fn ct_eq(&self, other: &Self) -> Choice {
((self == other) as u8).into()
u8::from(self == other).into()
}
}

Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#![allow(refining_impl_trait)]
#![allow(dead_code)]
#![feature(async_fn_traits)]
#![allow(clippy::cast_possible_truncation)]

mod algebra;
pub mod net;
Expand Down
48 changes: 24 additions & 24 deletions src/net/agency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use futures::Future;
use itertools::Itertools;

pub trait Broadcast {
type Error: Error + Send + 'static;
type BroadcastError: Error + Send + 'static;
// type Error: Error + 'static;

/// Broadcast a message to all other parties.
Expand All @@ -38,7 +38,7 @@ pub trait Broadcast {
fn broadcast(
&mut self,
msg: &(impl serde::Serialize + Sync),
) -> impl std::future::Future<Output = Result<(), Self::Error>>;
) -> impl std::future::Future<Output = Result<(), Self::BroadcastError>>;

/// Broadcast a message to all parties and await their messages
/// Messages are ordered by their index.
Expand All @@ -50,7 +50,7 @@ pub trait Broadcast {
fn symmetric_broadcast<T>(
&mut self,
msg: T,
) -> impl Future<Output = Result<Vec<T>, Self::Error>>
) -> impl Future<Output = Result<Vec<T>, Self::BroadcastError>>
where
T: serde::Serialize + serde::de::DeserializeOwned + Sync;

Expand All @@ -60,27 +60,27 @@ pub trait Broadcast {
fn recv_from<T: serde::de::DeserializeOwned>(
&mut self,
idx: usize,
) -> impl Future<Output = Result<T, Self::Error>>;
) -> impl Future<Output = Result<T, Self::BroadcastError>>;

/// Size of the broadcasting network including yourself,
/// as such there is n-1 outgoing connections
fn size(&self) -> usize;
}

impl<'a, B: Broadcast> Broadcast for &'a mut B {
type Error = B::Error;
type BroadcastError = B::BroadcastError;

fn broadcast(
&mut self,
msg: &(impl serde::Serialize + Sync),
) -> impl std::future::Future<Output = Result<(), Self::Error>> {
) -> impl std::future::Future<Output = Result<(), Self::BroadcastError>> {
(**self).broadcast(msg)
}

fn symmetric_broadcast<T>(
&mut self,
msg: T,
) -> impl Future<Output = Result<Vec<T>, Self::Error>>
) -> impl Future<Output = Result<Vec<T>, Self::BroadcastError>>
where
T: serde::Serialize + serde::de::DeserializeOwned + Sync,
{
Expand All @@ -90,7 +90,7 @@ impl<'a, B: Broadcast> Broadcast for &'a mut B {
fn recv_from<T: serde::de::DeserializeOwned>(
&mut self,
idx: usize,
) -> impl Future<Output = Result<T, Self::Error>> {
) -> impl Future<Output = Result<T, Self::BroadcastError>> {
(**self).recv_from(idx)
}

Expand All @@ -101,7 +101,7 @@ impl<'a, B: Broadcast> Broadcast for &'a mut B {

// TODO: Possible rename this trait as it's name is confusing.
pub trait Unicast {
type Error: Error + Send + 'static;
type UnicastError: Error + Send + 'static;

/// Unicast messages to each party
///
Expand All @@ -114,7 +114,7 @@ pub trait Unicast {
fn unicast(
&mut self,
msgs: &[impl serde::Serialize + Sync],
) -> impl std::future::Future<Output = Result<(), Self::Error>>;
) -> impl std::future::Future<Output = Result<(), Self::UnicastError>>;

/// Unicast a message to each party and await their messages
/// Messages are supposed to be in order, meaning message `i`
Expand All @@ -124,7 +124,7 @@ pub trait Unicast {
fn symmetric_unicast<T>(
&mut self,
msgs: Vec<T>,
) -> impl Future<Output = Result<Vec<T>, Self::Error>>
) -> impl Future<Output = Result<Vec<T>, Self::UnicastError>>
where
T: serde::Serialize + serde::de::DeserializeOwned + Sync;

Expand All @@ -135,37 +135,37 @@ pub trait Unicast {
/// Returns: A list sorted by the connections (skipping yourself)
fn receive_all<T: serde::de::DeserializeOwned>(
&mut self,
) -> impl Future<Output = Result<Vec<T>, Self::Error>>;
) -> impl Future<Output = Result<Vec<T>, Self::UnicastError>>;

/// Size of the unicasting network including yourself,
/// as such there is n-1 outgoing connections
fn size(&self) -> usize;
}

impl<'a, U: Unicast> Unicast for &'a mut U {
type Error = U::Error;
type UnicastError = U::UnicastError;

fn size(&self) -> usize {
(**self).size()
}

fn receive_all<T: serde::de::DeserializeOwned>(
&mut self,
) -> impl Future<Output = Result<Vec<T>, Self::Error>> {
) -> impl Future<Output = Result<Vec<T>, Self::UnicastError>> {
(**self).receive_all()
}

fn unicast(
&mut self,
msgs: &[impl serde::Serialize + Sync],
) -> impl std::future::Future<Output = Result<(), Self::Error>> {
) -> impl std::future::Future<Output = Result<(), Self::UnicastError>> {
(**self).unicast(msgs)
}

fn symmetric_unicast<T>(
&mut self,
msgs: Vec<T>,
) -> impl Future<Output = Result<Vec<T>, Self::Error>>
) -> impl Future<Output = Result<Vec<T>, Self::UnicastError>>
where
T: serde::Serialize + serde::de::DeserializeOwned + Sync,
{
Expand All @@ -191,7 +191,7 @@ impl<B: Broadcast, D: Digest> VerifiedBroadcast<B, D> {
pub async fn symmetric_broadcast<T>(
&mut self,
msg: T,
) -> Result<Vec<T>, BroadcastVerificationError<B::Error>>
) -> Result<Vec<T>, BroadcastVerificationError<B::BroadcastError>>
where
T: serde::Serialize + serde::de::DeserializeOwned,
{
Expand All @@ -213,7 +213,7 @@ impl<B: Broadcast, D: Digest> VerifiedBroadcast<B, D> {
// 3. Hash the hashes together and broadcast that
event!(Level::INFO, "Broadcast sum of all commits");
let mut digest = D::new();
for hash in msg_hashes.iter() {
for hash in &msg_hashes {
digest.update(hash);
}
let sum: Box<[u8]> = digest.finalize().to_vec().into_boxed_slice();
Expand Down Expand Up @@ -263,7 +263,7 @@ impl<B: Broadcast, D: Digest> VerifiedBroadcast<B, D> {
pub async fn broadcast<T>(
&mut self,
msg: &T,
) -> Result<(), BroadcastVerificationError<B::Error>>
) -> Result<(), BroadcastVerificationError<B::BroadcastError>>
where
T: serde::Serialize,
{
Expand Down Expand Up @@ -293,7 +293,7 @@ impl<B: Broadcast, D: Digest> VerifiedBroadcast<B, D> {
pub async fn recv_from<T>(
&mut self,
party: usize,
) -> Result<T, BroadcastVerificationError<B::Error>>
) -> Result<T, BroadcastVerificationError<B::BroadcastError>>
where
T: serde::de::DeserializeOwned,
{
Expand Down Expand Up @@ -354,13 +354,13 @@ pub enum BroadcastVerificationError<E> {
}

impl<B: Broadcast, D: Digest> Broadcast for VerifiedBroadcast<B, D> {
type Error = BroadcastVerificationError<<B as Broadcast>::Error>;
type BroadcastError = BroadcastVerificationError<<B as Broadcast>::BroadcastError>;

async fn broadcast(&mut self, msg: &impl serde::Serialize) -> Result<(), Self::Error> {
async fn broadcast(&mut self, msg: &impl serde::Serialize) -> Result<(), Self::BroadcastError> {
self.broadcast(msg).await
}

async fn symmetric_broadcast<T>(&mut self, msg: T) -> Result<Vec<T>, Self::Error>
async fn symmetric_broadcast<T>(&mut self, msg: T) -> Result<Vec<T>, Self::BroadcastError>
where
T: serde::Serialize + serde::de::DeserializeOwned,
{
Expand All @@ -370,7 +370,7 @@ impl<B: Broadcast, D: Digest> Broadcast for VerifiedBroadcast<B, D> {
fn recv_from<T: serde::de::DeserializeOwned>(
&mut self,
idx: usize,
) -> impl Future<Output = Result<T, Self::Error>> {
) -> impl Future<Output = Result<T, Self::BroadcastError>> {
self.recv_from(idx)
}

Expand Down
Loading

0 comments on commit 81ef369

Please sign in to comment.