Split validator into ValidatorMutable

This commit is contained in:
Michael Sproul
2022-09-28 11:43:58 +10:00
parent 9ec454aa52
commit 9a1799f235
22 changed files with 133 additions and 97 deletions

View File

@@ -129,10 +129,10 @@ impl SingleEpochParticipationCache {
// All active validators increase the total active balance.
self.total_active_balance
.safe_add_assign(validator.effective_balance)?;
.safe_add_assign(validator.effective_balance())?;
// Only unslashed validators may proceed.
if validator.slashed {
if validator.slashed() {
return Ok(());
}
@@ -140,7 +140,7 @@ impl SingleEpochParticipationCache {
// are set for `val_index`.
for (flag, balance) in self.total_flag_balances.iter_mut().enumerate() {
if epoch_participation.has_flag(flag)? {
balance.safe_add_assign(validator.effective_balance)?;
balance.safe_add_assign(validator.effective_balance())?;
}
}
@@ -288,11 +288,11 @@ impl ParticipationCache {
)?;
}
if val.slashed
if val.slashed()
&& current_epoch.safe_add(T::EpochsPerSlashingsVector::to_u64().safe_div(2)?)?
== val.withdrawable_epoch
== val.withdrawable_epoch()
{
process_slashings_indices.push((val_index, val.effective_balance));
process_slashings_indices.push((val_index, val.effective_balance()));
}
// Note: a validator might still be "eligible" whilst returning `false` to
@@ -303,10 +303,10 @@ impl ParticipationCache {
}
let mut validator_info = ValidatorInfo {
effective_balance: val.effective_balance,
effective_balance: val.effective_balance(),
base_reward: 0, // not read
is_eligible,
is_slashed: val.slashed,
is_slashed: val.slashed(),
is_active_current_epoch,
is_active_previous_epoch,
previous_epoch_participation: *prev_epoch_flags,
@@ -332,7 +332,7 @@ impl ParticipationCache {
#[allow(clippy::indexing_slicing)]
if is_eligible || is_active_current_epoch {
let effective_balance = val.effective_balance;
let effective_balance = val.effective_balance();
let base_reward =
get_base_reward(effective_balance, base_reward_per_increment, spec)?;
validator_info.base_reward = base_reward;

View File

@@ -198,7 +198,7 @@ impl ValidatorStatuses {
for (i, validator) in state.validators().iter().enumerate() {
let effective_balance = state.get_effective_balance(i)?;
let mut status = ValidatorStatus {
is_slashed: validator.slashed,
is_slashed: validator.slashed(),
is_withdrawable_in_current_epoch: validator
.is_withdrawable_at(state.current_epoch()),
current_epoch_effective_balance: effective_balance,

View File

@@ -28,23 +28,23 @@ pub fn process_effective_balance_updates<T: EthSpec>(
.ok_or(BeaconStateError::BalancesOutOfBounds(index))?;
let new_effective_balance = if balance.safe_add(downward_threshold)?
< validator.effective_balance
|| validator.effective_balance.safe_add(upward_threshold)? < balance
< validator.effective_balance()
|| validator.effective_balance().safe_add(upward_threshold)? < balance
{
std::cmp::min(
balance.safe_sub(balance.safe_rem(spec.effective_balance_increment)?)?,
spec.max_effective_balance,
)
} else {
validator.effective_balance
validator.effective_balance()
};
if validator.is_active_at(next_epoch) {
new_total_active_balance.safe_add_assign(new_effective_balance)?;
}
if new_effective_balance != validator.effective_balance {
validator.to_mut().effective_balance = new_effective_balance;
if new_effective_balance != validator.effective_balance() {
validator.to_mut().mutable.effective_balance = new_effective_balance;
}
}

View File

@@ -17,7 +17,7 @@ pub fn process_registry_updates<T: EthSpec>(
let current_epoch = state.current_epoch();
let is_ejectable = |validator: &Validator| {
validator.is_active_at(current_epoch)
&& validator.effective_balance <= spec.ejection_balance
&& validator.effective_balance() <= spec.ejection_balance
};
let indices_to_update: Vec<_> = state
.validators()
@@ -32,7 +32,7 @@ pub fn process_registry_updates<T: EthSpec>(
for index in indices_to_update {
let validator = state.get_validator_mut(index)?;
if validator.is_eligible_for_activation_queue(spec) {
validator.activation_eligibility_epoch = current_epoch.safe_add(1)?;
validator.mutable.activation_eligibility_epoch = current_epoch.safe_add(1)?;
}
if is_ejectable(validator) {
initiate_validator_exit(state, index, spec)?;
@@ -45,7 +45,7 @@ pub fn process_registry_updates<T: EthSpec>(
.iter()
.enumerate()
.filter(|(_, validator)| validator.is_eligible_for_activation(state, spec))
.sorted_by_key(|(index, validator)| (validator.activation_eligibility_epoch, *index))
.sorted_by_key(|(index, validator)| (validator.activation_eligibility_epoch(), *index))
.map(|(index, _)| index)
.collect_vec();
@@ -53,7 +53,7 @@ pub fn process_registry_updates<T: EthSpec>(
let churn_limit = state.get_churn_limit(spec)? as usize;
let delayed_activation_epoch = state.compute_activation_exit_epoch(current_epoch, spec)?;
for index in activation_queue.into_iter().take(churn_limit) {
state.get_validator_mut(index)?.activation_epoch = delayed_activation_epoch;
state.get_validator_mut(index)?.mutable.activation_epoch = delayed_activation_epoch;
}
Ok(())

View File

@@ -26,9 +26,9 @@ pub fn process_slashings<T: EthSpec>(
.iter()
.enumerate()
.filter(|(_, validator)| {
validator.slashed && target_withdrawable_epoch == validator.withdrawable_epoch
validator.slashed() && target_withdrawable_epoch == validator.withdrawable_epoch()
})
.map(|(index, validator)| (index, validator.effective_balance))
.map(|(index, validator)| (index, validator.effective_balance()))
.collect()
});