mithril_common/crypto_helper/
conversions.rs

1use super::super::entities;
2use super::types;
3use crate::crypto_helper::{ProtocolPartyId, ProtocolStake};
4
5impl From<types::ProtocolParameters> for entities::ProtocolParameters {
6    fn from(other: types::ProtocolParameters) -> Self {
7        entities::ProtocolParameters::new(other.k, other.m, other.phi_f)
8    }
9}
10
11impl From<entities::ProtocolParameters> for types::ProtocolParameters {
12    fn from(other: entities::ProtocolParameters) -> Self {
13        types::ProtocolParameters {
14            k: other.k,
15            m: other.m,
16            phi_f: other.phi_f,
17        }
18    }
19}
20
21impl From<&entities::SignerWithStake> for (types::ProtocolPartyId, types::ProtocolStake) {
22    fn from(other: &entities::SignerWithStake) -> Self {
23        (
24            other.party_id.clone() as ProtocolPartyId,
25            other.stake as ProtocolStake,
26        )
27    }
28}
29
30#[cfg(test)]
31pub mod tests {
32
33    use crate::test_utils::MithrilFixtureBuilder;
34
35    use super::*;
36
37    #[test]
38    fn test_protocol_parameters_from_into() {
39        let protocol_parameters_expected = types::ProtocolParameters {
40            k: 100,
41            m: 1000,
42            phi_f: 1.0,
43        };
44        let protocol_initializer_entities_expected = entities::ProtocolParameters::new(
45            protocol_parameters_expected.k,
46            protocol_parameters_expected.m,
47            protocol_parameters_expected.phi_f,
48        );
49
50        let protocol_initializer_entities_into: entities::ProtocolParameters =
51            protocol_parameters_expected.into();
52        assert_eq!(
53            protocol_initializer_entities_expected,
54            protocol_initializer_entities_into
55        );
56
57        let protocol_initializer_from: types::ProtocolParameters =
58            protocol_initializer_entities_expected.into();
59        assert_eq!(protocol_parameters_expected, protocol_initializer_from);
60    }
61
62    #[test]
63    fn test_stake_distribution_from_into() {
64        let stake_expected = (
65            "1".to_string() as types::ProtocolPartyId,
66            100 as types::ProtocolStake,
67        );
68        let verification_key = MithrilFixtureBuilder::default()
69            .with_signers(1)
70            .build()
71            .signers_with_stake()[0]
72            .verification_key;
73        let signer_with_stake_expected = &entities::SignerWithStake::new(
74            "1".to_string(),
75            verification_key,
76            None,
77            None,
78            None,
79            100,
80        );
81
82        let signer_with_stake_expected_into: (types::ProtocolPartyId, types::ProtocolStake) =
83            signer_with_stake_expected.into();
84        assert_eq!(stake_expected, signer_with_stake_expected_into);
85    }
86}