Skip to content

Commit 775eaac

Browse files
committed
Save group name on the group creation
1 parent c10847f commit 775eaac

File tree

4 files changed

+67
-24
lines changed

4 files changed

+67
-24
lines changed

src/group.rs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -87,24 +87,23 @@ impl Group {
8787
}
8888
}
8989

90-
// impl From<&Group> for crate::proto::Group {
91-
// fn from(group: &Group) -> Self {
92-
// crate::proto::Group {
93-
// identifier: group.identifier().to_vec(),
94-
// name: group.name().to_owned(),
95-
// threshold: group.threshold(),
96-
// device_ids: group
97-
// .devices()
98-
// .iter()
99-
// .map(|x| x.identifier())
100-
// .map(Vec::from)
101-
// .collect(),
102-
// protocol: group.protocol().into(),
103-
// key_type: group.key_type().into(),
104-
// note: group.note().map(String::from),
105-
// }
106-
// }
107-
// }
90+
impl From<&Group> for crate::proto::Group {
91+
fn from(group: &Group) -> Self {
92+
crate::proto::Group {
93+
identifier: group.identifier().to_vec(),
94+
name: group.name().to_owned(),
95+
threshold: group.threshold(),
96+
device_ids: group
97+
.participants()
98+
.iter()
99+
.flat_map(|p| std::iter::repeat(p.device.id.clone()).take(p.shares as usize))
100+
.collect(),
101+
protocol: group.protocol() as i32,
102+
key_type: group.key_type() as i32,
103+
note: group.note().map(String::from),
104+
}
105+
}
106+
}
108107

109108
#[cfg(test)]
110109
mod tests {

src/interfaces/grpc.rs

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use tonic::{Request, Response, Status};
1818
use uuid::Uuid;
1919

2020
use crate::persistence::DeviceKind;
21-
use crate::proto::{Group, KeyType, MeeSign, MeeSignServer, ProtocolType};
21+
use crate::proto::{KeyType, MeeSign, MeeSignServer, ProtocolType};
2222
use crate::state::State;
2323
use crate::{proto as msg, utils, CA_CERT, CA_KEY};
2424

@@ -275,21 +275,52 @@ impl MeeSign for MeeSignService {
275275
debug!("GroupsRequest device_id={}", device_str);
276276

277277
let state = self.state.lock().await;
278-
// TODO: refactor, consider storing device IDS in the group model directly
279278
let groups = if let Some(device_id) = device_id {
280279
state.activate_device(&device_id);
281280
state
282281
.get_device_groups(&device_id)
283282
.await?
284283
.into_iter()
285-
.map(Group::from_model)
284+
.map(|group_model| {
285+
msg::Group {
286+
identifier: group_model.id.clone(),
287+
name: group_model.name.clone(),
288+
threshold: group_model.threshold as u32,
289+
device_ids: group_model
290+
.participant_ids_shares
291+
.iter()
292+
.flat_map(|(device_id, shares)| {
293+
std::iter::repeat(device_id.clone()).take(*shares as usize)
294+
})
295+
.collect(),
296+
protocol: group_model.protocol as i32,
297+
key_type: group_model.key_type as i32,
298+
note: group_model.note.clone(),
299+
}
300+
})
286301
.collect()
287302
} else {
288303
state
289304
.get_groups()
290305
.await?
291306
.into_iter()
292-
.map(Group::from_model)
307+
.map(|group_model| {
308+
msg::Group {
309+
identifier: group_model.id.clone(),
310+
name: group_model.name.clone(),
311+
threshold: group_model.threshold as u32,
312+
device_ids: group_model
313+
.participant_ids_shares
314+
.iter()
315+
.flat_map(|(device_id, shares)| {
316+
std::iter::repeat(device_id.clone()).take(*shares as usize)
317+
})
318+
.collect(),
319+
protocol: group_model.protocol as i32,
320+
key_type: group_model.key_type as i32,
321+
note: group_model.note.clone(),
322+
}
323+
})
293324
.collect()
294325
};
295326

src/state.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,13 @@ impl State {
286286
Ok(self.get_repo().get_groups().await?)
287287
}
288288

289+
pub async fn get_group_participants(
290+
&self,
291+
group_id: &[u8],
292+
) -> Result<Vec<Participant>, Error> {
293+
Ok(self.get_repo().get_group_participants(group_id).await?)
294+
}
295+
289296
pub async fn get_tasks(&self) -> Result<Vec<TaskModel>, Error> {
290297
Ok(self.repo.get_tasks().await?)
291298
}

src/tasks/group.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::group::Group;
44
use crate::persistence::Participant;
55
use crate::persistence::PersistenceError;
66
use crate::persistence::Task as TaskModel;
7-
use crate::proto::{KeyType, ProtocolType, TaskType};
7+
use crate::proto::{GroupRequest, KeyType, ProtocolType, TaskType};
88
use crate::protocols::{create_keygen_protocol, Protocol};
99
use crate::tasks::{DecisionUpdate, RestartUpdate, RoundUpdate, Task, TaskResult};
1010
use crate::utils;
@@ -125,6 +125,10 @@ impl GroupTask {
125125
model.protocol_round as u16,
126126
)?;
127127

128+
// Parse the GroupRequest from the task's request field to get the name
129+
let group_request = GroupRequest::decode(model.request.as_slice())
130+
.map_err(|e| Error::GeneralProtocolError(format!("Failed to parse GroupRequest: {}", e)))?;
131+
128132
// TODO: refactor
129133
let result = model.result.map(|res| res.try_into_result()).transpose()?;
130134
let result = match result {
@@ -142,10 +146,12 @@ impl GroupTask {
142146
Some(Err(err)) => Some(Err(err)),
143147
None => None,
144148
};
149+
// Use the name from the GroupRequest if the group hasn't been established yet,
150+
// otherwise use the name from the established group
145151
let name = if let Some(Ok(group)) = &result {
146152
group.name().into()
147153
} else {
148-
"".into() // TODO add field to the task table
154+
group_request.name
149155
};
150156
let Some(certificates_sent) = model.group_certificates_sent else {
151157
return Err(Error::PersistenceError(

0 commit comments

Comments
 (0)