Skip to content

Commit

Permalink
Use assert_last_certificate_eq in certificate_chain test
Browse files Browse the repository at this point in the history
  • Loading branch information
Alenar committed Jun 22, 2023
1 parent 83a28be commit d7bf13a
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 90 deletions.
182 changes: 94 additions & 88 deletions mithril-aggregator/tests/certificate_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ mod test_extensions;

use mithril_aggregator::{Configuration, VerificationKeyStorer};
use mithril_common::{
entities::{Beacon, ProtocolParameters, SignedEntityTypeDiscriminants},
entities::{
Beacon, Epoch, ProtocolParameters, SignedEntityType, SignedEntityTypeDiscriminants,
},
test_utils::MithrilFixtureBuilder,
};
use test_extensions::{utilities::get_test_dir, RuntimeTester};
use test_extensions::{utilities::get_test_dir, ExpectedCertificate, RuntimeTester};

#[tokio::test]
async fn certificate_chain() {
Expand All @@ -30,12 +32,19 @@ async fn certificate_chain() {
.build();
let signers = fixture.signers_fixture();
tester.init_state_from_fixture(&fixture).await.unwrap();
let mut signers_with_stake = fixture.signers_with_stake();
let mut current_epoch = observer.current_epoch().await;

comment!("Boostrap the genesis certificate, {:?}", current_epoch);
tester.register_genesis_certificate(&fixture).await.unwrap();

assert_last_certificate_eq!(
tester,
ExpectedCertificate::new_genesis(
Beacon::new("devnet".to_string(), 1, 1),
fixture.compute_and_encode_avk()
)
);

comment!("Increase immutable number");
tester.increase_immutable_number().await.unwrap();

Expand All @@ -54,9 +63,16 @@ async fn certificate_chain() {

comment!("The state machine should issue a certificate for the MithrilStakeDistribution");
cycle!(tester, "ready");
let (last_certificates, snapshots) =
tester.get_last_certificates_and_snapshots().await.unwrap();
assert_eq!((2, 0), (last_certificates.len(), snapshots.len()));
assert_last_certificate_eq!(
tester,
ExpectedCertificate::new(
Beacon::new("devnet".to_string(), 1, 2),
&fixture.signers_with_stake(),
fixture.compute_and_encode_avk(),
SignedEntityType::MithrilStakeDistribution(Epoch(1)),
ExpectedCertificate::genesis_identifier(&Beacon::new("devnet".to_string(), 1, 1)),
)
);

comment!("The state machine should get back to signing to sign CardanoImmutableFilesFull");
tester.increase_immutable_number().await.unwrap();
Expand All @@ -70,9 +86,16 @@ async fn certificate_chain() {
.unwrap();
comment!("The state machine should issue a certificate for the CardanoImmutableFilesFull");
cycle!(tester, "ready");
let (last_certificates, snapshots) =
tester.get_last_certificates_and_snapshots().await.unwrap();
assert_eq!((3, 1), (last_certificates.len(), snapshots.len()));
assert_last_certificate_eq!(
tester,
ExpectedCertificate::new(
Beacon::new("devnet".to_string(), 1, 3),
&fixture.signers_with_stake(),
fixture.compute_and_encode_avk(),
SignedEntityType::CardanoImmutableFilesFull(Beacon::new("devnet".to_string(), 1, 3)),
ExpectedCertificate::genesis_identifier(&Beacon::new("devnet".to_string(), 1, 1)),
)
);

comment!(
"Increase immutable number to do a second CardanoImmutableFilesFull certificate for this epoch, {:?}",
Expand All @@ -88,33 +111,30 @@ async fn certificate_chain() {
.await
.unwrap();
cycle!(tester, "ready");
let (last_certificates, snapshots) =
tester.get_last_certificates_and_snapshots().await.unwrap();
assert_eq!((4, 2), (last_certificates.len(), snapshots.len()));
assert_eq!(
(
last_certificates[0].beacon.immutable_file_number,
last_certificates[0].beacon.epoch,
),
(
last_certificates[1].beacon.immutable_file_number + 1,
last_certificates[1].beacon.epoch,
),
"Only the immutable_file_number should have changed"
);
assert_eq!(
&last_certificates[0].previous_hash, &last_certificates[3].hash,
"A new certificate on the same epoch should be linked to the first certificate of the current epoch"
assert_last_certificate_eq!(
tester,
ExpectedCertificate::new(
Beacon::new("devnet".to_string(), 1, 4),
&fixture.signers_with_stake(),
fixture.compute_and_encode_avk(),
SignedEntityType::CardanoImmutableFilesFull(Beacon::new("devnet".to_string(), 1, 4)),
ExpectedCertificate::genesis_identifier(&Beacon::new("devnet".to_string(), 1, 1)),
)
);

comment!("Change stake distribution");
for (i, signer) in signers_with_stake.iter_mut().enumerate() {
signer.stake += (i * 1000) as u64;
}
let new_signers = tester
.update_stake_distribution(signers_with_stake)
.await
.unwrap();
let next_fixture = {
let mut updated_signers = fixture.signers_with_stake();
for (i, signer) in updated_signers.iter_mut().enumerate() {
signer.stake += (i * 1000) as u64;
}

tester
.update_stake_distribution(updated_signers)
.await
.unwrap()
};
let next_signers = next_fixture.signers_fixture();

comment!(
"Increase epoch, triggering stake distribution update, Next epoch: {:?}",
Expand Down Expand Up @@ -145,7 +165,7 @@ async fn certificate_chain() {
comment!(
"Signers register & send signatures, the new certificate should be link to the first of the previous epoch"
);
tester.register_signers(&new_signers).await.unwrap();
tester.register_signers(&next_signers).await.unwrap();
tester
.send_single_signatures(
SignedEntityTypeDiscriminants::MithrilStakeDistribution,
Expand All @@ -154,28 +174,16 @@ async fn certificate_chain() {
.await
.unwrap();
cycle!(tester, "ready");
let (last_certificates, snapshots) =
tester.get_last_certificates_and_snapshots().await.unwrap();
assert_eq!((5, 2), (last_certificates.len(), snapshots.len()));
assert_eq!(
(
last_certificates[0].beacon.immutable_file_number,
last_certificates[0].beacon.epoch,
),
(
last_certificates[1].beacon.immutable_file_number,
last_certificates[1].beacon.epoch + 1,
),
"Only the epoch should have changed"
);
assert_eq!(
&last_certificates[0].previous_hash, &last_certificates[4].hash,
"The new epoch certificate should be linked to the first certificate of the previous epoch"
);
assert_eq!(
last_certificates[0].metadata.get_stake_distribution(),
last_certificates[2].metadata.get_stake_distribution(),
"The stake distribution update should only be taken into account at the next epoch",
// Note: the new stake distribution will only be taken into account in the next epoch
assert_last_certificate_eq!(
tester,
ExpectedCertificate::new(
Beacon::new("devnet".to_string(), 2, 4),
&fixture.signers_with_stake(),
fixture.compute_and_encode_avk(),
SignedEntityType::MithrilStakeDistribution(Epoch(2)),
ExpectedCertificate::genesis_identifier(&Beacon::new("devnet".to_string(), 1, 1)),
)
);

comment!(
Expand All @@ -198,6 +206,16 @@ async fn certificate_chain() {
.await
.unwrap();
cycle!(tester, "ready");
assert_last_certificate_eq!(
tester,
ExpectedCertificate::new(
Beacon::new("devnet".to_string(), 3, 5),
&fixture.signers_with_stake(),
fixture.compute_and_encode_avk(),
SignedEntityType::MithrilStakeDistribution(Epoch(3)),
ExpectedCertificate::identifier(&SignedEntityType::MithrilStakeDistribution(Epoch(2))),
)
);

comment!(
"Increase epoch & immutable, stake distribution updated at {} should be signed in the new certificate, Next epoch: {:?}",
Expand All @@ -214,35 +232,20 @@ async fn certificate_chain() {
tester
.send_single_signatures(
SignedEntityTypeDiscriminants::MithrilStakeDistribution,
&new_signers,
&next_signers,
)
.await
.unwrap();
cycle!(tester, "ready");

let (last_certificates, snapshots) =
tester.get_last_certificates_and_snapshots().await.unwrap();
assert_eq!((7, 2), (last_certificates.len(), snapshots.len()));
assert_eq!(
(
last_certificates[0].beacon.immutable_file_number,
last_certificates[0].beacon.epoch,
),
(
last_certificates[1].beacon.immutable_file_number + 1,
last_certificates[1].beacon.epoch + 1,
),
"Both the epoch & immutable file number should have change"
);

assert_eq!(
&last_certificates[0].previous_hash, &last_certificates[1].hash,
"The new epoch certificate should be linked to the first certificate of the previous epoch"
);
assert_ne!(
last_certificates[0].metadata.get_stake_distribution(),
last_certificates[2].metadata.get_stake_distribution(),
"The stake distribution update should have been applied for this epoch",
assert_last_certificate_eq!(
tester,
ExpectedCertificate::new(
Beacon::new("devnet".to_string(), 4, 6),
&next_fixture.signers_with_stake(),
next_fixture.compute_and_encode_avk(),
SignedEntityType::MithrilStakeDistribution(Epoch(4)),
ExpectedCertificate::identifier(&SignedEntityType::MithrilStakeDistribution(Epoch(3))),
)
);

comment!(
Expand All @@ -255,7 +258,7 @@ async fn certificate_chain() {
tester
.send_single_signatures(
SignedEntityTypeDiscriminants::CardanoImmutableFilesFull,
&new_signers,
&next_signers,
)
.await
.unwrap();
Expand All @@ -265,11 +268,14 @@ async fn certificate_chain() {
"A CardanoImmutableFilesFull, linked to the MithrilStakeDistribution of the current epoch, should have been created, {:?}",
current_epoch
);
let (last_certificates, snapshots) =
tester.get_last_certificates_and_snapshots().await.unwrap();
assert_eq!((8, 3), (last_certificates.len(), snapshots.len()));
assert_eq!(
&last_certificates[0].previous_hash, &last_certificates[1].hash,
"The CardanoImmutableFilesFull certificate should be linked to the MithrilStakeDistribution certificate of the current epoch"
assert_last_certificate_eq!(
tester,
ExpectedCertificate::new(
Beacon::new("devnet".to_string(), 4, 7),
&next_fixture.signers_with_stake(),
next_fixture.compute_and_encode_avk(),
SignedEntityType::CardanoImmutableFilesFull(Beacon::new("devnet".to_string(), 4, 7)),
ExpectedCertificate::identifier(&SignedEntityType::MithrilStakeDistribution(Epoch(4))),
)
);
}
4 changes: 2 additions & 2 deletions mithril-aggregator/tests/test_extensions/runtime_tester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ impl RuntimeTester {
pub async fn update_stake_distribution(
&mut self,
signers_with_stake: Vec<SignerWithStake>,
) -> Result<Vec<SignerFixture>, String> {
) -> Result<MithrilFixture, String> {
self.chain_observer
.set_signers(signers_with_stake.clone())
.await;
Expand Down Expand Up @@ -354,7 +354,7 @@ impl RuntimeTester {
))
.build();

Ok(fixture.signers_fixture())
Ok(fixture)
}

/// Update the digester result using the current beacon
Expand Down

0 comments on commit d7bf13a

Please sign in to comment.