mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-17 03:42:46 +00:00
Use CoW
This commit is contained in:
@@ -32,13 +32,11 @@ pub fn initiate_validator_exit<T: EthSpec>(
|
||||
.exit_cache_mut()
|
||||
.record_validator_exit(exit_queue_epoch)?;
|
||||
|
||||
let mut validators = state.validators_mut();
|
||||
let validator = validators.get_validator_mut(index)?;
|
||||
|
||||
// FIXME(sproul): could avoid this second lookup with some clever borrowing
|
||||
let mut validator = state.get_validator_mut(index)?;
|
||||
validator.exit_epoch = exit_queue_epoch;
|
||||
validator.withdrawable_epoch =
|
||||
exit_queue_epoch.safe_add(spec.min_validator_withdrawability_delay)?;
|
||||
drop(validators);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ pub use initiate_validator_exit::initiate_validator_exit;
|
||||
pub use slash_validator::slash_validator;
|
||||
|
||||
use safe_arith::SafeArith;
|
||||
use types::{BeaconState, BeaconStateError, EthSpec, GetBalanceMut};
|
||||
use types::{BeaconState, BeaconStateError, EthSpec};
|
||||
|
||||
/// Increase the balance of a validator, erroring upon overflow, as per the spec.
|
||||
pub fn increase_balance<E: EthSpec>(
|
||||
@@ -24,10 +24,7 @@ pub fn increase_balance<E: EthSpec>(
|
||||
index: usize,
|
||||
delta: u64,
|
||||
) -> Result<(), BeaconStateError> {
|
||||
state
|
||||
.balances_mut()
|
||||
.get_balance_mut(index)?
|
||||
.safe_add_assign(delta)?;
|
||||
state.get_balance_mut(index)?.safe_add_assign(delta)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -37,8 +34,7 @@ pub fn decrease_balance<E: EthSpec>(
|
||||
index: usize,
|
||||
delta: u64,
|
||||
) -> Result<(), BeaconStateError> {
|
||||
let mut balances = state.balances_mut();
|
||||
let balance = balances.get_balance_mut(index)?;
|
||||
let balance = state.get_balance_mut(index)?;
|
||||
*balance = balance.saturating_sub(delta);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -17,16 +17,13 @@ pub fn slash_validator<T: EthSpec>(
|
||||
|
||||
initiate_validator_exit(state, slashed_index, spec)?;
|
||||
|
||||
let mut validators = state.validators_mut();
|
||||
let validator = validators.get_validator_mut(slashed_index)?;
|
||||
let validator = state.get_validator_mut(slashed_index)?;
|
||||
validator.slashed = true;
|
||||
validator.withdrawable_epoch = cmp::max(
|
||||
validator.withdrawable_epoch,
|
||||
epoch.safe_add(T::EpochsPerSlashingsVector::to_u64())?,
|
||||
);
|
||||
let validator_effective_balance = validator.effective_balance;
|
||||
drop(validators);
|
||||
|
||||
state.set_slashings(
|
||||
epoch,
|
||||
state
|
||||
|
||||
@@ -96,9 +96,10 @@ pub fn process_activations<T: EthSpec>(
|
||||
state: &mut BeaconState<T>,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), Error> {
|
||||
let (mut validators, balances) = state.validators_and_balances_mut();
|
||||
for index in 0..validators.len() {
|
||||
let validator = validators.get_validator_mut(index)?;
|
||||
let (validators, balances) = state.validators_and_balances_mut();
|
||||
let mut validators_iter = validators.iter_cow();
|
||||
while let Some((index, validator)) = validators_iter.next_cow() {
|
||||
let validator = validator.to_mut();
|
||||
let balance = balances
|
||||
.get(index)
|
||||
.copied()
|
||||
|
||||
@@ -19,6 +19,7 @@ use types::{
|
||||
TIMELY_TARGET_FLAG_INDEX,
|
||||
},
|
||||
BeaconState, BeaconStateError, ChainSpec, Epoch, EthSpec, ParticipationFlags, RelativeEpoch,
|
||||
Validator,
|
||||
};
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
@@ -120,12 +121,10 @@ impl SingleEpochParticipationCache {
|
||||
fn process_active_validator<T: EthSpec>(
|
||||
&mut self,
|
||||
val_index: usize,
|
||||
validator: &Validator,
|
||||
state: &BeaconState<T>,
|
||||
relative_epoch: RelativeEpoch,
|
||||
) -> Result<(), BeaconStateError> {
|
||||
let val_balance = state.get_effective_balance(val_index)?;
|
||||
let validator = state.get_validator(val_index)?;
|
||||
|
||||
// Sanity check to ensure the validator is active.
|
||||
let epoch = relative_epoch.into_epoch(state.current_epoch());
|
||||
if !validator.is_active_at(epoch) {
|
||||
@@ -141,7 +140,8 @@ impl SingleEpochParticipationCache {
|
||||
.ok_or(BeaconStateError::ParticipationOutOfBounds(val_index))?;
|
||||
|
||||
// All active validators increase the total active balance.
|
||||
self.total_active_balance.safe_add_assign(val_balance)?;
|
||||
self.total_active_balance
|
||||
.safe_add_assign(validator.effective_balance)?;
|
||||
|
||||
// Only unslashed validators may proceed.
|
||||
if validator.slashed {
|
||||
@@ -156,7 +156,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(val_balance)?;
|
||||
balance.safe_add_assign(validator.effective_balance)?;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -223,6 +223,7 @@ impl ParticipationCache {
|
||||
if val.is_active_at(current_epoch) {
|
||||
current_epoch_participation.process_active_validator(
|
||||
val_index,
|
||||
val,
|
||||
state,
|
||||
RelativeEpoch::Current,
|
||||
)?;
|
||||
@@ -231,6 +232,7 @@ impl ParticipationCache {
|
||||
if val.is_active_at(previous_epoch) {
|
||||
previous_epoch_participation.process_active_validator(
|
||||
val_index,
|
||||
val,
|
||||
state,
|
||||
RelativeEpoch::Previous,
|
||||
)?;
|
||||
|
||||
@@ -69,6 +69,7 @@ pub fn get_flag_index_deltas<T: EthSpec>(
|
||||
let active_increments = total_active_balance.safe_div(spec.effective_balance_increment)?;
|
||||
|
||||
for &index in participation_cache.eligible_validator_indices() {
|
||||
// FIXME(sproul): compute base reward in participation cache
|
||||
let base_reward = get_base_reward(state, index, total_active_balance, spec)?;
|
||||
let mut delta = Delta::default();
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ use super::errors::EpochProcessingError;
|
||||
use safe_arith::SafeArith;
|
||||
use types::beacon_state::BeaconState;
|
||||
use types::chain_spec::ChainSpec;
|
||||
use types::{BeaconStateError, EthSpec, GetValidatorMut};
|
||||
use types::{BeaconStateError, EthSpec};
|
||||
|
||||
pub fn process_effective_balance_updates<T: EthSpec>(
|
||||
state: &mut BeaconState<T>,
|
||||
@@ -13,9 +13,9 @@ pub fn process_effective_balance_updates<T: EthSpec>(
|
||||
.safe_div(spec.hysteresis_quotient)?;
|
||||
let downward_threshold = hysteresis_increment.safe_mul(spec.hysteresis_downward_multiplier)?;
|
||||
let upward_threshold = hysteresis_increment.safe_mul(spec.hysteresis_upward_multiplier)?;
|
||||
let (mut validators, balances) = state.validators_and_balances_mut();
|
||||
for index in 0..validators.len() {
|
||||
let validator = validators.get_validator_mut(index)?;
|
||||
let (validators, balances) = state.validators_and_balances_mut();
|
||||
let mut validators_iter = validators.iter_cow();
|
||||
while let Some((index, validator)) = validators_iter.next_cow() {
|
||||
let balance = balances
|
||||
.get(index)
|
||||
.copied()
|
||||
@@ -24,7 +24,7 @@ pub fn process_effective_balance_updates<T: EthSpec>(
|
||||
if balance.safe_add(downward_threshold)? < validator.effective_balance
|
||||
|| validator.effective_balance.safe_add(upward_threshold)? < balance
|
||||
{
|
||||
validator.effective_balance = std::cmp::min(
|
||||
validator.to_mut().effective_balance = std::cmp::min(
|
||||
balance.safe_sub(balance.safe_rem(spec.effective_balance_increment)?)?,
|
||||
spec.max_effective_balance,
|
||||
);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use crate::{common::initiate_validator_exit, per_epoch_processing::Error};
|
||||
use itertools::Itertools;
|
||||
use safe_arith::SafeArith;
|
||||
use types::{BeaconState, ChainSpec, EthSpec, GetValidatorMut, Validator};
|
||||
use types::{BeaconState, ChainSpec, EthSpec, Validator};
|
||||
|
||||
/// Performs a validator registry update, if required.
|
||||
///
|
||||
@@ -30,13 +30,11 @@ pub fn process_registry_updates<T: EthSpec>(
|
||||
.collect();
|
||||
|
||||
for index in indices_to_update {
|
||||
let mut validators = state.validators_mut();
|
||||
let validator = validators.get_validator_mut(index)?;
|
||||
let validator = state.get_validator_mut(index)?;
|
||||
if validator.is_eligible_for_activation_queue(spec) {
|
||||
validator.activation_eligibility_epoch = current_epoch.safe_add(1)?;
|
||||
}
|
||||
if is_ejectable(validator) {
|
||||
drop(validators);
|
||||
initiate_validator_exit(state, index, spec)?;
|
||||
}
|
||||
}
|
||||
@@ -54,9 +52,8 @@ pub fn process_registry_updates<T: EthSpec>(
|
||||
// Dequeue validators for activation up to churn limit
|
||||
let churn_limit = state.get_churn_limit(spec)? as usize;
|
||||
let delayed_activation_epoch = state.compute_activation_exit_epoch(current_epoch, spec)?;
|
||||
let mut validators = state.validators_mut();
|
||||
for index in activation_queue.into_iter().take(churn_limit) {
|
||||
validators.get_validator_mut(index)?.activation_epoch = delayed_activation_epoch;
|
||||
state.get_validator_mut(index)?.activation_epoch = delayed_activation_epoch;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use crate::per_epoch_processing::Error;
|
||||
use safe_arith::{SafeArith, SafeArithIter};
|
||||
use types::{BeaconState, ChainSpec, EthSpec, GetBalanceMut, GetValidatorMut, Unsigned};
|
||||
use types::{BeaconState, BeaconStateError, ChainSpec, EthSpec, Unsigned};
|
||||
|
||||
/// Process slashings.
|
||||
pub fn process_slashings<T: EthSpec>(
|
||||
@@ -16,9 +16,9 @@ pub fn process_slashings<T: EthSpec>(
|
||||
total_balance,
|
||||
);
|
||||
|
||||
let (validators, mut balances) = state.validators_and_balances_mut();
|
||||
for index in 0..validators.len() {
|
||||
let validator = validators.get_validator(index)?;
|
||||
let (validators, balances) = state.validators_and_balances_mut();
|
||||
let mut validators_iter = validators.iter_cow();
|
||||
while let Some((index, validator)) = validators_iter.next_cow() {
|
||||
if validator.slashed
|
||||
&& epoch.safe_add(T::EpochsPerSlashingsVector::to_u64().safe_div(2)?)?
|
||||
== validator.withdrawable_epoch
|
||||
@@ -33,7 +33,9 @@ pub fn process_slashings<T: EthSpec>(
|
||||
.safe_mul(increment)?;
|
||||
|
||||
// Equivalent to `decrease_balance(state, index, penalty)`, but avoids borrowing `state`.
|
||||
let balance = balances.get_balance_mut(index)?;
|
||||
let balance = balances
|
||||
.get_mut(index)
|
||||
.ok_or(BeaconStateError::BalancesOutOfBounds(index))?;
|
||||
*balance = balance.saturating_sub(penalty);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,7 +51,7 @@ pub fn upgrade_to_altair<E: EthSpec>(
|
||||
|
||||
let default_epoch_participation =
|
||||
VariableList::new(vec![ParticipationFlags::default(); pre.validators.len()])?;
|
||||
let inactivity_scores = VariableList::new(vec![0; pre.validators.len()])?;
|
||||
let inactivity_scores = VList::new(vec![0; pre.validators.len()])?;
|
||||
|
||||
let temp_sync_committee = Arc::new(SyncCommittee::temporary()?);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user