Skip to content

Commit

Permalink
Merge pull request #889 from input-output-hk/jpraynaud/870-fix-dyn-ar…
Browse files Browse the repository at this point in the history
…tifact-trait-object

Make `Artifact` usable as trait object
  • Loading branch information
jpraynaud authored May 2, 2023
2 parents b679c45 + a9e3910 commit cc9660f
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 18 deletions.
68 changes: 67 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions mithril-aggregator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ tar = "0.4.38"
thiserror = "1.0.31"
tokio = { version = "1.17.0", features = ["full"] }
tokio-util = { version = "0.7.1", features = ["codec"] }
typetag = "0.2.8"
uuid = { version = "1.3.0", features = ["v4", "fast-rng", "macro-diagnostics"] }
warp = "0.3"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use async_trait::async_trait;

use std::sync::Arc;

use mithril_common::{
Expand All @@ -10,14 +12,29 @@ use crate::artifact_builder::ArtifactBuilder;

use super::MithrilStakeDistribution;

/// ArtifactBuilder Service
pub struct ArtifactBuilderService {
#[cfg(test)]
use mockall::automock;

/// ArtifactBuilder Service trait
#[cfg_attr(test, automock)]
#[async_trait]
pub trait ArtifactBuilderService: Send + Sync {
/// Compute artifact from signed entity type
async fn compute_artifact(
&self,
signed_entity_type: SignedEntityType,
certificate: &Certificate,
) -> StdResult<Arc<dyn Artifact>>;
}

/// Mithril ArtifactBuilder Service
pub struct MithrilArtifactBuilderService {
mithril_stake_distribution_artifact_builder:
Arc<dyn ArtifactBuilder<Epoch, MithrilStakeDistribution>>,
}

impl ArtifactBuilderService {
/// ArtifactBuilderService factory
impl MithrilArtifactBuilderService {
/// MithrilArtifactBuilderService factory
#[allow(dead_code)]
pub fn new(
mithril_stake_distribution_artifact_builder: Arc<
Expand All @@ -30,13 +47,14 @@ impl ArtifactBuilderService {
}
}

impl ArtifactBuilderService {
#[async_trait]
impl ArtifactBuilderService for MithrilArtifactBuilderService {
#[allow(dead_code)]
async fn compute_artifact(
&self,
signed_entity_type: SignedEntityType,
certificate: &Certificate,
) -> StdResult<Arc<impl Artifact>> {
) -> StdResult<Arc<dyn Artifact>> {
let artifact = match signed_entity_type {
SignedEntityType::MithrilStakeDistribution(e) => Arc::new(
self.mithril_stake_distribution_artifact_builder
Expand Down Expand Up @@ -70,18 +88,21 @@ mod tests {
.once()
.return_once(move |_, _| Ok(mithril_stake_distribution_clone));

let artifact_builder_service =
ArtifactBuilderService::new(Arc::new(mock_mithril_stake_distribution_artifact_builder));
let artifact_builder_service = MithrilArtifactBuilderService::new(Arc::new(
mock_mithril_stake_distribution_artifact_builder,
));
let certificate = Certificate::default();

let signed_entity_type = SignedEntityType::MithrilStakeDistribution(Epoch(1));
let artifact = artifact_builder_service
.compute_artifact(signed_entity_type, &certificate)
.await
.unwrap();
let mithril_stake_distribution_computed: MithrilStakeDistribution =
serde_json::from_str(&serde_json::to_string(&artifact).unwrap()).unwrap();
assert_eq!(
serde_json::to_string(&mithril_stake_distribution_expected).unwrap(),
serde_json::to_string(&artifact).unwrap()
serde_json::to_string(&mithril_stake_distribution_computed).unwrap()
);
}
}
1 change: 1 addition & 0 deletions mithril-aggregator/src/artifact_builder/dummy_artifact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ impl DummyArtifact {
}
}

#[typetag::serde]
impl Artifact for DummyArtifact {}

/// A [DummyArtifact] builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ impl MithrilStakeDistribution {
}
}

#[typetag::serde]
impl Artifact for MithrilStakeDistribution {}

/// A [MithrilStakeDistributionArtifact] builder
Expand Down
2 changes: 1 addition & 1 deletion mithril-aggregator/src/dependency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ pub struct DependencyManager {
pub signable_builder_service: Arc<SignableBuilderService>,

/// Artifact Builder Service
pub artifact_builder_service: Arc<ArtifactBuilderService>,
pub artifact_builder_service: Arc<dyn ArtifactBuilderService>,

/// Certifier Service
pub certifier_service: Arc<dyn CertifierService>,
Expand Down
15 changes: 10 additions & 5 deletions mithril-aggregator/src/dependency_injection/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ use tokio::{
use warp::Filter;

use crate::{
artifact_builder::{ArtifactBuilderService, MithrilStakeDistributionArtifactBuilder},
artifact_builder::{
ArtifactBuilderService, MithrilArtifactBuilderService,
MithrilStakeDistributionArtifactBuilder,
},
certifier_service::{CertifierService, MithrilCertifierService},
configuration::{ExecutionEnvironment, LIST_SNAPSHOTS_MAX_ITEMS},
database::provider::{
Expand Down Expand Up @@ -170,7 +173,7 @@ pub struct DependenciesBuilder {
pub signable_builder_service: Option<Arc<SignableBuilderService>>,

/// Artifact Builder Service
pub artifact_builder_service: Option<Arc<ArtifactBuilderService>>,
pub artifact_builder_service: Option<Arc<dyn ArtifactBuilderService>>,

/// Certifier service
pub certifier_service: Option<Arc<dyn CertifierService>>,
Expand Down Expand Up @@ -897,19 +900,21 @@ impl DependenciesBuilder {
Ok(self.signable_builder_service.as_ref().cloned().unwrap())
}

async fn build_artifact_builder_service(&mut self) -> Result<Arc<ArtifactBuilderService>> {
async fn build_artifact_builder_service(&mut self) -> Result<Arc<dyn ArtifactBuilderService>> {
let multi_signer = self.get_multi_signer().await?;
let mithril_stake_distribution_artifact_builder =
Arc::new(MithrilStakeDistributionArtifactBuilder::new(multi_signer));
let artifact_builder_service = Arc::new(ArtifactBuilderService::new(
let artifact_builder_service = Arc::new(MithrilArtifactBuilderService::new(
mithril_stake_distribution_artifact_builder,
));

Ok(artifact_builder_service)
}

/// [ArtifactBuilderService] service
pub async fn get_artifact_builder_service(&mut self) -> Result<Arc<ArtifactBuilderService>> {
pub async fn get_artifact_builder_service(
&mut self,
) -> Result<Arc<dyn ArtifactBuilderService>> {
if self.artifact_builder_service.is_none() {
self.artifact_builder_service = Some(self.build_artifact_builder_service().await?);
}
Expand Down
1 change: 1 addition & 0 deletions mithril-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ strum = "0.24.1"
strum_macros = "0.24.1"
thiserror = "1.0.31"
tokio = { version = "1.17.0", features = ["full"] }
typetag = "0.2.8"
walkdir = "2"
warp = "0.3"

Expand Down
4 changes: 2 additions & 2 deletions mithril-common/src/signable_builder/interface.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use async_trait::async_trait;
use serde::{de::DeserializeOwned, Serialize};
use std::fmt::Debug;

use crate::{entities::ProtocolMessage, StdResult};
Expand All @@ -14,7 +13,8 @@ pub trait Signable: Send + Sync {
}

/// Artifact is a trait for types that represent signed artifacts
pub trait Artifact: Serialize + DeserializeOwned + PartialEq + Debug + Send + Sync {}
#[typetag::serde(tag = "type")]
pub trait Artifact: Debug + Send + Sync {}

/// SignableBuilder is trait for building a signable for a beacon
#[async_trait]
Expand Down

0 comments on commit cc9660f

Please sign in to comment.