1
1
//! Test data builders for Mithril STM types, for testing purpose.
2
- use super :: cardano:: ColdKeyGenerator ;
3
2
use super :: { genesis:: * , key_encode_hex, types:: * , OpCert , SerDeShelleyFileFormat } ;
4
- use crate :: certificate_chain:: CertificateGenesisProducer ;
5
3
use crate :: {
4
+ certificate_chain:: CertificateGenesisProducer ,
6
5
entities:: { Certificate , Epoch , ProtocolMessage , ProtocolMessagePartKey , SignerWithStake } ,
7
6
fake_data,
7
+ test_utils:: { MithrilFixtureBuilder , SignerFixture } ,
8
8
} ;
9
9
10
- use kes_summed_ed25519:: kes:: Sum6Kes ;
11
- use kes_summed_ed25519:: traits:: KesSk ;
12
10
use rand_chacha:: ChaCha20Rng ;
13
- use rand_core:: { RngCore , SeedableRng } ;
11
+ use rand_core:: SeedableRng ;
14
12
use std:: { cmp:: min, fs, sync:: Arc } ;
15
13
16
14
use std:: { collections:: HashMap , path:: PathBuf } ;
@@ -52,60 +50,11 @@ pub fn setup_protocol_parameters() -> ProtocolParameters {
52
50
}
53
51
}
54
52
55
- /// Instantiate a list of protocol signers, use this for tests only.
56
- pub fn setup_signers (
57
- total : u64 ,
58
- protocol_parameters : & ProtocolParameters ,
59
- ) -> Vec < ( SignerWithStake , ProtocolSigner , ProtocolInitializer ) > {
60
- let mut stake_rng = ChaCha20Rng :: from_seed ( [ 0u8 ; 32 ] ) ;
61
- let mut kes_keys_seed = [ 0u8 ; 32 ] ;
62
-
63
- let stake_distribution = ( 0 ..total)
64
- . into_iter ( )
65
- . map ( |party_idx| {
66
- let party_id = if party_idx % 2 == 0
67
- || cfg ! ( not( feature = "allow_skip_signer_certification" ) )
68
- {
69
- // 50% of signers with key certification if allow unverified signer registration
70
- // Or 100% of signers otherwise
71
- let keypair = ColdKeyGenerator :: create_deterministic_keypair ( [ party_idx as u8 ; 32 ] ) ;
72
- let ( kes_secret_key, kes_verification_key) = Sum6Kes :: keygen ( & mut kes_keys_seed) ;
73
- let operational_certificate = OpCert :: new ( kes_verification_key, 0 , 0 , keypair) ;
74
- let party_id = operational_certificate
75
- . compute_protocol_party_id ( )
76
- . expect ( "compute protocol party id should not fail" ) ;
77
- let temp_dir = setup_temp_directory_for_signer ( & party_id, true )
78
- . expect ( "setup temp directory should return a value" ) ;
79
- if !temp_dir. join ( "kes.sk" ) . exists ( ) {
80
- kes_secret_key
81
- . to_file ( temp_dir. join ( "kes.sk" ) )
82
- . expect ( "KES secret key file export should not fail" ) ;
83
- }
84
- if !temp_dir. join ( "opcert.cert" ) . exists ( ) {
85
- operational_certificate
86
- . to_file ( temp_dir. join ( "opcert.cert" ) )
87
- . expect ( "operational certificate file export should not fail" ) ;
88
- }
89
- party_id
90
- } else {
91
- // 50% of signers without key certification (legacy) if allow unverified signer registration
92
- // Or 0% of signers otherwise
93
- // TODO: Should be removed once the signer certification is fully deployed
94
- format ! ( "{:<032}" , party_idx)
95
- } ;
96
-
97
- let stake = 1 + stake_rng. next_u64 ( ) % 999 ;
98
- ( party_id, stake)
99
- } )
100
- . collect :: < Vec < _ > > ( ) ;
101
- setup_signers_from_stake_distribution ( & stake_distribution, protocol_parameters)
102
- }
103
-
104
53
/// Instantiate a list of protocol signers based on the given [ProtocolStakeDistribution] and [ProtocolParameters], use this for tests only.
105
54
pub fn setup_signers_from_stake_distribution (
106
55
stake_distribution : & ProtocolStakeDistribution ,
107
56
protocol_parameters : & ProtocolParameters ,
108
- ) -> Vec < ( SignerWithStake , ProtocolSigner , ProtocolInitializer ) > {
57
+ ) -> Vec < SignerFixture > {
109
58
let signers = stake_distribution
110
59
. iter ( )
111
60
. map ( |( party_id, stake) | {
@@ -163,34 +112,37 @@ pub fn setup_signers_from_stake_distribution(
163
112
. expect ( "operational certificate decoding should not fail" )
164
113
} ) ;
165
114
let kes_period = 0 ;
166
- (
167
- SignerWithStake :: new (
168
- party_id,
169
- key_encode_hex ( protocol_initializer. verification_key ( ) )
170
- . expect ( "key_encode_hex of verification_key should not fail" ) ,
171
- protocol_initializer
172
- . verification_key_signature ( )
173
- . as_ref ( )
174
- . map ( |verification_key_signature| {
175
- key_encode_hex ( verification_key_signature) . expect (
176
- "key_encode_hex of verification_key_signature should not fail" ,
177
- )
178
- } ) ,
179
- operational_certificate
180
- . as_ref ( )
181
- . map ( |operational_certificate| {
182
- key_encode_hex ( operational_certificate)
183
- . expect ( "key_encode_hex of operational_certificate should not fail" )
184
- } ) ,
185
- operational_certificate. as_ref ( ) . map ( |_| kes_period) ,
186
- stake,
187
- ) ,
115
+
116
+ let signer_with_stake = SignerWithStake :: new (
117
+ party_id,
118
+ key_encode_hex ( protocol_initializer. verification_key ( ) )
119
+ . expect ( "key_encode_hex of verification_key should not fail" ) ,
188
120
protocol_initializer
189
- . clone ( )
190
- . new_signer ( closed_key_registration. clone ( ) )
191
- . expect ( "creating a new protocol signer should not fail" ) ,
121
+ . verification_key_signature ( )
122
+ . as_ref ( )
123
+ . map ( |verification_key_signature| {
124
+ key_encode_hex ( verification_key_signature)
125
+ . expect ( "key_encode_hex of verification_key_signature should not fail" )
126
+ } ) ,
127
+ operational_certificate
128
+ . as_ref ( )
129
+ . map ( |operational_certificate| {
130
+ key_encode_hex ( operational_certificate)
131
+ . expect ( "key_encode_hex of operational_certificate should not fail" )
132
+ } ) ,
133
+ operational_certificate. as_ref ( ) . map ( |_| kes_period) ,
134
+ stake,
135
+ ) ;
136
+ let protocol_signer = protocol_initializer
137
+ . clone ( )
138
+ . new_signer ( closed_key_registration. clone ( ) )
139
+ . expect ( "creating a new protocol signer should not fail" ) ;
140
+
141
+ SignerFixture {
142
+ signer_with_stake,
143
+ protocol_signer,
192
144
protocol_initializer,
193
- )
145
+ }
194
146
} )
195
147
. collect :: < _ > ( )
196
148
}
@@ -219,21 +171,24 @@ pub fn setup_certificate_chain(
219
171
_ => Epoch ( i / certificates_per_epoch + 1 ) ,
220
172
} )
221
173
. collect :: < Vec < _ > > ( ) ;
222
- let signers_by_epoch = epochs
174
+ let fixture_per_epoch = epochs
223
175
. clone ( )
224
176
. into_iter ( )
225
177
. map ( |epoch| {
226
178
(
227
179
epoch,
228
- setup_signers ( min ( 2 + epoch. 0 , 5 ) , & protocol_parameters) ,
180
+ MithrilFixtureBuilder :: default ( )
181
+ . with_protocol_parameters ( protocol_parameters. into ( ) )
182
+ . with_signers ( min ( 2 + epoch. 0 as usize , 5 ) )
183
+ . build ( ) ,
229
184
)
230
185
} )
231
186
. collect :: < HashMap < _ , _ > > ( ) ;
232
- let clerk_for_signers = |signers : & [ ( _ , ProtocolSigner , _ ) ] | -> ProtocolClerk {
233
- let first_signer = & signers. first ( ) . unwrap ( ) . 1 ;
187
+ let clerk_for_signers = |signers : & [ SignerFixture ] | -> ProtocolClerk {
188
+ let first_signer = & signers[ 0 ] . protocol_signer ;
234
189
ProtocolClerk :: from_signer ( first_signer)
235
190
} ;
236
- let avk_for_signers = |signers : & [ ( _ , ProtocolSigner , _ ) ] | -> ProtocolAggregateVerificationKey {
191
+ let avk_for_signers = |signers : & [ SignerFixture ] | -> ProtocolAggregateVerificationKey {
237
192
let clerk = clerk_for_signers ( signers) ;
238
193
clerk. compute_avk ( )
239
194
} ;
@@ -247,10 +202,10 @@ pub fn setup_certificate_chain(
247
202
let immutable_file_number = i as u64 * 10 ;
248
203
let digest = format ! ( "digest{}" , i) ;
249
204
let certificate_hash = format ! ( "certificate_hash-{}" , i) ;
250
- let signers = signers_by_epoch . get ( & epoch) . unwrap ( ) ;
251
- let next_signers = signers_by_epoch . get ( & ( epoch + 1 ) ) . unwrap ( ) ;
252
- let avk = avk_for_signers ( signers ) ;
253
- let next_avk = avk_for_signers ( next_signers ) ;
205
+ let fixture = fixture_per_epoch . get ( & epoch) . unwrap ( ) ;
206
+ let next_fixture = fixture_per_epoch . get ( & ( epoch + 1 ) ) . unwrap ( ) ;
207
+ let avk = avk_for_signers ( & fixture . signers_fixture ( ) ) ;
208
+ let next_avk = avk_for_signers ( & next_fixture . signers_fixture ( ) ) ;
254
209
let mut fake_certificate = fake_data:: certificate ( certificate_hash) ;
255
210
fake_certificate. beacon . epoch = epoch;
256
211
fake_certificate. beacon . immutable_file_number = immutable_file_number;
@@ -281,17 +236,17 @@ pub fn setup_certificate_chain(
281
236
. unwrap ( )
282
237
}
283
238
_ => {
284
- fake_certificate. metadata . signers =
285
- signers . iter ( ) . map ( |s| s . 0 . to_owned ( ) ) . collect ( ) ;
286
- let mut single_signatures = Vec :: new ( ) ;
287
- signers . iter ( ) . for_each ( | ( _ , protocol_signer , _ ) | {
288
- if let Some ( signature ) =
289
- protocol_signer . sign ( fake_certificate . signed_message . as_bytes ( ) )
290
- {
291
- single_signatures . push ( signature ) ;
292
- }
293
- } ) ;
294
- let clerk = clerk_for_signers ( signers ) ;
239
+ fake_certificate. metadata . signers = fixture . signers_with_stake ( ) ;
240
+
241
+ let single_signatures = fixture
242
+ . signers_fixture ( )
243
+ . iter ( )
244
+ . filter_map ( |s| {
245
+ s . protocol_signer
246
+ . sign ( fake_certificate . signed_message . as_bytes ( ) )
247
+ } )
248
+ . collect :: < Vec < _ > > ( ) ;
249
+ let clerk = clerk_for_signers ( & fixture . signers_fixture ( ) ) ;
295
250
let multi_signature = clerk
296
251
. aggregate (
297
252
& single_signatures,
0 commit comments