get_active_validator_indices() now has bound check (#1300)

This commit is contained in:
ethDreamer
2020-06-29 04:21:51 -04:00
committed by GitHub
parent 163fda2c26
commit 721323f045
4 changed files with 29 additions and 14 deletions

View File

@@ -375,9 +375,16 @@ impl<T: EthSpec> BeaconState<T> {
/// Does not utilize the cache, performs a full iteration over the validator registry.
///
/// Spec v0.12.1
pub fn get_active_validator_indices(&self, epoch: Epoch) -> Vec<usize> {
// FIXME(sproul): put a bounds check on here based on the maximum lookahead
get_active_validator_indices(&self.validators, epoch)
pub fn get_active_validator_indices(
&self,
epoch: Epoch,
spec: &ChainSpec,
) -> Result<Vec<usize>, Error> {
if epoch >= self.compute_activation_exit_epoch(self.current_epoch(), spec) {
Err(BeaconStateError::EpochOutOfBounds)
} else {
Ok(get_active_validator_indices(&self.validators, epoch))
}
}
/// Return the cached active validator indices at some epoch.
@@ -505,7 +512,7 @@ impl<T: EthSpec> BeaconState<T> {
pub fn get_beacon_proposer_index(&self, slot: Slot, spec: &ChainSpec) -> Result<usize, Error> {
let epoch = slot.epoch(T::slots_per_epoch());
let seed = self.get_beacon_proposer_seed(slot, spec)?;
let indices = self.get_active_validator_indices(epoch);
let indices = self.get_active_validator_indices(epoch, spec)?;
self.compute_proposer_index(&indices, &seed, spec)
}

View File

@@ -19,10 +19,10 @@ fn test_beacon_proposer_index<T: EthSpec>() {
};
// Get the i'th candidate proposer for the given state and slot
let ith_candidate = |state: &BeaconState<T>, slot: Slot, i: usize| {
let ith_candidate = |state: &BeaconState<T>, slot: Slot, i: usize, spec: &ChainSpec| {
let epoch = slot.epoch(T::slots_per_epoch());
let seed = state.get_beacon_proposer_seed(slot, &spec).unwrap();
let active_validators = state.get_active_validator_indices(epoch);
let active_validators = state.get_active_validator_indices(epoch, spec).unwrap();
active_validators[compute_shuffled_index(
i,
active_validators.len(),
@@ -36,7 +36,7 @@ fn test_beacon_proposer_index<T: EthSpec>() {
let test = |state: &BeaconState<T>, slot: Slot, candidate_index: usize| {
assert_eq!(
state.get_beacon_proposer_index(slot, &spec),
Ok(ith_candidate(state, slot, candidate_index))
Ok(ith_candidate(state, slot, candidate_index, &spec))
);
};
@@ -56,7 +56,7 @@ fn test_beacon_proposer_index<T: EthSpec>() {
// Test with two validators per slot, first validator has zero balance.
let mut state = build_state(T::slots_per_epoch() as usize * 2);
let slot0_candidate0 = ith_candidate(&state, Slot::new(0), 0);
let slot0_candidate0 = ith_candidate(&state, Slot::new(0), 0, &spec);
state.validators[slot0_candidate0].effective_balance = 0;
test(&state, Slot::new(0), 1);
for i in 1..T::slots_per_epoch() {