Add safe_sum and use it in state_processing (#1620)

## Issue Addressed

Closes #1098

## Proposed Changes

Add a `SafeArithIter` trait with a `safe_sum` method, and use it in `state_processing`. This seems to be the only place in `consensus` where it is relevant -- i.e. where we were using `sum` and the integer_arith lint is enabled.

## Additional Info

This PR doesn't include any Clippy linting to prevent `sum` from being called. It seems there is no existing Clippy lint that suits our purpose, but I'm going to look into that and maybe schedule writing one as a lower-priority task.

This theoretically _is_ a consensus breaking change, but it shouldn't impact Medalla (or any other testnet) because `slashings` shouldn't overflow!
This commit is contained in:
Michael Sproul
2020-09-22 05:40:04 +00:00
parent 4fca306397
commit 7aceff4d13
3 changed files with 76 additions and 3 deletions

View File

@@ -1,4 +1,4 @@
use safe_arith::SafeArith;
use safe_arith::{SafeArith, SafeArithIter};
use types::{BeaconStateError as Error, *};
/// Process slashings.
@@ -10,7 +10,7 @@ pub fn process_slashings<T: EthSpec>(
spec: &ChainSpec,
) -> Result<(), Error> {
let epoch = state.current_epoch();
let sum_slashings = state.get_all_slashings().iter().sum::<u64>();
let sum_slashings = state.get_all_slashings().iter().copied().safe_sum()?;
for (index, validator) in state.validators.iter().enumerate() {
if validator.slashed