mirror of
https://github.com/sigp/lighthouse.git
synced 2026-04-04 00:30:06 +00:00
Very spicy consensus optimisations
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
use crate::common::decrease_balance;
|
||||
use crate::per_epoch_processing::Error;
|
||||
use safe_arith::{SafeArith, SafeArithIter};
|
||||
use types::{BeaconState, BeaconStateError, ChainSpec, EthSpec, Unsigned};
|
||||
use types::{BeaconState, ChainSpec, EthSpec, Unsigned};
|
||||
|
||||
/// Process slashings.
|
||||
pub fn process_slashings<T: EthSpec>(
|
||||
state: &mut BeaconState<T>,
|
||||
indices: Option<Vec<(usize, u64)>>,
|
||||
total_balance: u64,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), Error> {
|
||||
@@ -16,28 +18,30 @@ pub fn process_slashings<T: EthSpec>(
|
||||
total_balance,
|
||||
);
|
||||
|
||||
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
|
||||
{
|
||||
let increment = spec.effective_balance_increment;
|
||||
let penalty_numerator = validator
|
||||
.effective_balance
|
||||
.safe_div(increment)?
|
||||
.safe_mul(adjusted_total_slashing_balance)?;
|
||||
let penalty = penalty_numerator
|
||||
.safe_div(total_balance)?
|
||||
.safe_mul(increment)?;
|
||||
let target_withdrawable_epoch =
|
||||
epoch.safe_add(T::EpochsPerSlashingsVector::to_u64().safe_div(2)?)?;
|
||||
let indices = indices.unwrap_or_else(|| {
|
||||
state
|
||||
.validators()
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(_, validator)| {
|
||||
validator.slashed && target_withdrawable_epoch == validator.withdrawable_epoch
|
||||
})
|
||||
.map(|(index, validator)| (index, validator.effective_balance))
|
||||
.collect()
|
||||
});
|
||||
|
||||
// Equivalent to `decrease_balance(state, index, penalty)`, but avoids borrowing `state`.
|
||||
let balance = balances
|
||||
.get_mut(index)
|
||||
.ok_or(BeaconStateError::BalancesOutOfBounds(index))?;
|
||||
*balance = balance.saturating_sub(penalty);
|
||||
}
|
||||
for (index, validator_effective_balance) in indices {
|
||||
let increment = spec.effective_balance_increment;
|
||||
let penalty_numerator = validator_effective_balance
|
||||
.safe_div(increment)?
|
||||
.safe_mul(adjusted_total_slashing_balance)?;
|
||||
let penalty = penalty_numerator
|
||||
.safe_div(total_balance)?
|
||||
.safe_mul(increment)?;
|
||||
|
||||
decrease_balance(state, index, penalty)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
||||
Reference in New Issue
Block a user