1
+ use async_trait:: async_trait;
2
+
1
3
use std:: sync:: Arc ;
2
4
3
5
use mithril_common:: {
@@ -10,14 +12,29 @@ use crate::artifact_builder::ArtifactBuilder;
10
12
11
13
use super :: MithrilStakeDistribution ;
12
14
13
- /// ArtifactBuilder Service
14
- pub struct ArtifactBuilderService {
15
+ #[ cfg( test) ]
16
+ use mockall:: automock;
17
+
18
+ /// ArtifactBuilder Service trait
19
+ #[ cfg_attr( test, automock) ]
20
+ #[ async_trait]
21
+ pub trait ArtifactBuilderService : Send + Sync {
22
+ /// Compute artifact from signed entity type
23
+ async fn compute_artifact (
24
+ & self ,
25
+ signed_entity_type : SignedEntityType ,
26
+ certificate : & Certificate ,
27
+ ) -> StdResult < Arc < dyn Artifact > > ;
28
+ }
29
+
30
+ /// Mithril ArtifactBuilder Service
31
+ pub struct MithrilArtifactBuilderService {
15
32
mithril_stake_distribution_artifact_builder :
16
33
Arc < dyn ArtifactBuilder < Epoch , MithrilStakeDistribution > > ,
17
34
}
18
35
19
- impl ArtifactBuilderService {
20
- /// ArtifactBuilderService factory
36
+ impl MithrilArtifactBuilderService {
37
+ /// MithrilArtifactBuilderService factory
21
38
#[ allow( dead_code) ]
22
39
pub fn new (
23
40
mithril_stake_distribution_artifact_builder : Arc <
@@ -30,13 +47,14 @@ impl ArtifactBuilderService {
30
47
}
31
48
}
32
49
33
- impl ArtifactBuilderService {
50
+ #[ async_trait]
51
+ impl ArtifactBuilderService for MithrilArtifactBuilderService {
34
52
#[ allow( dead_code) ]
35
53
async fn compute_artifact (
36
54
& self ,
37
55
signed_entity_type : SignedEntityType ,
38
56
certificate : & Certificate ,
39
- ) -> StdResult < Arc < impl Artifact > > {
57
+ ) -> StdResult < Arc < dyn Artifact > > {
40
58
let artifact = match signed_entity_type {
41
59
SignedEntityType :: MithrilStakeDistribution ( e) => Arc :: new (
42
60
self . mithril_stake_distribution_artifact_builder
@@ -70,18 +88,21 @@ mod tests {
70
88
. once ( )
71
89
. return_once ( move |_, _| Ok ( mithril_stake_distribution_clone) ) ;
72
90
73
- let artifact_builder_service =
74
- ArtifactBuilderService :: new ( Arc :: new ( mock_mithril_stake_distribution_artifact_builder) ) ;
91
+ let artifact_builder_service = MithrilArtifactBuilderService :: new ( Arc :: new (
92
+ mock_mithril_stake_distribution_artifact_builder,
93
+ ) ) ;
75
94
let certificate = Certificate :: default ( ) ;
76
95
77
96
let signed_entity_type = SignedEntityType :: MithrilStakeDistribution ( Epoch ( 1 ) ) ;
78
97
let artifact = artifact_builder_service
79
98
. compute_artifact ( signed_entity_type, & certificate)
80
99
. await
81
100
. unwrap ( ) ;
101
+ let mithril_stake_distribution_computed: MithrilStakeDistribution =
102
+ serde_json:: from_str ( & serde_json:: to_string ( & artifact) . unwrap ( ) ) . unwrap ( ) ;
82
103
assert_eq ! (
83
104
serde_json:: to_string( & mithril_stake_distribution_expected) . unwrap( ) ,
84
- serde_json:: to_string( & artifact ) . unwrap( )
105
+ serde_json:: to_string( & mithril_stake_distribution_computed ) . unwrap( )
85
106
) ;
86
107
}
87
108
}
0 commit comments