Skip to content

Commit 10c793b

Browse files
committed
Tidy, remove unused function, fix init length
1 parent 96efe87 commit 10c793b

File tree

2 files changed

+19
-26
lines changed

2 files changed

+19
-26
lines changed

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ pub struct ParticipationCache {
148148
}
149149

150150
impl ParticipationCache {
151-
/// Instantiate `Self`, returning a cache that is fully initialized and ready-to-go.
151+
/// Instantiate `Self`, returning a fully initialized cache.
152152
///
153153
/// ## Errors
154154
///
@@ -167,11 +167,20 @@ impl ParticipationCache {
167167
.get_cached_active_validator_indices(RelativeEpoch::Current)?
168168
.len();
169169

170+
// Both the current/previous epoch participations are set to a capacity that is slightly
171+
// larger than required. The difference will be due slashed-but-active validators.
170172
let mut current_epoch_participation =
171173
SingleEpochParticipationCache::new(num_current_epoch_active_vals, spec);
172174
let mut previous_epoch_participation =
173175
SingleEpochParticipationCache::new(num_previous_epoch_active_vals, spec);
174-
let mut eligible_indices = Vec::with_capacity(num_previous_epoch_active_vals);
176+
// Contains the set of validators which are:
177+
//
178+
// - Active in the previous epoch.
179+
// - Slashed, but not yet withdrawable.
180+
//
181+
// Using the full length of `state.validators` is almost always overkill, but it ensures no
182+
// reallocations.
183+
let mut eligible_indices = Vec::with_capacity(state.validators().len());
175184

176185
for (val_index, val) in state.validators().iter().enumerate() {
177186
if val.is_active_at(current_epoch) {
@@ -195,6 +204,14 @@ impl ParticipationCache {
195204
}
196205
}
197206

207+
eligible_indices.shrink_to_fit();
208+
current_epoch_participation
209+
.unslashed_participating_indices
210+
.shrink_to_fit();
211+
previous_epoch_participation
212+
.unslashed_participating_indices
213+
.shrink_to_fit();
214+
198215
Ok(Self {
199216
current_epoch,
200217
current_epoch_participation,

consensus/types/src/beacon_state.rs

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1451,30 +1451,6 @@ impl<T: EthSpec> BeaconState<T> {
14511451
self.clone_with(CloneConfig::committee_caches_only())
14521452
}
14531453

1454-
pub fn get_eligible_validator_indices(&self) -> Result<Vec<usize>, Error> {
1455-
match self {
1456-
BeaconState::Base(_) => Err(Error::IncorrectStateVariant),
1457-
BeaconState::Altair(_) => {
1458-
let previous_epoch = self.previous_epoch();
1459-
Ok(self
1460-
.validators()
1461-
.iter()
1462-
.enumerate()
1463-
.filter_map(|(i, val)| {
1464-
if val.is_active_at(previous_epoch)
1465-
|| (val.slashed
1466-
&& previous_epoch + Epoch::new(1) < val.withdrawable_epoch)
1467-
{
1468-
Some(i)
1469-
} else {
1470-
None
1471-
}
1472-
})
1473-
.collect())
1474-
}
1475-
}
1476-
}
1477-
14781454
pub fn is_eligible_validator(&self, val_index: usize) -> Result<bool, Error> {
14791455
match self {
14801456
BeaconState::Base(_) => Err(Error::IncorrectStateVariant),

0 commit comments

Comments
 (0)