|
| 1 | +mod test_extensions; |
| 2 | + |
| 3 | +use mithril_aggregator::Configuration; |
| 4 | +use mithril_common::{ |
| 5 | + entities::{ |
| 6 | + BlockNumber, CardanoDbBeacon, ChainPoint, Epoch, ProtocolParameters, SignedEntityType, |
| 7 | + SignedEntityTypeDiscriminants, SlotNumber, StakeDistribution, StakeDistributionParty, |
| 8 | + TimePoint, |
| 9 | + }, |
| 10 | + test_utils::MithrilFixtureBuilder, |
| 11 | +}; |
| 12 | +use test_extensions::{utilities::get_test_dir, ExpectedCertificate, RuntimeTester}; |
| 13 | + |
| 14 | +#[tokio::test] |
| 15 | +async fn cardano_stake_distribution_verify_stakes() { |
| 16 | + let protocol_parameters = ProtocolParameters { |
| 17 | + k: 5, |
| 18 | + m: 150, |
| 19 | + phi_f: 0.95, |
| 20 | + }; |
| 21 | + let configuration = Configuration { |
| 22 | + protocol_parameters: protocol_parameters.clone(), |
| 23 | + signed_entity_types: Some( |
| 24 | + SignedEntityTypeDiscriminants::CardanoStakeDistribution.to_string(), |
| 25 | + ), |
| 26 | + data_stores_directory: get_test_dir("cardano_stake_distribution_verify_stakes"), |
| 27 | + ..Configuration::new_sample() |
| 28 | + }; |
| 29 | + let mut tester = RuntimeTester::build( |
| 30 | + TimePoint::new( |
| 31 | + 2, |
| 32 | + 1, |
| 33 | + ChainPoint::new(SlotNumber(10), BlockNumber(1), "block_hash-1"), |
| 34 | + ), |
| 35 | + configuration, |
| 36 | + ) |
| 37 | + .await; |
| 38 | + |
| 39 | + comment!("create signers & declare the initial stake distribution"); |
| 40 | + let fixture = MithrilFixtureBuilder::default() |
| 41 | + .with_signers(5) |
| 42 | + .with_protocol_parameters(protocol_parameters.clone()) |
| 43 | + .build(); |
| 44 | + let signers = &fixture.signers_fixture(); |
| 45 | + let initial_stake_distribution = fixture.stake_distribution(); |
| 46 | + |
| 47 | + tester.init_state_from_fixture(&fixture).await.unwrap(); |
| 48 | + |
| 49 | + comment!("Bootstrap the genesis certificate"); |
| 50 | + tester.register_genesis_certificate(&fixture).await.unwrap(); |
| 51 | + |
| 52 | + assert_last_certificate_eq!( |
| 53 | + tester, |
| 54 | + ExpectedCertificate::new_genesis( |
| 55 | + CardanoDbBeacon::new("devnet".to_string(), 2, 1), |
| 56 | + fixture.compute_and_encode_avk() |
| 57 | + ) |
| 58 | + ); |
| 59 | + |
| 60 | + comment!("start the runtime state machine"); |
| 61 | + cycle!(tester, "ready"); |
| 62 | + |
| 63 | + tester.register_signers(signers).await.unwrap(); |
| 64 | + |
| 65 | + tester.increase_epoch().await.unwrap(); |
| 66 | + |
| 67 | + comment!("Change stake distribution"); |
| 68 | + let mut signers_with_updated_stake_distribution = fixture.signers_with_stake(); |
| 69 | + signers_with_updated_stake_distribution[0].stake = 9999; |
| 70 | + tester |
| 71 | + .chain_observer |
| 72 | + .set_signers(signers_with_updated_stake_distribution.clone()) |
| 73 | + .await; |
| 74 | + let updated_stake_distribution: StakeDistribution = signers_with_updated_stake_distribution |
| 75 | + .iter() |
| 76 | + .map(|s| (s.party_id.clone(), s.stake)) |
| 77 | + .collect(); |
| 78 | + |
| 79 | + cycle!(tester, "idle"); |
| 80 | + cycle!(tester, "ready"); |
| 81 | + cycle!(tester, "signing"); |
| 82 | + tester.register_signers(signers).await.unwrap(); |
| 83 | + tester |
| 84 | + .send_single_signatures( |
| 85 | + SignedEntityTypeDiscriminants::MithrilStakeDistribution, |
| 86 | + signers, |
| 87 | + ) |
| 88 | + .await |
| 89 | + .unwrap(); |
| 90 | + |
| 91 | + comment!("The state machine should issue a certificate for the MithrilStakeDistribution"); |
| 92 | + cycle!(tester, "ready"); |
| 93 | + assert_last_certificate_eq!( |
| 94 | + tester, |
| 95 | + ExpectedCertificate::new( |
| 96 | + CardanoDbBeacon::new("devnet".to_string(), 3, 1), |
| 97 | + StakeDistributionParty::from_signers(fixture.signers_with_stake()).as_slice(), |
| 98 | + fixture.compute_and_encode_avk(), |
| 99 | + SignedEntityType::MithrilStakeDistribution(Epoch(3)), |
| 100 | + ExpectedCertificate::genesis_identifier(&CardanoDbBeacon::new( |
| 101 | + "devnet".to_string(), |
| 102 | + 2, |
| 103 | + 1 |
| 104 | + )), |
| 105 | + ) |
| 106 | + ); |
| 107 | + |
| 108 | + cycle!(tester, "signing"); |
| 109 | + tester |
| 110 | + .send_single_signatures( |
| 111 | + SignedEntityTypeDiscriminants::CardanoStakeDistribution, |
| 112 | + signers, |
| 113 | + ) |
| 114 | + .await |
| 115 | + .unwrap(); |
| 116 | + |
| 117 | + comment!("The state machine should issue a certificate for the CardanoStakeDistribution"); |
| 118 | + cycle!(tester, "ready"); |
| 119 | + assert_last_certificate_eq!( |
| 120 | + tester, |
| 121 | + ExpectedCertificate::new( |
| 122 | + CardanoDbBeacon::new("devnet".to_string(), 3, 1), |
| 123 | + StakeDistributionParty::from_signers(fixture.signers_with_stake()).as_slice(), |
| 124 | + fixture.compute_and_encode_avk(), |
| 125 | + SignedEntityType::CardanoStakeDistribution(Epoch(2)), |
| 126 | + ExpectedCertificate::identifier(&SignedEntityType::MithrilStakeDistribution(Epoch(3))), |
| 127 | + ) |
| 128 | + ); |
| 129 | + |
| 130 | + let message = tester |
| 131 | + .dependencies |
| 132 | + .message_service |
| 133 | + .get_cardano_stake_distribution_message_by_epoch(Epoch(2)) |
| 134 | + .await |
| 135 | + .unwrap() |
| 136 | + .unwrap(); |
| 137 | + |
| 138 | + assert_eq!(initial_stake_distribution, message.stake_distribution); |
| 139 | + |
| 140 | + comment!("Increase epoch to verify the certification of the CardanoStakeDistribution with the updated stake distribution"); |
| 141 | + tester.increase_epoch().await.unwrap(); |
| 142 | + cycle!(tester, "idle"); |
| 143 | + cycle!(tester, "ready"); |
| 144 | + |
| 145 | + cycle!(tester, "signing"); |
| 146 | + tester |
| 147 | + .send_single_signatures( |
| 148 | + SignedEntityTypeDiscriminants::MithrilStakeDistribution, |
| 149 | + signers, |
| 150 | + ) |
| 151 | + .await |
| 152 | + .unwrap(); |
| 153 | + |
| 154 | + comment!("The state machine should issue a certificate for the MithrilStakeDistribution"); |
| 155 | + cycle!(tester, "ready"); |
| 156 | + assert_last_certificate_eq!( |
| 157 | + tester, |
| 158 | + ExpectedCertificate::new( |
| 159 | + CardanoDbBeacon::new("devnet".to_string(), 4, 1), |
| 160 | + StakeDistributionParty::from_signers(fixture.signers_with_stake()).as_slice(), |
| 161 | + fixture.compute_and_encode_avk(), |
| 162 | + SignedEntityType::MithrilStakeDistribution(Epoch(4)), |
| 163 | + ExpectedCertificate::identifier(&SignedEntityType::MithrilStakeDistribution(Epoch(3))), |
| 164 | + ) |
| 165 | + ); |
| 166 | + |
| 167 | + cycle!(tester, "signing"); |
| 168 | + tester |
| 169 | + .send_single_signatures( |
| 170 | + SignedEntityTypeDiscriminants::CardanoStakeDistribution, |
| 171 | + signers, |
| 172 | + ) |
| 173 | + .await |
| 174 | + .unwrap(); |
| 175 | + |
| 176 | + comment!("The state machine should issue a certificate for the CardanoStakeDistribution"); |
| 177 | + cycle!(tester, "ready"); |
| 178 | + assert_last_certificate_eq!( |
| 179 | + tester, |
| 180 | + ExpectedCertificate::new( |
| 181 | + CardanoDbBeacon::new("devnet".to_string(), 4, 1), |
| 182 | + StakeDistributionParty::from_signers(fixture.signers_with_stake()).as_slice(), |
| 183 | + fixture.compute_and_encode_avk(), |
| 184 | + SignedEntityType::CardanoStakeDistribution(Epoch(3)), |
| 185 | + ExpectedCertificate::identifier(&SignedEntityType::MithrilStakeDistribution(Epoch(4))), |
| 186 | + ) |
| 187 | + ); |
| 188 | + |
| 189 | + let message = tester |
| 190 | + .dependencies |
| 191 | + .message_service |
| 192 | + .get_cardano_stake_distribution_message_by_epoch(Epoch(3)) |
| 193 | + .await |
| 194 | + .unwrap() |
| 195 | + .unwrap(); |
| 196 | + |
| 197 | + assert_eq!(updated_stake_distribution, message.stake_distribution); |
| 198 | +} |
0 commit comments