Skip to content

Commit 81b6768

Browse files
committed
Fix eligible indices
1 parent 3b01744 commit 81b6768

File tree

3 files changed

+11
-27
lines changed

3 files changed

+11
-27
lines changed

consensus/state_processing/src/per_epoch_processing/altair/inactivity_updates.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub fn process_inactivity_updates<T: EthSpec>(
2222
let unslashed_indices = participation_cache
2323
.get_unslashed_participating_indices(TIMELY_TARGET_FLAG_INDEX, state.previous_epoch())?;
2424

25-
for &index in participation_cache.eligible_validator_indices()? {
25+
for &index in participation_cache.eligible_validator_indices() {
2626
// Increase inactivity score of inactive validators
2727
if unslashed_indices.contains(index)? {
2828
let inactivity_score = state.get_inactivity_score_mut(index)?;

consensus/state_processing/src/per_epoch_processing/altair/participation_cache.rs

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ struct EpochParticipation {
1313
unslashed_participating_indices: HashMap<usize, ParticipationFlags>,
1414
total_flag_balances: [u64; NUM_FLAG_INDICES],
1515
total_active_balance: u64,
16-
eligible_indices_opt: Option<Vec<usize>>,
1716
}
1817

1918
#[derive(PartialEq, Debug)]
@@ -22,6 +21,7 @@ pub struct ParticipationCache {
2221
current_epoch_participation: EpochParticipation,
2322
previous_epoch: Epoch,
2423
previous_epoch_participation: EpochParticipation,
24+
eligible_indices: Vec<usize>,
2525
}
2626

2727
impl ParticipationCache {
@@ -37,14 +37,12 @@ impl ParticipationCache {
3737
current_epoch_participation: get_epoch_participation(state, current_epoch, spec)?,
3838
previous_epoch,
3939
previous_epoch_participation: get_epoch_participation(state, previous_epoch, spec)?,
40+
eligible_indices: state.get_eligible_validator_indices()?,
4041
})
4142
}
4243

43-
pub fn eligible_validator_indices(&self) -> Result<&[usize], BeaconStateError> {
44-
self.previous_epoch_participation
45-
.eligible_indices_opt
46-
.as_deref()
47-
.ok_or(BeaconStateError::EpochOutOfBounds)
44+
pub fn eligible_validator_indices(&self) -> &[usize] {
45+
&self.eligible_indices
4846
}
4947

5048
pub fn previous_epoch_total_active_balance(&self) -> u64 {
@@ -182,11 +180,11 @@ fn get_epoch_participation<T: EthSpec>(
182180
if epoch == state.current_epoch() {
183181
active_validator_indices =
184182
state.get_cached_active_validator_indices(RelativeEpoch::Current)?;
185-
epoch_participation = state.current_epoch_participation()?
183+
epoch_participation = state.current_epoch_participation()?;
186184
} else if epoch == state.previous_epoch() {
187185
active_validator_indices =
188186
state.get_cached_active_validator_indices(RelativeEpoch::Previous)?;
189-
epoch_participation = state.previous_epoch_participation()?
187+
epoch_participation = state.previous_epoch_participation()?;
190188
} else {
191189
return Err(BeaconStateError::EpochOutOfBounds);
192190
};
@@ -196,26 +194,11 @@ fn get_epoch_participation<T: EthSpec>(
196194
HashMap::with_capacity(active_validator_indices.len());
197195
let mut total_flag_balances = [0; NUM_FLAG_INDICES];
198196
let mut total_active_balance = 0;
199-
let mut eligible_indices_opt = if epoch == state.previous_epoch() {
200-
// This Vec might be the wrong size since it'll be filled with the *previous* epoch values,
201-
// no current epoch values.
202-
//
203-
// The difference should be fairly small and there's little impact otherwise.
204-
Some(Vec::with_capacity(active_validator_indices.len()))
205-
} else {
206-
None
207-
};
208197

209198
for &val_index in active_validator_indices {
210199
let val_balance = state.get_effective_balance(val_index)?;
211200
total_active_balance.safe_add_assign(val_balance)?;
212201

213-
if let Some(eligible_indices) = &mut eligible_indices_opt {
214-
if state.is_eligible_validator(val_index)? {
215-
eligible_indices.push(val_index)
216-
}
217-
}
218-
219202
if !state.get_validator(val_index)?.slashed {
220203
// Iterate through all the flags and increment total balances.
221204
total_flag_balances
@@ -248,10 +231,11 @@ fn get_epoch_participation<T: EthSpec>(
248231
*balance = std::cmp::max(*balance, spec.effective_balance_increment)
249232
}
250233

234+
unslashed_participating_indices.shrink_to_fit();
235+
251236
Ok(EpochParticipation {
252237
unslashed_participating_indices,
253238
total_flag_balances,
254239
total_active_balance,
255-
eligible_indices_opt,
256240
})
257241
}

consensus/state_processing/src/per_epoch_processing/altair/rewards_and_penalties.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ pub fn get_flag_index_deltas<T: EthSpec>(
6868
unslashed_participating_balance.safe_div(spec.effective_balance_increment)?;
6969
let active_increments = total_active_balance.safe_div(spec.effective_balance_increment)?;
7070

71-
for &index in participation_cache.eligible_validator_indices()? {
71+
for &index in participation_cache.eligible_validator_indices() {
7272
let base_reward = get_base_reward(state, index, total_active_balance, spec)?;
7373
let mut delta = Delta::default();
7474

@@ -109,7 +109,7 @@ pub fn get_inactivity_penalty_deltas<T: EthSpec>(
109109
let previous_epoch = state.previous_epoch();
110110
let matching_target_indices = participation_cache
111111
.get_unslashed_participating_indices(TIMELY_TARGET_FLAG_INDEX, previous_epoch)?;
112-
for &index in participation_cache.eligible_validator_indices()? {
112+
for &index in participation_cache.eligible_validator_indices() {
113113
let mut delta = Delta::default();
114114

115115
if !matching_target_indices.contains(index)? {

0 commit comments

Comments
 (0)