Skip to content

Commit

Permalink
refactor: follow rustfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
MarekMracna committed Sep 5, 2024
1 parent ef46054 commit 133a05c
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 39 deletions.
78 changes: 47 additions & 31 deletions src/communicator.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::device::Device;
use crate::get_timestamp;
use crate::proto::ProtocolType;
use meesign_crypto::proto::{Message, ClientMessage, ServerMessage};
use meesign_crypto::proto::{ClientMessage, Message, ServerMessage};
use rand::prelude::SliceRandom;
use rand::thread_rng;
use std::collections::HashMap;
Expand Down Expand Up @@ -100,7 +100,8 @@ impl Communicator {

/// Moves messages from incoming buffers to outgoing buffers
pub fn relay(&mut self) {
self.output = self.get_protocol_indices()
self.output = self
.get_protocol_indices()
.into_iter()
.map(|idx| {
let mut unicasts = HashMap::new();
Expand All @@ -119,7 +120,8 @@ impl Communicator {
}

let msg = ServerMessage {
protocol_type: meesign_crypto::proto::ProtocolType::from(self.protocol_type) as i32,
protocol_type: meesign_crypto::proto::ProtocolType::from(self.protocol_type)
.into(),
unicasts,
broadcasts,
};
Expand All @@ -136,7 +138,8 @@ impl Communicator {
where
F: Fn(u32) -> Vec<u8>,
{
self.output = self.get_protocol_indices()
self.output = self
.get_protocol_indices()
.into_iter()
.map(|idx| (idx, f(idx)))
.collect();
Expand All @@ -163,7 +166,8 @@ impl Communicator {

/// Get final message
pub fn get_final_message(&self) -> Option<Vec<u8>> {
let results: Vec<_> = self.input
let results: Vec<_> = self
.input
.iter()
.map(|(_, msg)| msg.broadcast.clone())
.collect();
Expand Down Expand Up @@ -508,13 +512,14 @@ mod tests {
communicator.decide(devices[1].identifier(), true);
communicator.decide(devices[2].identifier(), true);
communicator.set_active_devices();
communicator.receive_messages(devices[0].identifier(), vec![
ClientMessage {
communicator.receive_messages(
devices[0].identifier(),
vec![ClientMessage {
protocol_type: 0,
unicasts: HashMap::new(),
broadcast: None,
}
]);
}],
);
}

#[test]
Expand All @@ -526,13 +531,14 @@ mod tests {
communicator.decide(devices[1].identifier(), true);
communicator.decide(devices[2].identifier(), true);
communicator.set_active_devices();
communicator.receive_messages(devices[0].identifier(), vec![
ClientMessage {
communicator.receive_messages(
devices[0].identifier(),
vec![ClientMessage {
protocol_type: 0,
unicasts: (0..6 as u32).map(|i| (i, vec![])).collect(),
broadcast: None,
}
]);
}],
);
}

#[test]
Expand Down Expand Up @@ -605,20 +611,24 @@ mod tests {
devices[2].identifier().to_vec()
])
);
communicator.send_all(|idx| ProtocolInit {
protocol_type: ProtocolType::Frost as i32,
indices: Vec::new(),
index: idx,
data: "hello".as_bytes().to_vec(),
}.encode_to_vec());
communicator.send_all(|idx| {
ProtocolInit {
protocol_type: ProtocolType::Frost as i32,
indices: Vec::new(),
index: idx,
data: "hello".as_bytes().to_vec(),
}
.encode_to_vec()
});
assert_eq!(
communicator.get_messages(devices[0].identifier()),
vec![ProtocolInit {
protocol_type: ProtocolType::Frost as i32,
indices: Vec::new(),
index: 1,
data: "hello".as_bytes().to_vec(),
}.encode_to_vec()]
}
.encode_to_vec()]
);
assert_eq!(
communicator.get_messages(devices[1].identifier()),
Expand All @@ -631,7 +641,8 @@ mod tests {
indices: Vec::new(),
index: 3,
data: "hello".as_bytes().to_vec(),
}.encode_to_vec()]
}
.encode_to_vec()]
);
}

Expand Down Expand Up @@ -663,14 +674,17 @@ mod tests {
assert_eq!(communicator.get_protocol_indices(), vec![1, 2]);

for i in 0..2 {
assert_eq!(communicator.receive_messages(
devices[i].identifier(),
vec![ClientMessage {
protocol_type: ProtocolType::Frost.into(),
unicasts: HashMap::new(),
broadcast: Some(vec![i as u8]),
}]
), true);
assert_eq!(
communicator.receive_messages(
devices[i].identifier(),
vec![ClientMessage {
protocol_type: ProtocolType::Frost.into(),
unicasts: HashMap::new(),
broadcast: Some(vec![i as u8]),
}]
),
true
);
}

assert_eq!(communicator.round_received(), true);
Expand All @@ -684,15 +698,17 @@ mod tests {
protocol_type: ProtocolType::Frost.into(),
unicasts: HashMap::new(),
broadcasts: HashMap::from([(2, vec![1])]),
}.encode_to_vec()],
}
.encode_to_vec()],
);
assert_eq!(
communicator.get_messages(devices[1].identifier()),
vec![ServerMessage {
protocol_type: ProtocolType::Frost.into(),
unicasts: HashMap::new(),
broadcasts: HashMap::from([(1, vec![0])]),
}.encode_to_vec()],
}
.encode_to_vec()],
);
}

Expand Down
3 changes: 1 addition & 2 deletions src/protocols/frost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ impl FROSTSign {
impl Protocol for FROSTSign {
fn initialize(&mut self, communicator: &mut Communicator, data: &[u8]) {
communicator.set_active_devices();
let participant_indices: Vec<_> = communicator
.get_protocol_indices();
let participant_indices = communicator.get_protocol_indices();
communicator.send_all(|idx| {
(ProtocolInit {
protocol_type: meesign_crypto::proto::ProtocolType::Frost as i32,
Expand Down
5 changes: 3 additions & 2 deletions src/tasks/decrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::protocols::Protocol;
use crate::tasks::{Task, TaskResult, TaskStatus};
use crate::{get_timestamp, utils};
use log::info;
use meesign_crypto::proto::{Message as _, ClientMessage};
use meesign_crypto::proto::{ClientMessage, Message as _};
use prost::Message as _;
use tonic::codegen::Arc;

Expand Down Expand Up @@ -96,7 +96,8 @@ impl DecryptTask {
return Err("Wasn't waiting for a message from this ID.".to_string());
}

let messages = data.iter()
let messages = data
.iter()
.map(|d| ClientMessage::decode(d.as_slice()))
.collect::<Result<Vec<_>, _>>()
.map_err(|_| "Failed to decode messages".to_string())?;
Expand Down
5 changes: 3 additions & 2 deletions src/tasks/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::protocols::Protocol;
use crate::tasks::{Task, TaskResult, TaskStatus};
use crate::{get_timestamp, utils};
use log::{info, warn};
use meesign_crypto::proto::{Message as _, ClientMessage};
use meesign_crypto::proto::{ClientMessage, Message as _};
use prost::Message as _;
use std::io::Read;
use std::process::{Command, Stdio};
Expand Down Expand Up @@ -204,7 +204,8 @@ impl Task for GroupTask {
return Err("Wasn't waiting for a message from this ID.".to_string());
}

let messages = data.iter()
let messages = data
.iter()
.map(|d| ClientMessage::decode(d.as_slice()))
.collect::<Result<Vec<_>, _>>()
.map_err(|_| "Failed to decode messages".to_string())?;
Expand Down
5 changes: 3 additions & 2 deletions src/tasks/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::protocols::Protocol;
use crate::tasks::{Task, TaskResult, TaskStatus};
use crate::{get_timestamp, utils};
use log::{info, warn};
use meesign_crypto::proto::{Message as _, ClientMessage};
use meesign_crypto::proto::{ClientMessage, Message as _};
use prost::Message as _;
use tonic::codegen::Arc;

Expand Down Expand Up @@ -118,7 +118,8 @@ impl SignTask {
return Err("Wasn't waiting for a message from this ID.".to_string());
}

let messages = data.iter()
let messages = data
.iter()
.map(|d| ClientMessage::decode(d.as_slice()))
.collect::<Result<Vec<_>, _>>()
.map_err(|_| "Failed to decode messages".to_string())?;
Expand Down

0 comments on commit 133a05c

Please sign in to comment.