@@ -56,24 +56,24 @@ pub const MINIMUM_SLOTS_PER_EPOCH: u64 = 32;
5656#[ derive( Debug , CloneZeroed , PartialEq , Eq ) ]
5757pub struct EpochSchedule {
5858 /// The maximum number of slots in each epoch.
59- pub slots_per_epoch : u64 ,
59+ slots_per_epoch : [ u8 ; 8 ] ,
6060
6161 /// A number of slots before beginning of an epoch to calculate
6262 /// a leader schedule for that epoch.
63- pub leader_schedule_slot_offset : u64 ,
63+ leader_schedule_slot_offset : [ u8 ; 8 ] ,
6464
6565 /// Whether epochs start short and grow.
66- pub warmup : bool ,
66+ pub warmup : u8 ,
6767
6868 /// The first epoch after the warmup period.
6969 ///
7070 /// Basically: `log2(slots_per_epoch) - log2(MINIMUM_SLOTS_PER_EPOCH)`.
71- pub first_normal_epoch : u64 ,
71+ first_normal_epoch : [ u8 ; 8 ] ,
7272
7373 /// The first slot after the warmup period.
7474 ///
7575 /// Basically: `MINIMUM_SLOTS_PER_EPOCH * (2.pow(first_normal_epoch) - 1)`.
76- pub first_normal_slot : u64 ,
76+ first_normal_slot : [ u8 ; 8 ] ,
7777}
7878
7979impl Default for EpochSchedule {
@@ -87,6 +87,30 @@ impl Default for EpochSchedule {
8787}
8888
8989impl EpochSchedule {
90+ pub fn slots_per_epoch ( & self ) -> u64 {
91+ u64:: from_le_bytes ( self . slots_per_epoch )
92+ }
93+
94+ pub fn leader_schedule_slot_offset ( & self ) -> u64 {
95+ u64:: from_le_bytes ( self . leader_schedule_slot_offset )
96+ }
97+
98+ pub fn warmup ( & self ) -> bool {
99+ match self . warmup {
100+ 0 => false ,
101+ 1 => true ,
102+ _ => panic ! ( "invalid warmup value" ) ,
103+ }
104+ }
105+
106+ pub fn first_normal_epoch ( & self ) -> u64 {
107+ u64:: from_le_bytes ( self . first_normal_epoch )
108+ }
109+
110+ pub fn first_normal_slot ( & self ) -> u64 {
111+ u64:: from_le_bytes ( self . first_normal_slot )
112+ }
113+
90114 pub fn new ( slots_per_epoch : u64 ) -> Self {
91115 Self :: custom ( slots_per_epoch, slots_per_epoch, true )
92116 }
@@ -113,40 +137,40 @@ impl EpochSchedule {
113137 ( 0 , 0 )
114138 } ;
115139 EpochSchedule {
116- slots_per_epoch,
117- leader_schedule_slot_offset,
118- warmup,
119- first_normal_epoch,
120- first_normal_slot,
140+ slots_per_epoch : slots_per_epoch . to_le_bytes ( ) ,
141+ leader_schedule_slot_offset : leader_schedule_slot_offset . to_le_bytes ( ) ,
142+ warmup : warmup as u8 ,
143+ first_normal_epoch : first_normal_epoch . to_le_bytes ( ) ,
144+ first_normal_slot : first_normal_slot . to_le_bytes ( ) ,
121145 }
122146 }
123147
124148 /// get the length of the given epoch (in slots)
125149 pub fn get_slots_in_epoch ( & self , epoch : u64 ) -> u64 {
126- if epoch < self . first_normal_epoch {
150+ if epoch < self . first_normal_epoch ( ) {
127151 2u64 . saturating_pow (
128152 ( epoch as u32 ) . saturating_add ( MINIMUM_SLOTS_PER_EPOCH . trailing_zeros ( ) ) ,
129153 )
130154 } else {
131- self . slots_per_epoch
155+ self . slots_per_epoch ( )
132156 }
133157 }
134158
135159 /// get the epoch for which the given slot should save off
136160 /// information about stakers
137161 pub fn get_leader_schedule_epoch ( & self , slot : u64 ) -> u64 {
138- if slot < self . first_normal_slot {
162+ if slot < self . first_normal_slot ( ) {
139163 // until we get to normal slots, behave as if leader_schedule_slot_offset == slots_per_epoch
140164 self . get_epoch_and_slot_index ( slot) . 0 . saturating_add ( 1 )
141165 } else {
142- let new_slots_since_first_normal_slot = slot. saturating_sub ( self . first_normal_slot ) ;
143- let new_first_normal_leader_schedule_slot =
144- new_slots_since_first_normal_slot . saturating_add ( self . leader_schedule_slot_offset ) ;
166+ let new_slots_since_first_normal_slot = slot. saturating_sub ( self . first_normal_slot ( ) ) ;
167+ let new_first_normal_leader_schedule_slot = new_slots_since_first_normal_slot
168+ . saturating_add ( self . leader_schedule_slot_offset ( ) ) ;
145169 let new_epochs_since_first_normal_leader_schedule =
146170 new_first_normal_leader_schedule_slot
147- . checked_div ( self . slots_per_epoch )
171+ . checked_div ( self . slots_per_epoch ( ) )
148172 . unwrap_or ( 0 ) ;
149- self . first_normal_epoch
173+ self . first_normal_epoch ( )
150174 . saturating_add ( new_epochs_since_first_normal_leader_schedule)
151175 }
152176 }
@@ -158,7 +182,7 @@ impl EpochSchedule {
158182
159183 /// get epoch and offset into the epoch for the given slot
160184 pub fn get_epoch_and_slot_index ( & self , slot : u64 ) -> ( u64 , u64 ) {
161- if slot < self . first_normal_slot {
185+ if slot < self . first_normal_slot ( ) {
162186 let epoch = slot
163187 . saturating_add ( MINIMUM_SLOTS_PER_EPOCH )
164188 . saturating_add ( 1 )
@@ -175,28 +199,28 @@ impl EpochSchedule {
175199 slot. saturating_sub ( epoch_len. saturating_sub ( MINIMUM_SLOTS_PER_EPOCH ) ) ,
176200 )
177201 } else {
178- let normal_slot_index = slot. saturating_sub ( self . first_normal_slot ) ;
202+ let normal_slot_index = slot. saturating_sub ( self . first_normal_slot ( ) ) ;
179203 let normal_epoch_index = normal_slot_index
180- . checked_div ( self . slots_per_epoch )
204+ . checked_div ( self . slots_per_epoch ( ) )
181205 . unwrap_or ( 0 ) ;
182- let epoch = self . first_normal_epoch . saturating_add ( normal_epoch_index) ;
206+ let epoch = self . first_normal_epoch ( ) . saturating_add ( normal_epoch_index) ;
183207 let slot_index = normal_slot_index
184- . checked_rem ( self . slots_per_epoch )
208+ . checked_rem ( self . slots_per_epoch ( ) )
185209 . unwrap_or ( 0 ) ;
186210 ( epoch, slot_index)
187211 }
188212 }
189213
190214 pub fn get_first_slot_in_epoch ( & self , epoch : u64 ) -> u64 {
191- if epoch <= self . first_normal_epoch {
215+ if epoch <= self . first_normal_epoch ( ) {
192216 2u64 . saturating_pow ( epoch as u32 )
193217 . saturating_sub ( 1 )
194218 . saturating_mul ( MINIMUM_SLOTS_PER_EPOCH )
195219 } else {
196220 epoch
197- . saturating_sub ( self . first_normal_epoch )
198- . saturating_mul ( self . slots_per_epoch )
199- . saturating_add ( self . first_normal_slot )
221+ . saturating_sub ( self . first_normal_epoch ( ) )
222+ . saturating_mul ( self . slots_per_epoch ( ) )
223+ . saturating_add ( self . first_normal_slot ( ) )
200224 }
201225 }
202226
@@ -271,13 +295,7 @@ mod tests {
271295
272296 #[ test]
273297 fn test_clone ( ) {
274- let epoch_schedule = EpochSchedule {
275- slots_per_epoch : 1 ,
276- leader_schedule_slot_offset : 2 ,
277- warmup : true ,
278- first_normal_epoch : 4 ,
279- first_normal_slot : 5 ,
280- } ;
298+ let epoch_schedule = EpochSchedule :: custom ( 1 , 2 , true ) ;
281299 #[ allow( clippy:: clone_on_copy) ]
282300 let cloned_epoch_schedule = epoch_schedule. clone ( ) ;
283301 assert_eq ! ( cloned_epoch_schedule, epoch_schedule) ;
0 commit comments