@@ -307,17 +307,21 @@ impl<T: EthSpec> BeaconState<T> {
307307 self . current_epoch ( ) + 1
308308 }
309309
310- // FIXME(sproul): comments
310+ /// Compute the number of committees at `slot`.
311+ ///
312+ /// Makes use of the committee cache and will fail if no cache exists for the slot's epoch.
313+ ///
314+ /// Spec v0.9.1
311315 pub fn get_committee_count_at_slot ( & self , slot : Slot ) -> Result < u64 , Error > {
312- // TODO(sproul): factor into cache at slot method?
313- let epoch = slot. epoch ( T :: slots_per_epoch ( ) ) ;
314- let relative_epoch = RelativeEpoch :: from_epoch ( self . current_epoch ( ) , epoch) ?;
315- let cache = self . cache ( relative_epoch) ?;
316+ let cache = self . committee_cache_at_slot ( slot) ?;
316317 Ok ( cache. committees_per_slot ( ) as u64 )
317318 }
318319
320+ /// Compute the number of committees in an entire epoch.
321+ ///
322+ /// Spec v0.9.1
319323 pub fn get_epoch_committee_count ( & self , relative_epoch : RelativeEpoch ) -> Result < u64 , Error > {
320- let cache = self . cache ( relative_epoch) ?;
324+ let cache = self . committee_cache ( relative_epoch) ?;
321325 Ok ( cache. epoch_committee_count ( ) as u64 )
322326 }
323327
@@ -330,7 +334,7 @@ impl<T: EthSpec> BeaconState<T> {
330334 & self ,
331335 relative_epoch : RelativeEpoch ,
332336 ) -> Result < & [ usize ] , Error > {
333- let cache = self . cache ( relative_epoch) ?;
337+ let cache = self . committee_cache ( relative_epoch) ?;
334338
335339 Ok ( & cache. active_validator_indices ( ) )
336340 }
@@ -350,13 +354,15 @@ impl<T: EthSpec> BeaconState<T> {
350354 ///
351355 /// Returns an error if that epoch is not cached, or the cache is not initialized.
352356 pub fn get_shuffling ( & self , relative_epoch : RelativeEpoch ) -> Result < & [ usize ] , Error > {
353- let cache = self . cache ( relative_epoch) ?;
357+ let cache = self . committee_cache ( relative_epoch) ?;
354358
355359 Ok ( cache. shuffling ( ) )
356360 }
357361
358362 /// Get the Beacon committee at the given slot and index.
359363 ///
364+ /// Utilises the committee cache.
365+ ///
360366 /// Spec v0.9.1
361367 pub fn get_beacon_committee (
362368 & self ,
@@ -365,18 +371,20 @@ impl<T: EthSpec> BeaconState<T> {
365371 ) -> Result < BeaconCommittee , Error > {
366372 let epoch = slot. epoch ( T :: slots_per_epoch ( ) ) ;
367373 let relative_epoch = RelativeEpoch :: from_epoch ( self . current_epoch ( ) , epoch) ?;
368- let cache = self . cache ( relative_epoch) ?;
374+ let cache = self . committee_cache ( relative_epoch) ?;
369375
370376 cache
371377 . get_beacon_committee ( slot, index)
372378 . ok_or ( Error :: NoCommittee { slot, index } )
373379 }
374380
375- // TODO(sproul): optimise?
381+ /// Get all of the Beacon committees at a given slot.
382+ ///
383+ /// Utilises the committee cache.
384+ ///
385+ /// Spec v0.9.1
376386 pub fn get_beacon_committees_at_slot ( & self , slot : Slot ) -> Result < Vec < BeaconCommittee > , Error > {
377- let epoch = slot. epoch ( T :: slots_per_epoch ( ) ) ;
378- let relative_epoch = RelativeEpoch :: from_epoch ( self . current_epoch ( ) , epoch) ?;
379- let cache = self . cache ( relative_epoch) ?;
387+ let cache = self . committee_cache_at_slot ( slot) ?;
380388 cache. get_beacon_committees_at_slot ( slot)
381389 }
382390
@@ -697,7 +705,8 @@ impl<T: EthSpec> BeaconState<T> {
697705 pub fn get_churn_limit ( & self , spec : & ChainSpec ) -> Result < u64 , Error > {
698706 Ok ( std:: cmp:: max (
699707 spec. min_per_epoch_churn_limit ,
700- self . cache ( RelativeEpoch :: Current ) ?. active_validator_count ( ) as u64
708+ self . committee_cache ( RelativeEpoch :: Current ) ?
709+ . active_validator_count ( ) as u64
701710 / spec. churn_limit_quotient ,
702711 ) )
703712 }
@@ -713,7 +722,7 @@ impl<T: EthSpec> BeaconState<T> {
713722 validator_index : usize ,
714723 relative_epoch : RelativeEpoch ,
715724 ) -> Result < Option < AttestationDuty > , Error > {
716- let cache = self . cache ( relative_epoch) ?;
725+ let cache = self . committee_cache ( relative_epoch) ?;
717726
718727 Ok ( cache. get_attestation_duties ( validator_index) )
719728 }
@@ -760,7 +769,7 @@ impl<T: EthSpec> BeaconState<T> {
760769 relative_epoch : RelativeEpoch ,
761770 spec : & ChainSpec ,
762771 ) -> Result < ( ) , Error > {
763- let i = Self :: cache_index ( relative_epoch) ;
772+ let i = Self :: committee_cache_index ( relative_epoch) ;
764773
765774 if self . committee_caches [ i]
766775 . is_initialized_at ( relative_epoch. into_epoch ( self . current_epoch ( ) ) )
@@ -779,7 +788,7 @@ impl<T: EthSpec> BeaconState<T> {
779788 ) -> Result < ( ) , Error > {
780789 let epoch = relative_epoch. into_epoch ( self . current_epoch ( ) ) ;
781790
782- self . committee_caches [ Self :: cache_index ( relative_epoch) ] =
791+ self . committee_caches [ Self :: committee_cache_index ( relative_epoch) ] =
783792 CommitteeCache :: initialized ( & self , epoch, spec) ?;
784793 Ok ( ( ) )
785794 }
@@ -790,28 +799,36 @@ impl<T: EthSpec> BeaconState<T> {
790799 ///
791800 /// Note: whilst this function will preserve already-built caches, it will not build any.
792801 pub fn advance_caches ( & mut self ) {
793- let next = Self :: cache_index ( RelativeEpoch :: Previous ) ;
794- let current = Self :: cache_index ( RelativeEpoch :: Current ) ;
802+ let next = Self :: committee_cache_index ( RelativeEpoch :: Previous ) ;
803+ let current = Self :: committee_cache_index ( RelativeEpoch :: Current ) ;
795804
796805 let caches = & mut self . committee_caches [ ..] ;
797806 caches. rotate_left ( 1 ) ;
798807 caches[ next] = CommitteeCache :: default ( ) ;
799808 caches[ current] = CommitteeCache :: default ( ) ;
800809 }
801810
802- fn cache_index ( relative_epoch : RelativeEpoch ) -> usize {
811+ fn committee_cache_index ( relative_epoch : RelativeEpoch ) -> usize {
803812 match relative_epoch {
804813 RelativeEpoch :: Previous => 0 ,
805814 RelativeEpoch :: Current => 1 ,
806815 RelativeEpoch :: Next => 2 ,
807816 }
808817 }
809818
819+ /// Get the committee cache for some `slot`.
820+ ///
821+ /// Return an error if the cache for the slot's epoch is not initialized.
822+ fn committee_cache_at_slot ( & self , slot : Slot ) -> Result < & CommitteeCache , Error > {
823+ let epoch = slot. epoch ( T :: slots_per_epoch ( ) ) ;
824+ let relative_epoch = RelativeEpoch :: from_epoch ( self . current_epoch ( ) , epoch) ?;
825+ self . committee_cache ( relative_epoch)
826+ }
827+
810828 /// Returns the cache for some `RelativeEpoch`. Returns an error if the cache has not been
811829 /// initialized.
812- // FIXME(sproul): rename to commitee_cache
813- fn cache ( & self , relative_epoch : RelativeEpoch ) -> Result < & CommitteeCache , Error > {
814- let cache = & self . committee_caches [ Self :: cache_index ( relative_epoch) ] ;
830+ fn committee_cache ( & self , relative_epoch : RelativeEpoch ) -> Result < & CommitteeCache , Error > {
831+ let cache = & self . committee_caches [ Self :: committee_cache_index ( relative_epoch) ] ;
815832
816833 if cache. is_initialized_at ( relative_epoch. into_epoch ( self . current_epoch ( ) ) ) {
817834 Ok ( cache)
@@ -822,7 +839,8 @@ impl<T: EthSpec> BeaconState<T> {
822839
823840 /// Drops the cache, leaving it in an uninitialized state.
824841 fn drop_committee_cache ( & mut self , relative_epoch : RelativeEpoch ) {
825- self . committee_caches [ Self :: cache_index ( relative_epoch) ] = CommitteeCache :: default ( ) ;
842+ self . committee_caches [ Self :: committee_cache_index ( relative_epoch) ] =
843+ CommitteeCache :: default ( ) ;
826844 }
827845
828846 /// Updates the pubkey cache, if required.
0 commit comments