Skip to content

Commit 73e05d1

Browse files
authored
fix: Save group name from the group creation request (#55)
2 parents 78bc8ec + a1ef6b2 commit 73e05d1

File tree

10 files changed

+29
-12
lines changed

10 files changed

+29
-12
lines changed

migrations/2025-08-27-171451_initial_schema/up.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ CREATE TABLE task (
5252
"protocol_round" integer NOT NULL CHECK ("protocol_round" >= 0),
5353
"attempt_count" integer NOT NULL CHECK ("attempt_count" >= 0),
5454
"threshold" integer NOT NULL CHECK ("threshold" > 0),
55+
"name" varchar NOT NULL,
5556
"task_data" bytea,
5657
"preprocessed" bytea,
5758
"request" bytea NOT NULL,

src/interfaces/grpc.rs

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

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

@@ -261,21 +261,20 @@ impl MeeSign for MeeSignService {
261261
.unwrap_or_else(|| "unknown".to_string());
262262
debug!("GroupsRequest device_id={}", device_str);
263263

264-
// TODO: refactor, consider storing device IDS in the group model directly
265264
let groups = if let Some(device_id) = device_id {
266265
self.state.activate_device(&device_id);
267266
self.state
268267
.get_device_groups(&device_id)
269268
.await?
270269
.into_iter()
271-
.map(Group::from_model)
270+
.map(msg::Group::from_model)
272271
.collect()
273272
} else {
274273
self.state
275274
.get_groups()
276275
.await?
277276
.into_iter()
278-
.map(Group::from_model)
277+
.map(msg::Group::from_model)
279278
.collect()
280279
};
281280

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ mod proto {
169169
let device_ids = model
170170
.participant_ids_shares
171171
.into_iter()
172-
.map(|(device_id, _)| device_id)
172+
.flat_map(|(device_id, shares)| std::iter::repeat_n(device_id, shares as usize))
173173
.collect();
174174
Self {
175175
identifier: model.id,

src/persistence/models.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ pub struct Task {
107107
pub protocol_round: i32,
108108
pub attempt_count: i32,
109109
pub threshold: i32,
110+
pub name: String,
110111
pub task_data: Option<Vec<u8>>,
111112
pub preprocessed: Option<Vec<u8>>,
112113
pub request: Vec<u8>,
@@ -128,6 +129,7 @@ pub struct NewTask<'a> {
128129
pub protocol_round: i32,
129130
pub attempt_count: i32,
130131
pub threshold: i32,
132+
pub name: &'a str,
131133
pub task_data: Option<&'a [u8]>,
132134
pub preprocessed: Option<&'a [u8]>,
133135
pub request: &'a [u8],

src/persistence/repository.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ impl Repository {
178178
id: Option<&Uuid>,
179179
participants: &[(&[u8], u32)],
180180
threshold: u32,
181+
name: &str,
181182
protocol_type: ProtocolType,
182183
key_type: KeyType,
183184
request: &[u8],
@@ -192,6 +193,7 @@ impl Repository {
192193
id,
193194
participants,
194195
threshold,
196+
name,
195197
key_type,
196198
protocol_type,
197199
request,

src/persistence/repository/group.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ mod test {
299299
None,
300300
participants,
301301
threshold,
302+
GROUP_1_NAME,
302303
KeyType::SignPdf,
303304
ProtocolType::Gg18,
304305
&[],
@@ -387,6 +388,7 @@ mod test {
387388
None,
388389
group_1_participants,
389390
threshold,
391+
GROUP_1_NAME,
390392
KeyType::Decrypt,
391393
ProtocolType::ElGamal,
392394
&[],
@@ -411,6 +413,7 @@ mod test {
411413
None,
412414
group_2_participants,
413415
threshold,
416+
GROUP_2_NAME,
414417
KeyType::SignChallenge,
415418
ProtocolType::Frost,
416419
&[],

src/persistence/repository/task.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ where
4242
protocol_round: 0,
4343
attempt_count: 0,
4444
threshold: threshold as i32,
45+
name,
4546
task_data,
4647
preprocessed: None,
4748
request,
@@ -89,6 +90,7 @@ pub async fn create_group_task<Conn>(
8990
id: Option<&Uuid>,
9091
participants: &[(&[u8], u32)],
9192
threshold: u32,
93+
name: &str,
9294
key_type: KeyType,
9395
protocol_type: ProtocolType,
9496
request: &[u8],
@@ -97,6 +99,12 @@ pub async fn create_group_task<Conn>(
9799
where
98100
Conn: AsyncConnection<Backend = Pg>,
99101
{
102+
if !name.is_name_valid() {
103+
return Err(PersistenceError::InvalidArgumentError(format!(
104+
"Invalid group name {name}"
105+
)));
106+
}
107+
100108
let total_shares: u32 = participants.iter().map(|(_, shares)| shares).sum();
101109
if !(1..=total_shares).contains(&threshold) {
102110
return Err(PersistenceError::InvalidArgumentError(format!(
@@ -111,6 +119,7 @@ where
111119
protocol_round: 0,
112120
attempt_count: 0,
113121
threshold,
122+
name,
114123
task_data: None,
115124
preprocessed: None,
116125
request,
@@ -158,6 +167,7 @@ macro_rules! task_model_columns {
158167
task::protocol_round,
159168
task::attempt_count,
160169
task::threshold,
170+
task::name,
161171
task::task_data,
162172
task::preprocessed,
163173
task::request,

src/persistence/repository/utils.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
const MAX_USERNAME_LENGTH: usize = 64;
1+
const MAX_NAME_LENGTH: usize = 256;
22

33
pub trait NameValidator {
44
fn is_name_valid(&self) -> bool;
55
}
66

77
impl NameValidator for &str {
88
fn is_name_valid(&self) -> bool {
9-
self.chars().count() <= MAX_USERNAME_LENGTH
10-
&& !self
11-
.chars()
12-
.any(|x| x.is_ascii_punctuation() || x.is_control())
9+
self.chars().count() <= MAX_NAME_LENGTH
10+
&& !self.chars().any(|x| x.is_control())
1311
&& !self.is_empty()
1412
}
1513
}

src/persistence/schema.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ diesel::table! {
7878
protocol_round -> Int4,
7979
attempt_count -> Int4,
8080
threshold -> Int4,
81+
name -> Varchar,
8182
task_data -> Nullable<Bytea>,
8283
preprocessed -> Nullable<Bytea>,
8384
request -> Bytea,

src/task_store.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ impl TaskStore {
168168
Some(&task.task_info.id),
169169
participant_ids_shares,
170170
threshold,
171+
&task.task_info.name,
171172
task.task_info.protocol_type,
172173
task.task_info.key_type,
173174
&task.request,
@@ -190,7 +191,7 @@ impl TaskStore {
190191
&group.id,
191192
participant_ids_shares,
192193
group.threshold as u32,
193-
"name", // TODO: Fix name checks
194+
&task.task_info.name,
194195
data,
195196
&task.request,
196197
task_type,
@@ -255,7 +256,7 @@ impl TaskStore {
255256
let participants = task_id_participants.remove(&task_model.id).unwrap();
256257
let task_info = TaskInfo {
257258
id: task_model.id,
258-
name: "".into(), // TODO: Persist "name" in TaskModel
259+
name: task_model.name.clone(),
259260
task_type: task_model.task_type,
260261
protocol_type: task_model.protocol_type,
261262
key_type: task_model.key_type,

0 commit comments

Comments
 (0)