mirror of
https://github.com/sigp/lighthouse.git
synced 2026-04-18 13:28:33 +00:00
Fix various clippy lints
This commit is contained in:
@@ -2,7 +2,6 @@ use crate::traits::{BeaconNode, BeaconNodeError, PublishOutcome};
|
||||
use std::sync::RwLock;
|
||||
use types::{BeaconBlock, Signature, Slot};
|
||||
|
||||
type NonceResult = Result<u64, BeaconNodeError>;
|
||||
type ProduceResult = Result<Option<BeaconBlock>, BeaconNodeError>;
|
||||
type PublishResult = Result<PublishOutcome, BeaconNodeError>;
|
||||
|
||||
|
||||
@@ -333,7 +333,7 @@ fn validate_attestation_signature_optional(
|
||||
ensure!(
|
||||
(attestation.data.latest_crosslink
|
||||
== state.latest_crosslinks[attestation.data.shard as usize])
|
||||
|| (attestation.data.latest_crosslink
|
||||
| (attestation.data.latest_crosslink
|
||||
== state.latest_crosslinks[attestation.data.shard as usize]),
|
||||
AttestationValidationError::BadLatestCrosslinkRoot
|
||||
);
|
||||
|
||||
@@ -226,7 +226,7 @@ impl EpochProcessable for BeaconState {
|
||||
*/
|
||||
|
||||
let mut new_justified_epoch = self.justified_epoch;
|
||||
self.justification_bitfield = self.justification_bitfield << 1;
|
||||
self.justification_bitfield <<= 1;
|
||||
|
||||
// If > 2/3 of the total balance attested to the previous epoch boundary
|
||||
//
|
||||
@@ -277,8 +277,7 @@ impl EpochProcessable for BeaconState {
|
||||
// - The presently justified epoch was two epochs ago.
|
||||
//
|
||||
// Then, set the finalized epoch to two epochs ago.
|
||||
if ((self.justification_bitfield >> 0) % 8 == 0b111)
|
||||
& (self.justified_epoch == previous_epoch - 1)
|
||||
if (self.justification_bitfield % 8 == 0b111) & (self.justified_epoch == previous_epoch - 1)
|
||||
{
|
||||
self.finalized_epoch = self.justified_epoch;
|
||||
trace!("epoch - 2 was finalized (3rd condition).");
|
||||
@@ -289,9 +288,7 @@ impl EpochProcessable for BeaconState {
|
||||
// - Set the previous epoch to be justified.
|
||||
//
|
||||
// Then, set the finalized epoch to be the previous epoch.
|
||||
if ((self.justification_bitfield >> 0) % 4 == 0b11)
|
||||
& (self.justified_epoch == previous_epoch)
|
||||
{
|
||||
if (self.justification_bitfield % 4 == 0b11) & (self.justified_epoch == previous_epoch) {
|
||||
self.finalized_epoch = self.justified_epoch;
|
||||
trace!("epoch - 1 was finalized (4th condition).");
|
||||
}
|
||||
@@ -386,10 +383,8 @@ impl EpochProcessable for BeaconState {
|
||||
base_reward * previous_epoch_justified_attesting_balance
|
||||
/ previous_total_balance
|
||||
);
|
||||
} else {
|
||||
if active_validator_indices_hashset.contains(&index) {
|
||||
safe_sub_assign!(self.validator_balances[index], base_reward);
|
||||
}
|
||||
} else if active_validator_indices_hashset.contains(&index) {
|
||||
safe_sub_assign!(self.validator_balances[index], base_reward);
|
||||
}
|
||||
|
||||
if previous_epoch_boundary_attester_indices_hashset.contains(&index) {
|
||||
@@ -398,10 +393,8 @@ impl EpochProcessable for BeaconState {
|
||||
base_reward * previous_epoch_boundary_attesting_balance
|
||||
/ previous_total_balance
|
||||
);
|
||||
} else {
|
||||
if active_validator_indices_hashset.contains(&index) {
|
||||
safe_sub_assign!(self.validator_balances[index], base_reward);
|
||||
}
|
||||
} else if active_validator_indices_hashset.contains(&index) {
|
||||
safe_sub_assign!(self.validator_balances[index], base_reward);
|
||||
}
|
||||
|
||||
if previous_epoch_head_attester_indices_hashset.contains(&index) {
|
||||
@@ -410,10 +403,8 @@ impl EpochProcessable for BeaconState {
|
||||
base_reward * previous_epoch_head_attesting_balance
|
||||
/ previous_total_balance
|
||||
);
|
||||
} else {
|
||||
if active_validator_indices_hashset.contains(&index) {
|
||||
safe_sub_assign!(self.validator_balances[index], base_reward);
|
||||
}
|
||||
} else if active_validator_indices_hashset.contains(&index) {
|
||||
safe_sub_assign!(self.validator_balances[index], base_reward);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -505,7 +496,7 @@ impl EpochProcessable for BeaconState {
|
||||
if let Some(Ok(winning_root)) = winning_root_for_shards.get(&shard) {
|
||||
// TODO: remove the map.
|
||||
let attesting_validator_indices: HashSet<usize> = HashSet::from_iter(
|
||||
winning_root.attesting_validator_indices.iter().map(|i| *i),
|
||||
winning_root.attesting_validator_indices.iter().cloned(),
|
||||
);
|
||||
|
||||
for index in 0..self.validator_balances.len() {
|
||||
@@ -664,7 +655,7 @@ fn winning_root(
|
||||
}
|
||||
|
||||
let candidate_root = WinningRoot {
|
||||
shard_block_root: shard_block_root.clone(),
|
||||
shard_block_root: *shard_block_root,
|
||||
attesting_validator_indices,
|
||||
total_attesting_balance,
|
||||
total_balance,
|
||||
|
||||
@@ -30,8 +30,6 @@ where
|
||||
|
||||
self.slot += 1;
|
||||
|
||||
let block_proposer = self.get_beacon_proposer_index(self.slot, spec)?;
|
||||
|
||||
self.latest_randao_mixes[self.slot.as_usize() % spec.latest_randao_mixes_length] =
|
||||
self.latest_randao_mixes[(self.slot.as_usize() - 1) % spec.latest_randao_mixes_length];
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
use super::{AggregatePublicKey, AggregateSignature, AttestationData, Bitfield, Hash256};
|
||||
use crate::test_utils::TestRandom;
|
||||
use bls::bls_verify_aggregate;
|
||||
use rand::RngCore;
|
||||
use serde_derive::Serialize;
|
||||
use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
|
||||
|
||||
@@ -283,7 +283,7 @@ impl BeaconState {
|
||||
|
||||
shuffled_active_validator_indices
|
||||
.honey_badger_split(committees_per_epoch as usize)
|
||||
.filter_map(|slice: &[usize]| Some(slice.to_vec()))
|
||||
.map(|slice: &[usize]| slice.to_vec())
|
||||
.collect()
|
||||
}
|
||||
|
||||
@@ -522,11 +522,9 @@ impl BeaconState {
|
||||
.filter(|i| eligible(*i))
|
||||
.collect();
|
||||
eligable_indices.sort_by_key(|i| self.validator_registry[*i].exit_epoch);
|
||||
let mut withdrawn_so_far = 0;
|
||||
for index in eligable_indices {
|
||||
self.prepare_validator_for_withdrawal(index);
|
||||
withdrawn_so_far += 1;
|
||||
if withdrawn_so_far >= spec.max_withdrawals_per_epoch {
|
||||
for (withdrawn_so_far, index) in eligable_indices.iter().enumerate() {
|
||||
self.prepare_validator_for_withdrawal(*index);
|
||||
if withdrawn_so_far as u64 >= spec.max_withdrawals_per_epoch {
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -796,11 +794,7 @@ impl BeaconState {
|
||||
for (i, a) in attestations.iter().enumerate() {
|
||||
let participants =
|
||||
self.get_attestation_participants(&a.data, &a.aggregation_bitfield, spec)?;
|
||||
if participants
|
||||
.iter()
|
||||
.find(|i| **i == validator_index)
|
||||
.is_some()
|
||||
{
|
||||
if participants.iter().any(|i| *i == validator_index) {
|
||||
included_attestations.push(i);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,9 +13,10 @@ use crate::test_utils::TestRandom;
|
||||
use rand::RngCore;
|
||||
use serde_derive::Serialize;
|
||||
use slog;
|
||||
use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
|
||||
use ssz::{hash, ssz_encode, Decodable, DecodeError, Encodable, SszStream, TreeHash};
|
||||
use std::cmp::{Ord, Ordering};
|
||||
use std::fmt;
|
||||
use std::hash::{Hash, Hasher};
|
||||
use std::iter::Iterator;
|
||||
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Rem, Sub, SubAssign};
|
||||
|
||||
@@ -243,6 +244,18 @@ macro_rules! impl_ssz {
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! impl_hash {
|
||||
($type: ident) => {
|
||||
// Implemented to stop clippy lint:
|
||||
// https://rust-lang.github.io/rust-clippy/master/index.html#derive_hash_xor_eq
|
||||
impl Hash for $type {
|
||||
fn hash<H: Hasher>(&self, state: &mut H) {
|
||||
ssz_encode(self).hash(state)
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! impl_common {
|
||||
($type: ident) => {
|
||||
impl_from_into_u64!($type);
|
||||
@@ -252,13 +265,14 @@ macro_rules! impl_common {
|
||||
impl_math!($type);
|
||||
impl_display!($type);
|
||||
impl_ssz!($type);
|
||||
impl_hash!($type);
|
||||
};
|
||||
}
|
||||
|
||||
#[derive(Eq, Debug, Clone, Copy, Default, Serialize, Hash)]
|
||||
#[derive(Eq, Debug, Clone, Copy, Default, Serialize)]
|
||||
pub struct Slot(u64);
|
||||
|
||||
#[derive(Eq, Debug, Clone, Copy, Default, Serialize, Hash)]
|
||||
#[derive(Eq, Debug, Clone, Copy, Default, Serialize)]
|
||||
pub struct Epoch(u64);
|
||||
|
||||
impl_common!(Slot);
|
||||
@@ -269,7 +283,7 @@ impl Slot {
|
||||
Slot(slot)
|
||||
}
|
||||
|
||||
pub fn epoch(&self, epoch_length: u64) -> Epoch {
|
||||
pub fn epoch(self, epoch_length: u64) -> Epoch {
|
||||
Epoch::from(self.0 / epoch_length)
|
||||
}
|
||||
|
||||
@@ -287,11 +301,11 @@ impl Epoch {
|
||||
Epoch(u64::max_value())
|
||||
}
|
||||
|
||||
pub fn start_slot(&self, epoch_length: u64) -> Slot {
|
||||
pub fn start_slot(self, epoch_length: u64) -> Slot {
|
||||
Slot::from(self.0.saturating_mul(epoch_length))
|
||||
}
|
||||
|
||||
pub fn end_slot(&self, epoch_length: u64) -> Slot {
|
||||
pub fn end_slot(self, epoch_length: u64) -> Slot {
|
||||
Slot::from(
|
||||
self.0
|
||||
.saturating_add(1)
|
||||
|
||||
@@ -78,7 +78,7 @@ impl Default for Validator {
|
||||
impl<T: RngCore> TestRandom<T> for StatusFlags {
|
||||
fn random_for_test(rng: &mut T) -> Self {
|
||||
let options = vec![StatusFlags::InitiatedExit, StatusFlags::Withdrawable];
|
||||
options[(rng.next_u32() as usize) % options.len()].clone()
|
||||
options[(rng.next_u32() as usize) % options.len()]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -130,7 +130,7 @@ impl TreeHash for Validator {
|
||||
result.append(&mut self.exit_epoch.hash_tree_root());
|
||||
result.append(&mut self.withdrawal_epoch.hash_tree_root());
|
||||
result.append(&mut self.penalized_epoch.hash_tree_root());
|
||||
result.append(&mut (status_flag_to_byte(self.status_flags) as u64).hash_tree_root());
|
||||
result.append(&mut u64::from(status_flag_to_byte(self.status_flags)).hash_tree_root());
|
||||
hash(&result)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user