Skip to content

Commit 2ed8eca

Browse files
committed
Document new BeaconState functions
1 parent 2651255 commit 2ed8eca

File tree

4 files changed

+52
-40
lines changed

4 files changed

+52
-40
lines changed

eth2/state_processing/tests/tests.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,4 @@ mod signatures_minimal {
205205
spec,
206206
);
207207
}
208-
209-
// Cannot test transfers because their length is zero.
210208
}

eth2/types/src/beacon_state.rs

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -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.

eth2/types/src/beacon_state/committee_cache.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,18 @@ impl CommitteeCache {
101101
&self.shuffling
102102
}
103103

104+
/// Get the Beacon committee for the given `slot` and `index`.
105+
///
106+
/// Return `None` if the cache is uninitialized, or the `slot` or `index` is out of range.
104107
pub fn get_beacon_committee(
105108
&self,
106109
slot: Slot,
107110
index: CommitteeIndex,
108111
) -> Option<BeaconCommittee> {
109-
if self.initialized_epoch.is_none() || index >= self.committees_per_slot {
112+
if self.initialized_epoch.is_none()
113+
|| !self.is_initialized_at(slot.epoch(self.slots_per_epoch))
114+
|| index >= self.committees_per_slot
115+
{
110116
return None;
111117
}
112118

@@ -121,6 +127,7 @@ impl CommitteeCache {
121127
})
122128
}
123129

130+
/// Get all the Beacon committees at a given `slot`.
124131
pub fn get_beacon_committees_at_slot(&self, slot: Slot) -> Result<Vec<BeaconCommittee>, Error> {
125132
if self.initialized_epoch.is_none() {
126133
return Err(Error::CommitteeCacheUninitialized(None));
@@ -165,6 +172,7 @@ impl CommitteeCache {
165172
})
166173
}
167174

175+
/// Convert an index addressing the list of all epoch committees into a slot and per-slot index.
168176
fn convert_to_slot_and_index(
169177
&self,
170178
global_committee_index: u64,
@@ -193,6 +201,7 @@ impl CommitteeCache {
193201
self.committees_per_slot as usize * self.slots_per_epoch as usize
194202
}
195203

204+
/// Returns the number of committees per slot for this cache's epoch.
196205
pub fn committees_per_slot(&self) -> u64 {
197206
self.committees_per_slot
198207
}
@@ -251,13 +260,3 @@ pub fn get_active_validator_indices(validators: &[Validator], epoch: Epoch) -> V
251260

252261
active
253262
}
254-
255-
/*
256-
/// Returns the count of all `validators` indices where the validator is active at the given
257-
/// `epoch`.
258-
///
259-
/// Spec v0.9.1
260-
fn get_active_validator_count(validators: &[Validator], epoch: Epoch) -> usize {
261-
validators.iter().filter(|v| v.is_active_at(epoch)).count()
262-
}
263-
*/

eth2/types/src/beacon_state/tests.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,13 @@ fn test_beacon_proposer_index<T: EthSpec>() {
4444
// 0th candidate should be chosen every time.
4545
let state = build_state(T::slots_per_epoch() as usize);
4646
for i in 0..T::slots_per_epoch() {
47-
println!("i = {}", i);
4847
test(&state, Slot::from(i), 0);
4948
}
5049

5150
// Test where we have two validators per slot.
5251
// 0th candidate should be chosen every time.
5352
let state = build_state(T::slots_per_epoch() as usize * 2);
5453
for i in 0..T::slots_per_epoch() {
55-
println!("i = {}", i);
5654
test(&state, Slot::from(i), 0);
5755
}
5856

@@ -62,7 +60,6 @@ fn test_beacon_proposer_index<T: EthSpec>() {
6260
state.validators[slot0_candidate0].effective_balance = 0;
6361
test(&state, Slot::new(0), 1);
6462
for i in 1..T::slots_per_epoch() {
65-
println!("i = {}", i);
6663
test(&state, Slot::from(i), 0);
6764
}
6865
}

0 commit comments

Comments
 (0)