|
| 1 | +use std::sync::Arc; |
| 2 | + |
| 3 | +use mithril_common::{ |
| 4 | + entities::{Certificate, SignedEntityType}, |
| 5 | + signable_builder::DummyBeacon, |
| 6 | + StdResult, |
| 7 | +}; |
| 8 | + |
| 9 | +use crate::artifact_builder::{Artifact, DummyArtifactBuilder}; |
| 10 | + |
| 11 | +use super::ArtifactBuilder; |
| 12 | + |
| 13 | +/// ArtifactBuilder Service |
| 14 | +// TODO: temporary implementation |
| 15 | +pub struct ArtifactBuilderService { |
| 16 | + dummy_artifact_builder: DummyArtifactBuilder, |
| 17 | +} |
| 18 | + |
| 19 | +impl ArtifactBuilderService { |
| 20 | + /// ArtifactBuilderService factory |
| 21 | + pub fn new(dummy_artifact_builder: DummyArtifactBuilder) -> Self { |
| 22 | + Self { |
| 23 | + dummy_artifact_builder, |
| 24 | + } |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +impl ArtifactBuilderService { |
| 29 | + #[allow(dead_code)] |
| 30 | + async fn compute_artifact( |
| 31 | + &self, |
| 32 | + signed_entity_type: SignedEntityType, |
| 33 | + certificate: Certificate, |
| 34 | + ) -> StdResult<Arc<impl Artifact>> { |
| 35 | + let artifact = match signed_entity_type { |
| 36 | + SignedEntityType::MithrilStakeDistribution(e) => Arc::new( |
| 37 | + self.dummy_artifact_builder |
| 38 | + .compute_artifact(DummyBeacon { epoch: e }, certificate) |
| 39 | + .await?, |
| 40 | + ), |
| 41 | + SignedEntityType::CardanoStakeDistribution(e) => Arc::new( |
| 42 | + self.dummy_artifact_builder |
| 43 | + .compute_artifact(DummyBeacon { epoch: e }, certificate) |
| 44 | + .await?, |
| 45 | + ), |
| 46 | + SignedEntityType::CardanoImmutableFilesFull(b) => Arc::new( |
| 47 | + self.dummy_artifact_builder |
| 48 | + .compute_artifact(DummyBeacon { epoch: b.epoch }, certificate) |
| 49 | + .await?, |
| 50 | + ), |
| 51 | + }; |
| 52 | + |
| 53 | + Ok(artifact) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +#[cfg(test)] |
| 58 | +mod tests { |
| 59 | + use mithril_common::entities::{Beacon, Epoch}; |
| 60 | + |
| 61 | + use super::*; |
| 62 | + |
| 63 | + // TODO: temporary test |
| 64 | + #[tokio::test] |
| 65 | + async fn test_artifact_builder_service() { |
| 66 | + let dummy_artifact_builder = DummyArtifactBuilder::default(); |
| 67 | + let artifact_builder_service = ArtifactBuilderService::new(dummy_artifact_builder); |
| 68 | + let certificate = Certificate::default(); |
| 69 | + |
| 70 | + let signed_entity_type_1 = SignedEntityType::MithrilStakeDistribution(Epoch(1)); |
| 71 | + let artifact_1 = artifact_builder_service |
| 72 | + .compute_artifact(signed_entity_type_1, certificate.clone()) |
| 73 | + .await |
| 74 | + .unwrap(); |
| 75 | + |
| 76 | + let signed_entity_type_2 = SignedEntityType::CardanoStakeDistribution(Epoch(0)); |
| 77 | + let artifact_2 = artifact_builder_service |
| 78 | + .compute_artifact(signed_entity_type_2, certificate.clone()) |
| 79 | + .await |
| 80 | + .unwrap(); |
| 81 | + |
| 82 | + let signed_entity_type_3 = SignedEntityType::CardanoImmutableFilesFull(Beacon::default()); |
| 83 | + let artifact_3 = artifact_builder_service |
| 84 | + .compute_artifact(signed_entity_type_3, certificate) |
| 85 | + .await |
| 86 | + .unwrap(); |
| 87 | + |
| 88 | + assert_ne!(artifact_1, artifact_2); |
| 89 | + assert_ne!(artifact_1, artifact_3); |
| 90 | + assert_eq!(artifact_2, artifact_3); |
| 91 | + } |
| 92 | +} |
0 commit comments