1
- use crate :: {
2
- NEXT_SIGNER_EPOCH_RETRIEVAL_OFFSET , SIGNER_EPOCH_RECORDING_OFFSET ,
3
- SIGNER_EPOCH_RETRIEVAL_OFFSET ,
4
- } ;
5
1
use serde:: { Deserialize , Serialize } ;
6
2
use std:: {
7
3
fmt:: { Display , Formatter } ,
@@ -16,9 +12,19 @@ use thiserror::Error;
16
12
pub struct Epoch ( pub u64 ) ;
17
13
18
14
impl Epoch {
15
+ /// The epoch offset used for signers stake distribution and verification keys retrieval.
16
+ pub const SIGNER_RETRIEVAL_OFFSET : i64 = -1 ;
17
+
18
+ /// The epoch offset used to retrieve the signers stake distribution and verification keys that's
19
+ /// currently being signed so it can be used in the next epoch.
20
+ pub const NEXT_SIGNER_RETRIEVAL_OFFSET : u64 = 0 ;
21
+
22
+ /// The epoch offset used for signers stake distribution and verification keys recording.
23
+ pub const SIGNER_RECORDING_OFFSET : u64 = 1 ;
24
+
19
25
/// Computes a new Epoch by applying an epoch offset.
20
26
///
21
- /// Will fails if the computed epoch is negative.
27
+ /// Will fail if the computed epoch is negative.
22
28
pub fn offset_by ( & self , epoch_offset : i64 ) -> Result < Self , EpochError > {
23
29
let epoch_new = self . 0 as i64 + epoch_offset;
24
30
if epoch_new < 0 {
@@ -27,19 +33,19 @@ impl Epoch {
27
33
Ok ( Epoch ( epoch_new as u64 ) )
28
34
}
29
35
30
- /// Apply the [SIGNER_EPOCH_RETRIEVAL_OFFSET] to this epoch
36
+ /// Apply the [Self:: SIGNER_EPOCH_RETRIEVAL_OFFSET] to this epoch
31
37
pub fn offset_to_signer_retrieval_epoch ( & self ) -> Result < Self , EpochError > {
32
- self . offset_by ( SIGNER_EPOCH_RETRIEVAL_OFFSET )
38
+ self . offset_by ( Self :: SIGNER_RETRIEVAL_OFFSET )
33
39
}
34
40
35
- /// Apply the [NEXT_SIGNER_EPOCH_RETRIEVAL_OFFSET] to this epoch
41
+ /// Apply the [Self:: NEXT_SIGNER_EPOCH_RETRIEVAL_OFFSET] to this epoch
36
42
pub fn offset_to_next_signer_retrieval_epoch ( & self ) -> Self {
37
- * self + NEXT_SIGNER_EPOCH_RETRIEVAL_OFFSET
43
+ * self + Self :: NEXT_SIGNER_RETRIEVAL_OFFSET
38
44
}
39
45
40
- /// Apply the [SIGNER_EPOCH_RECORDING_OFFSET] to this epoch
46
+ /// Apply the [Self:: SIGNER_EPOCH_RECORDING_OFFSET] to this epoch
41
47
pub fn offset_to_recording_epoch ( & self ) -> Self {
42
- * self + SIGNER_EPOCH_RECORDING_OFFSET
48
+ * self + Self :: SIGNER_RECORDING_OFFSET
43
49
}
44
50
}
45
51
@@ -59,22 +65,6 @@ impl Add<u64> for Epoch {
59
65
}
60
66
}
61
67
62
- impl Add < i32 > for Epoch {
63
- type Output = Self ;
64
-
65
- fn add ( self , rhs : i32 ) -> Self :: Output {
66
- Self ( self . 0 + rhs as u64 )
67
- }
68
- }
69
-
70
- impl Add < i64 > for Epoch {
71
- type Output = Self ;
72
-
73
- fn add ( self , rhs : i64 ) -> Self :: Output {
74
- Self ( self . 0 + rhs as u64 )
75
- }
76
- }
77
-
78
68
impl AddAssign for Epoch {
79
69
fn add_assign ( & mut self , rhs : Self ) {
80
70
* self = self . add ( rhs) ;
@@ -87,18 +77,6 @@ impl AddAssign<u64> for Epoch {
87
77
}
88
78
}
89
79
90
- impl AddAssign < i32 > for Epoch {
91
- fn add_assign ( & mut self , rhs : i32 ) {
92
- * self = self . add ( rhs) ;
93
- }
94
- }
95
-
96
- impl AddAssign < i64 > for Epoch {
97
- fn add_assign ( & mut self , rhs : i64 ) {
98
- * self = self . add ( rhs) ;
99
- }
100
- }
101
-
102
80
impl Sub for Epoch {
103
81
type Output = Self ;
104
82
@@ -115,22 +93,6 @@ impl Sub<u64> for Epoch {
115
93
}
116
94
}
117
95
118
- impl Sub < i32 > for Epoch {
119
- type Output = Self ;
120
-
121
- fn sub ( self , rhs : i32 ) -> Self :: Output {
122
- Self ( self . 0 - rhs as u64 )
123
- }
124
- }
125
-
126
- impl Sub < i64 > for Epoch {
127
- type Output = Self ;
128
-
129
- fn sub ( self , rhs : i64 ) -> Self :: Output {
130
- Self ( self . 0 - rhs as u64 )
131
- }
132
- }
133
-
134
96
impl SubAssign for Epoch {
135
97
fn sub_assign ( & mut self , rhs : Self ) {
136
98
* self = self . sub ( rhs) ;
@@ -143,18 +105,6 @@ impl SubAssign<u64> for Epoch {
143
105
}
144
106
}
145
107
146
- impl SubAssign < i32 > for Epoch {
147
- fn sub_assign ( & mut self , rhs : i32 ) {
148
- * self = self . sub ( rhs) ;
149
- }
150
- }
151
-
152
- impl SubAssign < i64 > for Epoch {
153
- fn sub_assign ( & mut self , rhs : i64 ) {
154
- * self = self . sub ( rhs) ;
155
- }
156
- }
157
-
158
108
impl PartialEq < u64 > for Epoch {
159
109
fn eq ( & self , other : & u64 ) -> bool {
160
110
self . 0 . eq ( other)
@@ -189,8 +139,6 @@ mod tests {
189
139
fn test_add ( ) {
190
140
assert_eq ! ( Epoch ( 4 ) , Epoch ( 1 ) + Epoch ( 3 ) ) ;
191
141
assert_eq ! ( Epoch ( 4 ) , Epoch ( 1 ) + 3_u64 ) ;
192
- assert_eq ! ( Epoch ( 4 ) , Epoch ( 1 ) + 3_i32 ) ;
193
- assert_eq ! ( Epoch ( 4 ) , Epoch ( 1 ) + 3_i64 ) ;
194
142
195
143
let mut epoch = Epoch ( 1 ) ;
196
144
epoch += Epoch ( 3 ) ;
@@ -199,22 +147,12 @@ mod tests {
199
147
let mut epoch = Epoch ( 1 ) ;
200
148
epoch += 3_u64 ;
201
149
assert_eq ! ( Epoch ( 4 ) , epoch) ;
202
-
203
- let mut epoch = Epoch ( 1 ) ;
204
- epoch += 3_i32 ;
205
- assert_eq ! ( Epoch ( 4 ) , epoch) ;
206
-
207
- let mut epoch = Epoch ( 1 ) ;
208
- epoch += 3_i64 ;
209
- assert_eq ! ( Epoch ( 4 ) , epoch) ;
210
150
}
211
151
212
152
#[ test]
213
153
fn test_sub ( ) {
214
154
assert_eq ! ( Epoch ( 8 ) , Epoch ( 14 ) - Epoch ( 6 ) ) ;
215
155
assert_eq ! ( Epoch ( 8 ) , Epoch ( 14 ) - 6_u64 ) ;
216
- assert_eq ! ( Epoch ( 8 ) , Epoch ( 14 ) - 6_i32 ) ;
217
- assert_eq ! ( Epoch ( 8 ) , Epoch ( 14 ) - 6_i64 ) ;
218
156
219
157
let mut epoch = Epoch ( 14 ) ;
220
158
epoch -= Epoch ( 6 ) ;
@@ -223,13 +161,5 @@ mod tests {
223
161
let mut epoch = Epoch ( 14 ) ;
224
162
epoch -= 6_u64 ;
225
163
assert_eq ! ( Epoch ( 8 ) , epoch) ;
226
-
227
- let mut epoch = Epoch ( 14 ) ;
228
- epoch -= 6_i32 ;
229
- assert_eq ! ( Epoch ( 8 ) , epoch) ;
230
-
231
- let mut epoch = Epoch ( 14 ) ;
232
- epoch -= 6_i64 ;
233
- assert_eq ! ( Epoch ( 8 ) , epoch) ;
234
164
}
235
165
}
0 commit comments