Skip to content

Commit 72766c9

Browse files
dlachaumesfauvel
andcommitted
refactor: remove unnecessary dependencies, refactor 'epoch_settings' function by separating responsibilities
Co-authored-by: Sébastien Fauvel <[email protected]>
1 parent d4330bb commit 72766c9

File tree

1 file changed

+103
-138
lines changed

1 file changed

+103
-138
lines changed

mithril-aggregator/src/http_server/routes/epoch_routes.rs

Lines changed: 103 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1-
use crate::http_server::routes::middlewares;
2-
use crate::DependencyContainer;
31
use std::sync::Arc;
42
use warp::Filter;
53

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+
614
pub fn routes(
715
dependency_manager: Arc<DependencyContainer>,
816
) -> impl Filter<Extract = (impl warp::Reply,), Error = warp::Rejection> + Clone {
@@ -16,93 +24,83 @@ fn epoch_settings(
1624
warp::path!("epoch-settings")
1725
.and(warp::get())
1826
.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))
2328
.and_then(handlers::epoch_settings)
2429
}
2530

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+
2662
mod handlers {
63+
use slog_scope::debug;
2764
use std::convert::Infallible;
28-
29-
use slog_scope::{debug, warn};
3065
use warp::http::StatusCode;
3166

32-
use mithril_common::{
33-
entities::{SignedEntityConfig, SignedEntityTypeDiscriminants},
34-
messages::{EpochSettingsMessage, SignerMessagePart},
35-
};
67+
use mithril_common::entities::SignedEntityConfig;
3668

69+
use crate::dependency_injection::EpochServiceWrapper;
70+
use crate::http_server::routes::epoch_routes::get_epoch_settings_message;
3771
use crate::http_server::routes::reply;
38-
use crate::{dependency_injection::EpochServiceWrapper, Configuration};
3972

4073
/// Epoch Settings
4174
pub async fn epoch_settings(
4275
epoch_service: EpochServiceWrapper,
4376
signed_entity_config: SignedEntityConfig,
44-
configuration: Configuration,
4577
) -> Result<impl warp::Reply, Infallible> {
4678
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)),
8885
}
8986
}
9087
}
9188

9289
#[cfg(test)]
9390
mod tests {
94-
use std::collections::BTreeSet;
95-
9691
use serde_json::Value::Null;
92+
use std::collections::BTreeSet;
9793
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+
};
10098

10199
use mithril_common::{
102100
entities::{
103-
BlockNumber, CardanoTransactionsSigningConfig, Epoch, SignedEntityTypeDiscriminants,
101+
BlockNumber, CardanoTransactionsSigningConfig, Epoch, SignedEntityConfig,
102+
SignedEntityTypeDiscriminants,
104103
},
105-
messages::EpochSettingsMessage,
106104
test_utils::{apispec::APISpec, MithrilFixtureBuilder},
107105
};
108106

@@ -126,109 +124,76 @@ mod tests {
126124
}
127125

128126
#[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));
136131

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+
};
142143

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+
);
153156
}
154157

155158
#[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 {
168165
security_parameter: BlockNumber(70),
169166
step: BlockNumber(15),
170167
};
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+
};
182173

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();
187177

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);
198180
}
199181

200182
#[tokio::test]
201-
async fn test_epoch_settings_get_ok_with_cardano_transactions_not_enabled() {
183+
async fn test_epoch_settings_get_ok() {
202184
let method = Method::GET.as_str();
203185
let path = "/epoch-settings";
204186
let mut dependency_manager = initialize_dependencies().await;
205187
let fixture = MithrilFixtureBuilder::default().with_signers(5).build();
206188
let epoch_service = FakeEpochService::from_fixture(Epoch(5), &fixture);
207189
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();
218190

219191
let response = request()
220192
.method(method)
221193
.path(&format!("/{SERVER_BASE_PATH}{path}"))
222194
.reply(&setup_router(Arc::new(dependency_manager)))
223195
.await;
224196

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-
232197
APISpec::verify_conformity(
233198
APISpec::get_all_spec_files(),
234199
method,

0 commit comments

Comments
 (0)