@@ -4,7 +4,10 @@ use slog_scope::info;
4
4
use std:: sync:: Arc ;
5
5
6
6
use mithril_common:: {
7
- entities:: { Beacon , Certificate , Epoch , MithrilStakeDistribution , SignedEntityType , Snapshot } ,
7
+ entities:: {
8
+ Beacon , Certificate , Epoch , MithrilStakeDistribution , SignedEntity , SignedEntityType ,
9
+ SignedEntityTypeDiscriminants , Snapshot ,
10
+ } ,
8
11
signable_builder:: Artifact ,
9
12
StdResult ,
10
13
} ;
@@ -14,31 +17,47 @@ use crate::{
14
17
database:: provider:: { SignedEntityRecord , SignedEntityStorer } ,
15
18
} ;
16
19
17
- #[ cfg( test) ]
18
- use mockall:: automock;
19
-
20
20
/// ArtifactBuilder Service trait
21
- #[ cfg_attr( test, automock) ]
22
21
#[ async_trait]
23
- pub trait ArtifactBuilderService : Send + Sync {
22
+ pub trait SignedEntityService : Send + Sync {
24
23
/// Create artifact for a signed entity type and a certificate
25
24
async fn create_artifact (
26
25
& self ,
27
26
signed_entity_type : SignedEntityType ,
28
27
certificate : & Certificate ,
29
28
) -> 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 > > > ;
30
49
}
31
50
32
51
/// Mithril ArtifactBuilder Service
33
- pub struct MithrilArtifactBuilderService {
52
+ pub struct MithrilSignedEntityService {
34
53
signed_entity_storer : Arc < dyn SignedEntityStorer > ,
35
54
mithril_stake_distribution_artifact_builder :
36
55
Arc < dyn ArtifactBuilder < Epoch , MithrilStakeDistribution > > ,
37
56
cardano_immutable_files_full_artifact_builder : Arc < dyn ArtifactBuilder < Beacon , Snapshot > > ,
38
57
}
39
58
40
- impl MithrilArtifactBuilderService {
41
- /// MithrilArtifactBuilderService factory
59
+ impl MithrilSignedEntityService {
60
+ /// MithrilSignedEntityService factory
42
61
pub fn new (
43
62
signed_entity_storer : Arc < dyn SignedEntityStorer > ,
44
63
mithril_stake_distribution_artifact_builder : Arc <
@@ -76,14 +95,14 @@ impl MithrilArtifactBuilderService {
76
95
}
77
96
78
97
#[ async_trait]
79
- impl ArtifactBuilderService for MithrilArtifactBuilderService {
98
+ impl SignedEntityService for MithrilSignedEntityService {
80
99
async fn create_artifact (
81
100
& self ,
82
101
signed_entity_type : SignedEntityType ,
83
102
certificate : & Certificate ,
84
103
) -> StdResult < ( ) > {
85
104
info ! (
86
- "MithrilArtifactBuilderService ::create_artifact" ;
105
+ "MithrilSignedEntityService ::create_artifact" ;
87
106
"signed_entity_type" => ?signed_entity_type,
88
107
"certificate_hash" => & certificate. hash
89
108
) ;
@@ -105,6 +124,78 @@ impl ArtifactBuilderService for MithrilArtifactBuilderService {
105
124
106
125
Ok ( ( ) )
107
126
}
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
+ }
108
199
}
109
200
110
201
#[ cfg( test) ]
@@ -140,7 +231,7 @@ mod tests {
140
231
let mock_cardano_immutable_files_full_artifact_builder =
141
232
MockArtifactBuilder :: < Beacon , Snapshot > :: new ( ) ;
142
233
143
- let artifact_builder_service = MithrilArtifactBuilderService :: new (
234
+ let artifact_builder_service = MithrilSignedEntityService :: new (
144
235
Arc :: new ( mock_signed_entity_storer) ,
145
236
Arc :: new ( mock_mithril_stake_distribution_artifact_builder) ,
146
237
Arc :: new ( mock_cardano_immutable_files_full_artifact_builder) ,
@@ -177,7 +268,7 @@ mod tests {
177
268
. once ( )
178
269
. return_once ( move |_, _| Ok ( snapshot_expected_clone) ) ;
179
270
180
- let artifact_builder_service = MithrilArtifactBuilderService :: new (
271
+ let artifact_builder_service = MithrilSignedEntityService :: new (
181
272
Arc :: new ( mock_signed_entity_storer) ,
182
273
Arc :: new ( mock_mithril_stake_distribution_artifact_builder) ,
183
274
Arc :: new ( mock_cardano_immutable_files_full_artifact_builder) ,
0 commit comments