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 {
@@ -16,93 +24,83 @@ fn epoch_settings(
16
24
warp:: path!( "epoch-settings" )
17
25
. and ( warp:: get ( ) )
18
26
. and ( middlewares:: with_epoch_service ( dependency_manager. clone ( ) ) )
19
- . and ( middlewares:: with_signed_entity_config (
20
- dependency_manager. clone ( ) ,
21
- ) )
22
- . and ( middlewares:: with_config ( dependency_manager) )
27
+ . and ( middlewares:: with_signed_entity_config ( dependency_manager) )
23
28
. and_then ( handlers:: epoch_settings)
24
29
}
25
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
+
26
62
mod handlers {
63
+ use slog_scope:: debug;
27
64
use std:: convert:: Infallible ;
28
-
29
- use slog_scope:: { debug, warn} ;
30
65
use warp:: http:: StatusCode ;
31
66
32
- use mithril_common:: {
33
- entities:: { SignedEntityConfig , SignedEntityTypeDiscriminants } ,
34
- messages:: { EpochSettingsMessage , SignerMessagePart } ,
35
- } ;
67
+ use mithril_common:: entities:: SignedEntityConfig ;
36
68
69
+ use crate :: dependency_injection:: EpochServiceWrapper ;
70
+ use crate :: http_server:: routes:: epoch_routes:: get_epoch_settings_message;
37
71
use crate :: http_server:: routes:: reply;
38
- use crate :: { dependency_injection:: EpochServiceWrapper , Configuration } ;
39
72
40
73
/// Epoch Settings
41
74
pub async fn epoch_settings (
42
75
epoch_service : EpochServiceWrapper ,
43
76
signed_entity_config : SignedEntityConfig ,
44
- configuration : Configuration ,
45
77
) -> Result < impl warp:: Reply , Infallible > {
46
78
debug ! ( "⇄ HTTP SERVER: epoch_settings" ) ;
47
- let epoch_service = epoch_service. read ( ) . await ;
48
-
49
- let cardano_transactions_signing_config = signed_entity_config
50
- . list_allowed_signed_entity_types_discriminants ( )
51
- . contains ( & SignedEntityTypeDiscriminants :: CardanoTransactions )
52
- . then_some ( configuration. cardano_transactions_signing_config ) ;
53
-
54
- match (
55
- epoch_service. epoch_of_current_data ( ) ,
56
- epoch_service. next_protocol_parameters ( ) ,
57
- epoch_service. upcoming_protocol_parameters ( ) ,
58
- epoch_service. current_signers ( ) ,
59
- epoch_service. next_signers ( ) ,
60
- ) {
61
- (
62
- Ok ( epoch) ,
63
- Ok ( protocol_parameters) ,
64
- Ok ( next_protocol_parameters) ,
65
- Ok ( current_signers) ,
66
- Ok ( next_signers) ,
67
- ) => {
68
- let epoch_settings_message = EpochSettingsMessage {
69
- epoch,
70
- protocol_parameters : protocol_parameters. clone ( ) ,
71
- next_protocol_parameters : next_protocol_parameters. clone ( ) ,
72
- current_signers : SignerMessagePart :: from_signers ( current_signers. to_vec ( ) ) ,
73
- next_signers : SignerMessagePart :: from_signers ( next_signers. to_vec ( ) ) ,
74
- cardano_transactions_signing_config : cardano_transactions_signing_config
75
- . clone ( ) ,
76
- next_cardano_transactions_signing_config : cardano_transactions_signing_config,
77
- } ;
78
- Ok ( reply:: json ( & epoch_settings_message, StatusCode :: OK ) )
79
- }
80
- ( Err ( err) , _, _, _, _)
81
- | ( _, Err ( err) , _, _, _)
82
- | ( _, _, Err ( err) , _, _)
83
- | ( _, _, _, Err ( err) , _)
84
- | ( _, _, _, _, Err ( err) ) => {
85
- warn ! ( "epoch_settings::error" ; "error" => ?err) ;
86
- Ok ( reply:: server_error ( err) )
87
- }
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) ) ,
88
85
}
89
86
}
90
87
}
91
88
92
89
#[ cfg( test) ]
93
90
mod tests {
94
- use std:: collections:: BTreeSet ;
95
-
96
91
use serde_json:: Value :: Null ;
92
+ use std:: collections:: BTreeSet ;
97
93
use tokio:: sync:: RwLock ;
98
- use warp:: http:: { Method , StatusCode } ;
99
- use warp:: test:: request;
94
+ use warp:: {
95
+ http:: { Method , StatusCode } ,
96
+ test:: request,
97
+ } ;
100
98
101
99
use mithril_common:: {
102
100
entities:: {
103
- BlockNumber , CardanoTransactionsSigningConfig , Epoch , SignedEntityTypeDiscriminants ,
101
+ BlockNumber , CardanoTransactionsSigningConfig , Epoch , SignedEntityConfig ,
102
+ SignedEntityTypeDiscriminants ,
104
103
} ,
105
- messages:: EpochSettingsMessage ,
106
104
test_utils:: { apispec:: APISpec , MithrilFixtureBuilder } ,
107
105
} ;
108
106
@@ -126,109 +124,76 @@ mod tests {
126
124
}
127
125
128
126
#[ tokio:: test]
129
- async fn test_epoch_settings_get_ok ( ) {
130
- let method = Method :: GET . as_str ( ) ;
131
- let path = "/epoch-settings" ;
132
- let mut dependency_manager = initialize_dependencies ( ) . await ;
133
- let fixture = MithrilFixtureBuilder :: default ( ) . with_signers ( 5 ) . build ( ) ;
134
- let epoch_service = FakeEpochService :: from_fixture ( Epoch ( 5 ) , & fixture) ;
135
- dependency_manager. epoch_service = Arc :: new ( RwLock :: new ( epoch_service) ) ;
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) ) ;
136
131
137
- let response = request ( )
138
- . method ( method)
139
- . path ( & format ! ( "/{SERVER_BASE_PATH}{path}" ) )
140
- . reply ( & setup_router ( Arc :: new ( dependency_manager) ) )
141
- . await ;
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
+ } ;
142
143
143
- APISpec :: verify_conformity (
144
- APISpec :: get_all_spec_files ( ) ,
145
- method,
146
- path,
147
- "application/json" ,
148
- & Null ,
149
- & response,
150
- & StatusCode :: OK ,
151
- )
152
- . unwrap ( ) ;
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
+ ) ;
153
156
}
154
157
155
158
#[ tokio:: test]
156
- async fn test_epoch_settings_get_ok_with_cardano_transactions_enabled ( ) {
157
- let method = Method :: GET . as_str ( ) ;
158
- let path = "/epoch-settings" ;
159
- let mut dependency_manager = initialize_dependencies ( ) . await ;
160
- let fixture = MithrilFixtureBuilder :: default ( ) . with_signers ( 5 ) . build ( ) ;
161
- let epoch_service = FakeEpochService :: from_fixture ( Epoch ( 5 ) , & fixture) ;
162
- dependency_manager. epoch_service = Arc :: new ( RwLock :: new ( epoch_service) ) ;
163
- dependency_manager
164
- . signed_entity_config
165
- . allowed_discriminants =
166
- BTreeSet :: from ( [ SignedEntityTypeDiscriminants :: CardanoTransactions ] ) ;
167
- let signing_config = CardanoTransactionsSigningConfig {
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 {
168
165
security_parameter : BlockNumber ( 70 ) ,
169
166
step : BlockNumber ( 15 ) ,
170
167
} ;
171
- dependency_manager
172
- . config
173
- . cardano_transactions_signing_config = signing_config. clone ( ) ;
174
-
175
- let response = request ( )
176
- . method ( method)
177
- . path ( & format ! ( "/{SERVER_BASE_PATH}{path}" ) )
178
- . reply ( & setup_router ( Arc :: new ( dependency_manager) ) )
179
- . await ;
180
-
181
- let response_body: EpochSettingsMessage = serde_json:: from_slice ( response. body ( ) ) . unwrap ( ) ;
168
+ let signed_entity_config = SignedEntityConfig {
169
+ cardano_transactions_signing_config,
170
+ allowed_discriminants : BTreeSet :: new ( ) ,
171
+ ..SignedEntityConfig :: dummy ( )
172
+ } ;
182
173
183
- assert_eq ! (
184
- response_body. current_cardano_transactions_signing_config,
185
- Some ( signing_config)
186
- ) ;
174
+ let message = get_epoch_settings_message ( epoch_service, signed_entity_config)
175
+ . await
176
+ . unwrap ( ) ;
187
177
188
- APISpec :: verify_conformity (
189
- APISpec :: get_all_spec_files ( ) ,
190
- method,
191
- path,
192
- "application/json" ,
193
- & Null ,
194
- & response,
195
- & StatusCode :: OK ,
196
- )
197
- . unwrap ( ) ;
178
+ assert_eq ! ( message. cardano_transactions_signing_config, None ) ;
179
+ assert_eq ! ( message. next_cardano_transactions_signing_config, None ) ;
198
180
}
199
181
200
182
#[ tokio:: test]
201
- async fn test_epoch_settings_get_ok_with_cardano_transactions_not_enabled ( ) {
183
+ async fn test_epoch_settings_get_ok ( ) {
202
184
let method = Method :: GET . as_str ( ) ;
203
185
let path = "/epoch-settings" ;
204
186
let mut dependency_manager = initialize_dependencies ( ) . await ;
205
187
let fixture = MithrilFixtureBuilder :: default ( ) . with_signers ( 5 ) . build ( ) ;
206
188
let epoch_service = FakeEpochService :: from_fixture ( Epoch ( 5 ) , & fixture) ;
207
189
dependency_manager. epoch_service = Arc :: new ( RwLock :: new ( epoch_service) ) ;
208
- dependency_manager
209
- . signed_entity_config
210
- . allowed_discriminants = BTreeSet :: new ( ) ;
211
- let signing_config = CardanoTransactionsSigningConfig {
212
- security_parameter : BlockNumber ( 70 ) ,
213
- step : BlockNumber ( 15 ) ,
214
- } ;
215
- dependency_manager
216
- . config
217
- . cardano_transactions_signing_config = signing_config. clone ( ) ;
218
190
219
191
let response = request ( )
220
192
. method ( method)
221
193
. path ( & format ! ( "/{SERVER_BASE_PATH}{path}" ) )
222
194
. reply ( & setup_router ( Arc :: new ( dependency_manager) ) )
223
195
. await ;
224
196
225
- let response_body: EpochSettingsMessage = serde_json:: from_slice ( response. body ( ) ) . unwrap ( ) ;
226
-
227
- assert_eq ! (
228
- response_body. current_cardano_transactions_signing_config,
229
- None
230
- ) ;
231
-
232
197
APISpec :: verify_conformity (
233
198
APISpec :: get_all_spec_files ( ) ,
234
199
method,
0 commit comments