Skip to content

Commit 5c13964

Browse files
committed
Add missing certificate hash on Mithril Stake Distribution
1 parent 6eea402 commit 5c13964

File tree

11 files changed

+80
-17
lines changed

11 files changed

+80
-17
lines changed

mithril-aggregator/src/artifact_builder/artifact_builder_service.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,11 @@ mod tests {
121121
async fn build_mithril_stake_distribution_artifact_when_given_mithril_stake_distribution_entity_type(
122122
) {
123123
let signers_with_stake = fake_data::signers_with_stakes(5);
124-
let mithril_stake_distribution_expected =
125-
MithrilStakeDistribution::new(Epoch(1), signers_with_stake);
124+
let mithril_stake_distribution_expected = MithrilStakeDistribution::new(
125+
Epoch(1),
126+
signers_with_stake,
127+
"certificate-123".to_string(),
128+
);
126129
let mithril_stake_distribution_clone = mithril_stake_distribution_expected.clone();
127130

128131
let mock_signed_entity_storer = MockSignedEntityStorer::new();

mithril-aggregator/src/artifact_builder/cardano_immutable_files_full.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ mod tests {
157157
#[tokio::test]
158158
async fn should_compute_valid_artifact() {
159159
let beacon = fake_data::beacon();
160-
let certificate = fake_data::certificate("cert-123".to_string());
160+
let certificate = fake_data::certificate("certificate-123".to_string());
161161
let snapshot_digest = certificate
162162
.protocol_message
163163
.get_message_part(&ProtocolMessagePartKey::SnapshotDigest)

mithril-aggregator/src/artifact_builder/mithril_stake_distribution.rs

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@ impl ArtifactBuilder<Epoch, MithrilStakeDistribution> for MithrilStakeDistributi
2626
async fn compute_artifact(
2727
&self,
2828
beacon: Epoch,
29-
_certificate: &Certificate,
29+
certificate: &Certificate,
3030
) -> StdResult<MithrilStakeDistribution> {
3131
let multi_signer = self.multi_signer.read().await;
3232
Ok(MithrilStakeDistribution::new(
3333
beacon,
3434
multi_signer.get_next_signers_with_stake().await?,
35+
certificate.hash.to_owned(),
3536
))
3637
}
3738
}
@@ -48,7 +49,7 @@ mod tests {
4849
async fn should_compute_valid_artifact() {
4950
let signers_with_stake = fake_data::signers_with_stakes(5);
5051
let signers_with_stake_clone = signers_with_stake.clone();
51-
let certificate = fake_data::certificate("cert-123".to_string());
52+
let certificate = fake_data::certificate("certificate-123".to_string());
5253
let mut mock_multi_signer = MockMultiSigner::new();
5354
mock_multi_signer
5455
.expect_get_next_signers_with_stake()
@@ -59,7 +60,11 @@ mod tests {
5960
.compute_artifact(Epoch(1), &certificate)
6061
.await
6162
.unwrap();
62-
let artifact_expected = MithrilStakeDistribution::new(Epoch(1), signers_with_stake);
63+
let artifact_expected = MithrilStakeDistribution::new(
64+
Epoch(1),
65+
signers_with_stake,
66+
"certificate-123".to_string(),
67+
);
6368
assert_eq!(artifact_expected, artifact);
6469
}
6570

@@ -68,18 +73,33 @@ mod tests {
6873
let signers_with_stake = fake_data::signers_with_stakes(5);
6974

7075
assert_eq!(
71-
MithrilStakeDistribution::new(Epoch(1), signers_with_stake.clone()),
72-
MithrilStakeDistribution::new(Epoch(1), signers_with_stake.into_iter().rev().collect())
76+
MithrilStakeDistribution::new(
77+
Epoch(1),
78+
signers_with_stake.clone(),
79+
"certificate-123".to_string()
80+
),
81+
MithrilStakeDistribution::new(
82+
Epoch(1),
83+
signers_with_stake.into_iter().rev().collect(),
84+
"certificate-123".to_string()
85+
)
7386
);
7487
}
7588

7689
#[test]
7790
fn hash_value_doesnt_change_if_signers_order_change() {
7891
let signers_with_stake = fake_data::signers_with_stakes(5);
7992

80-
let sd = MithrilStakeDistribution::new(Epoch(1), signers_with_stake.clone());
81-
let sd2 =
82-
MithrilStakeDistribution::new(Epoch(1), signers_with_stake.into_iter().rev().collect());
93+
let sd = MithrilStakeDistribution::new(
94+
Epoch(1),
95+
signers_with_stake.clone(),
96+
"certificate-123".to_string(),
97+
);
98+
let sd2 = MithrilStakeDistribution::new(
99+
Epoch(1),
100+
signers_with_stake.into_iter().rev().collect(),
101+
"certificate-123".to_string(),
102+
);
83103

84104
assert_eq!(sd.hash, sd2.hash);
85105
}

mithril-aggregator/src/database/provider/certificate.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -743,11 +743,13 @@ mod tests {
743743
fn get_certificate_record_by_certificate_id() {
744744
let connection = Connection::open(":memory:").unwrap();
745745
let provider = CertificateRecordProvider::new(&connection);
746-
let condition = provider.condition_by_certificate_id("cert-123").unwrap();
746+
let condition = provider
747+
.condition_by_certificate_id("certificate-123")
748+
.unwrap();
747749
let (filter, values) = condition.expand();
748750

749751
assert_eq!("certificate_id = ?1".to_string(), filter);
750-
assert_eq!(vec![Value::String("cert-123".to_string())], values);
752+
assert_eq!(vec![Value::String("certificate-123".to_string())], values);
751753
}
752754

753755
#[test]

mithril-aggregator/src/message_adapters/to_mithril_stake_distribution_list_message.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ impl MessageAdapter<Vec<MithrilStakeDistribution>, MithrilStakeDistributionListM
1717
|stake_distribution| MithrilStakeDistributionListItemMessage {
1818
epoch: stake_distribution.epoch,
1919
hash: stake_distribution.hash,
20+
certificate_hash: stake_distribution.certificate_hash,
2021
},
2122
)
2223
.collect()
@@ -35,13 +36,15 @@ mod tests {
3536
epoch: Epoch(1),
3637
signers_with_stake: signers_with_stakes(1),
3738
hash: "hash-123".to_string(),
39+
certificate_hash: "certificate-hash-123".to_string(),
3840
};
3941
let mithril_stake_distribution_list_message =
4042
ToMithrilStakeDistributionListMessageAdapter::adapt(vec![mithril_stake_distribution]);
4143
let mithril_stake_distribution_list_message_expected =
4244
vec![MithrilStakeDistributionListItemMessage {
4345
epoch: Epoch(1),
4446
hash: "hash-123".to_string(),
47+
certificate_hash: "certificate-hash-123".to_string(),
4548
}];
4649

4750
assert_eq!(

mithril-aggregator/src/message_adapters/to_mithril_stake_distribution_message.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ impl MessageAdapter<MithrilStakeDistribution, MithrilStakeDistributionMessage>
1313
epoch: from.epoch,
1414
signers_with_stake: from.signers_with_stake,
1515
hash: from.hash,
16+
certificate_hash: from.certificate_hash,
1617
}
1718
}
1819
}
@@ -29,11 +30,13 @@ mod tests {
2930
epoch: Epoch(1),
3031
signers_with_stake: fake_data::signers_with_stakes(2),
3132
hash: "hash-123".to_string(),
33+
certificate_hash: "cert-hash-123".to_string(),
3234
};
3335
let mithril_stake_distribution_message_expected = MithrilStakeDistributionMessage {
3436
epoch: Epoch(1),
3537
signers_with_stake: fake_data::signers_with_stakes(2),
3638
hash: "hash-123".to_string(),
39+
certificate_hash: "cert-hash-123".to_string(),
3740
};
3841
let mithril_stake_distribution_message =
3942
ToMithrilStakeDistributionMessageAdapter::adapt(mithril_stake_distribution);

mithril-common/src/entities/mithril_stake_distribution.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,25 @@ pub struct MithrilStakeDistribution {
1717

1818
/// Hash of the Mithril Stake Distribution (different from the AVK).
1919
pub hash: String,
20+
21+
/// Hash of the associated certificate
22+
pub certificate_hash: String,
2023
}
2124

2225
impl MithrilStakeDistribution {
2326
/// MithrilStakeDistribution artifact factory
24-
pub fn new(epoch: Epoch, signers_with_stake: Vec<SignerWithStake>) -> Self {
27+
pub fn new(
28+
epoch: Epoch,
29+
signers_with_stake: Vec<SignerWithStake>,
30+
certificate_hash: String,
31+
) -> Self {
2532
let mut signers_with_stake_sorted = signers_with_stake;
2633
signers_with_stake_sorted.sort();
2734
let mut mithril_stake_distribution = Self {
2835
epoch,
2936
signers_with_stake: signers_with_stake_sorted,
3037
hash: "".to_string(),
38+
certificate_hash,
3139
};
3240
mithril_stake_distribution.hash = mithril_stake_distribution.compute_hash();
3341
mithril_stake_distribution
@@ -39,6 +47,7 @@ impl MithrilStakeDistribution {
3947
for signer_with_stake in &self.signers_with_stake {
4048
hasher.update(signer_with_stake.compute_hash().as_bytes());
4149
}
50+
hasher.update(self.certificate_hash.as_bytes());
4251
hex::encode(hasher.finalize())
4352
}
4453
}

mithril-common/src/messages/mithril_stake_distribution.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ pub struct MithrilStakeDistributionMessage {
1515

1616
/// Hash of the Mithril Stake Distribution (different from the AVK).
1717
pub hash: String,
18+
19+
/// Hash of the associated certificate
20+
pub certificate_hash: String,
1821
}
1922

2023
impl MithrilStakeDistributionMessage {
@@ -27,6 +30,7 @@ impl MithrilStakeDistributionMessage {
2730
.unwrap()
2831
.to_owned()],
2932
hash: "hash-123".to_string(),
33+
certificate_hash: "cert-hash-123".to_string(),
3034
}
3135
}
3236
}
@@ -40,6 +44,7 @@ mod tests {
4044
epoch: Epoch(1),
4145
signers_with_stake: fake_data::signers_with_stakes(1),
4246
hash: "hash-123".to_string(),
47+
certificate_hash: "cert-hash-123".to_string(),
4348
}
4449
}
4550

@@ -55,7 +60,8 @@ mod tests {
5560
"stake": 826
5661
}
5762
],
58-
"hash": "hash-123"
63+
"hash": "hash-123",
64+
"certificate_hash": "cert-hash-123"
5965
}"#;
6066
let message: MithrilStakeDistributionMessage = serde_json::from_str(json).expect(
6167
"This JSON is expected to be succesfully parsed into a MithrilStakeDistributionMessage instance.",

mithril-common/src/messages/mithril_stake_distribution_list.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ pub struct MithrilStakeDistributionListItemMessage {
1313

1414
/// Hash of the Mithril Stake Distribution (different from the AVK).
1515
pub hash: String,
16+
17+
/// Hash of the associated certificate
18+
pub certificate_hash: String,
1619
}
1720

1821
impl MithrilStakeDistributionListItemMessage {
@@ -21,6 +24,7 @@ impl MithrilStakeDistributionListItemMessage {
2124
Self {
2225
epoch: Epoch(1),
2326
hash: "hash-123".to_string(),
27+
certificate_hash: "certificate-hash-123".to_string(),
2428
}
2529
}
2630
}
@@ -33,6 +37,7 @@ mod tests {
3337
vec![MithrilStakeDistributionListItemMessage {
3438
epoch: Epoch(1),
3539
hash: "hash-123".to_string(),
40+
certificate_hash: "certificate-hash-123".to_string(),
3641
}]
3742
}
3843

@@ -41,7 +46,8 @@ mod tests {
4146
fn test_v1() {
4247
let json = r#"[{
4348
"epoch": 1,
44-
"hash": "hash-123"
49+
"hash": "hash-123",
50+
"certificate_hash": "certificate-hash-123"
4551
}]"#;
4652
let message: MithrilStakeDistributionListMessage = serde_json::from_str(json).expect(
4753
"This JSON is expected to be succesfully parsed into a MithrilStakeDistributionListMessage instance.",

mithril-common/src/test_utils/fake_data.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ pub fn mithril_stake_distributions(total: u64) -> Vec<entities::MithrilStakeDist
192192
epoch: Epoch(epoch_idx),
193193
signers_with_stake: self::signers_with_stakes(5),
194194
hash: format!("hash-epoch-{epoch_idx}"),
195+
certificate_hash: format!("certificate-hash-epoch-{epoch_idx}"),
195196
})
196197
.collect::<Vec<entities::MithrilStakeDistribution>>()
197198
}

0 commit comments

Comments
 (0)