Skip to content

Commit 8b5944a

Browse files
committed
Apply review comments
1 parent 4cc1342 commit 8b5944a

File tree

9 files changed

+63
-35
lines changed

9 files changed

+63
-35
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mithril-aggregator/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ semver = "1.0.16"
2323
serde = { version = "1.0", features = ["derive"] }
2424
serde_json = "1.0"
2525
serde_yaml = "0.9.10"
26+
sha2 = "0.10.2"
2627
slog = { version = "2.7.0", features = ["max_level_trace", "release_max_level_debug"] }
2728
slog-async = "2.7.0"
2829
slog-bunyan = "2.4.0"

mithril-aggregator/src/artifact_builder/artifact_builder_service.rs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,11 @@ use mockall::automock;
2323
#[cfg_attr(test, automock)]
2424
#[async_trait]
2525
pub trait ArtifactBuilderService: Send + Sync {
26-
/// Compute artifact from signed entity type
27-
async fn compute_artifact(
26+
/// Create artifact for a signed entity type and a certificate
27+
async fn create_artifact(
2828
&self,
2929
signed_entity_type: SignedEntityType,
3030
certificate: &Certificate,
31-
) -> StdResult<Arc<dyn Artifact>>;
32-
33-
/// Create an save signed entity
34-
async fn create_and_save_signed_entity(
35-
&self,
36-
signed_entity_type: SignedEntityType,
37-
certificate: &Certificate,
38-
artifact: Arc<dyn Artifact>,
3931
) -> StdResult<()>;
4032
}
4133

@@ -62,10 +54,8 @@ impl MithrilArtifactBuilderService {
6254
cardano_immutable_files_full_artifact_builder,
6355
}
6456
}
65-
}
6657

67-
#[async_trait]
68-
impl ArtifactBuilderService for MithrilArtifactBuilderService {
58+
/// Compute artifact from signed entity type
6959
async fn compute_artifact(
7060
&self,
7161
signed_entity_type: SignedEntityType,
@@ -85,13 +75,18 @@ impl ArtifactBuilderService for MithrilArtifactBuilderService {
8575
SignedEntityType::CardanoStakeDistribution(_) => todo!(),
8676
}
8777
}
78+
}
8879

89-
async fn create_and_save_signed_entity(
80+
#[async_trait]
81+
impl ArtifactBuilderService for MithrilArtifactBuilderService {
82+
async fn create_artifact(
9083
&self,
9184
signed_entity_type: SignedEntityType,
9285
certificate: &Certificate,
93-
artifact: Arc<dyn Artifact>,
9486
) -> StdResult<()> {
87+
let artifact = self
88+
.compute_artifact(signed_entity_type.clone(), certificate)
89+
.await?;
9590
let signed_entity = SignedEntityRecord {
9691
signed_entity_id: artifact.get_id(),
9792
signed_entity_type,

mithril-aggregator/src/artifact_builder/mithril_stake_distribution.rs

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use async_trait::async_trait;
22
use serde::{Deserialize, Serialize};
3+
use sha2::{Digest, Sha256};
34
use std::sync::Arc;
45
use tokio::sync::RwLock;
56

@@ -15,17 +16,37 @@ use mithril_common::{
1516
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
1617
pub struct MithrilStakeDistribution {
1718
signers_with_stake: Vec<SignerWithStake>,
19+
hash: String,
1820
}
1921

2022
impl MithrilStakeDistribution {
2123
/// MithrilStakeDistribution artifact factory
2224
pub fn new(signers_with_stake: Vec<SignerWithStake>) -> Self {
23-
Self { signers_with_stake }
25+
let mut signers_with_stake_sorted = signers_with_stake;
26+
signers_with_stake_sorted.sort();
27+
let mut mithril_stake_distribution = Self {
28+
signers_with_stake: signers_with_stake_sorted,
29+
hash: "".to_string(),
30+
};
31+
mithril_stake_distribution.hash = mithril_stake_distribution.compute_hash();
32+
mithril_stake_distribution
33+
}
34+
35+
fn compute_hash(&self) -> String {
36+
let mut hasher = Sha256::new();
37+
for signer_with_stake in &self.signers_with_stake {
38+
hasher.update(signer_with_stake.compute_hash().as_bytes());
39+
}
40+
hex::encode(hasher.finalize())
2441
}
2542
}
2643

2744
#[typetag::serde]
28-
impl Artifact for MithrilStakeDistribution {}
45+
impl Artifact for MithrilStakeDistribution {
46+
fn get_id(&self) -> String {
47+
self.hash.clone()
48+
}
49+
}
2950

3051
/// A [MithrilStakeDistributionArtifact] builder
3152
pub struct MithrilStakeDistributionArtifactBuilder {
@@ -79,4 +100,24 @@ mod tests {
79100
let artifact_expected = MithrilStakeDistribution::new(signers_with_stake);
80101
assert_eq!(artifact_expected, artifact);
81102
}
103+
104+
#[test]
105+
fn sort_given_signers_when_created() {
106+
let signers_with_stake = fake_data::signers_with_stakes(5);
107+
108+
assert_eq!(
109+
MithrilStakeDistribution::new(signers_with_stake.clone()),
110+
MithrilStakeDistribution::new(signers_with_stake.into_iter().rev().collect())
111+
);
112+
}
113+
114+
#[test]
115+
fn hash_value_doesnt_change_if_signers_order_change() {
116+
let signers_with_stake = fake_data::signers_with_stakes(5);
117+
118+
let sd = MithrilStakeDistribution::new(signers_with_stake.clone());
119+
let sd2 = MithrilStakeDistribution::new(signers_with_stake.into_iter().rev().collect());
120+
121+
assert_eq!(sd.hash, sd2.hash);
122+
}
82123
}

mithril-aggregator/src/runtime/runner.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ pub trait AggregatorRunnerTrait: Sync + Send {
122122
) -> Result<Option<Certificate>, Box<dyn StdError + Sync + Send>>;
123123

124124
/// Create an artifact and persist it.
125-
async fn create_and_save_artifact(
125+
async fn create_artifact(
126126
&self,
127127
signed_entity_type: &SignedEntityType,
128128
certificate: &Certificate,
@@ -441,20 +441,15 @@ impl AggregatorRunnerTrait for AggregatorRunner {
441441
.await
442442
}
443443

444-
async fn create_and_save_artifact(
444+
async fn create_artifact(
445445
&self,
446446
signed_entity_type: &SignedEntityType,
447447
certificate: &Certificate,
448448
) -> Result<(), Box<dyn StdError + Sync + Send>> {
449-
debug!("RUNNER: create and save artifact");
450-
let artifact = self
451-
.dependencies
452-
.artifact_builder_service
453-
.compute_artifact(signed_entity_type.to_owned(), certificate)
454-
.await?;
449+
debug!("RUNNER: create artifact");
455450
self.dependencies
456451
.artifact_builder_service
457-
.create_and_save_signed_entity(signed_entity_type.to_owned(), certificate, artifact)
452+
.create_artifact(signed_entity_type.to_owned(), certificate)
458453
.await?;
459454

460455
Ok(())

mithril-aggregator/src/runtime/state_machine.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ impl AggregatorRuntime {
283283

284284
self.runner.drop_pending_certificate().await?;
285285
self.runner
286-
.create_and_save_artifact(&state.signed_entity_type, &certificate)
286+
.create_artifact(&state.signed_entity_type, &certificate)
287287
.await?;
288288

289289
Ok(IdleState {
@@ -671,7 +671,7 @@ mod tests {
671671
.once()
672672
.returning(|| Ok(Some(fake_data::certificate_pending())));
673673
runner
674-
.expect_create_and_save_artifact()
674+
.expect_create_artifact()
675675
.once()
676676
.returning(|_, _| Ok(()));
677677

mithril-common/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ strum_macros = "0.24.1"
5050
thiserror = "1.0.31"
5151
tokio = { version = "1.17.0", features = ["full"] }
5252
typetag = "0.2.8"
53-
uuid = { version = "1.3.0", features = ["v4", "fast-rng", "macro-diagnostics"] }
5453
walkdir = "2"
5554
warp = "0.3"
5655

mithril-common/src/entities/signer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ impl From<SignerWithStake> for Signer {
8181
}
8282

8383
/// Signer represents a signing party in the network (including its stakes)
84-
#[derive(Clone, Debug, PartialEq, Eq, Default, Serialize, Deserialize)]
84+
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Default, Serialize, Deserialize)]
8585
pub struct SignerWithStake {
8686
/// The unique identifier of the signer
8787
// TODO: Should be removed once the signer certification is fully deployed

mithril-common/src/signable_builder/interface.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use async_trait::async_trait;
22
use std::fmt::Debug;
3-
use uuid::Uuid;
43

54
use crate::{entities::ProtocolMessage, StdResult};
65

@@ -14,9 +13,7 @@ pub trait Beacon: Send + Sync {}
1413
#[typetag::serde(tag = "type")]
1514
pub trait Artifact: Debug + Send + Sync {
1615
/// Get artifact identifier
17-
fn get_id(&self) -> String {
18-
Uuid::new_v4().to_string()
19-
}
16+
fn get_id(&self) -> String;
2017
}
2118

2219
/// SignableBuilder is trait for building a protocol message for a beacon

0 commit comments

Comments
 (0)