mithril_aggregator/message_adapters/
to_cardano_transactions_proof_message.rs

1use mithril_common::{
2    entities::{CardanoTransactionsSetProof, CardanoTransactionsSnapshot, TransactionHash},
3    messages::{CardanoTransactionsProofsMessage, CardanoTransactionsSetProofMessagePart},
4    signable_builder::SignedEntity,
5    StdResult,
6};
7
8/// Adapter to spawn [CardanoTransactionsProofsMessage] from [CardanoTransactionsProofs] instances.
9pub struct ToCardanoTransactionsProofsMessageAdapter;
10
11impl ToCardanoTransactionsProofsMessageAdapter {
12    /// Turn an entity instance into message.
13    pub fn try_adapt(
14        signed_entity: SignedEntity<CardanoTransactionsSnapshot>,
15        transactions_set_proofs: Vec<CardanoTransactionsSetProof>,
16        transaction_hashes_to_certify: Vec<TransactionHash>,
17    ) -> StdResult<CardanoTransactionsProofsMessage> {
18        let transactions_hashes_not_certified = compute_not_certified_transactions(
19            &transactions_set_proofs,
20            &transaction_hashes_to_certify,
21        );
22
23        Ok(CardanoTransactionsProofsMessage::new(
24            &signed_entity.certificate_id,
25            try_adapt_set_proof_message(transactions_set_proofs)?,
26            transactions_hashes_not_certified,
27            signed_entity.artifact.block_number,
28        ))
29    }
30}
31
32fn compute_not_certified_transactions(
33    transactions_set_proofs: &[CardanoTransactionsSetProof],
34    transaction_hashes_to_certify: &[TransactionHash],
35) -> Vec<TransactionHash> {
36    let transactions_hashes_certified = transactions_set_proofs
37        .iter()
38        .flat_map(|proof| proof.transactions_hashes().to_vec())
39        .collect::<Vec<_>>();
40
41    transaction_hashes_to_certify
42        .iter()
43        .filter(|hash| !transactions_hashes_certified.contains(hash))
44        .cloned()
45        .collect()
46}
47
48fn try_adapt_set_proof_message(
49    transactions_set_proofs: Vec<CardanoTransactionsSetProof>,
50) -> StdResult<Vec<CardanoTransactionsSetProofMessagePart>> {
51    let mut messages = vec![];
52
53    for set_proof in transactions_set_proofs {
54        messages.push(set_proof.try_into()?);
55    }
56
57    Ok(messages)
58}
59
60#[cfg(test)]
61mod tests {
62    use mithril_common::crypto_helper::MKProof;
63
64    use super::*;
65
66    #[test]
67    fn try_adapt_cardano_transaction_proof_to_message() {
68        let transaction_hashes = &[
69            "tx-1".to_string(),
70            "tx-2".to_string(),
71            "tx-3".to_string(),
72            "tx-4".to_string(),
73            "tx-5".to_string(),
74            "tx-6".to_string(),
75            "tx-7".to_string(),
76        ];
77        let transactions_hashes_certified = &transaction_hashes[0..5];
78        let transactions_hashes_non_certified = &transaction_hashes[5..];
79
80        let transactions_set_proofs = transactions_hashes_certified
81            .chunks(2)
82            .map(|transaction_hashes_in_chunk| {
83                let mk_proof = MKProof::from_leaves(transaction_hashes_in_chunk).unwrap();
84                CardanoTransactionsSetProof::new(transaction_hashes_in_chunk.to_vec(), mk_proof)
85            })
86            .collect::<Vec<_>>();
87
88        let signed_entity = SignedEntity::<CardanoTransactionsSnapshot>::dummy();
89
90        let message = ToCardanoTransactionsProofsMessageAdapter::try_adapt(
91            signed_entity.clone(),
92            transactions_set_proofs.clone(),
93            transaction_hashes.to_vec(),
94        )
95        .unwrap();
96
97        let transactions_set_proof_message_part = transactions_set_proofs
98            .into_iter()
99            .map(|p| p.try_into().unwrap())
100            .collect();
101
102        let expected_message = CardanoTransactionsProofsMessage::new(
103            &signed_entity.certificate_id,
104            transactions_set_proof_message_part,
105            transactions_hashes_non_certified.to_vec(),
106            signed_entity.artifact.block_number,
107        );
108        assert_eq!(expected_message, message);
109    }
110}