Address clippy arith lints (#1038)

This commit is contained in:
Paul Hauner
2020-04-22 14:46:19 +10:00
committed by GitHub
parent ca538e887e
commit 018a666731
6 changed files with 33 additions and 15 deletions

View File

@@ -3,6 +3,7 @@ use super::{
Signature, SignedRoot, SubnetId,
};
use crate::{test_utils::TestRandom, Hash256};
use safe_arith::{ArithError, SafeArith};
use serde_derive::{Deserialize, Serialize};
use ssz_derive::{Decode, Encode};
@@ -13,6 +14,7 @@ use tree_hash_derive::TreeHash;
pub enum Error {
SszTypesError(ssz_types::Error),
AlreadySigned(usize),
SubnetCountIsZero(ArithError),
}
/// Details an attestation that can be slashable.
@@ -86,8 +88,12 @@ impl<T: EthSpec> Attestation<T> {
///
/// Note, this will return the subnet id for an aggregated attestation. This is done
/// to avoid checking aggregate bits every time we wish to get an id.
pub fn subnet_id(&self) -> SubnetId {
SubnetId::new(self.data.index % T::default_spec().attestation_subnet_count)
pub fn subnet_id(&self, spec: &ChainSpec) -> Result<SubnetId, Error> {
self.data
.index
.safe_rem(spec.attestation_subnet_count)
.map(SubnetId::new)
.map_err(Error::SubnetCountIsZero)
}
}

View File

@@ -485,7 +485,7 @@ impl<T: EthSpec> BeaconState<T> {
let committee = self.get_beacon_committee(slot, index)?;
let modulo = std::cmp::max(
1,
committee.committee.len() as u64 / spec.target_aggregators_per_committee,
(committee.committee.len() as u64).safe_div(spec.target_aggregators_per_committee)?,
);
let signature_hash = hash(&slot_signature.as_bytes());
let signature_hash_int = u64::from_le_bytes(
@@ -493,7 +493,8 @@ impl<T: EthSpec> BeaconState<T> {
.try_into()
.expect("first 8 bytes of signature should always convert to fixed array"),
);
Ok(signature_hash_int % modulo == 0)
Ok(signature_hash_int.safe_rem(modulo)? == 0)
}
/// Returns the beacon proposer index for the `slot` in the given `relative_epoch`.

View File

@@ -1,4 +1,5 @@
use crate::{ChainSpec, Domain, EthSpec, Fork, Hash256, SecretKey, Signature, SignedRoot, Slot};
use safe_arith::{ArithError, SafeArith};
use std::convert::TryInto;
use tree_hash::TreeHash;
@@ -24,7 +25,7 @@ impl SelectionProof {
Self(Signature::new(message.as_bytes(), secret_key))
}
pub fn is_aggregator(&self, modulo: u64) -> bool {
pub fn is_aggregator(&self, modulo: u64) -> Result<bool, ArithError> {
let signature_hash = self.0.tree_hash_root();
let signature_hash_int = u64::from_le_bytes(
signature_hash[0..8]
@@ -33,7 +34,7 @@ impl SelectionProof {
.expect("first 8 bytes of signature should always convert to fixed array"),
);
signature_hash_int % modulo == 0
signature_hash_int.safe_rem(modulo).map(|rem| rem == 0)
}
}