From ce73705498f3c39504b2822f67f422da7a3bfdb2 Mon Sep 17 00:00:00 2001 From: Grant Wuerker Date: Tue, 6 Aug 2019 19:17:15 +0200 Subject: [PATCH] formatting --- beacon_node/beacon_chain/src/beacon_chain.rs | 52 +++++++++++++------- beacon_node/beacon_chain/src/errors.rs | 6 ++- beacon_node/beacon_chain/src/fork_choice.rs | 22 +++++---- beacon_node/beacon_chain/src/test_utils.rs | 7 +-- beacon_node/beacon_chain/tests/tests.rs | 34 ++++++++----- beacon_node/rpc/src/attestation.rs | 14 ++++-- eth2/lmd_ghost/src/reduced_tree.rs | 6 +-- 7 files changed, 85 insertions(+), 56 deletions(-) diff --git a/beacon_node/beacon_chain/src/beacon_chain.rs b/beacon_node/beacon_chain/src/beacon_chain.rs index 49e2cec83..0becbf2c9 100644 --- a/beacon_node/beacon_chain/src/beacon_chain.rs +++ b/beacon_node/beacon_chain/src/beacon_chain.rs @@ -3,6 +3,7 @@ use crate::errors::{BeaconChainError as Error, BlockProductionError}; use crate::fork_choice::{Error as ForkChoiceError, ForkChoice}; use crate::metrics::Metrics; use crate::persisted_beacon_chain::{PersistedBeaconChain, BEACON_CHAIN_DB_KEY}; +use crate::BeaconChainError; use lmd_ghost::LmdGhost; use log::trace; use operation_pool::DepositInsertStatus; @@ -11,19 +12,18 @@ use parking_lot::{RwLock, RwLockReadGuard}; use slog::{error, info, warn, Logger}; use slot_clock::SlotClock; use state_processing::per_block_processing::errors::{ - AttesterSlashingValidationError, DepositValidationError, - ExitValidationError, ProposerSlashingValidationError, TransferValidationError, + AttesterSlashingValidationError, DepositValidationError, ExitValidationError, + ProposerSlashingValidationError, TransferValidationError, }; use state_processing::{ - per_block_processing, per_block_processing_without_verifying_block_signature, - per_slot_processing, BlockProcessingError, common + common, per_block_processing, per_block_processing_without_verifying_block_signature, + per_slot_processing, BlockProcessingError, }; use std::sync::Arc; use store::iter::{BestBlockRootsIterator, BlockIterator, BlockRootsIterator, StateRootsIterator}; use store::{Error as DBError, Store}; use tree_hash::TreeHash; use types::*; -use crate::BeaconChainError; // Text included in blocks. // Must be 32-bytes or panic. @@ -511,17 +511,21 @@ impl BeaconChain { /// /// If valid, the attestation is added to the `op_pool` and aggregated with another attestation /// if possible. - pub fn process_attestation( - &self, - attestation: Attestation, - ) -> Result<(), Error> { + pub fn process_attestation(&self, attestation: Attestation) -> Result<(), Error> { self.metrics.attestation_processing_requests.inc(); let timer = self.metrics.attestation_processing_times.start_timer(); if let Some(state) = self.get_attestation_state(&attestation) { - if self.fork_choice.should_process_attestation(&state, &attestation)? { + if self + .fork_choice + .should_process_attestation(&state, &attestation)? + { let indexed_attestation = common::get_indexed_attestation(&state, &attestation)?; - per_block_processing::is_valid_indexed_attestation(&state, &indexed_attestation, &self.spec)?; + per_block_processing::is_valid_indexed_attestation( + &state, + &indexed_attestation, + &self.spec, + )?; self.fork_choice.process_attestation(&state, &attestation)?; } } @@ -540,12 +544,20 @@ impl BeaconChain { } /// Retrieves the `BeaconState` used to create the attestation. - fn get_attestation_state(&self, attestation: &Attestation) -> Option> { + fn get_attestation_state( + &self, + attestation: &Attestation, + ) -> Option> { // Current state is used if the attestation targets a historic block and a slot within an // equal or adjacent epoch. let slots_per_epoch = T::EthSpec::slots_per_epoch(); - let min_slot = (self.state.read().slot.epoch(slots_per_epoch) - 1).start_slot(slots_per_epoch); - let blocks = BestBlockRootsIterator::owned(self.store.clone(), self.state.read().clone(), self.state.read().slot.clone()); + let min_slot = + (self.state.read().slot.epoch(slots_per_epoch) - 1).start_slot(slots_per_epoch); + let blocks = BestBlockRootsIterator::owned( + self.store.clone(), + self.state.read().clone(), + self.state.read().slot.clone(), + ); for (root, slot) in blocks { if root == attestation.data.target.root { return Some(self.state.read().clone()); @@ -554,15 +566,18 @@ impl BeaconChain { if slot == min_slot { break; } - }; + } // A different state is retrieved from the database. - match self.store.get::>(&attestation.data.target.root) { + match self + .store + .get::>(&attestation.data.target.root) + { Ok(Some(block)) => match self.store.get::>(&block.state_root) { Ok(state) => state, - _ => None + _ => None, }, - _ => None + _ => None, } } @@ -1031,4 +1046,3 @@ impl From for Error { Error::BeaconStateError(e) } } - diff --git a/beacon_node/beacon_chain/src/errors.rs b/beacon_node/beacon_chain/src/errors.rs index 4e2170ca8..266c598ac 100644 --- a/beacon_node/beacon_chain/src/errors.rs +++ b/beacon_node/beacon_chain/src/errors.rs @@ -1,9 +1,11 @@ use crate::fork_choice::Error as ForkChoiceError; use crate::metrics::Error as MetricsError; +use state_processing::per_block_processing::errors::{ + AttestationValidationError, IndexedAttestationValidationError, +}; use state_processing::BlockProcessingError; use state_processing::SlotProcessingError; use types::*; -use state_processing::per_block_processing::errors::{AttestationValidationError, IndexedAttestationValidationError}; macro_rules! easy_from_to { ($from: ident, $to: ident) => { @@ -33,7 +35,7 @@ pub enum BeaconChainError { SlotProcessingError(SlotProcessingError), MetricsError(String), AttestationValidationError(AttestationValidationError), - IndexedAttestationValidationError(IndexedAttestationValidationError) + IndexedAttestationValidationError(IndexedAttestationValidationError), } easy_from_to!(SlotProcessingError, BeaconChainError); diff --git a/beacon_node/beacon_chain/src/fork_choice.rs b/beacon_node/beacon_chain/src/fork_choice.rs index 3900575ae..d16a8f9a8 100644 --- a/beacon_node/beacon_chain/src/fork_choice.rs +++ b/beacon_node/beacon_chain/src/fork_choice.rs @@ -3,7 +3,9 @@ use lmd_ghost::LmdGhost; use state_processing::common::get_attesting_indices; use std::sync::Arc; use store::{Error as StoreError, Store}; -use types::{Attestation, BeaconBlock, BeaconState, BeaconStateError, Epoch, EthSpec, Hash256, Slot}; +use types::{ + Attestation, BeaconBlock, BeaconState, BeaconStateError, Epoch, EthSpec, Hash256, Slot, +}; type Result = std::result::Result; @@ -171,7 +173,11 @@ impl ForkChoice { } /// Determines whether or not the given attestation contains a latest message. - pub fn should_process_attestation(&self, state: &BeaconState, attestation: &Attestation) -> Result { + pub fn should_process_attestation( + &self, + state: &BeaconState, + attestation: &Attestation, + ) -> Result { let validator_indices = get_attesting_indices(state, &attestation.data, &attestation.aggregation_bits)?; @@ -179,12 +185,11 @@ impl ForkChoice { Ok(validator_indices .iter() - .find(|&&v| { - match self.backend.latest_message(v) { - Some((_, slot)) => block_slot > slot, - None => true - } - }).is_some()) + .find(|&&v| match self.backend.latest_message(v) { + Some((_, slot)) => block_slot > slot, + None => true, + }) + .is_some()) } // Returns the latest message for a given validator @@ -224,4 +229,3 @@ impl From for Error { Error::BackendError(e) } } - diff --git a/beacon_node/beacon_chain/src/test_utils.rs b/beacon_node/beacon_chain/src/test_utils.rs index 8f0d4c8ee..ab1a31690 100644 --- a/beacon_node/beacon_chain/src/test_utils.rs +++ b/beacon_node/beacon_chain/src/test_utils.rs @@ -178,12 +178,7 @@ where if let BlockProcessingOutcome::Processed { block_root } = outcome { head_block_root = Some(block_root); - self.add_free_attestations( - &attestation_strategy, - &new_state, - block_root, - slot, - ); + self.add_free_attestations(&attestation_strategy, &new_state, block_root, slot); } else { panic!("block should be successfully processed: {:?}", outcome); } diff --git a/beacon_node/beacon_chain/tests/tests.rs b/beacon_node/beacon_chain/tests/tests.rs index cc1a84973..5b8a09faf 100644 --- a/beacon_node/beacon_chain/tests/tests.rs +++ b/beacon_node/beacon_chain/tests/tests.rs @@ -8,7 +8,7 @@ use lmd_ghost::ThreadSafeReducedTree; use rand::Rng; use store::{MemoryStore, Store}; use types::test_utils::{SeedableRng, TestRandom, XorShiftRng}; -use types::{Deposit, EthSpec, Hash256, MinimalEthSpec, Slot, RelativeEpoch}; +use types::{Deposit, EthSpec, Hash256, MinimalEthSpec, RelativeEpoch, Slot}; // Should ideally be divisible by 3. pub const VALIDATOR_COUNT: usize = 24; @@ -270,20 +270,23 @@ fn free_attestations_added_to_fork_choice_some_none() { let validators: Vec = (0..VALIDATOR_COUNT).collect(); let slots: Vec = validators .iter() - .map(|&v| - state.get_attestation_duties(v, RelativeEpoch::Current) + .map(|&v| { + state + .get_attestation_duties(v, RelativeEpoch::Current) .expect("should get attester duties") .unwrap() .slot - ).collect(); + }) + .collect(); let validator_slots: Vec<(&usize, Slot)> = validators.iter().zip(slots).collect(); for (validator, slot) in validator_slots.clone() { let latest_message = fork_choice.latest_message(*validator); - if slot <= num_blocks_produced && slot != 0{ + if slot <= num_blocks_produced && slot != 0 { assert_eq!( - latest_message.unwrap().1, slot, + latest_message.unwrap().1, + slot, "Latest message slot should be equal to attester duty." ) } else { @@ -313,30 +316,35 @@ fn free_attestations_added_to_fork_choice_all_updated() { let validators: Vec = (0..VALIDATOR_COUNT).collect(); let slots: Vec = validators .iter() - .map(|&v| - state.get_attestation_duties(v, RelativeEpoch::Current) + .map(|&v| { + state + .get_attestation_duties(v, RelativeEpoch::Current) .expect("should get attester duties") .unwrap() .slot - ).collect(); + }) + .collect(); let validator_slots: Vec<(&usize, Slot)> = validators.iter().zip(slots).collect(); for (validator, slot) in validator_slots { let latest_message = fork_choice.latest_message(*validator); assert_eq!( - latest_message.unwrap().1, slot, + latest_message.unwrap().1, + slot, "Latest message slot should be equal to attester duty." ); if slot != num_blocks_produced { - let block_root = state.get_block_root(slot) + let block_root = state + .get_block_root(slot) .expect("Should get block root at slot"); assert_eq!( - latest_message.unwrap().0, *block_root, + latest_message.unwrap().0, + *block_root, "Latest message block root should be equal to block at slot." ); } } -} \ No newline at end of file +} diff --git a/beacon_node/rpc/src/attestation.rs b/beacon_node/rpc/src/attestation.rs index c7b3a5711..00a643151 100644 --- a/beacon_node/rpc/src/attestation.rs +++ b/beacon_node/rpc/src/attestation.rs @@ -1,4 +1,4 @@ -use beacon_chain::{BeaconChain, BeaconChainTypes, BeaconChainError}; +use beacon_chain::{BeaconChain, BeaconChainError, BeaconChainTypes}; use eth2_libp2p::PubsubMessage; use eth2_libp2p::TopicBuilder; use eth2_libp2p::BEACON_ATTESTATION_TOPIC; @@ -179,7 +179,11 @@ impl AttestationService for AttestationServiceInstance { "error" => format!("{:?}", e), ); resp.set_success(false); - resp.set_msg(format!("InvalidIndexedAttestation: {:?}", e).as_bytes().to_vec()); + resp.set_msg( + format!("InvalidIndexedAttestation: {:?}", e) + .as_bytes() + .to_vec(), + ); } Err(e) => { // Some other error @@ -190,7 +194,11 @@ impl AttestationService for AttestationServiceInstance { "error" => format!("{:?}", e), ); resp.set_success(false); - resp.set_msg(format!("There was a beacon chain error: {:?}", e).as_bytes().to_vec()); + resp.set_msg( + format!("There was a beacon chain error: {:?}", e) + .as_bytes() + .to_vec(), + ); } }; diff --git a/eth2/lmd_ghost/src/reduced_tree.rs b/eth2/lmd_ghost/src/reduced_tree.rs index 0ef78c37e..5d7074804 100644 --- a/eth2/lmd_ghost/src/reduced_tree.rs +++ b/eth2/lmd_ghost/src/reduced_tree.rs @@ -111,9 +111,7 @@ where } fn latest_message(&self, validator_index: usize) -> Option<(Hash256, Slot)> { - self.core - .write() - .latest_message(validator_index) + self.core.write().latest_message(validator_index) } } @@ -263,7 +261,7 @@ where pub fn latest_message(&mut self, validator_index: usize) -> Option<(Hash256, Slot)> { match self.latest_votes.get(validator_index) { Some(v) => Some((v.hash.clone(), v.slot.clone())), - None => None + None => None, } }