Fix overflow in reward calculations

Overflow occurs when using updated issuance parameters.
This commit is contained in:
Shane Bammel 2023-04-04 23:21:08 -05:00
parent 37f77c19c7
commit 2b3eb7b12d
2 changed files with 6 additions and 6 deletions

View File

@ -44,7 +44,7 @@ fn get_base_reward_per_increment(
total_active_balance: u128,
spec: &ChainSpec,
) -> Result<u64, ArithError> {
spec.effective_balance_increment
.safe_mul(spec.base_reward_factor)?
.safe_div(total_active_balance.integer_sqrt() as u64)
Ok((spec.effective_balance_increment as u128)
.safe_mul(spec.base_reward_factor as u128)?
.safe_div(total_active_balance.integer_sqrt())? as u64)
}

View File

@ -22,14 +22,14 @@ pub fn process_slashings<T: EthSpec>(
&& epoch.safe_add(T::EpochsPerSlashingsVector::to_u64().safe_div(2)?)?
== validator.withdrawable_epoch
{
let increment = spec.effective_balance_increment;
let increment = spec.effective_balance_increment as u128;
let effective_balance = validator.effective_balance as u128;
let penalty_numerator = effective_balance
.safe_div(increment as u128)?
.safe_div(increment)?
.safe_mul(adjusted_total_slashing_balance)?;
let penalty = penalty_numerator
.safe_div(total_balance)?
.safe_mul(increment as u128)?;
.safe_mul(increment)?;
// Equivalent to `decrease_balance(state, index, penalty)`, but avoids borrowing `state`.
let balance = balances