Skip to content

Commit

Permalink
Remove unslashed indices function
Browse files Browse the repository at this point in the history
  • Loading branch information
paulhauner committed Jul 15, 2021
1 parent 6f87cd5 commit 22fbf3e
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 54 deletions.
4 changes: 2 additions & 2 deletions consensus/state_processing/src/per_epoch_processing/altair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ pub fn process_epoch<T: EthSpec>(
// Justification and finalization.
process_justification_and_finalization(state, &participation_cache, spec)?;

process_inactivity_updates(state, spec)?;
process_inactivity_updates(state, &participation_cache, spec)?;

// Rewards and Penalties.
process_rewards_and_penalties(state, spec)?;
process_rewards_and_penalties(state, &participation_cache, spec)?;

// Registry Updates.
process_registry_updates(state, spec)?;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use super::ParticipationCache;
use crate::EpochProcessingError;
use core::result::Result;
use core::result::Result::Ok;
Expand All @@ -10,22 +11,20 @@ use types::eth_spec::EthSpec;

pub fn process_inactivity_updates<T: EthSpec>(
state: &mut BeaconState<T>,
participation_cache: &ParticipationCache,
spec: &ChainSpec,
) -> Result<(), EpochProcessingError> {
// Score updates based on previous epoch participation, skip genesis epoch
if state.current_epoch() == T::genesis_epoch() {
return Ok(());
}

let unslashed_indices = state.get_unslashed_participating_indices(
TIMELY_TARGET_FLAG_INDEX,
state.previous_epoch(),
spec,
)?;
let unslashed_indices = participation_cache
.get_unslashed_participating_indices(TIMELY_TARGET_FLAG_INDEX, state.previous_epoch())?;

for index in state.get_eligible_validator_indices()? {
// Increase inactivity score of inactive validators
if unslashed_indices.contains(&index) {
if unslashed_indices.contains(index)? {
let inactivity_score = state.get_inactivity_score_mut(index)?;
inactivity_score.safe_sub_assign(min(1, *inactivity_score))?;
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::ParticipationCache;
use crate::per_epoch_processing::weigh_justification_and_finalization;
use crate::per_epoch_processing::{altair::ParticipationCache, Error};
use crate::per_epoch_processing::Error;
use safe_arith::SafeArith;
use types::consts::altair::TIMELY_TARGET_FLAG_INDEX;
use types::{BeaconState, ChainSpec, EthSpec};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use super::ParticipationCache;
use safe_arith::SafeArith;
use types::consts::altair::{
PARTICIPATION_FLAG_WEIGHTS, TIMELY_HEAD_FLAG_INDEX, TIMELY_TARGET_FLAG_INDEX,
Expand All @@ -13,6 +14,7 @@ use crate::per_epoch_processing::{Delta, Error};
/// Spec v1.1.0
pub fn process_rewards_and_penalties<T: EthSpec>(
state: &mut BeaconState<T>,
participation_cache: &ParticipationCache,
spec: &ChainSpec,
) -> Result<(), Error> {
if state.current_epoch() == T::genesis_epoch() {
Expand All @@ -24,10 +26,17 @@ pub fn process_rewards_and_penalties<T: EthSpec>(
let total_active_balance = state.get_total_active_balance(spec)?;

for flag_index in 0..PARTICIPATION_FLAG_WEIGHTS.len() {
get_flag_index_deltas(&mut deltas, state, flag_index, total_active_balance, spec)?;
get_flag_index_deltas(
&mut deltas,
state,
flag_index,
total_active_balance,
participation_cache,
spec,
)?;
}

get_inactivity_penalty_deltas(&mut deltas, state, spec)?;
get_inactivity_penalty_deltas(&mut deltas, state, participation_cache, spec)?;

// Apply the deltas, erroring on overflow above but not on overflow below (saturating at 0
// instead).
Expand All @@ -47,11 +56,12 @@ pub fn get_flag_index_deltas<T: EthSpec>(
state: &BeaconState<T>,
flag_index: usize,
total_active_balance: u64,
participation_cache: &ParticipationCache,
spec: &ChainSpec,
) -> Result<(), Error> {
let previous_epoch = state.previous_epoch();
let unslashed_participating_indices =
state.get_unslashed_participating_indices(flag_index, previous_epoch, spec)?;
participation_cache.get_unslashed_participating_indices(flag_index, previous_epoch)?;
let weight = get_flag_weight(flag_index)?;
let unslashed_participating_balance =
state.get_total_balance(&unslashed_participating_indices, spec)?;
Expand All @@ -63,7 +73,7 @@ pub fn get_flag_index_deltas<T: EthSpec>(
let base_reward = get_base_reward(state, index, total_active_balance, spec)?;
let mut delta = Delta::default();

if unslashed_participating_indices.contains(&(index as usize)) {
if unslashed_participating_indices.contains(index as usize)? {
if !state.is_in_inactivity_leak(spec) {
let reward_numerator = base_reward
.safe_mul(weight)?
Expand Down Expand Up @@ -94,18 +104,16 @@ pub fn get_flag_weight(flag_index: usize) -> Result<u64, Error> {
pub fn get_inactivity_penalty_deltas<T: EthSpec>(
deltas: &mut Vec<Delta>,
state: &BeaconState<T>,
participation_cache: &ParticipationCache,
spec: &ChainSpec,
) -> Result<(), Error> {
let previous_epoch = state.previous_epoch();
let matching_target_indices = state.get_unslashed_participating_indices(
TIMELY_TARGET_FLAG_INDEX,
previous_epoch,
spec,
)?;
let matching_target_indices = participation_cache
.get_unslashed_participating_indices(TIMELY_TARGET_FLAG_INDEX, previous_epoch)?;
for index in state.get_eligible_validator_indices()? {
let mut delta = Delta::default();

if !matching_target_indices.contains(&index) {
if !matching_target_indices.contains(index)? {
let penalty_numerator = state
.get_validator(index)?
.effective_balance
Expand Down
35 changes: 0 additions & 35 deletions consensus/types/src/beacon_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use serde_derive::{Deserialize, Serialize};
use ssz::{ssz_encode, Decode, DecodeError, Encode};
use ssz_derive::{Decode, Encode};
use ssz_types::{typenum::Unsigned, BitVector, FixedVector};
use std::collections::HashSet;
use std::convert::TryInto;
use std::{fmt, mem};
use superstruct::superstruct;
Expand Down Expand Up @@ -1452,40 +1451,6 @@ impl<T: EthSpec> BeaconState<T> {
self.clone_with(CloneConfig::committee_caches_only())
}

/// Get the unslashed participating indices for a given `flag_index`.
///
/// The `self` state must be Altair or later.
pub fn get_unslashed_participating_indices(
&self,
flag_index: usize,
epoch: Epoch,
spec: &ChainSpec,
) -> Result<HashSet<usize>, Error> {
let epoch_participation = if epoch == self.current_epoch() {
self.current_epoch_participation()?
} else if epoch == self.previous_epoch() {
self.previous_epoch_participation()?
} else {
return Err(Error::EpochOutOfBounds);
};
let active_validator_indices = self.get_active_validator_indices(epoch, spec)?;
itertools::process_results(
active_validator_indices.into_iter().map(|val_index| {
let has_flag = epoch_participation
.get(val_index)
.ok_or(Error::ParticipationOutOfBounds(val_index))?
.has_flag(flag_index)?;
let not_slashed = !self.get_validator(val_index)?.slashed;
Ok((val_index, has_flag && not_slashed))
}),
|iter| {
iter.filter(|(_, eligible)| *eligible)
.map(|(validator_index, _)| validator_index)
.collect()
},
)
}

pub fn get_eligible_validator_indices(&self) -> Result<Vec<usize>, Error> {
match self {
BeaconState::Base(_) => Err(Error::IncorrectStateVariant),
Expand Down

0 comments on commit 22fbf3e

Please sign in to comment.