From cb410cc4e08044216c7216606c04ce4b57264e1a Mon Sep 17 00:00:00 2001 From: Luke Parker Date: Wed, 15 Jan 2025 02:46:31 -0500 Subject: [PATCH] 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. --- substrate/validator-sets/primitives/src/slash_points.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/substrate/validator-sets/primitives/src/slash_points.rs b/substrate/validator-sets/primitives/src/slash_points.rs index 20bb4e72f..c045a4ef2 100644 --- a/substrate/validator-sets/primitives/src/slash_points.rs +++ b/substrate/validator-sets/primitives/src/slash_points.rs @@ -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 = @@ -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)) }