@@ -24,6 +24,15 @@ pub enum SignerRegistrationError {
24
24
#[ error( "a signer registration round has not yet to be opened" ) ]
25
25
RegistrationRoundNotYetOpened ,
26
26
27
+ /// Registration round for unexpected epoch
28
+ #[ error( "unexpected signer registration round epoch: current_round_epoch: {current_round_epoch}, received_epoch: {received_epoch}" ) ]
29
+ RegistrationRoundUnexpectedEpoch {
30
+ /// Epoch of the current round
31
+ current_round_epoch : Epoch ,
32
+ /// Epoch of the received signer registration
33
+ received_epoch : Epoch ,
34
+ } ,
35
+
27
36
/// Codec error.
28
37
#[ error( "codec error: '{0}'" ) ]
29
38
Codec ( String ) ,
@@ -52,7 +61,9 @@ pub enum SignerRegistrationError {
52
61
/// Represents the information needed to handle a signer registration round
53
62
#[ derive( Debug , Clone , PartialEq , Eq ) ]
54
63
pub struct SignerRegistrationRound {
55
- epoch : Epoch ,
64
+ /// Registration round epoch
65
+ pub epoch : Epoch ,
66
+
56
67
stake_distribution : StakeDistribution ,
57
68
}
58
69
@@ -73,8 +84,12 @@ pub trait SignerRegisterer: Sync + Send {
73
84
/// Register a signer
74
85
async fn register_signer (
75
86
& self ,
87
+ epoch : Epoch ,
76
88
signer : & Signer ,
77
89
) -> Result < SignerWithStake , SignerRegistrationError > ;
90
+
91
+ /// Get current open round if exists
92
+ async fn get_current_round ( & self ) -> Option < SignerRegistrationRound > ;
78
93
}
79
94
80
95
/// Trait to open a signer registration round
@@ -171,12 +186,19 @@ impl SignerRegistrationRoundOpener for MithrilSignerRegisterer {
171
186
impl SignerRegisterer for MithrilSignerRegisterer {
172
187
async fn register_signer (
173
188
& self ,
189
+ epoch : Epoch ,
174
190
signer : & Signer ,
175
191
) -> Result < SignerWithStake , SignerRegistrationError > {
176
192
let registration_round = self . current_round . read ( ) . await ;
177
193
let registration_round = registration_round
178
194
. as_ref ( )
179
195
. ok_or ( SignerRegistrationError :: RegistrationRoundNotYetOpened ) ?;
196
+ if registration_round. epoch != epoch {
197
+ return Err ( SignerRegistrationError :: RegistrationRoundUnexpectedEpoch {
198
+ current_round_epoch : registration_round. epoch ,
199
+ received_epoch : epoch,
200
+ } ) ;
201
+ }
180
202
181
203
let mut key_registration = ProtocolKeyRegistration :: init (
182
204
& registration_round
@@ -245,6 +267,10 @@ impl SignerRegisterer for MithrilSignerRegisterer {
245
267
None => Ok ( signer_save) ,
246
268
}
247
269
}
270
+
271
+ async fn get_current_round ( & self ) -> Option < SignerRegistrationRound > {
272
+ self . current_round . read ( ) . await . as_ref ( ) . cloned ( )
273
+ }
248
274
}
249
275
250
276
#[ cfg( test) ]
@@ -293,7 +319,7 @@ mod tests {
293
319
. expect ( "signer registration round opening should not fail" ) ;
294
320
295
321
signer_registerer
296
- . register_signer ( & signer_to_register)
322
+ . register_signer ( registration_epoch , & signer_to_register)
297
323
. await
298
324
. expect ( "signer registration should not fail" ) ;
299
325
@@ -342,7 +368,7 @@ mod tests {
342
368
. expect ( "signer registration round opening should not fail" ) ;
343
369
344
370
signer_registerer
345
- . register_signer ( & signer_to_register)
371
+ . register_signer ( registration_epoch , & signer_to_register)
346
372
. await
347
373
. expect ( "signer registration should not fail" ) ;
348
374
@@ -373,11 +399,12 @@ mod tests {
373
399
verification_key_store. clone ( ) ,
374
400
Arc :: new ( signer_recorder) ,
375
401
) ;
402
+ let registration_epoch = Epoch ( 1 ) ;
376
403
let fixture = MithrilFixtureBuilder :: default ( ) . with_signers ( 5 ) . build ( ) ;
377
404
let signer_to_register: Signer = fixture. signers ( ) [ 0 ] . to_owned ( ) ;
378
405
379
406
signer_registerer
380
- . register_signer ( & signer_to_register)
407
+ . register_signer ( registration_epoch , & signer_to_register)
381
408
. await
382
409
. expect_err ( "signer registration should fail if no round opened" ) ;
383
410
}
0 commit comments