Implement state.get_crosslink_committees_at_slot

This commit is contained in:
Paul Hauner 2019-05-19 17:21:24 +10:00
parent 9264ec1aa9
commit 94a39c3cb6
No known key found for this signature in database
GPG Key ID: 5E2CFF9B75FA63DF
2 changed files with 41 additions and 9 deletions

View File

@ -44,6 +44,7 @@ pub enum Error {
InsufficientSlashedBalances,
InsufficientStateRoots,
NoCommitteeForShard,
NoCommitteeForSlot,
PubkeyCacheInconsistent,
PubkeyCacheIncomplete {
cache_len: usize,
@ -346,10 +347,15 @@ impl<T: EthSpec> BeaconState<T> {
/// Spec v0.5.1
pub fn get_crosslink_committees_at_slot(
&self,
_slot: Slot,
_spec: &ChainSpec,
) -> Result<&Vec<CrosslinkCommittee>, Error> {
unimplemented!("FIXME(sproul)")
slot: Slot,
spec: &ChainSpec,
) -> Result<Vec<CrosslinkCommittee>, Error> {
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.

View File

@ -150,18 +150,44 @@ impl EpochCache {
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]> {
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 {
if position >= self.committee_count {
None
} else {
let committees_per_slot = self.committee_count / self.slots_per_epoch as usize;
let index = position * committees_per_slot;
self.compute_committee(index)
self.compute_committee(position)
}
}