Skip to content

Commit

Permalink
Correct how we handle rounding errors within the penalty fn
Browse files Browse the repository at this point in the history
We explicitly no longer slash stakes but we still set the maximum slash to the
allocated stake + the rewards. Now, the reward slash is bound to the rewards
and the stake slash is bound to the stake. This prevents an improperly rounded
reward slash from effecting a stake slash.
  • Loading branch information
kayabaNerve committed Jan 15, 2025
1 parent 6c145a5 commit cb410cc
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions substrate/validator-sets/primitives/src/slash_points.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ impl Slash {
}
})
};
// Ensure the slash never exceeds the amount slashable (due to rounding errors)
let reward_slash = reward_slash.min(session_rewards);

/*
let slash_points_for_entire_session =
Expand Down Expand Up @@ -192,10 +194,9 @@ impl Slash {
let offline_slash = 0;
let disruptive_slash = 0;

// The penalty is all slashes, but never more than the validator's balance
// (handles any rounding errors which may or may not exist)
let penalty_u128 =
(reward_slash + offline_slash + disruptive_slash).min(allocated_stake + session_rewards);
let stake_slash = (offline_slash + disruptive_slash).min(allocated_stake);

let penalty_u128 = reward_slash + stake_slash;
// saturating_into
Amount(u64::try_from(penalty_u128).unwrap_or(u64::MAX))
}
Expand Down

0 comments on commit cb410cc

Please sign in to comment.