Delete current epoch vals from ParticipationCache

This commit is contained in:
Michael Sproul
2022-02-18 14:22:25 +11:00
parent 0b171cf097
commit 82bf8a3351
3 changed files with 31 additions and 31 deletions

View File

@@ -25,6 +25,7 @@ use types::{
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub enum Error { pub enum Error {
InvalidFlagIndex(usize), InvalidFlagIndex(usize),
NoUnslashedParticipatingIndices,
MissingEffectiveBalance(usize), MissingEffectiveBalance(usize),
} }
@@ -66,7 +67,7 @@ struct SingleEpochParticipationCache {
/// It would be ideal to maintain a reference to the `BeaconState` here rather than copying the /// It would be ideal to maintain a reference to the `BeaconState` here rather than copying the
/// `ParticipationFlags`, however that would cause us to run into mutable reference limitations /// `ParticipationFlags`, however that would cause us to run into mutable reference limitations
/// upstream. /// upstream.
unslashed_participating_indices: HashMap<usize, ParticipationFlags>, unslashed_participating_indices: Option<HashMap<usize, ParticipationFlags>>,
/// Stores the sum of the balances for all validators in `self.unslashed_participating_indices` /// Stores the sum of the balances for all validators in `self.unslashed_participating_indices`
/// for all flags in `NUM_FLAG_INDICES`. /// for all flags in `NUM_FLAG_INDICES`.
/// ///
@@ -78,14 +79,12 @@ struct SingleEpochParticipationCache {
} }
impl SingleEpochParticipationCache { impl SingleEpochParticipationCache {
fn new(hashmap_len: usize, spec: &ChainSpec) -> Self { fn new(hashmap_len: Option<usize>, spec: &ChainSpec) -> Self {
let zero_balance = Balance::zero(spec.effective_balance_increment); let zero_balance = Balance::zero(spec.effective_balance_increment);
Self { Self {
unslashed_participating_indices: HashMap::with_capacity_and_hasher( unslashed_participating_indices: hashmap_len
hashmap_len, .map(|len| HashMap::with_capacity_and_hasher(len, Default::default())),
Default::default(),
),
total_flag_balances: [zero_balance; NUM_FLAG_INDICES], total_flag_balances: [zero_balance; NUM_FLAG_INDICES],
total_active_balance: zero_balance, total_active_balance: zero_balance,
} }
@@ -105,7 +104,7 @@ impl SingleEpochParticipationCache {
/// ///
/// May return an error if `flag_index` is out-of-bounds. /// May return an error if `flag_index` is out-of-bounds.
fn has_flag(&self, val_index: usize, flag_index: usize) -> Result<bool, Error> { fn has_flag(&self, val_index: usize, flag_index: usize) -> Result<bool, Error> {
if let Some(participation_flags) = self.unslashed_participating_indices.get(&val_index) { if let Some(participation_flags) = self.unslashed_participating_indices()?.get(&val_index) {
participation_flags participation_flags
.has_flag(flag_index) .has_flag(flag_index)
.map_err(|_| Error::InvalidFlagIndex(flag_index)) .map_err(|_| Error::InvalidFlagIndex(flag_index))
@@ -114,6 +113,14 @@ impl SingleEpochParticipationCache {
} }
} }
fn unslashed_participating_indices(
&self,
) -> Result<&HashMap<usize, ParticipationFlags>, Error> {
self.unslashed_participating_indices
.as_ref()
.ok_or(Error::NoUnslashedParticipatingIndices)
}
/// Process an **active** validator, reading from the `state` with respect to the /// Process an **active** validator, reading from the `state` with respect to the
/// `relative_epoch`. /// `relative_epoch`.
/// ///
@@ -145,9 +152,11 @@ impl SingleEpochParticipationCache {
return Ok(()); return Ok(());
} }
// Add their `ParticipationFlags` to the map. // Add their `ParticipationFlags` to the map *if* we need them (for the previous epoch).
self.unslashed_participating_indices if let Some(ref mut unslashed_participating_indices) = self.unslashed_participating_indices
.insert(val_index, *epoch_participation); {
unslashed_participating_indices.insert(val_index, *epoch_participation);
}
// Iterate through all the flags and increment the total flag balances for whichever flags // Iterate through all the flags and increment the total flag balances for whichever flags
// are set for `val_index`. // are set for `val_index`.
@@ -192,19 +201,15 @@ impl ParticipationCache {
let num_previous_epoch_active_vals = state let num_previous_epoch_active_vals = state
.get_cached_active_validator_indices(RelativeEpoch::Previous)? .get_cached_active_validator_indices(RelativeEpoch::Previous)?
.len(); .len();
let num_current_epoch_active_vals = state
.get_cached_active_validator_indices(RelativeEpoch::Current)?
.len();
// Both the current/previous epoch participations are set to a capacity that is slightly // Both the current/previous epoch participations are set to a capacity that is slightly
// larger than required. The difference will be due slashed-but-active validators. // larger than required. The difference will be due slashed-but-active validators.
let mut current_epoch_participation = let mut current_epoch_participation = SingleEpochParticipationCache::new(None, spec);
SingleEpochParticipationCache::new(num_current_epoch_active_vals, spec);
let mut previous_epoch_participation = let mut previous_epoch_participation =
SingleEpochParticipationCache::new(num_previous_epoch_active_vals, spec); SingleEpochParticipationCache::new(Some(num_previous_epoch_active_vals), spec);
let mut effective_balances = let mut effective_balances =
HashMap::with_capacity_and_hasher(num_current_epoch_active_vals, Default::default()); HashMap::with_capacity_and_hasher(num_previous_epoch_active_vals, Default::default());
// Contains the set of validators which are either: // Contains the set of validators which are either:
// //
@@ -330,14 +335,13 @@ impl ParticipationCache {
pub fn is_active_unslashed_in_previous_epoch(&self, val_index: usize) -> bool { pub fn is_active_unslashed_in_previous_epoch(&self, val_index: usize) -> bool {
self.previous_epoch_participation self.previous_epoch_participation
.unslashed_participating_indices .unslashed_participating_indices()
.contains_key(&val_index) .map_or(false, |indices| indices.contains_key(&val_index))
} }
pub fn is_active_unslashed_in_current_epoch(&self, val_index: usize) -> bool { pub fn is_active_unslashed_in_current_epoch(&self, _val_index: usize) -> bool {
self.current_epoch_participation // FIXME(sproul): work out how to serve this for the validator API
.unslashed_participating_indices false
.contains_key(&val_index)
} }
pub fn get_effective_balance(&self, val_index: usize) -> Result<u64, Error> { pub fn get_effective_balance(&self, val_index: usize) -> Result<u64, Error> {

View File

@@ -9,9 +9,7 @@ pub fn process_participation_flag_updates<T: EthSpec>(
) -> Result<(), EpochProcessingError> { ) -> Result<(), EpochProcessingError> {
*state.previous_epoch_participation_mut()? = *state.previous_epoch_participation_mut()? =
std::mem::take(state.current_epoch_participation_mut()?); std::mem::take(state.current_epoch_participation_mut()?);
*state.current_epoch_participation_mut()? = VList::new(vec![ *state.current_epoch_participation_mut()? =
ParticipationFlags::default(); VList::repeat(ParticipationFlags::default(), state.validators().len())?;
state.validators().len()
])?;
Ok(()) Ok(())
} }

View File

@@ -486,10 +486,8 @@ impl<T: EthSpec> BeaconState<T> {
/// If the current epoch is the genesis epoch, the genesis_epoch is returned. /// If the current epoch is the genesis epoch, the genesis_epoch is returned.
pub fn previous_epoch(&self) -> Epoch { pub fn previous_epoch(&self) -> Epoch {
let current_epoch = self.current_epoch(); let current_epoch = self.current_epoch();
if current_epoch > T::genesis_epoch() { if let Ok(prev_epoch) = current_epoch.safe_sub(1) {
current_epoch prev_epoch
.safe_sub(1)
.expect("current epoch greater than genesis implies greater than 0")
} else { } else {
current_epoch current_epoch
} }