Skip to content

Commit a52e282

Browse files
authored
Merge pull request #1571 from input-output-hk/ensemble/1536/provide-latest-immutable-file-number-with-certified-ctx
Provide latest immutable file number with certified ctx
2 parents 9622126 + 98e7ee5 commit a52e282

File tree

227 files changed

+2945
-4324
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

227 files changed

+2945
-4324
lines changed

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

Lines changed: 64 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,34 @@ 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,
106+
transactions_set_proofs,
107+
transaction_hashes,
108+
)?;
109+
110+
Ok(message)
111+
}
101112
}
102113

103114
#[cfg(test)]
104115
mod tests {
105116
use super::*;
106117
use std::vec;
107118

108-
use mithril_common::test_utils::apispec::APISpec;
119+
use mithril_common::{entities::Beacon, test_utils::apispec::APISpec};
109120

110121
use anyhow::anyhow;
111122
use mithril_common::entities::{
@@ -136,6 +147,42 @@ mod tests {
136147
.and(routes(dependency_manager).with(cors))
137148
}
138149

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

mithril-aggregator/src/message_adapters/to_cardano_transactions_proof_message.rs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use mithril_common::entities::{CardanoTransactionsSnapshot, SignedEntity};
12
use mithril_common::messages::CardanoTransactionsSetProofMessagePart;
23
use mithril_common::{
34
entities::{CardanoTransactionsSetProof, TransactionHash},
@@ -11,7 +12,7 @@ pub struct ToCardanoTransactionsProofsMessageAdapter;
1112
impl ToCardanoTransactionsProofsMessageAdapter {
1213
/// Turn an entity instance into message.
1314
pub fn try_adapt(
14-
certificate_hash: &str,
15+
signed_entity: SignedEntity<CardanoTransactionsSnapshot>,
1516
transactions_set_proofs: Vec<CardanoTransactionsSetProof>,
1617
transaction_hashes_to_certify: Vec<TransactionHash>,
1718
) -> StdResult<CardanoTransactionsProofsMessage> {
@@ -21,9 +22,10 @@ impl ToCardanoTransactionsProofsMessageAdapter {
2122
);
2223

2324
Ok(CardanoTransactionsProofsMessage::new(
24-
certificate_hash,
25+
&signed_entity.certificate_id,
2526
try_adapt_set_proof_message(transactions_set_proofs)?,
2627
transactions_hashes_not_certified,
28+
signed_entity.artifact.beacon.immutable_file_number,
2729
))
2830
}
2931
}
@@ -76,30 +78,33 @@ mod tests {
7678
let transactions_hashes_certified = &transaction_hashes[0..5];
7779
let transactions_hashes_non_certified = &transaction_hashes[5..];
7880

79-
let mut transactions_set_proofs = Vec::new();
80-
for transaction_hashes_in_chunk in transactions_hashes_certified.chunks(2) {
81-
let mk_proof = MKProof::from_leaves(transaction_hashes_in_chunk).unwrap();
82-
transactions_set_proofs.push(CardanoTransactionsSetProof::new(
83-
transaction_hashes_in_chunk.to_vec(),
84-
mk_proof,
85-
))
86-
}
81+
let transactions_set_proofs = transactions_hashes_certified
82+
.chunks(2)
83+
.map(|transaction_hashes_in_chunk| {
84+
let mk_proof = MKProof::from_leaves(transaction_hashes_in_chunk).unwrap();
85+
CardanoTransactionsSetProof::new(transaction_hashes_in_chunk.to_vec(), mk_proof)
86+
})
87+
.collect::<Vec<_>>();
88+
89+
let signed_entity = SignedEntity::<CardanoTransactionsSnapshot>::dummy();
8790

88-
let certificate_hash = "certificate_hash";
8991
let message = ToCardanoTransactionsProofsMessageAdapter::try_adapt(
90-
certificate_hash,
92+
signed_entity.clone(),
9193
transactions_set_proofs.clone(),
9294
transaction_hashes.to_vec(),
9395
)
9496
.unwrap();
95-
let transactions_set_proofs = transactions_set_proofs
97+
98+
let transactions_set_proof_message_part = transactions_set_proofs
9699
.into_iter()
97100
.map(|p| p.try_into().unwrap())
98101
.collect();
102+
99103
let expected_message = CardanoTransactionsProofsMessage::new(
100-
certificate_hash,
101-
transactions_set_proofs,
104+
&signed_entity.certificate_id,
105+
transactions_set_proof_message_part,
102106
transactions_hashes_non_certified.to_vec(),
107+
signed_entity.artifact.beacon.immutable_file_number,
103108
);
104109
assert_eq!(expected_message, message);
105110
}

mithril-client/src/cardano_transaction_client.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,12 @@ mod tests {
239239
let mut aggregator_client = MockAggregatorHTTPClient::new();
240240
let certificate_hash = "cert-hash-123".to_string();
241241
let set_proof = CardanoTransactionsSetProof::dummy();
242-
let transactions_proofs =
243-
CardanoTransactionsProofs::new(&certificate_hash, vec![set_proof.clone()], vec![]);
242+
let transactions_proofs = CardanoTransactionsProofs::new(
243+
&certificate_hash,
244+
vec![set_proof.clone()],
245+
vec![],
246+
99999,
247+
);
244248
let expected_transactions_proofs = transactions_proofs.clone();
245249
aggregator_client
246250
.expect_get_content()

mithril-client/tests/extensions/fake.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ mod proof {
163163
proof: ProtocolMkProof::new(proof.clone()).to_json_hex().unwrap(),
164164
}],
165165
non_certified_transactions: vec![],
166+
latest_immutable_file_number: 9999,
166167
})
167168
.unwrap();
168169

@@ -175,6 +176,10 @@ mod proof {
175176
ProtocolMessagePartKey::CardanoTransactionsMerkleRoot,
176177
proof.root().to_hex(),
177178
);
179+
cert.protocol_message.set_message_part(
180+
ProtocolMessagePartKey::LatestImmutableFileNumber,
181+
9999.to_string(),
182+
);
178183
cert.signed_message = cert.protocol_message.compute_hash();
179184
cert
180185
};

mithril-common/src/entities/protocol_message.rs

Lines changed: 74 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ pub enum ProtocolMessagePartKey {
1818
/// aka AVK(n-1)
1919
#[serde(rename = "next_aggregate_verification_key")]
2020
NextAggregateVerificationKey,
21+
22+
/// The ProtocolMessage part key associated to the latest immutable file number signed
23+
#[serde(rename = "latest_immutable_file_number")]
24+
LatestImmutableFileNumber,
2125
}
2226

2327
impl Display for ProtocolMessagePartKey {
@@ -26,6 +30,7 @@ impl Display for ProtocolMessagePartKey {
2630
Self::SnapshotDigest => write!(f, "snapshot_digest"),
2731
Self::NextAggregateVerificationKey => write!(f, "next_aggregate_verification_key"),
2832
Self::CardanoTransactionsMerkleRoot => write!(f, "cardano_transactions_merkle_root"),
33+
Self::LatestImmutableFileNumber => write!(f, "latest_immutable_file_number"),
2934
}
3035
}
3136
}
@@ -83,32 +88,88 @@ mod tests {
8388
use super::*;
8489

8590
#[test]
86-
fn test_protocol_message_compute_hash() {
87-
let hash_expected = "71dee1e558cd647cdbc219a24b766940f568e7e8287c30a8292209ef11666e03";
88-
89-
let mut protocol_message = ProtocolMessage::new();
90-
protocol_message.set_message_part(
91-
ProtocolMessagePartKey::SnapshotDigest,
92-
"snapshot-digest-123".to_string(),
93-
);
94-
protocol_message.set_message_part(
95-
ProtocolMessagePartKey::NextAggregateVerificationKey,
96-
"next-avk-123".to_string(),
97-
);
98-
assert_eq!(hash_expected, protocol_message.compute_hash());
91+
fn test_protocol_message_compute_hash_include_next_aggregate_verification_key() {
92+
let protocol_message = build_protocol_message_reference();
93+
let hash_expected = protocol_message.compute_hash();
9994

10095
let mut protocol_message_modified = protocol_message.clone();
10196
protocol_message_modified.set_message_part(
10297
ProtocolMessagePartKey::NextAggregateVerificationKey,
10398
"next-avk-456".to_string(),
10499
);
100+
105101
assert_ne!(hash_expected, protocol_message_modified.compute_hash());
102+
}
103+
104+
#[test]
105+
fn test_protocol_message_compute_hash_include_snapshot_digest() {
106+
let protocol_message = build_protocol_message_reference();
107+
let hash_expected = protocol_message.compute_hash();
106108

107109
let mut protocol_message_modified = protocol_message.clone();
108110
protocol_message_modified.set_message_part(
109111
ProtocolMessagePartKey::SnapshotDigest,
110112
"snapshot-digest-456".to_string(),
111113
);
114+
115+
assert_ne!(hash_expected, protocol_message_modified.compute_hash());
116+
}
117+
118+
#[test]
119+
fn test_protocol_message_compute_hash_include_cardano_transactions_merkle_root() {
120+
let protocol_message = build_protocol_message_reference();
121+
let hash_expected = protocol_message.compute_hash();
122+
123+
let mut protocol_message_modified = protocol_message.clone();
124+
protocol_message_modified.set_message_part(
125+
ProtocolMessagePartKey::CardanoTransactionsMerkleRoot,
126+
"ctx-merke-root-456".to_string(),
127+
);
128+
129+
assert_ne!(hash_expected, protocol_message_modified.compute_hash());
130+
}
131+
132+
#[test]
133+
fn test_protocol_message_compute_hash_include_lastest_immutable_file_number() {
134+
let protocol_message = build_protocol_message_reference();
135+
let hash_expected = protocol_message.compute_hash();
136+
137+
let mut protocol_message_modified = protocol_message.clone();
138+
protocol_message_modified.set_message_part(
139+
ProtocolMessagePartKey::LatestImmutableFileNumber,
140+
"latest-immutable-file-number-456".to_string(),
141+
);
142+
112143
assert_ne!(hash_expected, protocol_message_modified.compute_hash());
113144
}
145+
146+
#[test]
147+
fn test_protocol_message_compute_hash_the_same_hash_with_same_protocol_message() {
148+
assert_eq!(
149+
build_protocol_message_reference().compute_hash(),
150+
build_protocol_message_reference().compute_hash()
151+
);
152+
}
153+
154+
fn build_protocol_message_reference() -> ProtocolMessage {
155+
let mut protocol_message = ProtocolMessage::new();
156+
protocol_message.set_message_part(
157+
ProtocolMessagePartKey::SnapshotDigest,
158+
"snapshot-digest-123".to_string(),
159+
);
160+
protocol_message.set_message_part(
161+
ProtocolMessagePartKey::NextAggregateVerificationKey,
162+
"next-avk-123".to_string(),
163+
);
164+
protocol_message.set_message_part(
165+
ProtocolMessagePartKey::CardanoTransactionsMerkleRoot,
166+
"ctx-merkle-root-123".to_string(),
167+
);
168+
protocol_message.set_message_part(
169+
ProtocolMessagePartKey::LatestImmutableFileNumber,
170+
"latest-immutable-file-number-123".to_string(),
171+
);
172+
173+
protocol_message
174+
}
114175
}

0 commit comments

Comments
 (0)