Skip to content

Commit 88d4e46

Browse files
committed
rename signed entity service
1 parent fc57c6d commit 88d4e46

File tree

20 files changed

+463
-414
lines changed

20 files changed

+463
-414
lines changed

mithril-aggregator/src/artifact_builder/artifact_builder_service.rs

Lines changed: 104 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ use slog_scope::info;
44
use std::sync::Arc;
55

66
use mithril_common::{
7-
entities::{Beacon, Certificate, Epoch, MithrilStakeDistribution, SignedEntityType, Snapshot},
7+
entities::{
8+
Beacon, Certificate, Epoch, MithrilStakeDistribution, SignedEntity, SignedEntityType,
9+
SignedEntityTypeDiscriminants, Snapshot,
10+
},
811
signable_builder::Artifact,
912
StdResult,
1013
};
@@ -14,31 +17,47 @@ use crate::{
1417
database::provider::{SignedEntityRecord, SignedEntityStorer},
1518
};
1619

17-
#[cfg(test)]
18-
use mockall::automock;
19-
2020
/// ArtifactBuilder Service trait
21-
#[cfg_attr(test, automock)]
2221
#[async_trait]
23-
pub trait ArtifactBuilderService: Send + Sync {
22+
pub trait SignedEntityService: Send + Sync {
2423
/// Create artifact for a signed entity type and a certificate
2524
async fn create_artifact(
2625
&self,
2726
signed_entity_type: SignedEntityType,
2827
certificate: &Certificate,
2928
) -> StdResult<()>;
29+
30+
async fn get_last_signed_snapshots(
31+
&self,
32+
total: usize,
33+
) -> StdResult<Vec<SignedEntity<Snapshot>>>;
34+
35+
async fn get_last_signed_mithril_stake_distribution(
36+
&self,
37+
total: usize,
38+
) -> StdResult<Vec<SignedEntity<MithrilStakeDistribution>>>;
39+
40+
async fn get_signed_snapshot_by_id(
41+
&self,
42+
signed_entity_id: &str,
43+
) -> StdResult<Option<SignedEntity<Snapshot>>>;
44+
45+
async fn get_signed_mithril_stake_distribution_by_id(
46+
&self,
47+
signed_entity_id: &str,
48+
) -> StdResult<Option<SignedEntity<MithrilStakeDistribution>>>;
3049
}
3150

3251
/// Mithril ArtifactBuilder Service
33-
pub struct MithrilArtifactBuilderService {
52+
pub struct MithrilSignedEntityService {
3453
signed_entity_storer: Arc<dyn SignedEntityStorer>,
3554
mithril_stake_distribution_artifact_builder:
3655
Arc<dyn ArtifactBuilder<Epoch, MithrilStakeDistribution>>,
3756
cardano_immutable_files_full_artifact_builder: Arc<dyn ArtifactBuilder<Beacon, Snapshot>>,
3857
}
3958

40-
impl MithrilArtifactBuilderService {
41-
/// MithrilArtifactBuilderService factory
59+
impl MithrilSignedEntityService {
60+
/// MithrilSignedEntityService factory
4261
pub fn new(
4362
signed_entity_storer: Arc<dyn SignedEntityStorer>,
4463
mithril_stake_distribution_artifact_builder: Arc<
@@ -76,14 +95,14 @@ impl MithrilArtifactBuilderService {
7695
}
7796

7897
#[async_trait]
79-
impl ArtifactBuilderService for MithrilArtifactBuilderService {
98+
impl SignedEntityService for MithrilSignedEntityService {
8099
async fn create_artifact(
81100
&self,
82101
signed_entity_type: SignedEntityType,
83102
certificate: &Certificate,
84103
) -> StdResult<()> {
85104
info!(
86-
"MithrilArtifactBuilderService::create_artifact";
105+
"MithrilSignedEntityService::create_artifact";
87106
"signed_entity_type" => ?signed_entity_type,
88107
"certificate_hash" => &certificate.hash
89108
);
@@ -105,6 +124,78 @@ impl ArtifactBuilderService for MithrilArtifactBuilderService {
105124

106125
Ok(())
107126
}
127+
128+
async fn get_last_signed_snapshots(
129+
&self,
130+
total: usize,
131+
) -> StdResult<Vec<SignedEntity<Snapshot>>> {
132+
let signed_entities_records = self
133+
.signed_entity_storer
134+
.get_last_signed_entities(
135+
&SignedEntityTypeDiscriminants::CardanoImmutableFilesFull,
136+
total,
137+
)
138+
.await?;
139+
let mut signed_entities: Vec<SignedEntity<Snapshot>> = Vec::new();
140+
141+
for record in signed_entities_records {
142+
signed_entities.push(record.try_into()?);
143+
}
144+
145+
Ok(signed_entities)
146+
}
147+
148+
async fn get_last_signed_mithril_stake_distribution(
149+
&self,
150+
total: usize,
151+
) -> StdResult<Vec<SignedEntity<MithrilStakeDistribution>>> {
152+
let signed_entities_records = self
153+
.signed_entity_storer
154+
.get_last_signed_entities(
155+
&SignedEntityTypeDiscriminants::MithrilStakeDistribution,
156+
total,
157+
)
158+
.await?;
159+
let mut signed_entities: Vec<SignedEntity<MithrilStakeDistribution>> = Vec::new();
160+
161+
for record in signed_entities_records {
162+
signed_entities.push(record.try_into()?);
163+
}
164+
165+
Ok(signed_entities)
166+
}
167+
168+
async fn get_signed_snapshot_by_id(
169+
&self,
170+
signed_entity_id: &str,
171+
) -> StdResult<Option<SignedEntity<Snapshot>>> {
172+
let entity: Option<SignedEntity<Snapshot>> = match self
173+
.signed_entity_storer
174+
.get_signed_entity(signed_entity_id)
175+
.await?
176+
{
177+
Some(entity) => Some(entity.try_into()?),
178+
None => None,
179+
};
180+
181+
Ok(entity)
182+
}
183+
184+
async fn get_signed_mithril_stake_distribution_by_id(
185+
&self,
186+
signed_entity_id: &str,
187+
) -> StdResult<Option<SignedEntity<MithrilStakeDistribution>>> {
188+
let entity: Option<SignedEntity<MithrilStakeDistribution>> = match self
189+
.signed_entity_storer
190+
.get_signed_entity(signed_entity_id)
191+
.await?
192+
{
193+
Some(entity) => Some(entity.try_into()?),
194+
None => None,
195+
};
196+
197+
Ok(entity)
198+
}
108199
}
109200

110201
#[cfg(test)]
@@ -140,7 +231,7 @@ mod tests {
140231
let mock_cardano_immutable_files_full_artifact_builder =
141232
MockArtifactBuilder::<Beacon, Snapshot>::new();
142233

143-
let artifact_builder_service = MithrilArtifactBuilderService::new(
234+
let artifact_builder_service = MithrilSignedEntityService::new(
144235
Arc::new(mock_signed_entity_storer),
145236
Arc::new(mock_mithril_stake_distribution_artifact_builder),
146237
Arc::new(mock_cardano_immutable_files_full_artifact_builder),
@@ -177,7 +268,7 @@ mod tests {
177268
.once()
178269
.return_once(move |_, _| Ok(snapshot_expected_clone));
179270

180-
let artifact_builder_service = MithrilArtifactBuilderService::new(
271+
let artifact_builder_service = MithrilSignedEntityService::new(
181272
Arc::new(mock_signed_entity_storer),
182273
Arc::new(mock_mithril_stake_distribution_artifact_builder),
183274
Arc::new(mock_cardano_immutable_files_full_artifact_builder),

0 commit comments

Comments
 (0)