Skip to content

Commit 6f3b65a

Browse files
committed
Update ArtifactBuilderService to persist signed entities
1 parent b563520 commit 6f3b65a

File tree

1 file changed

+49
-4
lines changed

1 file changed

+49
-4
lines changed

mithril-aggregator/src/artifact_builder/artifact_builder_service.rs

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
use async_trait::async_trait;
2+
use chrono::Utc;
3+
use uuid::Uuid;
24

35
use std::sync::Arc;
46

@@ -8,7 +10,10 @@ use mithril_common::{
810
StdResult,
911
};
1012

11-
use crate::artifact_builder::ArtifactBuilder;
13+
use crate::{
14+
artifact_builder::ArtifactBuilder,
15+
database::provider::{SignedEntityRecord, SignedEntityStorer},
16+
};
1217

1318
use super::MithrilStakeDistribution;
1419

@@ -25,25 +30,35 @@ pub trait ArtifactBuilderService: Send + Sync {
2530
signed_entity_type: SignedEntityType,
2631
certificate: &Certificate,
2732
) -> StdResult<Arc<dyn Artifact>>;
33+
34+
/// Create an save signed entity
35+
async fn create_and_save_signed_entity(
36+
&self,
37+
signed_entity_type: SignedEntityType,
38+
certificate: &Certificate,
39+
artifact: Arc<dyn Artifact>,
40+
) -> StdResult<()>;
2841
}
2942

3043
/// Mithril ArtifactBuilder Service
3144
pub struct MithrilArtifactBuilderService {
45+
signed_entity_storer: Arc<dyn SignedEntityStorer>,
3246
mithril_stake_distribution_artifact_builder:
3347
Arc<dyn ArtifactBuilder<Epoch, MithrilStakeDistribution>>,
3448
cardano_immutable_files_full_artifact_builder: Arc<dyn ArtifactBuilder<Beacon, Snapshot>>,
3549
}
3650

3751
impl MithrilArtifactBuilderService {
3852
/// MithrilArtifactBuilderService factory
39-
#[allow(dead_code)]
4053
pub fn new(
54+
signed_entity_storer: Arc<dyn SignedEntityStorer>,
4155
mithril_stake_distribution_artifact_builder: Arc<
4256
dyn ArtifactBuilder<Epoch, MithrilStakeDistribution>,
4357
>,
4458
cardano_immutable_files_full_artifact_builder: Arc<dyn ArtifactBuilder<Beacon, Snapshot>>,
4559
) -> Self {
4660
Self {
61+
signed_entity_storer,
4762
mithril_stake_distribution_artifact_builder,
4863
cardano_immutable_files_full_artifact_builder,
4964
}
@@ -52,7 +67,6 @@ impl MithrilArtifactBuilderService {
5267

5368
#[async_trait]
5469
impl ArtifactBuilderService for MithrilArtifactBuilderService {
55-
#[allow(dead_code)]
5670
async fn compute_artifact(
5771
&self,
5872
signed_entity_type: SignedEntityType,
@@ -72,6 +86,27 @@ impl ArtifactBuilderService for MithrilArtifactBuilderService {
7286
SignedEntityType::CardanoStakeDistribution(_) => todo!(),
7387
}
7488
}
89+
90+
async fn create_and_save_signed_entity(
91+
&self,
92+
signed_entity_type: SignedEntityType,
93+
certificate: &Certificate,
94+
artifact: Arc<dyn Artifact>,
95+
) -> StdResult<()> {
96+
let signed_entity = SignedEntityRecord {
97+
signed_entity_id: Uuid::new_v4().to_string(),
98+
signed_entity_type,
99+
certificate_id: certificate.hash.clone(),
100+
entity: serde_json::to_string(&artifact)?,
101+
created_at: format!("{:?}", Utc::now()),
102+
};
103+
104+
self.signed_entity_storer
105+
.store_signed_entity(&signed_entity)
106+
.await?;
107+
108+
Ok(())
109+
}
75110
}
76111

77112
#[cfg(test)]
@@ -80,14 +115,19 @@ mod tests {
80115

81116
use super::*;
82117

83-
use crate::artifact_builder::MockArtifactBuilder;
118+
use crate::{
119+
artifact_builder::MockArtifactBuilder, database::provider::MockSignedEntityStorer,
120+
};
84121

85122
#[tokio::test]
86123
async fn build_mithril_stake_distribution_artifact_when_given_mithril_stake_distribution_entity_type(
87124
) {
88125
let signers_with_stake = fake_data::signers_with_stakes(5);
89126
let mithril_stake_distribution_expected = MithrilStakeDistribution::new(signers_with_stake);
90127
let mithril_stake_distribution_clone = mithril_stake_distribution_expected.clone();
128+
129+
let mock_signed_entity_storer = MockSignedEntityStorer::new();
130+
91131
let mut mock_mithril_stake_distribution_artifact_builder =
92132
MockArtifactBuilder::<Epoch, MithrilStakeDistribution>::new();
93133
mock_mithril_stake_distribution_artifact_builder
@@ -99,6 +139,7 @@ mod tests {
99139
MockArtifactBuilder::<Beacon, Snapshot>::new();
100140

101141
let artifact_builder_service = MithrilArtifactBuilderService::new(
142+
Arc::new(mock_signed_entity_storer),
102143
Arc::new(mock_mithril_stake_distribution_artifact_builder),
103144
Arc::new(mock_cardano_immutable_files_full_artifact_builder),
104145
);
@@ -121,6 +162,9 @@ mod tests {
121162
async fn build_snapshot_artifact_when_given_cardano_immutable_files_full_entity_type() {
122163
let snapshot_expected = fake_data::snapshots(1).first().unwrap().to_owned();
123164
let snapshot_expected_clone = snapshot_expected.clone();
165+
166+
let mock_signed_entity_storer = MockSignedEntityStorer::new();
167+
124168
let mock_mithril_stake_distribution_artifact_builder =
125169
MockArtifactBuilder::<Epoch, MithrilStakeDistribution>::new();
126170

@@ -132,6 +176,7 @@ mod tests {
132176
.return_once(move |_, _| Ok(snapshot_expected_clone));
133177

134178
let artifact_builder_service = MithrilArtifactBuilderService::new(
179+
Arc::new(mock_signed_entity_storer),
135180
Arc::new(mock_mithril_stake_distribution_artifact_builder),
136181
Arc::new(mock_cardano_immutable_files_full_artifact_builder),
137182
);

0 commit comments

Comments
 (0)