Skip to content

Commit

Permalink
Add missing certificate hash on Mithril Stake Distribution
Browse files Browse the repository at this point in the history
  • Loading branch information
jpraynaud committed May 15, 2023
1 parent 9dbf873 commit 1e67432
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,11 @@ mod tests {
async fn build_mithril_stake_distribution_artifact_when_given_mithril_stake_distribution_entity_type(
) {
let signers_with_stake = fake_data::signers_with_stakes(5);
let mithril_stake_distribution_expected =
MithrilStakeDistribution::new(Epoch(1), signers_with_stake);
let mithril_stake_distribution_expected = MithrilStakeDistribution::new(
Epoch(1),
signers_with_stake,
"certificate-123".to_string(),
);
let mithril_stake_distribution_clone = mithril_stake_distribution_expected.clone();

let mock_signed_entity_storer = MockSignedEntityStorer::new();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ mod tests {
#[tokio::test]
async fn should_compute_valid_artifact() {
let beacon = fake_data::beacon();
let certificate = fake_data::certificate("cert-123".to_string());
let certificate = fake_data::certificate("certificate-123".to_string());
let snapshot_digest = certificate
.protocol_message
.get_message_part(&ProtocolMessagePartKey::SnapshotDigest)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ impl ArtifactBuilder<Epoch, MithrilStakeDistribution> for MithrilStakeDistributi
async fn compute_artifact(
&self,
beacon: Epoch,
_certificate: &Certificate,
certificate: &Certificate,
) -> StdResult<MithrilStakeDistribution> {
let multi_signer = self.multi_signer.read().await;
Ok(MithrilStakeDistribution::new(
beacon,
multi_signer.get_next_signers_with_stake().await?,
certificate.hash.to_owned(),
))
}
}
Expand All @@ -48,7 +49,7 @@ mod tests {
async fn should_compute_valid_artifact() {
let signers_with_stake = fake_data::signers_with_stakes(5);
let signers_with_stake_clone = signers_with_stake.clone();
let certificate = fake_data::certificate("cert-123".to_string());
let certificate = fake_data::certificate("certificate-123".to_string());
let mut mock_multi_signer = MockMultiSigner::new();
mock_multi_signer
.expect_get_next_signers_with_stake()
Expand All @@ -59,7 +60,11 @@ mod tests {
.compute_artifact(Epoch(1), &certificate)
.await
.unwrap();
let artifact_expected = MithrilStakeDistribution::new(Epoch(1), signers_with_stake);
let artifact_expected = MithrilStakeDistribution::new(
Epoch(1),
signers_with_stake,
"certificate-123".to_string(),
);
assert_eq!(artifact_expected, artifact);
}

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

assert_eq!(
MithrilStakeDistribution::new(Epoch(1), signers_with_stake.clone()),
MithrilStakeDistribution::new(Epoch(1), signers_with_stake.into_iter().rev().collect())
MithrilStakeDistribution::new(
Epoch(1),
signers_with_stake.clone(),
"certificate-123".to_string()
),
MithrilStakeDistribution::new(
Epoch(1),
signers_with_stake.into_iter().rev().collect(),
"certificate-123".to_string()
)
);
}

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

let sd = MithrilStakeDistribution::new(Epoch(1), signers_with_stake.clone());
let sd2 =
MithrilStakeDistribution::new(Epoch(1), signers_with_stake.into_iter().rev().collect());
let sd = MithrilStakeDistribution::new(
Epoch(1),
signers_with_stake.clone(),
"certificate-123".to_string(),
);
let sd2 = MithrilStakeDistribution::new(
Epoch(1),
signers_with_stake.into_iter().rev().collect(),
"certificate-123".to_string(),
);

assert_eq!(sd.hash, sd2.hash);
}
Expand Down
6 changes: 4 additions & 2 deletions mithril-aggregator/src/database/provider/certificate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -743,11 +743,13 @@ mod tests {
fn get_certificate_record_by_certificate_id() {
let connection = Connection::open(":memory:").unwrap();
let provider = CertificateRecordProvider::new(&connection);
let condition = provider.condition_by_certificate_id("cert-123").unwrap();
let condition = provider
.condition_by_certificate_id("certificate-123")
.unwrap();
let (filter, values) = condition.expand();

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

#[test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ mod tests {
epoch: Epoch(1),
signers_with_stake: signers_with_stakes(1),
hash: "hash-123".to_string(),
certificate_hash: "cert-hash-123".to_string(),
};
let mithril_stake_distribution_list_message =
ToMithrilStakeDistributionListMessageAdapter::adapt(vec![mithril_stake_distribution]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ impl MessageAdapter<MithrilStakeDistribution, MithrilStakeDistributionMessage>
epoch: from.epoch,
signers_with_stake: from.signers_with_stake,
hash: from.hash,
certificate_hash: from.certificate_hash,
}
}
}
Expand All @@ -29,11 +30,13 @@ mod tests {
epoch: Epoch(1),
signers_with_stake: fake_data::signers_with_stakes(2),
hash: "hash-123".to_string(),
certificate_hash: "cert-hash-123".to_string(),
};
let mithril_stake_distribution_message_expected = MithrilStakeDistributionMessage {
epoch: Epoch(1),
signers_with_stake: fake_data::signers_with_stakes(2),
hash: "hash-123".to_string(),
certificate_hash: "cert-hash-123".to_string(),
};
let mithril_stake_distribution_message =
ToMithrilStakeDistributionMessageAdapter::adapt(mithril_stake_distribution);
Expand Down
11 changes: 10 additions & 1 deletion mithril-common/src/entities/mithril_stake_distribution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,25 @@ pub struct MithrilStakeDistribution {

/// Hash of the Mithril Stake Distribution (different from the AVK).
pub hash: String,

/// Hash of the associated certificate
pub certificate_hash: String,
}

impl MithrilStakeDistribution {
/// MithrilStakeDistribution artifact factory
pub fn new(epoch: Epoch, signers_with_stake: Vec<SignerWithStake>) -> Self {
pub fn new(
epoch: Epoch,
signers_with_stake: Vec<SignerWithStake>,
certificate_hash: String,
) -> Self {
let mut signers_with_stake_sorted = signers_with_stake;
signers_with_stake_sorted.sort();
let mut mithril_stake_distribution = Self {
epoch,
signers_with_stake: signers_with_stake_sorted,
hash: "".to_string(),
certificate_hash,
};
mithril_stake_distribution.hash = mithril_stake_distribution.compute_hash();
mithril_stake_distribution
Expand All @@ -39,6 +47,7 @@ impl MithrilStakeDistribution {
for signer_with_stake in &self.signers_with_stake {
hasher.update(signer_with_stake.compute_hash().as_bytes());
}
hasher.update(self.certificate_hash.as_bytes());
hex::encode(hasher.finalize())
}
}
Expand Down
8 changes: 7 additions & 1 deletion mithril-common/src/messages/mithril_stake_distribution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ pub struct MithrilStakeDistributionMessage {

/// Hash of the Mithril Stake Distribution (different from the AVK).
pub hash: String,

/// Hash of the associated certificate
pub certificate_hash: String,
}

impl MithrilStakeDistributionMessage {
Expand All @@ -27,6 +30,7 @@ impl MithrilStakeDistributionMessage {
.unwrap()
.to_owned()],
hash: "hash-123".to_string(),
certificate_hash: "cert-hash-123".to_string(),
}
}
}
Expand All @@ -40,6 +44,7 @@ mod tests {
epoch: Epoch(1),
signers_with_stake: fake_data::signers_with_stakes(1),
hash: "hash-123".to_string(),
certificate_hash: "cert-hash-123".to_string(),
}
}

Expand All @@ -55,7 +60,8 @@ mod tests {
"stake": 826
}
],
"hash": "hash-123"
"hash": "hash-123",
"certificate_hash": "cert-hash-123"
}"#;
let message: MithrilStakeDistributionMessage = serde_json::from_str(json).expect(
"This JSON is expected to be succesfully parsed into a MithrilStakeDistributionMessage instance.",
Expand Down
1 change: 1 addition & 0 deletions mithril-common/src/test_utils/fake_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ pub fn mithril_stake_distributions(total: u64) -> Vec<entities::MithrilStakeDist
epoch: Epoch(epoch_idx),
signers_with_stake: self::signers_with_stakes(5),
hash: format!("hash-epoch-{epoch_idx}"),
certificate_hash: format!("certificate-hash-epoch-{epoch_idx}"),
})
.collect::<Vec<entities::MithrilStakeDistribution>>()
}
Expand Down
12 changes: 11 additions & 1 deletion openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -767,10 +767,15 @@ components:
description: Hash of the Mithril stake distribution
type: string
format: bytes
certificate_hash:
description: Hash of the associated certificate
type: string
format: bytes
example:
{
"epoch": 123,
"hash":"24qQwKgWw4mr5kRZLIUA9XAsB0vSvijo8FIfrGFwBtdCNQVhBc9PXu7RiCHSRem3MmHoKbo"
"hash":"24qQwKgWw4mr5kRZLIUA9XAsB0vSvijo8FIfrGFwBtdCNQVhBc9PXu7RiCHSRem3MmHoKbo",
"certificate_hash": "AsB0vSvijo8FIfrG/FwBtdCNQVhBc9P24qQwKgWw4mr5kRZL+IUA9XXu7RiCHSRem3MmHoKbo"
}

MithrilStakeDistributionMessage:
Expand All @@ -790,6 +795,10 @@ components:
description: Hash of the Mithril stake distribution
type: string
format: bytes
certificate_hash:
description: Hash of the associated certificate
type: string
format: bytes
signers:
description: The list of the signers with their stakes and verification keys
type: array
Expand All @@ -799,6 +808,7 @@ components:
{
"epoch": 123,
"hash":"24qQwKgWw4mr5kRZLIUA9XAsB0vSvijo8FIfrGFwBtdCNQVhBc9PXu7RiCHSRem3MmHoKbo",
"certificate_hash": "AsB0vSvijo8FIfrG/FwBtdCNQVhBc9P24qQwKgWw4mr5kRZL+IUA9XXu7RiCHSRem3MmHoKbo",
"signers":
[
{
Expand Down

0 comments on commit 1e67432

Please sign in to comment.