Correct how we handle rounding errors within the penalty fn

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.
This commit is contained in:
Luke Parker
2025-01-15 02:46:31 -05:00
parent 6c145a5ec3
commit cb410cc4e0

View File

@@ -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 = let slash_points_for_entire_session =
@@ -192,10 +194,9 @@ impl Slash {
let offline_slash = 0; let offline_slash = 0;
let disruptive_slash = 0; let disruptive_slash = 0;
// The penalty is all slashes, but never more than the validator's balance let stake_slash = (offline_slash + disruptive_slash).min(allocated_stake);
// (handles any rounding errors which may or may not exist)
let penalty_u128 = let penalty_u128 = reward_slash + stake_slash;
(reward_slash + offline_slash + disruptive_slash).min(allocated_stake + session_rewards);
// saturating_into // saturating_into
Amount(u64::try_from(penalty_u128).unwrap_or(u64::MAX)) Amount(u64::try_from(penalty_u128).unwrap_or(u64::MAX))
} }