@@ -108,43 +108,45 @@ mod tests {
108
108
109
109
use mithril_common:: entities:: { Epoch , PartyId , Signer , SignerWithStake } ;
110
110
use mithril_common:: test_utils:: fake_keys;
111
- use std:: collections:: { BTreeMap , BTreeSet , HashMap } ;
111
+ use std:: collections:: HashMap ;
112
112
use std:: sync:: Arc ;
113
113
114
114
use crate :: VerificationKeyStorer ;
115
115
116
116
use super :: * ;
117
117
118
+ /// Build simple fake signers with stakes.
119
+ /// It could be done by `fake_data::signers_with_stakes` which produce verification keys dynamically
120
+ /// but take longer.
121
+ fn build_fake_signers_with_stakes ( nb : u64 ) -> Vec < SignerWithStake > {
122
+ let verification_keys = fake_keys:: signer_verification_key ( ) ;
123
+ let nb_keys = verification_keys. len ( ) as u64 ;
124
+ ( 1 ..=nb)
125
+ . map ( |party_idx| SignerWithStake {
126
+ party_id : format ! ( "party_id:{party_idx}" ) ,
127
+ verification_key : verification_keys[ ( party_idx % nb_keys) as usize ]
128
+ . try_into ( )
129
+ . unwrap ( ) ,
130
+ verification_key_signature : None ,
131
+ operational_certificate : None ,
132
+ kes_period : None ,
133
+ stake : 10 ,
134
+ } )
135
+ . collect ( )
136
+ }
137
+
118
138
fn build_signers (
119
139
nb_epoch : u64 ,
120
140
signers_per_epoch : usize ,
121
- ) -> Vec < ( Epoch , HashMap < PartyId , SignerWithStake > ) > {
122
- let mut values = vec ! [ ] ;
123
-
124
- for epoch in 1 ..=nb_epoch {
125
- let mut signers: HashMap < PartyId , SignerWithStake > =
126
- HashMap :: with_capacity ( signers_per_epoch) ;
127
-
128
- for party_idx in 1 ..=signers_per_epoch {
129
- let party_id = format ! ( "party_id:e{epoch}:{party_idx}" ) ;
130
- signers. insert (
131
- party_id. clone ( ) ,
132
- SignerWithStake {
133
- party_id : party_id. clone ( ) ,
134
- verification_key : fake_keys:: signer_verification_key ( ) [ 0 ]
135
- . try_into ( )
136
- . unwrap ( ) ,
137
- verification_key_signature : None ,
138
- operational_certificate : None ,
139
- kes_period : None ,
140
- stake : 10 ,
141
- } ,
142
- ) ;
143
- }
144
- values. push ( ( Epoch ( epoch) , signers) ) ;
145
- }
146
-
147
- values
141
+ ) -> HashMap < Epoch , Vec < SignerWithStake > > {
142
+ ( 1 ..=nb_epoch)
143
+ . map ( |epoch| {
144
+ (
145
+ Epoch ( epoch) ,
146
+ build_fake_signers_with_stakes ( signers_per_epoch as u64 ) ,
147
+ )
148
+ } )
149
+ . collect ( )
148
150
}
149
151
150
152
fn insert_golden_signer_registration ( connection : & SqliteConnection ) {
@@ -181,13 +183,11 @@ mod tests {
181
183
}
182
184
183
185
pub fn init_signer_registration_store (
184
- initial_data : Vec < ( Epoch , HashMap < PartyId , SignerWithStake > ) > ,
186
+ initial_data : HashMap < Epoch , Vec < SignerWithStake > > ,
185
187
) -> Arc < SignerRegistrationStore > {
186
188
let connection = main_db_connection ( ) . unwrap ( ) ;
187
- let initial_data: Vec < ( Epoch , Vec < SignerWithStake > ) > = initial_data
188
- . into_iter ( )
189
- . map ( |( e, signers) | ( e, signers. into_values ( ) . collect :: < Vec < _ > > ( ) ) )
190
- . collect ( ) ;
189
+
190
+ let initial_data = initial_data. into_iter ( ) . collect ( ) ;
191
191
insert_signer_registrations ( & connection, initial_data) . unwrap ( ) ;
192
192
193
193
Arc :: new ( SignerRegistrationStore :: new ( Arc :: new ( connection) ) )
@@ -218,12 +218,15 @@ mod tests {
218
218
#[ tokio:: test]
219
219
pub async fn update_signer_in_store ( ) {
220
220
let signers = build_signers ( 1 , 1 ) ;
221
+ let signers_on_epoch = signers. get ( & Epoch ( 1 ) ) . unwrap ( ) . clone ( ) ;
222
+ let first_signer = signers_on_epoch. first ( ) . unwrap ( ) ;
223
+
221
224
let store = init_signer_registration_store ( signers) ;
222
225
let res = store
223
226
. save_verification_key (
224
227
Epoch ( 1 ) ,
225
228
SignerWithStake {
226
- party_id : " party_id:e1:1" . to_string ( ) ,
229
+ party_id : first_signer . party_id . clone ( ) ,
227
230
verification_key : fake_keys:: signer_verification_key ( ) [ 2 ] . try_into ( ) . unwrap ( ) ,
228
231
verification_key_signature : None ,
229
232
operational_certificate : None ,
@@ -236,7 +239,7 @@ mod tests {
236
239
237
240
assert_eq ! (
238
241
Some ( SignerWithStake {
239
- party_id: " party_id:e1:1" . to_string ( ) ,
242
+ party_id: first_signer . party_id. clone ( ) ,
240
243
verification_key: fake_keys:: signer_verification_key( ) [ 2 ] . try_into( ) . unwrap( ) ,
241
244
verification_key_signature: None ,
242
245
operational_certificate: None ,
@@ -270,18 +273,15 @@ mod tests {
270
273
let signers = build_signers ( 2 , 2 ) ;
271
274
let store = init_signer_registration_store ( signers. clone ( ) ) ;
272
275
273
- let expected_signers: Option < BTreeMap < PartyId , Signer > > = signers
274
- . into_iter ( )
275
- . filter ( |( e, _) | e == 1 )
276
- . map ( |( _, signers) | {
277
- BTreeMap :: from_iter ( signers. into_iter ( ) . map ( |( p, s) | ( p, s. into ( ) ) ) )
278
- } )
279
- . next ( ) ;
280
- let res = store
281
- . get_verification_keys ( Epoch ( 1 ) )
282
- . await
276
+ let epoch = Epoch ( 1 ) ;
277
+ let expected_signers = signers
278
+ . get ( & epoch)
283
279
. unwrap ( )
284
- . map ( |x| BTreeMap :: from_iter ( x. into_iter ( ) ) ) ;
280
+ . iter ( )
281
+ . map ( |s| ( s. party_id . clone ( ) , Signer :: from ( s. clone ( ) ) ) )
282
+ . collect :: < HashMap < PartyId , Signer > > ( ) ;
283
+
284
+ let res = store. get_verification_keys ( epoch) . await . unwrap ( ) . unwrap ( ) ;
285
285
286
286
assert_eq ! ( expected_signers, res) ;
287
287
}
@@ -291,16 +291,12 @@ mod tests {
291
291
let signers = build_signers ( 2 , 2 ) ;
292
292
let store = init_signer_registration_store ( signers. clone ( ) ) ;
293
293
294
- let expected_signers: Option < BTreeSet < SignerWithStake > > = signers
295
- . into_iter ( )
296
- . filter ( |( e, _) | e == 1 )
297
- . map ( |( _, signers) | BTreeSet :: from_iter ( signers. into_values ( ) ) )
298
- . next ( ) ;
299
- let res = store
300
- . get_signers ( Epoch ( 1 ) )
301
- . await
302
- . unwrap ( )
303
- . map ( |x| BTreeSet :: from_iter ( x. into_iter ( ) ) ) ;
294
+ let epoch = Epoch ( 1 ) ;
295
+ let mut expected_signers = signers. get ( & epoch) . unwrap ( ) . clone ( ) ;
296
+ expected_signers. sort_by ( |a, b| a. party_id . cmp ( & b. party_id ) ) ;
297
+
298
+ let mut res = store. get_signers ( epoch) . await . unwrap ( ) . unwrap ( ) ;
299
+ res. sort_by ( |a, b| a. party_id . cmp ( & b. party_id ) ) ;
304
300
305
301
assert_eq ! ( expected_signers, res) ;
306
302
}
@@ -334,8 +330,8 @@ mod tests {
334
330
until_epoch : Epoch ,
335
331
) -> Vec < Epoch > {
336
332
let mut epochs_in_database = vec ! [ ] ;
337
- let mut current_epoch = Epoch ( 1 ) ;
338
- while current_epoch <= until_epoch {
333
+ for epoch_number in 1 ..= ( * until_epoch ) {
334
+ let current_epoch = Epoch ( epoch_number ) ;
339
335
if store
340
336
. get_verification_keys ( current_epoch)
341
337
. await
@@ -344,8 +340,8 @@ mod tests {
344
340
{
345
341
epochs_in_database. push ( current_epoch) ;
346
342
}
347
- current_epoch += 1 ;
348
343
}
344
+
349
345
epochs_in_database
350
346
}
351
347
0 commit comments