@@ -10,6 +10,7 @@ use mithril_common::{
10
10
use mithril_persistence:: store:: StakeStorer ;
11
11
12
12
use crate :: {
13
+ dependency_injection:: EpochServiceWrapper ,
13
14
services:: { AggregatorClient , EpochPruningTask } ,
14
15
SignerRegistrationVerifier , VerificationKeyStorer ,
15
16
} ;
@@ -21,6 +22,9 @@ use super::{
21
22
22
23
/// A [MithrilSignerRegistrationSlave] supports signer registrations in a slave aggregator
23
24
pub struct MithrilSignerRegistrationSlave {
25
+ /// Epoch service
26
+ pub epoch_service : EpochServiceWrapper ,
27
+
24
28
/// Verification key store
25
29
verification_key_store : Arc < dyn VerificationKeyStorer > ,
26
30
@@ -44,6 +48,7 @@ pub struct MithrilSignerRegistrationSlave {
44
48
impl MithrilSignerRegistrationSlave {
45
49
/// MithrilSignerRegistererSlave factory
46
50
pub fn new (
51
+ epoch_service : EpochServiceWrapper ,
47
52
verification_key_store : Arc < dyn VerificationKeyStorer > ,
48
53
signer_recorder : Arc < dyn SignerRecorder > ,
49
54
signer_registration_verifier : Arc < dyn SignerRegistrationVerifier > ,
@@ -52,6 +57,7 @@ impl MithrilSignerRegistrationSlave {
52
57
verification_key_epoch_retention_limit : Option < u64 > ,
53
58
) -> Self {
54
59
Self {
60
+ epoch_service,
55
61
verification_key_store,
56
62
signer_recorder,
57
63
signer_registration_verifier,
@@ -90,9 +96,16 @@ impl MithrilSignerRegistrationSlave {
90
96
epoch
91
97
)
92
98
} )
93
- . map_err ( |e| SignerRegistrationError :: StoreError ( anyhow ! ( e) ) ) ?;
99
+ . map_err ( |e| SignerRegistrationError :: Store ( anyhow ! ( e) ) ) ?;
94
100
}
95
101
102
+ self . epoch_service
103
+ . write ( )
104
+ . await
105
+ . update_next_signers_with_stake ( )
106
+ . await
107
+ . map_err ( |e| SignerRegistrationError :: EpochService ( anyhow ! ( e) ) ) ?;
108
+
96
109
Ok ( ( ) )
97
110
}
98
111
}
@@ -130,8 +143,8 @@ impl SignerSynchronizer for MithrilSignerRegistrationSlave {
130
143
. get_stakes ( registration_epoch)
131
144
. await
132
145
. with_context ( || "synchronize_all_signers failed" )
133
- . map_err ( SignerRegistrationError :: StoreError ) ?
134
- . ok_or ( SignerRegistrationError :: StoreError ( anyhow:: anyhow!(
146
+ . map_err ( SignerRegistrationError :: Store ) ?
147
+ . ok_or ( SignerRegistrationError :: Store ( anyhow:: anyhow!(
135
148
"Slave aggregator did not return any stake distribution"
136
149
) ) ) ?;
137
150
self . synchronize_signers ( registration_epoch, & next_signers, & stake_distribution)
@@ -214,8 +227,9 @@ mod tests {
214
227
database:: { repository:: SignerRegistrationStore , test_helper:: main_db_connection} ,
215
228
message_adapters:: FromEpochSettingsAdapter ,
216
229
services:: {
217
- AggregatorClient , AggregatorClientError , EpochPruningTask , MockAggregatorClient ,
218
- MockSignerRecorder , MockSignerRegistrationVerifier , SignerSynchronizer ,
230
+ AggregatorClient , AggregatorClientError , EpochPruningTask , FakeEpochService ,
231
+ MockAggregatorClient , MockSignerRecorder , MockSignerRegistrationVerifier ,
232
+ SignerSynchronizer ,
219
233
} ,
220
234
store:: MockVerificationKeyStorer ,
221
235
tools:: mocks:: MockStakeStore ,
@@ -226,10 +240,15 @@ mod tests {
226
240
use test_utils:: * ;
227
241
228
242
mod test_utils {
243
+ use tokio:: sync:: RwLock ;
244
+
245
+ use crate :: { dependency_injection:: EpochServiceWrapper , services:: FakeEpochService } ;
246
+
229
247
use super :: * ;
230
248
231
249
/// MithrilSignerRegistrationSlaveBuilder is a test builder for [MithrilSignerRegistrationSlave]
232
250
pub struct MithrilSignerRegistrationSlaveBuilder {
251
+ epoch_service : EpochServiceWrapper ,
233
252
signer_recorder : Arc < dyn SignerRecorder > ,
234
253
signer_registration_verifier : Arc < dyn SignerRegistrationVerifier > ,
235
254
master_aggregator_client : Arc < dyn AggregatorClient > ,
@@ -241,6 +260,7 @@ mod tests {
241
260
impl Default for MithrilSignerRegistrationSlaveBuilder {
242
261
fn default ( ) -> Self {
243
262
Self {
263
+ epoch_service : Arc :: new ( RwLock :: new ( FakeEpochService :: without_data ( ) ) ) ,
244
264
signer_recorder : Arc :: new ( MockSignerRecorder :: new ( ) ) ,
245
265
signer_registration_verifier : Arc :: new ( MockSignerRegistrationVerifier :: new ( ) ) ,
246
266
master_aggregator_client : Arc :: new ( MockAggregatorClient :: new ( ) ) ,
@@ -254,6 +274,13 @@ mod tests {
254
274
}
255
275
256
276
impl MithrilSignerRegistrationSlaveBuilder {
277
+ pub fn with_epoch_service ( self , epoch_service : FakeEpochService ) -> Self {
278
+ Self {
279
+ epoch_service : Arc :: new ( RwLock :: new ( epoch_service) ) ,
280
+ ..self
281
+ }
282
+ }
283
+
257
284
pub fn with_verification_key_store (
258
285
self ,
259
286
verification_key_store : Arc < dyn VerificationKeyStorer > ,
@@ -310,6 +337,7 @@ mod tests {
310
337
311
338
pub fn build ( self ) -> MithrilSignerRegistrationSlave {
312
339
MithrilSignerRegistrationSlave {
340
+ epoch_service : self . epoch_service ,
313
341
verification_key_store : self . verification_key_store ,
314
342
signer_recorder : self . signer_recorder ,
315
343
signer_registration_verifier : self . signer_registration_verifier ,
@@ -478,6 +506,73 @@ mod tests {
478
506
. expect_err ( "synchronize_all_signers should fail" ) ;
479
507
}
480
508
509
+ #[ tokio:: test]
510
+ async fn synchronize_all_signers_fails_if_epoch_service_update_next_signers_fails ( ) {
511
+ let registration_epoch = Epoch ( 1 ) ;
512
+ let fixture = MithrilFixtureBuilder :: default ( )
513
+ . with_signers ( 5 )
514
+ . disable_signers_certification ( )
515
+ . build ( ) ;
516
+ let signers = fixture. signers ( ) ;
517
+ let stake_distribution = fixture. stake_distribution ( ) ;
518
+ let epoch_settings_message = FromEpochSettingsAdapter :: try_adapt ( EpochSettingsMessage {
519
+ epoch : registration_epoch,
520
+ next_signers : SignerMessagePart :: from_signers ( signers) ,
521
+ ..EpochSettingsMessage :: dummy ( )
522
+ } )
523
+ . unwrap ( ) ;
524
+
525
+ let signer_registration_slave = MithrilSignerRegistrationSlaveBuilder :: default ( )
526
+ . with_epoch_service ( {
527
+ let mut epoch_service = FakeEpochService :: without_data ( ) ;
528
+ epoch_service. toggle_errors ( false , false , false , true ) ;
529
+
530
+ epoch_service
531
+ } )
532
+ . with_signer_recorder ( {
533
+ let mut signer_recorder = MockSignerRecorder :: new ( ) ;
534
+ signer_recorder
535
+ . expect_record_signer_registration ( )
536
+ . returning ( |_| Ok ( ( ) ) )
537
+ . times ( 5 ) ;
538
+
539
+ Arc :: new ( signer_recorder)
540
+ } )
541
+ . with_signer_registration_verifier ( {
542
+ let mut signer_registration_verifier = MockSignerRegistrationVerifier :: new ( ) ;
543
+ signer_registration_verifier
544
+ . expect_verify ( )
545
+ . returning ( |signer, _| Ok ( SignerWithStake :: from_signer ( signer. to_owned ( ) , 123 ) ) )
546
+ . times ( 5 ) ;
547
+
548
+ Arc :: new ( signer_registration_verifier)
549
+ } )
550
+ . with_master_aggregator_client ( {
551
+ let mut aggregator_client = MockAggregatorClient :: new ( ) ;
552
+ aggregator_client
553
+ . expect_retrieve_epoch_settings ( )
554
+ . returning ( move || Ok ( Some ( epoch_settings_message. clone ( ) ) ) )
555
+ . times ( 1 ) ;
556
+
557
+ Arc :: new ( aggregator_client)
558
+ } )
559
+ . with_stake_store ( {
560
+ let mut stake_store = MockStakeStore :: new ( ) ;
561
+ stake_store
562
+ . expect_get_stakes ( )
563
+ . returning ( move |_epoch| Ok ( Some ( stake_distribution. clone ( ) ) ) )
564
+ . times ( 1 ) ;
565
+
566
+ Arc :: new ( stake_store)
567
+ } )
568
+ . build ( ) ;
569
+
570
+ signer_registration_slave
571
+ . synchronize_all_signers ( )
572
+ . await
573
+ . expect_err ( "synchronize_all_signers should fail" ) ;
574
+ }
575
+
481
576
#[ tokio:: test]
482
577
async fn synchronize_all_signers_fails_if_fetching_epoch_settings_fails ( ) {
483
578
let signer_registration_slave = MithrilSignerRegistrationSlaveBuilder :: default ( )
0 commit comments