Skip to content

Commit

Permalink
Add the pre-electra slashing processing
Browse files Browse the repository at this point in the history
  • Loading branch information
pawanjay176 committed Oct 1, 2024
1 parent 9edfcca commit 65bac11
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions consensus/state_processing/src/per_epoch_processing/single_pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ struct RewardsAndPenaltiesContext {
}

struct SlashingsContext {
adjusted_total_slashing_balance: u64,
target_withdrawable_epoch: Epoch,
penalty_per_effective_balance_increment: u64,
}
Expand Down Expand Up @@ -299,7 +300,7 @@ pub fn process_epoch_single_pass<E: EthSpec>(

// `process_slashings`
if conf.slashings {
process_single_slashing(&mut balance, &validator, slashings_ctxt, spec)?;
process_single_slashing(&mut balance, &validator, slashings_ctxt, state_ctxt, spec)?;
}

// `process_pending_balance_deposits`
Expand Down Expand Up @@ -782,6 +783,7 @@ impl SlashingsContext {
)?;

Ok(Self {
adjusted_total_slashing_balance,
target_withdrawable_epoch,
penalty_per_effective_balance_increment,
})
Expand All @@ -792,16 +794,28 @@ fn process_single_slashing(
balance: &mut Cow<u64>,
validator: &Validator,
slashings_ctxt: &SlashingsContext,
state_ctxt: &StateContext,
spec: &ChainSpec,
) -> Result<(), Error> {
if validator.slashed && slashings_ctxt.target_withdrawable_epoch == validator.withdrawable_epoch
{
let increment = spec.effective_balance_increment;
let effective_balance_increments = validator.effective_balance.safe_div(increment)?;
let penalty = slashings_ctxt
.penalty_per_effective_balance_increment
.safe_mul(effective_balance_increments)?;

let penalty = if state_ctxt.fork_name.electra_enabled() {
let effective_balance_increments = validator.effective_balance.safe_div(increment)?;
let penalty = slashings_ctxt
.penalty_per_effective_balance_increment
.safe_mul(effective_balance_increments)?;
penalty
} else {
let penalty_numerator = validator
.effective_balance
.safe_div(increment)?
.safe_mul(slashings_ctxt.adjusted_total_slashing_balance)?;
let penalty = penalty_numerator
.safe_div(state_ctxt.total_active_balance)?
.safe_mul(increment)?;
penalty
};
*balance.make_mut()? = balance.saturating_sub(penalty);
}
Ok(())
Expand Down

0 comments on commit 65bac11

Please sign in to comment.