@@ -2,10 +2,10 @@ use alloc::collections::{BTreeMap, BTreeSet};
2
2
use core:: fmt:: Debug ;
3
3
4
4
use manul:: protocol:: {
5
- Artifact , BoxedFormat , BoxedRound , CommunicationInfo , DirectMessage , EchoBroadcast , EntryPoint , FinalizeOutcome ,
6
- LocalError , MessageValidationError , NormalBroadcast , PartyId , Payload , Protocol , ProtocolError , ProtocolMessage ,
7
- ProtocolMessagePart , ProtocolValidationError , ReceiveError , RequiredMessageParts , RequiredMessages , Round , RoundId ,
8
- TransitionInfo ,
5
+ BoxedFormat , BoxedRound , CommunicationInfo , DirectMessage , EchoBroadcast , EntryPoint , FinalizeOutcome , LocalError ,
6
+ MessageValidationError , NoMessage , NormalBroadcast , PartyId , Protocol , ProtocolError , ProtocolMessage ,
7
+ ProtocolMessagePart , ProtocolValidationError , ReceiveError , RequiredMessageParts , RequiredMessages , RoundId ,
8
+ StaticProtocolMessage , StaticRound , TransitionInfo ,
9
9
} ;
10
10
use rand_core:: CryptoRngCore ;
11
11
use serde:: { Deserialize , Serialize } ;
@@ -121,7 +121,7 @@ pub(crate) struct Context<Id> {
121
121
}
122
122
123
123
#[ derive( Debug ) ]
124
- pub struct Round1 < Id > {
124
+ pub ( crate ) struct Round1 < Id > {
125
125
pub ( crate ) context : Context < Id > ,
126
126
}
127
127
@@ -132,17 +132,17 @@ pub(crate) struct Round1Message {
132
132
}
133
133
134
134
#[ derive( Serialize , Deserialize ) ]
135
- struct Round1Echo {
135
+ pub ( crate ) struct Round1Echo {
136
136
my_position : u8 ,
137
137
}
138
138
139
139
#[ derive( Serialize , Deserialize ) ]
140
- struct Round1Broadcast {
140
+ pub ( crate ) struct Round1Broadcast {
141
141
x : u8 ,
142
142
my_position : u8 ,
143
143
}
144
144
145
- struct Round1Payload {
145
+ pub ( crate ) struct Round1Payload {
146
146
x : u8 ,
147
147
}
148
148
@@ -182,7 +182,7 @@ impl<Id: PartyId> EntryPoint<Id> for SimpleProtocolEntryPoint<Id> {
182
182
let mut ids = self . all_ids ;
183
183
ids. remove ( id) ;
184
184
185
- Ok ( BoxedRound :: new_dynamic ( Round1 {
185
+ Ok ( BoxedRound :: new_static ( Round1 {
186
186
context : Context {
187
187
id : id. clone ( ) ,
188
188
other_ids : ids,
@@ -192,7 +192,7 @@ impl<Id: PartyId> EntryPoint<Id> for SimpleProtocolEntryPoint<Id> {
192
192
}
193
193
}
194
194
195
- impl < Id : PartyId > Round < Id > for Round1 < Id > {
195
+ impl < Id : PartyId > StaticRound < Id > for Round1 < Id > {
196
196
type Protocol = SimpleProtocol ;
197
197
198
198
fn transition_info ( & self ) -> TransitionInfo {
@@ -203,92 +203,71 @@ impl<Id: PartyId> Round<Id> for Round1<Id> {
203
203
CommunicationInfo :: regular ( & self . context . other_ids )
204
204
}
205
205
206
- fn make_normal_broadcast (
207
- & self ,
208
- _rng : & mut dyn CryptoRngCore ,
209
- format : & BoxedFormat ,
210
- ) -> Result < NormalBroadcast , LocalError > {
211
- debug ! ( "{:?}: making normal broadcast" , self . context. id) ;
206
+ type NormalBroadcast = Round1Broadcast ;
207
+ type EchoBroadcast = Round1Echo ;
208
+ type DirectMessage = Round1Message ;
212
209
213
- let message = Round1Broadcast {
210
+ type Payload = Round1Payload ;
211
+ type Artifact = ( ) ;
212
+
213
+ fn make_normal_broadcast ( & self , _rng : & mut dyn CryptoRngCore ) -> Result < Self :: NormalBroadcast , LocalError > {
214
+ debug ! ( "{:?}: making normal broadcast" , self . context. id) ;
215
+ Ok ( Round1Broadcast {
214
216
x : 0 ,
215
217
my_position : self . context . ids_to_positions [ & self . context . id ] ,
216
- } ;
217
-
218
- NormalBroadcast :: new ( format, message)
218
+ } )
219
219
}
220
220
221
- fn make_echo_broadcast (
222
- & self ,
223
- _rng : & mut dyn CryptoRngCore ,
224
- format : & BoxedFormat ,
225
- ) -> Result < EchoBroadcast , LocalError > {
221
+ fn make_echo_broadcast ( & self , _rng : & mut dyn CryptoRngCore ) -> Result < Self :: EchoBroadcast , LocalError > {
226
222
debug ! ( "{:?}: making echo broadcast" , self . context. id) ;
227
-
228
- let message = Round1Echo {
223
+ Ok ( Round1Echo {
229
224
my_position : self . context . ids_to_positions [ & self . context . id ] ,
230
- } ;
231
-
232
- EchoBroadcast :: new ( format, message)
225
+ } )
233
226
}
234
227
235
228
fn make_direct_message (
236
229
& self ,
237
230
_rng : & mut dyn CryptoRngCore ,
238
- format : & BoxedFormat ,
239
231
destination : & Id ,
240
- ) -> Result < ( DirectMessage , Option < Artifact > ) , LocalError > {
232
+ ) -> Result < ( Self :: DirectMessage , Option < Self :: Artifact > ) , LocalError > {
241
233
debug ! ( "{:?}: making direct message for {:?}" , self . context. id, destination) ;
242
-
243
234
let message = Round1Message {
244
235
my_position : self . context . ids_to_positions [ & self . context . id ] ,
245
236
your_position : self . context . ids_to_positions [ destination] ,
246
237
} ;
247
- let dm = DirectMessage :: new ( format, message) ?;
248
- Ok ( ( dm, None ) )
238
+ Ok ( ( message, Some ( ( ) ) ) )
249
239
}
250
240
251
241
fn receive_message (
252
242
& self ,
253
- format : & BoxedFormat ,
254
243
from : & Id ,
255
- message : ProtocolMessage ,
256
- ) -> Result < Payload , ReceiveError < Id , Self :: Protocol > > {
244
+ message : StaticProtocolMessage < Id , Self > ,
245
+ ) -> Result < Self :: Payload , ReceiveError < Id , Self :: Protocol > > {
257
246
debug ! ( "{:?}: receiving message from {:?}" , self . context. id, from) ;
258
-
259
- let _echo = message. echo_broadcast . deserialize :: < Round1Echo > ( format) ?;
260
- let _normal = message. normal_broadcast . deserialize :: < Round1Broadcast > ( format) ?;
261
- let message = message. direct_message . deserialize :: < Round1Message > ( format) ?;
262
-
263
- debug ! ( "{:?}: received message: {:?}" , self . context. id, message) ;
247
+ let message = message. direct_message ;
264
248
265
249
if self . context . ids_to_positions [ & self . context . id ] != message. your_position {
266
250
return Err ( ReceiveError :: protocol ( SimpleProtocolError :: Round1InvalidPosition ) ) ;
267
251
}
268
-
269
- Ok ( Payload :: new ( Round1Payload { x : message. my_position } ) )
252
+ Ok ( Round1Payload { x : message. my_position } )
270
253
}
271
254
272
255
fn finalize (
273
- self : Box < Self > ,
256
+ self ,
274
257
_rng : & mut dyn CryptoRngCore ,
275
- payloads : BTreeMap < Id , Payload > ,
276
- _artifacts : BTreeMap < Id , Artifact > ,
258
+ payloads : BTreeMap < Id , Self :: Payload > ,
259
+ _artifacts : BTreeMap < Id , Self :: Artifact > ,
277
260
) -> Result < FinalizeOutcome < Id , Self :: Protocol > , LocalError > {
278
261
debug ! (
279
262
"{:?}: finalizing with messages from {:?}" ,
280
263
self . context. id,
281
264
payloads. keys( ) . cloned( ) . collect:: <Vec <_>>( )
282
265
) ;
283
266
284
- let typed_payloads = payloads
285
- . into_values ( )
286
- . map ( |payload| payload. downcast :: < Round1Payload > ( ) )
287
- . collect :: < Result < Vec < _ > , _ > > ( ) ?;
288
- let sum = self . context . ids_to_positions [ & self . context . id ]
289
- + typed_payloads. iter ( ) . map ( |payload| payload. x ) . sum :: < u8 > ( ) ;
267
+ let sum =
268
+ self . context . ids_to_positions [ & self . context . id ] + payloads. values ( ) . map ( |payload| payload. x ) . sum :: < u8 > ( ) ;
290
269
291
- let round2 = BoxedRound :: new_dynamic ( Round2 {
270
+ let round2 = BoxedRound :: new_static ( Round2 {
292
271
round1_sum : sum,
293
272
context : self . context ,
294
273
} ) ;
@@ -308,7 +287,7 @@ pub(crate) struct Round2Message {
308
287
pub ( crate ) your_position : u8 ,
309
288
}
310
289
311
- impl < Id : PartyId > Round < Id > for Round2 < Id > {
290
+ impl < Id : PartyId > StaticRound < Id > for Round2 < Id > {
312
291
type Protocol = SimpleProtocol ;
313
292
314
293
fn transition_info ( & self ) -> TransitionInfo {
@@ -319,62 +298,59 @@ impl<Id: PartyId> Round<Id> for Round2<Id> {
319
298
CommunicationInfo :: regular ( & self . context . other_ids )
320
299
}
321
300
301
+ type DirectMessage = Round2Message ;
302
+ type EchoBroadcast = NoMessage ;
303
+ type NormalBroadcast = NoMessage ;
304
+
305
+ type Payload = Round1Payload ;
306
+ type Artifact = ( ) ;
307
+
322
308
fn make_direct_message (
323
309
& self ,
324
310
_rng : & mut dyn CryptoRngCore ,
325
- format : & BoxedFormat ,
326
311
destination : & Id ,
327
- ) -> Result < ( DirectMessage , Option < Artifact > ) , LocalError > {
312
+ ) -> Result < ( Self :: DirectMessage , Option < Self :: Artifact > ) , LocalError > {
328
313
debug ! ( "{:?}: making direct message for {:?}" , self . context. id, destination) ;
329
314
330
315
let message = Round2Message {
331
316
my_position : self . context . ids_to_positions [ & self . context . id ] ,
332
317
your_position : self . context . ids_to_positions [ destination] ,
333
318
} ;
334
- let dm = DirectMessage :: new ( format, message) ?;
335
- Ok ( ( dm, None ) )
319
+ Ok ( ( message, Some ( ( ) ) ) )
336
320
}
337
321
338
322
fn receive_message (
339
323
& self ,
340
- format : & BoxedFormat ,
341
324
from : & Id ,
342
- message : ProtocolMessage ,
343
- ) -> Result < Payload , ReceiveError < Id , Self :: Protocol > > {
325
+ message : StaticProtocolMessage < Id , Self > ,
326
+ ) -> Result < Self :: Payload , ReceiveError < Id , Self :: Protocol > > {
344
327
debug ! ( "{:?}: receiving message from {:?}" , self . context. id, from) ;
345
328
346
- message. echo_broadcast . assert_is_none ( ) ?;
347
- message. normal_broadcast . assert_is_none ( ) ?;
348
-
349
- let message = message. direct_message . deserialize :: < Round1Message > ( format) ?;
329
+ let message = message. direct_message ;
350
330
351
331
debug ! ( "{:?}: received message: {:?}" , self . context. id, message) ;
352
332
353
333
if self . context . ids_to_positions [ & self . context . id ] != message. your_position {
354
334
return Err ( ReceiveError :: protocol ( SimpleProtocolError :: Round2InvalidPosition ) ) ;
355
335
}
356
336
357
- Ok ( Payload :: new ( Round1Payload { x : message. my_position } ) )
337
+ Ok ( Round1Payload { x : message. my_position } )
358
338
}
359
339
360
340
fn finalize (
361
- self : Box < Self > ,
341
+ self : Self ,
362
342
_rng : & mut dyn CryptoRngCore ,
363
- payloads : BTreeMap < Id , Payload > ,
364
- _artifacts : BTreeMap < Id , Artifact > ,
343
+ payloads : BTreeMap < Id , Self :: Payload > ,
344
+ _artifacts : BTreeMap < Id , Self :: Artifact > ,
365
345
) -> Result < FinalizeOutcome < Id , Self :: Protocol > , LocalError > {
366
346
debug ! (
367
347
"{:?}: finalizing with messages from {:?}" ,
368
348
self . context. id,
369
349
payloads. keys( ) . cloned( ) . collect:: <Vec <_>>( )
370
350
) ;
371
351
372
- let typed_payloads = payloads
373
- . into_values ( )
374
- . map ( |payload| payload. downcast :: < Round1Payload > ( ) )
375
- . collect :: < Result < Vec < _ > , _ > > ( ) ?;
376
- let sum = self . context . ids_to_positions [ & self . context . id ]
377
- + typed_payloads. iter ( ) . map ( |payload| payload. x ) . sum :: < u8 > ( ) ;
352
+ let sum =
353
+ self . context . ids_to_positions [ & self . context . id ] + payloads. values ( ) . map ( |payload| payload. x ) . sum :: < u8 > ( ) ;
378
354
379
355
Ok ( FinalizeOutcome :: Result ( sum + self . round1_sum ) )
380
356
}
0 commit comments