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,7 @@
//! Library for safe arithmetic on integers, avoiding overflow and division by zero.
mod iter;
pub use iter::SafeArithIter;
/// Error representing the failure of an arithmetic operation.
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
@@ -7,7 +10,7 @@ pub enum ArithError {
DivisionByZero,
}
type Result<T> = std::result::Result<T, ArithError>;
pub type Result<T> = std::result::Result<T, ArithError>;
macro_rules! assign_method {
($name:ident, $op:ident, $doc_op:expr) => {