Skip to content

Commit da3a2e6

Browse files
Damien LACHAUME / PALO-ITsfauvel
authored andcommitted
Adapt http route /proof/cardano-transaction response
Extract the construction of the response message to make unit test on response values
1 parent 645dbc5 commit da3a2e6

File tree

1 file changed

+65
-17
lines changed

1 file changed

+65
-17
lines changed

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

Lines changed: 65 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ fn proof_cardano_transaction(
3737
}
3838

3939
mod handlers {
40+
use mithril_common::{
41+
entities::{CardanoTransactionsSnapshot, SignedEntity},
42+
messages::CardanoTransactionsProofsMessage,
43+
StdResult,
44+
};
4045
use reqwest::StatusCode;
4146
use slog_scope::{debug, warn};
4247
use std::{convert::Infallible, sync::Arc};
@@ -72,23 +77,9 @@ mod handlers {
7277
"proof_cardano_transaction::error"
7378
) {
7479
Some(signed_entity) => {
75-
let transactions_set_proofs = unwrap_to_internal_server_error!(
76-
prover_service
77-
.compute_transactions_proofs(
78-
&signed_entity.artifact.beacon,
79-
transaction_hashes.as_slice(),
80-
)
81-
.await,
82-
"proof_cardano_transaction::error"
83-
);
84-
8580
let message = unwrap_to_internal_server_error!(
86-
ToCardanoTransactionsProofsMessageAdapter::try_adapt(
87-
&signed_entity.certificate_id,
88-
transactions_set_proofs,
89-
transaction_hashes,
90-
),
91-
"proof_cardano_transaction::error"
81+
build_response_message(prover_service, signed_entity, transaction_hashes).await,
82+
"proof_cardano_transaction"
9283
);
9384
Ok(reply::json(&message, StatusCode::OK))
9485
}
@@ -98,14 +89,35 @@ mod handlers {
9889
}
9990
}
10091
}
92+
93+
pub async fn build_response_message(
94+
prover_service: Arc<dyn ProverService>,
95+
signed_entity: SignedEntity<CardanoTransactionsSnapshot>,
96+
transaction_hashes: Vec<String>,
97+
) -> StdResult<CardanoTransactionsProofsMessage> {
98+
let transactions_set_proofs = prover_service
99+
.compute_transactions_proofs(
100+
&signed_entity.artifact.beacon,
101+
transaction_hashes.as_slice(),
102+
)
103+
.await?;
104+
let message = ToCardanoTransactionsProofsMessageAdapter::try_adapt(
105+
&signed_entity.certificate_id,
106+
transactions_set_proofs,
107+
transaction_hashes,
108+
signed_entity.artifact.beacon.immutable_file_number,
109+
)?;
110+
111+
Ok(message)
112+
}
101113
}
102114

103115
#[cfg(test)]
104116
mod tests {
105117
use super::*;
106118
use std::vec;
107119

108-
use mithril_common::test_utils::apispec::APISpec;
120+
use mithril_common::{entities::Beacon, test_utils::apispec::APISpec};
109121

110122
use anyhow::anyhow;
111123
use mithril_common::entities::{
@@ -136,6 +148,42 @@ mod tests {
136148
.and(routes(dependency_manager).with(cors))
137149
}
138150

151+
#[tokio::test]
152+
async fn build_response_message_return_immutable_file_number_from_artifact_beacon() {
153+
// Arrange
154+
let mut mock_prover_service = MockProverService::new();
155+
mock_prover_service
156+
.expect_compute_transactions_proofs()
157+
.returning(|_, _| Ok(vec![CardanoTransactionsSetProof::dummy()]));
158+
159+
let cardano_transactions_snapshot = {
160+
let merkle_root = String::new();
161+
let beacon = Beacon {
162+
immutable_file_number: 2309,
163+
..Beacon::default()
164+
};
165+
CardanoTransactionsSnapshot::new(merkle_root, beacon)
166+
};
167+
168+
let signed_entity = SignedEntity::<CardanoTransactionsSnapshot> {
169+
artifact: cardano_transactions_snapshot,
170+
..SignedEntity::<CardanoTransactionsSnapshot>::dummy()
171+
};
172+
173+
// Action
174+
let transaction_hashes = vec![];
175+
let message = handlers::build_response_message(
176+
Arc::new(mock_prover_service),
177+
signed_entity,
178+
transaction_hashes,
179+
)
180+
.await
181+
.unwrap();
182+
183+
// Assert
184+
assert_eq!(message.latest_immutable_file_number, 2309)
185+
}
186+
139187
#[tokio::test]
140188
async fn proof_cardano_transaction_ok() {
141189
let config = Configuration::new_sample();

0 commit comments

Comments
 (0)