Move get_active_validator_indices to state

This commit is contained in:
Paul Hauner
2019-03-19 09:09:57 +11:00
parent 4c4952e71b
commit 37b8e9f39a
17 changed files with 57 additions and 239 deletions

View File

@@ -34,7 +34,7 @@ pub fn get_genesis_state(
// Set all the active index roots to be the genesis active index root.
let active_validator_indices = state
.get_active_validator_indices(spec.genesis_epoch, spec)?
.get_cached_active_validator_indices(RelativeEpoch::Current, spec)?
.to_vec();
let genesis_active_index_root = Hash256::from_slice(&active_validator_indices.hash_tree_root());
state.fill_active_index_roots_with(genesis_active_index_root, spec);

View File

@@ -7,7 +7,7 @@ use process_validator_registry::process_validator_registry;
use rayon::prelude::*;
use ssz::TreeHash;
use std::collections::HashMap;
use types::{validator_registry::get_active_validator_indices, *};
use types::*;
use validator_statuses::{TotalBalances, ValidatorStatuses};
use winning_root::{winning_root, WinningRoot};
@@ -70,16 +70,6 @@ pub fn per_epoch_processing(state: &mut BeaconState, spec: &ChainSpec) -> Result
Ok(())
}
/// Returns a list of active validator indices for the state's current epoch.
///
/// Spec v0.5.0
pub fn calculate_active_validator_indices(state: &BeaconState, spec: &ChainSpec) -> Vec<usize> {
get_active_validator_indices(
&state.validator_registry,
state.slot.epoch(spec.slots_per_epoch),
)
}
/// Calculates various sets of attesters, including:
///
/// - current epoch attesters
@@ -454,11 +444,10 @@ pub fn update_active_tree_index_roots(
) -> Result<(), Error> {
let next_epoch = state.next_epoch(spec);
let active_tree_root = get_active_validator_indices(
&state.validator_registry,
next_epoch + Epoch::from(spec.activation_exit_delay),
)
.hash_tree_root();
let active_tree_root = state
.get_active_validator_indices(next_epoch + Epoch::from(spec.activation_exit_delay))
.to_vec()
.hash_tree_root();
state.set_active_index_root(next_epoch, Hash256::from_slice(&active_tree_root[..]), spec)?;

View File

@@ -9,7 +9,7 @@ pub fn process_ejections(state: &mut BeaconState, spec: &ChainSpec) -> Result<()
// There is an awkward double (triple?) loop here because we can't loop across the borrowed
// active validator indices and mutate state in the one loop.
let exitable: Vec<usize> = state
.get_active_validator_indices(state.current_epoch(spec), spec)?
.get_cached_active_validator_indices(RelativeEpoch::Current, spec)?
.iter()
.filter_map(|&i| {
if state.validator_balances[i as usize] < spec.ejection_balance {

View File

@@ -7,7 +7,8 @@ use types::{BeaconStateError as Error, *};
/// Spec v0.4.0
pub fn process_slashings(state: &mut BeaconState, spec: &ChainSpec) -> Result<(), Error> {
let current_epoch = state.current_epoch(spec);
let active_validator_indices = state.get_active_validator_indices(current_epoch, spec)?;
let active_validator_indices =
state.get_cached_active_validator_indices(RelativeEpoch::Current, spec)?;
let total_balance = state.get_total_balance(&active_validator_indices[..], spec)?;
for (index, validator) in state.validator_registry.iter().enumerate() {

View File

@@ -21,7 +21,7 @@ pub fn process_validator_registry(state: &mut BeaconState, spec: &ChainSpec) ->
state.current_shuffling_start_shard = (state.current_shuffling_start_shard
+ spec.get_epoch_committee_count(
state
.get_active_validator_indices(current_epoch, spec)?
.get_cached_active_validator_indices(RelativeEpoch::Current, spec)?
.len(),
) as u64)
% spec.shard_count;
@@ -53,7 +53,7 @@ pub fn should_update_validator_registry(
}
let num_active_validators = state
.get_active_validator_indices(state.current_epoch(spec), spec)?
.get_cached_active_validator_indices(RelativeEpoch::Current, spec)?
.len();
let current_epoch_committee_count = spec.get_epoch_committee_count(num_active_validators);

View File

@@ -8,7 +8,8 @@ use types::{BeaconStateError as Error, *};
/// Spec v0.4.0
pub fn update_validator_registry(state: &mut BeaconState, spec: &ChainSpec) -> Result<(), Error> {
let current_epoch = state.current_epoch(spec);
let active_validator_indices = state.get_active_validator_indices(current_epoch, spec)?;
let active_validator_indices =
state.get_cached_active_validator_indices(RelativeEpoch::Current, spec)?;
let total_balance = state.get_total_balance(&active_validator_indices[..], spec)?;
let max_balance_churn = std::cmp::max(