Implement state.get_crosslink_committees_at_slot
This commit is contained in:
		
							parent
							
								
									9264ec1aa9
								
							
						
					
					
						commit
						94a39c3cb6
					
				| @ -44,6 +44,7 @@ pub enum Error { | |||||||
|     InsufficientSlashedBalances, |     InsufficientSlashedBalances, | ||||||
|     InsufficientStateRoots, |     InsufficientStateRoots, | ||||||
|     NoCommitteeForShard, |     NoCommitteeForShard, | ||||||
|  |     NoCommitteeForSlot, | ||||||
|     PubkeyCacheInconsistent, |     PubkeyCacheInconsistent, | ||||||
|     PubkeyCacheIncomplete { |     PubkeyCacheIncomplete { | ||||||
|         cache_len: usize, |         cache_len: usize, | ||||||
| @ -346,10 +347,15 @@ impl<T: EthSpec> BeaconState<T> { | |||||||
|     /// Spec v0.5.1
 |     /// Spec v0.5.1
 | ||||||
|     pub fn get_crosslink_committees_at_slot( |     pub fn get_crosslink_committees_at_slot( | ||||||
|         &self, |         &self, | ||||||
|         _slot: Slot, |         slot: Slot, | ||||||
|         _spec: &ChainSpec, |         spec: &ChainSpec, | ||||||
|     ) -> Result<&Vec<CrosslinkCommittee>, Error> { |     ) -> Result<Vec<CrosslinkCommittee>, Error> { | ||||||
|         unimplemented!("FIXME(sproul)") |         let relative_epoch = RelativeEpoch::from_slot(self.slot, slot, T::slots_per_epoch())?; | ||||||
|  |         let cache = self.cache(relative_epoch)?; | ||||||
|  | 
 | ||||||
|  |         cache | ||||||
|  |             .get_crosslink_committees_for_slot(slot) | ||||||
|  |             .ok_or_else(|| Error::NoCommitteeForSlot) | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     /// Returns the crosslink committees for some shard in some cached epoch.
 |     /// Returns the crosslink committees for some shard in some cached epoch.
 | ||||||
|  | |||||||
| @ -150,18 +150,44 @@ impl EpochCache { | |||||||
|         self.shuffling_start_shard |         self.shuffling_start_shard | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     pub fn get_crosslink_committees_for_slot(&self, slot: Slot) -> Option<Vec<CrosslinkCommittee>> { | ||||||
|  |         let position = self | ||||||
|  |             .initialized_epoch? | ||||||
|  |             .position(slot, self.slots_per_epoch)?; | ||||||
|  |         let committees_per_slot = self.committee_count / self.slots_per_epoch as usize; | ||||||
|  |         let position = position * committees_per_slot; | ||||||
|  | 
 | ||||||
|  |         if position >= self.committee_count { | ||||||
|  |             None | ||||||
|  |         } else { | ||||||
|  |             let mut committees = Vec::with_capacity(committees_per_slot); | ||||||
|  | 
 | ||||||
|  |             for index in position..position + committees_per_slot { | ||||||
|  |                 let committee = self.compute_committee(index)?; | ||||||
|  |                 let shard = (self.shuffling_start_shard + index as u64) % self.shard_count; | ||||||
|  | 
 | ||||||
|  |                 committees.push(CrosslinkCommittee { | ||||||
|  |                     committee, | ||||||
|  |                     shard, | ||||||
|  |                     slot, | ||||||
|  |                 }); | ||||||
|  |             } | ||||||
|  | 
 | ||||||
|  |             Some(committees) | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     pub fn first_committee_at_slot(&self, slot: Slot) -> Option<&[usize]> { |     pub fn first_committee_at_slot(&self, slot: Slot) -> Option<&[usize]> { | ||||||
|         let position = self |         let position = self | ||||||
|             .initialized_epoch? |             .initialized_epoch? | ||||||
|             .position(slot, self.slots_per_epoch)?; |             .position(slot, self.slots_per_epoch)?; | ||||||
|  |         let committees_per_slot = self.committee_count / self.slots_per_epoch as usize; | ||||||
|  |         let position = position * committees_per_slot; | ||||||
| 
 | 
 | ||||||
|         if position > self.committee_count { |         if position >= self.committee_count { | ||||||
|             None |             None | ||||||
|         } else { |         } else { | ||||||
|             let committees_per_slot = self.committee_count / self.slots_per_epoch as usize; |             self.compute_committee(position) | ||||||
|             let index = position * committees_per_slot; |  | ||||||
| 
 |  | ||||||
|             self.compute_committee(index) |  | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user