Skip to content

Commit 429004c

Browse files
committed
test(common): refactor CertificateChainBuilder so it returns a type
Instead of a tuple, this allow to extend the returned value with usefull fstructeatures for test (i.e. immediate access to genesis certificate). Also update the builder so generated certificates have different, deterministic, signed entity type.
1 parent 546b1a1 commit 429004c

File tree

6 files changed

+225
-65
lines changed

6 files changed

+225
-65
lines changed

mithril-aggregator/src/database/query/certificate/get_certificate.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,26 +123,30 @@ mod tests {
123123
#[test]
124124
fn test_get_all_genesis_certificate_records() {
125125
// Two chains with different protocol parameters so generated certificates are different.
126-
let (first_certificates_chain, _) = CertificateChainBuilder::new()
126+
let first_certificates_chain = CertificateChainBuilder::new()
127127
.with_total_certificates(2)
128128
.with_protocol_parameters(ProtocolParameters {
129129
m: 90,
130130
k: 4,
131131
phi_f: 0.65,
132132
})
133133
.build();
134-
let first_chain_genesis: CertificateRecord =
135-
first_certificates_chain.last().unwrap().clone().into();
136-
let (second_certificates_chain, _) = CertificateChainBuilder::new()
134+
let first_chain_genesis: CertificateRecord = first_certificates_chain
135+
.genesis_certificate()
136+
.clone()
137+
.into();
138+
let second_certificates_chain = CertificateChainBuilder::new()
137139
.with_total_certificates(2)
138140
.with_protocol_parameters(ProtocolParameters {
139141
m: 100,
140142
k: 5,
141143
phi_f: 0.65,
142144
})
143145
.build();
144-
let second_chain_genesis: CertificateRecord =
145-
second_certificates_chain.last().unwrap().clone().into();
146+
let second_chain_genesis: CertificateRecord = second_certificates_chain
147+
.genesis_certificate()
148+
.clone()
149+
.into();
146150
assert_ne!(first_chain_genesis, second_chain_genesis);
147151

148152
let connection = main_db_connection().unwrap();
@@ -151,14 +155,14 @@ mod tests {
151155
.unwrap();
152156
assert_eq!(Vec::<CertificateRecord>::new(), certificate_records);
153157

154-
insert_certificate_records(&connection, first_certificates_chain);
158+
insert_certificate_records(&connection, first_certificates_chain.certificates_chained);
155159

156160
let certificate_records: Vec<CertificateRecord> = connection
157161
.fetch_collect(GetCertificateRecordQuery::all_genesis())
158162
.unwrap();
159163
assert_eq!(vec![first_chain_genesis.to_owned()], certificate_records);
160164

161-
insert_certificate_records(&connection, second_certificates_chain);
165+
insert_certificate_records(&connection, second_certificates_chain.certificates_chained);
162166

163167
let certificate_records: Vec<CertificateRecord> = connection
164168
.fetch_collect(GetCertificateRecordQuery::all_genesis())

mithril-client/src/certificate_client/verify.rs

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -262,16 +262,18 @@ mod tests {
262262

263263
#[tokio::test]
264264
async fn validating_chain_send_feedbacks() {
265-
let (chain, verifier) = CertificateChainBuilder::new()
265+
let chain = CertificateChainBuilder::new()
266266
.with_total_certificates(3)
267267
.with_certificates_per_epoch(1)
268268
.build();
269269
let last_certificate_hash = chain.first().unwrap().hash.clone();
270270

271271
let feedback_receiver = Arc::new(StackFeedbackReceiver::new());
272272
let certificate_client = CertificateClientTestBuilder::default()
273-
.config_aggregator_client_mock(|mock| mock.expect_certificate_chain(chain.clone()))
274-
.with_genesis_verification_key(verifier.to_verification_key())
273+
.config_aggregator_client_mock(|mock| {
274+
mock.expect_certificate_chain(chain.certificates_chained.clone())
275+
})
276+
.with_genesis_verification_key(chain.genesis_verifier.to_verification_key())
275277
.add_feedback_receiver(feedback_receiver.clone())
276278
.build();
277279

@@ -287,14 +289,12 @@ mod tests {
287289
let mut vec = vec![MithrilEvent::CertificateChainValidationStarted {
288290
certificate_chain_validation_id: id.to_string(),
289291
}];
290-
vec.extend(
291-
chain
292-
.into_iter()
293-
.map(|c| MithrilEvent::CertificateValidated {
294-
certificate_chain_validation_id: id.to_string(),
295-
certificate_hash: c.hash,
296-
}),
297-
);
292+
vec.extend(chain.certificates_chained.into_iter().map(|c| {
293+
MithrilEvent::CertificateValidated {
294+
certificate_chain_validation_id: id.to_string(),
295+
certificate_hash: c.hash,
296+
}
297+
}));
298298
vec.push(MithrilEvent::CertificateChainValidated {
299299
certificate_chain_validation_id: id.to_string(),
300300
});
@@ -306,15 +306,17 @@ mod tests {
306306

307307
#[tokio::test]
308308
async fn verify_chain_return_certificate_with_given_hash() {
309-
let (chain, verifier) = CertificateChainBuilder::new()
309+
let chain = CertificateChainBuilder::new()
310310
.with_total_certificates(3)
311311
.with_certificates_per_epoch(1)
312312
.build();
313313
let last_certificate_hash = chain.first().unwrap().hash.clone();
314314

315315
let certificate_client = CertificateClientTestBuilder::default()
316-
.config_aggregator_client_mock(|mock| mock.expect_certificate_chain(chain.clone()))
317-
.with_genesis_verification_key(verifier.to_verification_key())
316+
.config_aggregator_client_mock(|mock| {
317+
mock.expect_certificate_chain(chain.certificates_chained.clone())
318+
})
319+
.with_genesis_verification_key(chain.genesis_verifier.to_verification_key())
318320
.build();
319321

320322
let certificate = certificate_client
@@ -359,7 +361,7 @@ mod tests {
359361

360362
#[tokio::test]
361363
async fn genesis_certificates_verification_result_is_not_cached() {
362-
let (chain, verifier) = CertificateChainBuilder::new()
364+
let chain = CertificateChainBuilder::new()
363365
.with_total_certificates(1)
364366
.with_certificates_per_epoch(1)
365367
.build();
@@ -369,7 +371,7 @@ mod tests {
369371
let cache = Arc::new(MemoryCertificateVerifierCache::new(TimeDelta::hours(1)));
370372
let verifier = build_verifier_with_cache(
371373
|_mock| {},
372-
verifier.to_verification_key(),
374+
chain.genesis_verifier.to_verification_key(),
373375
cache.clone(),
374376
);
375377

@@ -394,7 +396,7 @@ mod tests {
394396

395397
#[tokio::test]
396398
async fn non_genesis_certificates_verification_result_is_cached() {
397-
let (chain, verifier) = CertificateChainBuilder::new()
399+
let chain = CertificateChainBuilder::new()
398400
.with_total_certificates(2)
399401
.with_certificates_per_epoch(1)
400402
.build();
@@ -405,7 +407,7 @@ mod tests {
405407
let cache = Arc::new(MemoryCertificateVerifierCache::new(TimeDelta::hours(1)));
406408
let verifier = build_verifier_with_cache(
407409
|mock| mock.expect_certificate_chain(vec![genesis_certificate.clone()]),
408-
verifier.to_verification_key(),
410+
chain.genesis_verifier.to_verification_key(),
409411
cache.clone(),
410412
);
411413

@@ -428,7 +430,7 @@ mod tests {
428430
#[tokio::test]
429431
async fn verification_of_first_certificate_of_a_chain_should_always_fetch_it_from_network()
430432
{
431-
let (chain, verifier) = CertificateChainBuilder::new()
433+
let chain = CertificateChainBuilder::new()
432434
.with_total_certificates(2)
433435
.with_certificates_per_epoch(1)
434436
.build();
@@ -440,10 +442,10 @@ mod tests {
440442
);
441443
let certificate_client = CertificateClientTestBuilder::default()
442444
.config_aggregator_client_mock(|mock| {
443-
// Expect to first certificate to be fetched from the network
444-
mock.expect_certificate_chain(chain.clone());
445+
// Expect to fetch the first certificate from the network
446+
mock.expect_certificate_chain(chain.certificates_chained.clone());
445447
})
446-
.with_genesis_verification_key(verifier.to_verification_key())
448+
.with_genesis_verification_key(chain.genesis_verifier.to_verification_key())
447449
.with_verifier_cache(cache.clone())
448450
.build();
449451

@@ -466,7 +468,7 @@ mod tests {
466468
// | n°3 | 2 | n°2 | Yes | No |
467469
// | n°2 | 2 | n°1 | Yes | No |
468470
// | n°1 | 1 | None (genesis) | Yes | Yes |
469-
let (chain, verifier) = CertificateChainBuilder::new()
471+
let chain = CertificateChainBuilder::new()
470472
.with_total_certificates(6)
471473
.with_certificates_per_epoch(3)
472474
.with_certificate_chaining_method(CertificateChainingMethod::Sequential)
@@ -503,7 +505,7 @@ mod tests {
503505
.config_aggregator_client_mock(|mock| {
504506
mock.expect_certificate_chain(certificates_that_must_be_fully_verified);
505507
})
506-
.with_genesis_verification_key(verifier.to_verification_key())
508+
.with_genesis_verification_key(chain.genesis_verifier.to_verification_key())
507509
.with_verifier_cache(cache)
508510
.build();
509511

@@ -515,7 +517,7 @@ mod tests {
515517

516518
#[tokio::test]
517519
async fn verify_chain_return_certificate_with_cache() {
518-
let (chain, verifier) = CertificateChainBuilder::new()
520+
let chain = CertificateChainBuilder::new()
519521
.with_total_certificates(5)
520522
.with_certificates_per_epoch(1)
521523
.build();
@@ -531,7 +533,7 @@ mod tests {
531533
[chain[0..3].to_vec(), vec![chain.last().unwrap().clone()]].concat(),
532534
)
533535
})
534-
.with_genesis_verification_key(verifier.to_verification_key())
536+
.with_genesis_verification_key(chain.genesis_verifier.to_verification_key())
535537
.with_verifier_cache(Arc::new(cache))
536538
.build();
537539

mithril-common/src/certificate_chain/certificate_verifier.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,7 @@ mod tests {
813813
}
814814

815815
let (total_certificates, certificates_per_epoch) = (5, 1);
816-
let (fake_certificates, _) = CertificateChainBuilder::new()
816+
let fake_certificates = CertificateChainBuilder::new()
817817
.with_total_certificates(total_certificates)
818818
.with_certificates_per_epoch(certificates_per_epoch)
819819
.with_standard_certificate_processor(&|certificate, context| {
@@ -1125,7 +1125,7 @@ mod tests {
11251125
}
11261126

11271127
let (total_certificates, certificates_per_epoch) = (7, 2);
1128-
let (fake_certificates, genesis_verifier) = CertificateChainBuilder::new()
1128+
let fake_certificates = CertificateChainBuilder::new()
11291129
.with_total_certificates(total_certificates)
11301130
.with_certificates_per_epoch(certificates_per_epoch)
11311131
.with_standard_certificate_processor(&|certificate, context| {
@@ -1146,7 +1146,7 @@ mod tests {
11461146
let error = verifier
11471147
.verify_certificate(
11481148
&certificate_to_verify,
1149-
&genesis_verifier.to_verification_key(),
1149+
&fake_certificates.genesis_verifier.to_verification_key(),
11501150
)
11511151
.await
11521152
.expect_err("verify_certificate_chain should fail");

mithril-common/src/crypto_helper/tests_setup.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,14 +192,19 @@ pub fn setup_signers_from_stake_distribution(
192192
}
193193

194194
/// Instantiate a certificate chain, use this for tests only.
195+
/// Todo: update to CertificateChainFixture
195196
pub fn setup_certificate_chain(
196197
total_certificates: u64,
197198
certificates_per_epoch: u64,
198199
) -> (Vec<Certificate>, ProtocolGenesisVerifier) {
199-
let certificate_chain_builder = CertificateChainBuilder::new()
200+
let certificate_chain_fixture = CertificateChainBuilder::new()
200201
.with_total_certificates(total_certificates)
201202
.with_certificates_per_epoch(certificates_per_epoch)
202-
.with_protocol_parameters(setup_protocol_parameters());
203+
.with_protocol_parameters(setup_protocol_parameters())
204+
.build();
203205

204-
certificate_chain_builder.build()
206+
(
207+
certificate_chain_fixture.certificates_chained,
208+
certificate_chain_fixture.genesis_verifier,
209+
)
205210
}

0 commit comments

Comments
 (0)