Skip to content

Commit e5db8fc

Browse files
committed
feature(aggregator, openapi): handle 404 result for protocol-configuration route
1 parent aa82f6c commit e5db8fc

File tree

4 files changed

+62
-19
lines changed

4 files changed

+62
-19
lines changed

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

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ fn protocol_configuration(
2626
mod handlers {
2727
use slog::{Logger, warn};
2828
use std::{collections::BTreeSet, convert::Infallible, sync::Arc};
29+
use warp::http::StatusCode;
2930

3031
use mithril_common::entities::{Epoch, SignedEntityTypeDiscriminants};
3132

@@ -54,7 +55,11 @@ mod handlers {
5455
.await;
5556

5657
match protocol_configuration_message {
57-
Ok(message) => Ok(reply::json(&message, warp::http::StatusCode::OK)),
58+
Ok(Some(message)) => Ok(reply::json(&message, warp::http::StatusCode::OK)),
59+
Ok(None) => {
60+
warn!(logger, "protocol_configuration::not_found");
61+
Ok(reply::empty(StatusCode::NOT_FOUND))
62+
}
5863
Err(err) => {
5964
slog::warn!(logger, "protocol_configuration::error"; "error" => ?err);
6065
Ok(reply::server_error(err))
@@ -100,7 +105,7 @@ mod tests {
100105
let mut mock_http_message_service = MockMessageService::new();
101106
mock_http_message_service
102107
.expect_get_protocol_configuration_message()
103-
.return_once(|_, _| Ok(ProtocolConfigurationMessage::dummy()))
108+
.return_once(|_, _| Ok(Some(ProtocolConfigurationMessage::dummy())))
104109
.once();
105110
dependency_manager.message_service = Arc::new(mock_http_message_service);
106111

@@ -124,6 +129,38 @@ mod tests {
124129
.unwrap();
125130
}
126131

132+
#[tokio::test]
133+
async fn test_protocol_configuration_return_404_when_no_configuration_found() {
134+
let method = Method::GET.as_str();
135+
let base_path = "/protocol-configuration";
136+
let mut dependency_manager = initialize_dependencies!().await;
137+
let mut mock_http_message_service = MockMessageService::new();
138+
mock_http_message_service
139+
.expect_get_protocol_configuration_message()
140+
.return_once(|_, _| Ok(None))
141+
.once();
142+
dependency_manager.message_service = Arc::new(mock_http_message_service);
143+
144+
let response = request()
145+
.method(method)
146+
.path(&format!("{base_path}/42"))
147+
.reply(&setup_router(RouterState::new_with_dummy_config(Arc::new(
148+
dependency_manager,
149+
))))
150+
.await;
151+
152+
APISpec::verify_conformity(
153+
APISpec::get_default_spec_file_from(crate::http_server::API_SPEC_LOCATION),
154+
method,
155+
&format!("{base_path}/{{epoch}}"),
156+
"application/json",
157+
&Null,
158+
&response,
159+
&StatusCode::NOT_FOUND,
160+
)
161+
.unwrap();
162+
}
163+
127164
#[tokio::test]
128165
async fn test_protocol_configuration_get_ko_500() {
129166
let method = Method::GET.as_str();

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::ServeCommandDependenciesContainer;
22
use crate::http_server::SERVER_BASE_PATH;
33
use crate::http_server::routes::{
4-
artifact_routes, certificate_routes, epoch_routes, root_routes, signatures_routes,
5-
signer_routes, statistics_routes, status,
4+
artifact_routes, certificate_routes, epoch_routes, protocol_configuration_routes, root_routes,
5+
signatures_routes, signer_routes, statistics_routes, status,
66
};
77
use crate::tools::url_sanitizer::SanitizedUrlWithTrailingSlash;
88

@@ -140,6 +140,7 @@ pub fn routes(
140140
.or(signer_routes::routes(&state))
141141
.or(signatures_routes::routes(&state))
142142
.or(epoch_routes::routes(&state))
143+
.or(protocol_configuration_routes::routes(&state))
143144
.or(statistics_routes::routes(&state))
144145
.or(root_routes::routes(&state))
145146
.or(status::routes(&state)),

mithril-aggregator/src/services/message.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub trait MessageService: Sync + Send {
4040
&self,
4141
epoch: Epoch,
4242
allowed_discriminants: BTreeSet<SignedEntityTypeDiscriminants>,
43-
) -> StdResult<ProtocolConfigurationMessage>;
43+
) -> StdResult<Option<ProtocolConfigurationMessage>>;
4444

4545
/// Return the message representation of a certificate if it exists.
4646
async fn get_certificate_message(
@@ -198,8 +198,11 @@ impl MessageService for MithrilMessageService {
198198
&self,
199199
epoch: Epoch,
200200
enabled_discriminants: BTreeSet<SignedEntityTypeDiscriminants>,
201-
) -> StdResult<ProtocolConfigurationMessage> {
202-
let epoch_settings = self.epoch_settings_storer.get_epoch_settings(epoch).await?.unwrap();
201+
) -> StdResult<Option<ProtocolConfigurationMessage>> {
202+
let epoch_settings = match self.epoch_settings_storer.get_epoch_settings(epoch).await? {
203+
Some(settings) => settings,
204+
None => return Ok(None),
205+
};
203206

204207
let cardano_transactions_discriminant =
205208
enabled_discriminants.get(&SignedEntityTypeDiscriminants::CardanoTransactions);
@@ -212,7 +215,7 @@ impl MessageService for MithrilMessageService {
212215
cardano_transactions_signing_config,
213216
available_signed_entity_types: enabled_discriminants,
214217
};
215-
Ok(protocol_configuration_message)
218+
Ok(Some(protocol_configuration_message))
216219
}
217220

218221
async fn get_certificate_message(

openapi.yaml

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ paths:
129129
application/json:
130130
schema:
131131
$ref: "#/components/schemas/ProtocolConfigurationMessage"
132+
"404":
133+
description: protocol configuration not found
132134
default:
133135
description: protocol configuration error
134136
content:
@@ -1062,17 +1064,17 @@ components:
10621064
cardano_transactions_signing_config:
10631065
$ref: "#/components/schemas/CardanoTransactionsSigningConfig"
10641066
available_signed_entity_types:
1065-
description: Available signed entity types that are signed by the aggregator
1066-
type: array
1067-
minItems: 1
1068-
items:
1069-
description: Signed entity types that can be signed
1070-
type: string
1071-
enum:
1072-
- MithrilStakeDistribution
1073-
- CardanoStakeDistribution
1074-
- CardanoImmutableFilesFull
1075-
- CardanoTransactions
1067+
description: Available signed entity types that are signed by the aggregator
1068+
type: array
1069+
minItems: 1
1070+
items:
1071+
description: Signed entity types that can be signed
1072+
type: string
1073+
enum:
1074+
- MithrilStakeDistribution
1075+
- CardanoStakeDistribution
1076+
- CardanoImmutableFilesFull
1077+
- CardanoTransactions
10761078
examples:
10771079
- {
10781080
"protocol_parameters": { "k": 9, "m": 77, "phi_f": 0.5 },

0 commit comments

Comments
 (0)