1
- use crate :: http_server:: routes:: middlewares;
2
- use crate :: DependencyContainer ;
3
1
use std:: sync:: Arc ;
4
2
use warp:: Filter ;
5
3
4
+ use mithril_common:: {
5
+ entities:: { SignedEntityConfig , SignedEntityTypeDiscriminants } ,
6
+ messages:: { EpochSettingsMessage , SignerMessagePart } ,
7
+ StdResult ,
8
+ } ;
9
+
10
+ use crate :: dependency_injection:: EpochServiceWrapper ;
11
+ use crate :: http_server:: routes:: middlewares;
12
+ use crate :: DependencyContainer ;
13
+
6
14
pub fn routes (
7
15
dependency_manager : Arc < DependencyContainer > ,
8
16
) -> impl Filter < Extract = ( impl warp:: Reply , ) , Error = warp:: Rejection > + Clone {
@@ -15,70 +23,86 @@ fn epoch_settings(
15
23
) -> impl Filter < Extract = ( impl warp:: Reply , ) , Error = warp:: Rejection > + Clone {
16
24
warp:: path!( "epoch-settings" )
17
25
. and ( warp:: get ( ) )
18
- . and ( middlewares:: with_epoch_service ( dependency_manager) )
26
+ . and ( middlewares:: with_epoch_service ( dependency_manager. clone ( ) ) )
27
+ . and ( middlewares:: with_signed_entity_config ( dependency_manager) )
19
28
. and_then ( handlers:: epoch_settings)
20
29
}
21
30
31
+ async fn get_epoch_settings_message (
32
+ epoch_service : EpochServiceWrapper ,
33
+ signed_entity_config : SignedEntityConfig ,
34
+ ) -> StdResult < EpochSettingsMessage > {
35
+ let epoch_service = epoch_service. read ( ) . await ;
36
+
37
+ let epoch = epoch_service. epoch_of_current_data ( ) ?;
38
+ let protocol_parameters = epoch_service. next_protocol_parameters ( ) ?. clone ( ) ;
39
+ let next_protocol_parameters = epoch_service. upcoming_protocol_parameters ( ) ?. clone ( ) ;
40
+ let current_signers = epoch_service. current_signers ( ) ?;
41
+ let next_signers = epoch_service. next_signers ( ) ?;
42
+
43
+ let cardano_transactions_signing_config = signed_entity_config
44
+ . list_allowed_signed_entity_types_discriminants ( )
45
+ . contains ( & SignedEntityTypeDiscriminants :: CardanoTransactions )
46
+ . then_some ( signed_entity_config. cardano_transactions_signing_config ) ;
47
+ let next_cardano_transactions_signing_config = cardano_transactions_signing_config. clone ( ) ;
48
+
49
+ let epoch_settings_message = EpochSettingsMessage {
50
+ epoch,
51
+ protocol_parameters,
52
+ next_protocol_parameters,
53
+ current_signers : SignerMessagePart :: from_signers ( current_signers. to_vec ( ) ) ,
54
+ next_signers : SignerMessagePart :: from_signers ( next_signers. to_vec ( ) ) ,
55
+ cardano_transactions_signing_config,
56
+ next_cardano_transactions_signing_config,
57
+ } ;
58
+
59
+ Ok ( epoch_settings_message)
60
+ }
61
+
22
62
mod handlers {
23
- use crate :: dependency_injection:: EpochServiceWrapper ;
24
- use crate :: http_server:: routes:: reply;
25
- use mithril_common:: messages:: { EpochSettingsMessage , SignerMessagePart } ;
26
- use slog_scope:: { debug, warn} ;
63
+ use slog_scope:: debug;
27
64
use std:: convert:: Infallible ;
28
65
use warp:: http:: StatusCode ;
29
66
67
+ use mithril_common:: entities:: SignedEntityConfig ;
68
+
69
+ use crate :: dependency_injection:: EpochServiceWrapper ;
70
+ use crate :: http_server:: routes:: epoch_routes:: get_epoch_settings_message;
71
+ use crate :: http_server:: routes:: reply;
72
+
30
73
/// Epoch Settings
31
74
pub async fn epoch_settings (
32
75
epoch_service : EpochServiceWrapper ,
76
+ signed_entity_config : SignedEntityConfig ,
33
77
) -> Result < impl warp:: Reply , Infallible > {
34
78
debug ! ( "⇄ HTTP SERVER: epoch_settings" ) ;
35
- let epoch_service = epoch_service. read ( ) . await ;
36
-
37
- match (
38
- epoch_service. epoch_of_current_data ( ) ,
39
- epoch_service. next_protocol_parameters ( ) ,
40
- epoch_service. upcoming_protocol_parameters ( ) ,
41
- epoch_service. current_signers ( ) ,
42
- epoch_service. next_signers ( ) ,
43
- ) {
44
- (
45
- Ok ( epoch) ,
46
- Ok ( protocol_parameters) ,
47
- Ok ( next_protocol_parameters) ,
48
- Ok ( current_signers) ,
49
- Ok ( next_signers) ,
50
- ) => {
51
- let epoch_settings_message = EpochSettingsMessage {
52
- epoch,
53
- protocol_parameters : protocol_parameters. clone ( ) ,
54
- next_protocol_parameters : next_protocol_parameters. clone ( ) ,
55
- current_signers : SignerMessagePart :: from_signers ( current_signers. to_vec ( ) ) ,
56
- next_signers : SignerMessagePart :: from_signers ( next_signers. to_vec ( ) ) ,
57
- } ;
58
- Ok ( reply:: json ( & epoch_settings_message, StatusCode :: OK ) )
59
- }
60
- ( Err ( err) , _, _, _, _)
61
- | ( _, Err ( err) , _, _, _)
62
- | ( _, _, Err ( err) , _, _)
63
- | ( _, _, _, Err ( err) , _)
64
- | ( _, _, _, _, Err ( err) ) => {
65
- warn ! ( "epoch_settings::error" ; "error" => ?err) ;
66
- Ok ( reply:: server_error ( err) )
67
- }
79
+ let epoch_settings_message =
80
+ get_epoch_settings_message ( epoch_service, signed_entity_config) . await ;
81
+
82
+ match epoch_settings_message {
83
+ Ok ( message) => Ok ( reply:: json ( & message, StatusCode :: OK ) ) ,
84
+ Err ( err) => Ok ( reply:: server_error ( err) ) ,
68
85
}
69
86
}
70
87
}
71
88
72
89
#[ cfg( test) ]
73
90
mod tests {
91
+ use serde_json:: Value :: Null ;
92
+ use std:: collections:: BTreeSet ;
93
+ use tokio:: sync:: RwLock ;
94
+ use warp:: {
95
+ http:: { Method , StatusCode } ,
96
+ test:: request,
97
+ } ;
98
+
74
99
use mithril_common:: {
75
- entities:: Epoch ,
100
+ entities:: {
101
+ BlockNumber , CardanoTransactionsSigningConfig , Epoch , SignedEntityConfig ,
102
+ SignedEntityTypeDiscriminants ,
103
+ } ,
76
104
test_utils:: { apispec:: APISpec , MithrilFixtureBuilder } ,
77
105
} ;
78
- use serde_json:: Value :: Null ;
79
- use tokio:: sync:: RwLock ;
80
- use warp:: http:: { Method , StatusCode } ;
81
- use warp:: test:: request;
82
106
83
107
use crate :: http_server:: SERVER_BASE_PATH ;
84
108
use crate :: initialize_dependencies;
@@ -99,6 +123,62 @@ mod tests {
99
123
. and ( routes ( dependency_manager) . with ( cors) )
100
124
}
101
125
126
+ #[ tokio:: test]
127
+ async fn get_epoch_settings_message_with_cardano_transactions_enabled ( ) {
128
+ let fixture = MithrilFixtureBuilder :: default ( ) . with_signers ( 3 ) . build ( ) ;
129
+ let epoch_service = FakeEpochService :: from_fixture ( Epoch ( 4 ) , & fixture) ;
130
+ let epoch_service = Arc :: new ( RwLock :: new ( epoch_service) ) ;
131
+
132
+ let cardano_transactions_signing_config = CardanoTransactionsSigningConfig {
133
+ security_parameter : BlockNumber ( 70 ) ,
134
+ step : BlockNumber ( 15 ) ,
135
+ } ;
136
+ let signed_entity_config = SignedEntityConfig {
137
+ cardano_transactions_signing_config : cardano_transactions_signing_config. clone ( ) ,
138
+ allowed_discriminants : BTreeSet :: from ( [
139
+ SignedEntityTypeDiscriminants :: CardanoTransactions ,
140
+ ] ) ,
141
+ ..SignedEntityConfig :: dummy ( )
142
+ } ;
143
+
144
+ let message = get_epoch_settings_message ( epoch_service, signed_entity_config)
145
+ . await
146
+ . unwrap ( ) ;
147
+
148
+ assert_eq ! (
149
+ message. cardano_transactions_signing_config,
150
+ Some ( cardano_transactions_signing_config. clone( ) )
151
+ ) ;
152
+ assert_eq ! (
153
+ message. next_cardano_transactions_signing_config,
154
+ Some ( cardano_transactions_signing_config)
155
+ ) ;
156
+ }
157
+
158
+ #[ tokio:: test]
159
+ async fn get_epoch_settings_message_with_cardano_transactions_not_enabled ( ) {
160
+ let fixture = MithrilFixtureBuilder :: default ( ) . with_signers ( 3 ) . build ( ) ;
161
+ let epoch_service = FakeEpochService :: from_fixture ( Epoch ( 4 ) , & fixture) ;
162
+ let epoch_service = Arc :: new ( RwLock :: new ( epoch_service) ) ;
163
+
164
+ let cardano_transactions_signing_config = CardanoTransactionsSigningConfig {
165
+ security_parameter : BlockNumber ( 70 ) ,
166
+ step : BlockNumber ( 15 ) ,
167
+ } ;
168
+ let signed_entity_config = SignedEntityConfig {
169
+ cardano_transactions_signing_config,
170
+ allowed_discriminants : BTreeSet :: new ( ) ,
171
+ ..SignedEntityConfig :: dummy ( )
172
+ } ;
173
+
174
+ let message = get_epoch_settings_message ( epoch_service, signed_entity_config)
175
+ . await
176
+ . unwrap ( ) ;
177
+
178
+ assert_eq ! ( message. cardano_transactions_signing_config, None ) ;
179
+ assert_eq ! ( message. next_cardano_transactions_signing_config, None ) ;
180
+ }
181
+
102
182
#[ tokio:: test]
103
183
async fn test_epoch_settings_get_ok ( ) {
104
184
let method = Method :: GET . as_str ( ) ;
0 commit comments