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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
use crate::interfaces::{
    DEFAULT_ACTIVE_SLOT_COEFFICIENT, MAXIMUM_ACTIVE_SLOT_COEFFICIENT,
    MINIMUM_ACTIVE_SLOT_COEFFICIENT,
};
use chain_impl_mockchain::{config::ConfigParam, milli::Milli};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::{convert::TryFrom, fmt, str::FromStr as _};
use thiserror::Error;

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct ActiveSlotCoefficient(Milli);

impl ActiveSlotCoefficient {
    /// minimal value for the active slot coefficient
    ///
    /// ```
    /// # use jormungandr_lib::interfaces::ActiveSlotCoefficient;
    /// # use chain_impl_mockchain::milli::Milli;
    ///
    /// assert_eq!(ActiveSlotCoefficient::MINIMUM, ActiveSlotCoefficient::new(Milli::from_millis(0_001)).unwrap())
    /// ```
    pub const MINIMUM: Self =
        ActiveSlotCoefficient(Milli::from_millis(MINIMUM_ACTIVE_SLOT_COEFFICIENT));

    /// maximal value for the active slot coefficient
    ///
    /// ```
    /// # use jormungandr_lib::interfaces::ActiveSlotCoefficient;
    /// # use chain_impl_mockchain::milli::Milli;
    ///
    /// assert_eq!(ActiveSlotCoefficient::MAXIMUM, ActiveSlotCoefficient::new(Milli::from_millis(1_000)).unwrap())
    /// ```
    pub const MAXIMUM: Self =
        ActiveSlotCoefficient(Milli::from_millis(MAXIMUM_ACTIVE_SLOT_COEFFICIENT));

    pub fn new(milli: Milli) -> Option<Self> {
        if milli.to_millis() < MINIMUM_ACTIVE_SLOT_COEFFICIENT
            || MAXIMUM_ACTIVE_SLOT_COEFFICIENT < milli.to_millis()
        {
            None
        } else {
            Some(ActiveSlotCoefficient(milli))
        }
    }
}

#[derive(Debug, Error)]
pub enum TryFromActiveSlotCoefficientError {
    #[error("Incompatible Config param, expected active slot coefficient")]
    Incompatible,
    #[error("invalid active slot coefficient {coefficient}")]
    Invalid { coefficient: Milli },
}

impl TryFrom<ConfigParam> for ActiveSlotCoefficient {
    type Error = TryFromActiveSlotCoefficientError;
    fn try_from(config_param: ConfigParam) -> Result<Self, Self::Error> {
        match config_param {
            ConfigParam::ConsensusGenesisPraosActiveSlotsCoeff(coefficient) => {
                ActiveSlotCoefficient::new(coefficient)
                    .ok_or(TryFromActiveSlotCoefficientError::Invalid { coefficient })
            }
            _ => Err(TryFromActiveSlotCoefficientError::Incompatible),
        }
    }
}

impl From<ActiveSlotCoefficient> for ConfigParam {
    fn from(active_slot_coefficient: ActiveSlotCoefficient) -> Self {
        ConfigParam::ConsensusGenesisPraosActiveSlotsCoeff(active_slot_coefficient.0)
    }
}

impl fmt::Display for ActiveSlotCoefficient {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.0.fmt(f)
    }
}

impl Default for ActiveSlotCoefficient {
    fn default() -> Self {
        ActiveSlotCoefficient::new(Milli::from_millis(DEFAULT_ACTIVE_SLOT_COEFFICIENT))
            .expect("Default should be a valid value at all time")
    }
}

impl Serialize for ActiveSlotCoefficient {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        if self.0 == Milli::ONE {
            serializer.serialize_u64(1)
        } else {
            serializer.serialize_str(&self.0.to_string())
        }
    }
}

impl<'de> Deserialize<'de> for ActiveSlotCoefficient {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        use serde::de::{self, Visitor};
        struct ActiveSlotCoefficientVisitor;
        impl<'de> Visitor<'de> for ActiveSlotCoefficientVisitor {
            type Value = ActiveSlotCoefficient;
            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
                write!(
                    formatter,
                    "active slot coefficient within range of {} and {}",
                    ActiveSlotCoefficient::MINIMUM,
                    ActiveSlotCoefficient::MAXIMUM,
                )
            }

            fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E>
            where
                E: de::Error,
            {
                if v == 1 {
                    Ok(ActiveSlotCoefficient(Milli::ONE))
                } else {
                    Err(E::custom(format!(
                        "value out of bound, can only accept within range of {} and {}",
                        ActiveSlotCoefficient::MINIMUM,
                        ActiveSlotCoefficient::MAXIMUM,
                    )))
                }
            }

            fn visit_f64<E>(self, v: f64) -> Result<Self::Value, E>
            where
                E: de::Error,
            {
                self.visit_str(&format!("{:.3}", v))
            }

            fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
            where
                E: de::Error,
            {
                let milli = Milli::from_str(s).map_err(E::custom)?;
                let milli = milli.to_millis();

                if milli < MINIMUM_ACTIVE_SLOT_COEFFICIENT {
                    Err(E::custom(format!(
                        "cannot have active slot coefficient below {}",
                        ActiveSlotCoefficient::MINIMUM,
                    )))
                } else if MAXIMUM_ACTIVE_SLOT_COEFFICIENT < milli {
                    Err(E::custom(format!(
                        "cannot have active slot coefficient above {}",
                        ActiveSlotCoefficient::MAXIMUM,
                    )))
                } else {
                    Ok(ActiveSlotCoefficient(Milli::from_millis(milli)))
                }
            }
        }

        deserializer.deserialize_any(ActiveSlotCoefficientVisitor)
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use quickcheck::{Arbitrary, Gen};

    impl Arbitrary for ActiveSlotCoefficient {
        fn arbitrary<G: Gen>(g: &mut G) -> Self {
            use rand07::Rng as _;
            let v = g.gen_range(
                MINIMUM_ACTIVE_SLOT_COEFFICIENT,
                MAXIMUM_ACTIVE_SLOT_COEFFICIENT,
            );
            ActiveSlotCoefficient(Milli::from_millis(v))
        }
    }

    #[test]
    #[should_panic]
    fn deserialize_from_invalid_type() {
        const EXAMPLE: &str = "---\ntrue";

        let _: ActiveSlotCoefficient = serde_yaml::from_str(EXAMPLE).unwrap();
    }

    #[test]
    #[should_panic]
    fn deserialize_from_below_bounds() {
        const VALUE: u64 = MINIMUM_ACTIVE_SLOT_COEFFICIENT - 1;
        let example = format!("---\n{}", VALUE);

        let _: ActiveSlotCoefficient = serde_yaml::from_str(&example).unwrap();
    }

    #[test]
    #[should_panic]
    fn deserialize_from_above_bounds() {
        const VALUE: u64 = MAXIMUM_ACTIVE_SLOT_COEFFICIENT + 1;
        let example = format!("---\n{}", VALUE);

        let _: ActiveSlotCoefficient = serde_yaml::from_str(&example).unwrap();
    }

    #[test]
    fn deserialize_from_float() {
        const VALUE: Milli = Milli::from_millis(500);
        let example = format!("---\n{}", VALUE);

        let decoded: ActiveSlotCoefficient = serde_yaml::from_str(&example).unwrap();

        assert_eq!(decoded.0, VALUE)
    }

    #[test]
    fn deserialize_from_number() {
        const VALUE: Milli = Milli::ONE;
        let example = format!("---\n{}", 1);

        let decoded: ActiveSlotCoefficient = serde_yaml::from_str(&example).unwrap();

        assert_eq!(decoded.0, VALUE)
    }

    #[test]
    fn deserialize_from_str() {
        const VALUE: Milli = Milli::from_millis(220);
        const ACTIVE_SLOT_STR: &str = "---\n\"0.220\"";

        let decoded: ActiveSlotCoefficient = serde_yaml::from_str(ACTIVE_SLOT_STR).unwrap();

        assert_eq!(decoded.0, VALUE)
    }

    quickcheck! {
        fn serde_encode_decode(active_slot_coefficient: ActiveSlotCoefficient) -> bool {
            let s = serde_yaml::to_string(&active_slot_coefficient).unwrap();
            let active_slot_coefficient_dec: ActiveSlotCoefficient = serde_yaml::from_str(&s).unwrap();

            active_slot_coefficient == active_slot_coefficient_dec
        }

        fn convert_from_to_config_param(active_slot_coefficient: ActiveSlotCoefficient) -> bool {
            let cp = ConfigParam::from(active_slot_coefficient);
            let active_slot_coefficient_dec = ActiveSlotCoefficient::try_from(cp).unwrap();

            active_slot_coefficient == active_slot_coefficient_dec
        }
    }
}