1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#![allow(deprecated)]
use mithril_common::{
    entities::{CardanoDbBeacon, CertificatePending, SignedEntityType},
    messages::{CertificatePendingMessage, SignerMessagePart},
    CardanoNetwork,
};

/// Adapter to turn [CertificatePending] instances into [CertificatePendingMessage].
pub struct ToCertificatePendingMessageAdapter;

impl ToCertificatePendingMessageAdapter {
    /// Method to trigger the conversion
    pub fn adapt(
        network: CardanoNetwork,
        certificate_pending: CertificatePending,
    ) -> CertificatePendingMessage {
        #[allow(deprecated)]
        let beacon = match &certificate_pending.signed_entity_type {
            SignedEntityType::CardanoImmutableFilesFull(beacon) => beacon.clone(),
            _ => CardanoDbBeacon::empty(),
        };

        #[allow(deprecated)]
        CertificatePendingMessage {
            epoch: certificate_pending.epoch,
            beacon: Some((beacon, network).into()),
            signed_entity_type: (certificate_pending.signed_entity_type, network).into(),
            protocol_parameters: certificate_pending.protocol_parameters,
            next_protocol_parameters: certificate_pending.next_protocol_parameters,
            signers: SignerMessagePart::from_signers(certificate_pending.signers),
            next_signers: SignerMessagePart::from_signers(certificate_pending.next_signers),
        }
    }
}

#[cfg(test)]
mod tests {
    use mithril_common::entities::{Epoch, SignedEntityType};
    use mithril_common::messages::{CardanoDbBeaconMessagePart, SignedEntityTypeMessagePart};
    use mithril_common::test_utils::fake_data;

    use super::*;

    #[test]
    fn adapt_ok() {
        let certificate_pending = fake_data::certificate_pending();
        let epoch = certificate_pending.epoch;
        let message = ToCertificatePendingMessageAdapter::adapt(
            CardanoNetwork::DevNet(11),
            certificate_pending,
        );

        assert_eq!(epoch, message.epoch);
    }

    #[test]
    fn adapt_on_cardano_immutable_files_full_signed_entity_type_ok() {
        let beacon = fake_data::beacon();
        let signed_entity_type = SignedEntityType::CardanoImmutableFilesFull(beacon.clone());
        let certificate_pending = CertificatePending {
            signed_entity_type: signed_entity_type.clone(),
            ..fake_data::certificate_pending()
        };
        let network = CardanoNetwork::DevNet(11);

        let message = ToCertificatePendingMessageAdapter::adapt(network, certificate_pending);

        let beacon_from_message = message.beacon.unwrap();
        let expected_beacon = CardanoDbBeaconMessagePart::from((beacon, network));
        assert_eq!(expected_beacon, beacon_from_message);

        let expect_signed_entity_type =
            SignedEntityTypeMessagePart::from((signed_entity_type, network));
        assert_eq!(expect_signed_entity_type, message.signed_entity_type);
    }

    #[test]
    fn adapt_on_other_than_cardano_immutable_files_full_signed_entity_type_ok() {
        let mut certificate_pending = fake_data::certificate_pending();
        let beacon = CardanoDbBeacon::new(0, 0);
        certificate_pending.signed_entity_type =
            SignedEntityType::MithrilStakeDistribution(Epoch(15));

        let message = ToCertificatePendingMessageAdapter::adapt(
            CardanoNetwork::DevNet(11),
            certificate_pending,
        );

        #[allow(deprecated)]
        let beacon_from_message = message.beacon.unwrap();
        assert_eq!(beacon, beacon_from_message);
    }

    #[test]
    #[allow(deprecated)]
    fn adapt_signers() {
        let fake_signers = fake_data::signers(5);
        let signers = fake_signers[1..3].to_vec();
        let next_signers = fake_signers[2..5].to_vec();
        let certificate_pending = CertificatePending {
            signers,
            next_signers,
            ..fake_data::certificate_pending()
        };
        let message = ToCertificatePendingMessageAdapter::adapt(
            CardanoNetwork::DevNet(11),
            certificate_pending,
        );

        assert_eq!(2, message.signers.len());
        assert_eq!(3, message.next_signers.len());
    }
}