logo
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
// TODO - remove once schemars stops causing warning.
#![allow(clippy::field_reassign_with_default)]

use alloc::vec::Vec;
use core::{
    fmt::{self, Debug, Display, Formatter},
    num::ParseIntError,
    ops::{Add, AddAssign, Sub},
    str::FromStr,
};

#[cfg(feature = "datasize")]
use datasize::DataSize;
use rand::{
    distributions::{Distribution, Standard},
    Rng,
};
#[cfg(feature = "json-schema")]
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::{
    bytesrepr::{self, FromBytes, ToBytes},
    CLType, CLTyped,
};

/// Era ID newtype.
#[derive(
    Debug, Default, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize,
)]
#[cfg_attr(feature = "datasize", derive(DataSize))]
#[cfg_attr(feature = "json-schema", derive(JsonSchema))]
#[serde(deny_unknown_fields)]
pub struct EraId(u64);

impl EraId {
    /// Maximum possible value an [`EraId`] can hold.
    pub const MAX: EraId = EraId(u64::max_value());

    /// Creates new [`EraId`] instance.
    pub const fn new(value: u64) -> EraId {
        EraId(value)
    }

    /// Returns an iterator over era IDs of `num_eras` future eras starting from current.
    pub fn iter(&self, num_eras: u64) -> impl Iterator<Item = EraId> {
        let current_era_id = self.0;
        (current_era_id..current_era_id + num_eras).map(EraId)
    }

    /// Returns an iterator over era IDs of `num_eras` future eras starting from current, plus the
    /// provided one.
    pub fn iter_inclusive(&self, num_eras: u64) -> impl Iterator<Item = EraId> {
        let current_era_id = self.0;
        (current_era_id..=current_era_id + num_eras).map(EraId)
    }

    /// Returns a successor to current era.
    #[allow(clippy::integer_arithmetic)] // The caller must make sure this doesn't overflow.
    pub fn successor(self) -> EraId {
        EraId::from(self.0 + 1)
    }

    /// Returns the current era plus `x`, or `None` if that would overflow
    pub fn checked_add(&self, x: u64) -> Option<EraId> {
        self.0.checked_add(x).map(EraId)
    }

    /// Returns the current era minus `x`, or `None` if that would be less than `0`.
    pub fn checked_sub(&self, x: u64) -> Option<EraId> {
        self.0.checked_sub(x).map(EraId)
    }

    /// Returns the current era minus `x`, or `0` if that would be less than `0`.
    pub fn saturating_sub(&self, x: u64) -> EraId {
        EraId::from(self.0.saturating_sub(x))
    }

    /// Returns the current era plus `x`, or [`EraId::MAX`] if overflow would occur.
    pub fn saturating_add(self, rhs: EraId) -> EraId {
        EraId(self.0.saturating_add(rhs.0))
    }

    /// Returns the current era times `x`, or [`EraId::MAX`] if overflow would occur.
    pub fn saturating_mul(&self, x: u64) -> EraId {
        EraId::from(self.0.saturating_mul(x))
    }

    /// Returns whether this is era 0.
    pub fn is_genesis(&self) -> bool {
        self.0 == 0
    }

    /// Returns little endian bytes.
    pub fn to_le_bytes(self) -> [u8; 8] {
        self.0.to_le_bytes()
    }

    /// Returns a raw value held by this [`EraId`] instance.
    ///
    /// You should prefer [`From`] trait implementations over this method where possible.
    pub fn value(self) -> u64 {
        self.0
    }
}

impl FromStr for EraId {
    type Err = ParseIntError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        u64::from_str(s).map(EraId)
    }
}

impl Add<u64> for EraId {
    type Output = EraId;

    #[allow(clippy::integer_arithmetic)] // The caller must make sure this doesn't overflow.
    fn add(self, x: u64) -> EraId {
        EraId::from(self.0 + x)
    }
}

impl AddAssign<u64> for EraId {
    fn add_assign(&mut self, x: u64) {
        self.0 += x;
    }
}

impl Sub<u64> for EraId {
    type Output = EraId;

    #[allow(clippy::integer_arithmetic)] // The caller must make sure this doesn't overflow.
    fn sub(self, x: u64) -> EraId {
        EraId::from(self.0 - x)
    }
}

impl Display for EraId {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "era {}", self.0)
    }
}

impl From<EraId> for u64 {
    fn from(era_id: EraId) -> Self {
        era_id.value()
    }
}

impl From<u64> for EraId {
    fn from(era_id: u64) -> Self {
        EraId(era_id)
    }
}

impl ToBytes for EraId {
    fn to_bytes(&self) -> Result<Vec<u8>, bytesrepr::Error> {
        self.0.to_bytes()
    }

    fn serialized_length(&self) -> usize {
        self.0.serialized_length()
    }
}

impl FromBytes for EraId {
    fn from_bytes(bytes: &[u8]) -> Result<(Self, &[u8]), bytesrepr::Error> {
        let (id_value, remainder) = u64::from_bytes(bytes)?;
        let era_id = EraId::from(id_value);
        Ok((era_id, remainder))
    }
}

impl CLTyped for EraId {
    fn cl_type() -> CLType {
        CLType::U64
    }
}

impl Distribution<EraId> for Standard {
    fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> EraId {
        EraId(rng.gen_range(0..1_000_000))
    }
}

#[cfg(test)]
mod tests {
    use proptest::prelude::*;

    use super::*;
    use crate::gens::era_id_arb;

    #[test]
    fn should_calculate_correct_inclusive_future_eras() {
        let auction_delay = 3;

        let current_era = EraId::from(42);

        let window: Vec<EraId> = current_era.iter_inclusive(auction_delay).collect();
        assert_eq!(window.len(), auction_delay as usize + 1);
        assert_eq!(window.get(0), Some(&current_era));
        assert_eq!(
            window.iter().rev().next(),
            Some(&(current_era + auction_delay))
        );
    }

    #[test]
    fn should_have_valid_genesis_era_id() {
        let expected_initial_era_id = EraId::from(0);
        assert!(expected_initial_era_id.is_genesis());
        assert!(!expected_initial_era_id.successor().is_genesis())
    }

    proptest! {
        #[test]
        fn bytesrepr_roundtrip(era_id in era_id_arb()) {
            bytesrepr::test_serialization_roundtrip(&era_id);
        }
    }
}