From 10aee6214c4658322353de9b92df4e8194503c76 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Thu, 14 Mar 2019 14:59:00 +1100 Subject: [PATCH] Tidy per_epoch_processing Mainly renaming variables and files for readability. --- .../benches/bench_epoch_processing.rs | 19 ++---- .../src/per_epoch_processing.rs | 66 ++++++++----------- .../{attesters.rs => validator_statuses.rs} | 37 ++++++----- 3 files changed, 57 insertions(+), 65 deletions(-) rename eth2/state_processing/src/per_epoch_processing/{attesters.rs => validator_statuses.rs} (89%) diff --git a/eth2/state_processing/benches/bench_epoch_processing.rs b/eth2/state_processing/benches/bench_epoch_processing.rs index d95f1c819..49b4f4371 100644 --- a/eth2/state_processing/benches/bench_epoch_processing.rs +++ b/eth2/state_processing/benches/bench_epoch_processing.rs @@ -4,7 +4,7 @@ use ssz::TreeHash; use state_processing::{ per_epoch_processing, per_epoch_processing::{ - calculate_attester_sets, clean_attestations, process_crosslinks, process_eth1_data, + clean_attestations, initialize_validator_statuses, process_crosslinks, process_eth1_data, process_justification, process_rewards_and_penalities, process_validator_registry, update_active_tree_index_roots, update_latest_slashed_balances, }, @@ -93,11 +93,11 @@ fn bench_epoch_processing(c: &mut Criterion, state: &BeaconState, spec: &ChainSp let spec_clone = spec.clone(); c.bench( &format!("{}/epoch_processing", desc), - Benchmark::new("calculate_attester_sets", move |b| { + Benchmark::new("initialize_validator_statuses", move |b| { b.iter_batched( || state_clone.clone(), |mut state| { - calculate_attester_sets(&mut state, &spec_clone).unwrap(); + initialize_validator_statuses(&mut state, &spec_clone).unwrap(); state }, criterion::BatchSize::SmallInput, @@ -108,21 +108,14 @@ fn bench_epoch_processing(c: &mut Criterion, state: &BeaconState, spec: &ChainSp let state_clone = state.clone(); let spec_clone = spec.clone(); - let attesters = calculate_attester_sets(&state, &spec).unwrap(); + let attesters = initialize_validator_statuses(&state, &spec).unwrap(); c.bench( &format!("{}/epoch_processing", desc), Benchmark::new("process_justification", move |b| { b.iter_batched( || state_clone.clone(), |mut state| { - process_justification( - &mut state, - attesters.balances.current_epoch_total, - attesters.balances.previous_epoch_total, - attesters.balances.previous_epoch_boundary_attesters, - attesters.balances.current_epoch_boundary_attesters, - &spec_clone, - ); + process_justification(&mut state, &attesters.total_balances, &spec_clone); state }, criterion::BatchSize::SmallInput, @@ -147,7 +140,7 @@ fn bench_epoch_processing(c: &mut Criterion, state: &BeaconState, spec: &ChainSp let mut state_clone = state.clone(); let spec_clone = spec.clone(); - let attesters = calculate_attester_sets(&state, &spec).unwrap(); + let attesters = initialize_validator_statuses(&state, &spec).unwrap(); let winning_root_for_shards = process_crosslinks(&mut state_clone, &spec).unwrap(); c.bench( &format!("{}/epoch_processing", desc), diff --git a/eth2/state_processing/src/per_epoch_processing.rs b/eth2/state_processing/src/per_epoch_processing.rs index 03135df66..044d32eae 100644 --- a/eth2/state_processing/src/per_epoch_processing.rs +++ b/eth2/state_processing/src/per_epoch_processing.rs @@ -1,17 +1,17 @@ -use attesters::Attesters; use errors::EpochProcessingError as Error; use integer_sqrt::IntegerSquareRoot; use rayon::prelude::*; use ssz::TreeHash; use std::collections::HashMap; use types::{validator_registry::get_active_validator_indices, *}; +use validator_statuses::{TotalBalances, ValidatorStatuses}; use winning_root::{winning_root, WinningRoot}; pub mod attester_sets; -pub mod attesters; pub mod errors; pub mod inclusion_distance; pub mod tests; +pub mod validator_statuses; pub mod winning_root; /// Maps a shard to a winning root. @@ -31,24 +31,17 @@ pub fn per_epoch_processing(state: &mut BeaconState, spec: &ChainSpec) -> Result state.build_epoch_cache(RelativeEpoch::Current, spec)?; state.build_epoch_cache(RelativeEpoch::Next, spec)?; - let mut attesters = calculate_attester_sets(&state, spec)?; + let mut statuses = initialize_validator_statuses(&state, spec)?; process_eth1_data(state, spec); - process_justification( - state, - attesters.balances.current_epoch_total, - attesters.balances.previous_epoch_total, - attesters.balances.previous_epoch_boundary_attesters, - attesters.balances.current_epoch_boundary_attesters, - spec, - ); + process_justification(state, &statuses.total_balances, spec); // Crosslinks let winning_root_for_shards = process_crosslinks(state, spec)?; // Rewards and Penalities - process_rewards_and_penalities(state, &mut attesters, &winning_root_for_shards, spec)?; + process_rewards_and_penalities(state, &mut statuses, &winning_root_for_shards, spec)?; // Ejections state.process_ejections(spec); @@ -85,15 +78,15 @@ pub fn calculate_active_validator_indices(state: &BeaconState, spec: &ChainSpec) /// - etc. /// /// Spec v0.4.0 -pub fn calculate_attester_sets( +pub fn initialize_validator_statuses( state: &BeaconState, spec: &ChainSpec, -) -> Result { - let mut attesters = Attesters::new(state, spec); +) -> Result { + let mut statuses = ValidatorStatuses::new(state, spec); - attesters.process_attestations(&state, &state.latest_attestations, spec)?; + statuses.process_attestations(&state, &state.latest_attestations, spec)?; - Ok(attesters) + Ok(statuses) } /// Spec v0.4.0 @@ -121,10 +114,7 @@ pub fn process_eth1_data(state: &mut BeaconState, spec: &ChainSpec) { /// Spec v0.4.0 pub fn process_justification( state: &mut BeaconState, - current_total_balance: u64, - previous_total_balance: u64, - previous_epoch_boundary_attesting_balance: u64, - current_epoch_boundary_attesting_balance: u64, + total_balances: &TotalBalances, spec: &ChainSpec, ) { let previous_epoch = state.previous_epoch(spec); @@ -137,7 +127,8 @@ pub fn process_justification( // // - Set the 2nd bit of the bitfield. // - Set the previous epoch to be justified. - if (3 * previous_epoch_boundary_attesting_balance) >= (2 * previous_total_balance) { + if (3 * total_balances.previous_epoch_boundary_attesters) >= (2 * total_balances.previous_epoch) + { state.justification_bitfield |= 2; new_justified_epoch = previous_epoch; } @@ -145,7 +136,7 @@ pub fn process_justification( // // - Set the 1st bit of the bitfield. // - Set the current epoch to be justified. - if (3 * current_epoch_boundary_attesting_balance) >= (2 * current_total_balance) { + if (3 * total_balances.current_epoch_boundary_attesters) >= (2 * total_balances.current_epoch) { state.justification_bitfield |= 1; new_justified_epoch = current_epoch; } @@ -267,25 +258,26 @@ pub fn process_crosslinks( /// Spec v0.4.0 pub fn process_rewards_and_penalities( state: &mut BeaconState, - attesters: &mut Attesters, + statuses: &mut ValidatorStatuses, winning_root_for_shards: &WinningRootHashSet, spec: &ChainSpec, ) -> Result<(), Error> { let next_epoch = state.next_epoch(spec); - let previous_total_balance = attesters.balances.previous_epoch_total; + statuses.process_winning_roots(state, winning_root_for_shards, spec)?; - let base_reward_quotient = previous_total_balance.integer_sqrt() / spec.base_reward_quotient; + let total_balances = &statuses.total_balances; + + let base_reward_quotient = + total_balances.previous_epoch.integer_sqrt() / spec.base_reward_quotient; if base_reward_quotient == 0 { return Err(Error::BaseRewardQuotientIsZero); } - if previous_total_balance == 0 { + if total_balances.previous_epoch == 0 { return Err(Error::PreviousTotalBalanceIsZero); } - attesters.process_winning_roots(state, winning_root_for_shards, spec)?; - // Justification and finalization let epochs_since_finality = next_epoch - state.finalized_epoch; @@ -296,7 +288,7 @@ pub fn process_rewards_and_penalities( .enumerate() .map(|(index, &balance)| { let mut balance = balance; - let status = &attesters.statuses[index]; + let status = &statuses.get(index); let base_reward = state.base_reward(index, base_reward_quotient, spec); if epochs_since_finality <= 4 { @@ -304,8 +296,8 @@ pub fn process_rewards_and_penalities( if status.is_previous_epoch_attester { safe_add_assign!( balance, - base_reward * attesters.balances.previous_epoch_attesters - / previous_total_balance + base_reward * total_balances.previous_epoch_attesters + / total_balances.previous_epoch ); } else if status.is_active_in_previous_epoch { safe_sub_assign!(balance, base_reward); @@ -315,8 +307,8 @@ pub fn process_rewards_and_penalities( if status.is_previous_epoch_boundary_attester { safe_add_assign!( balance, - base_reward * attesters.balances.previous_epoch_boundary_attesters - / previous_total_balance + base_reward * total_balances.previous_epoch_boundary_attesters + / total_balances.previous_epoch ); } else if status.is_active_in_previous_epoch { safe_sub_assign!(balance, base_reward); @@ -326,8 +318,8 @@ pub fn process_rewards_and_penalities( if status.is_previous_epoch_head_attester { safe_add_assign!( balance, - base_reward * attesters.balances.previous_epoch_head_attesters - / previous_total_balance + base_reward * total_balances.previous_epoch_head_attesters + / total_balances.previous_epoch ); } else if status.is_active_in_previous_epoch { safe_sub_assign!(balance, base_reward); @@ -376,7 +368,7 @@ pub fn process_rewards_and_penalities( // Attestation inclusion for (index, _validator) in state.validator_registry.iter().enumerate() { - let status = &attesters.statuses[index]; + let status = &statuses.get(index); if status.is_previous_epoch_attester { let proposer_index = status.inclusion_info.proposer_index; diff --git a/eth2/state_processing/src/per_epoch_processing/attesters.rs b/eth2/state_processing/src/per_epoch_processing/validator_statuses.rs similarity index 89% rename from eth2/state_processing/src/per_epoch_processing/attesters.rs rename to eth2/state_processing/src/per_epoch_processing/validator_statuses.rs index 1ffbdf652..70eeaf82a 100644 --- a/eth2/state_processing/src/per_epoch_processing/attesters.rs +++ b/eth2/state_processing/src/per_epoch_processing/validator_statuses.rs @@ -74,8 +74,8 @@ impl AttesterStatus { #[derive(Default, Clone)] pub struct TotalBalances { - pub current_epoch_total: u64, - pub previous_epoch_total: u64, + pub current_epoch: u64, + pub previous_epoch: u64, pub current_epoch_attesters: u64, pub current_epoch_boundary_attesters: u64, pub previous_epoch_attesters: u64, @@ -84,33 +84,40 @@ pub struct TotalBalances { } #[derive(Clone)] -pub struct Attesters { - pub statuses: Vec, - pub balances: TotalBalances, +pub struct ValidatorStatuses { + statuses: Vec, + pub total_balances: TotalBalances, } -impl Attesters { +impl ValidatorStatuses { pub fn new(state: &BeaconState, spec: &ChainSpec) -> Self { let mut statuses = Vec::with_capacity(state.validator_registry.len()); - let mut balances = TotalBalances::default(); + let mut total_balances = TotalBalances::default(); for (i, validator) in state.validator_registry.iter().enumerate() { let mut status = AttesterStatus::default(); if validator.is_active_at(state.current_epoch(spec)) { status.is_active_in_current_epoch = true; - balances.current_epoch_total += state.get_effective_balance(i, spec); + total_balances.current_epoch += state.get_effective_balance(i, spec); } if validator.is_active_at(state.previous_epoch(spec)) { status.is_active_in_previous_epoch = true; - balances.previous_epoch_total += state.get_effective_balance(i, spec); + total_balances.previous_epoch += state.get_effective_balance(i, spec); } statuses.push(status); } - Self { statuses, balances } + Self { + statuses, + total_balances, + } + } + + pub fn get(&self, i: usize) -> &AttesterStatus { + &self.statuses[i] } pub fn process_attestations( @@ -129,15 +136,15 @@ impl Attesters { // Profile this attestation, updating the total balances and generating an // `AttesterStatus` object that applies to all participants in the attestation. if is_from_epoch(a, state.current_epoch(spec), spec) { - self.balances.current_epoch_attesters += attesting_balance; + self.total_balances.current_epoch_attesters += attesting_balance; status.is_current_epoch_attester = true; if has_common_epoch_boundary_root(a, state, state.current_epoch(spec), spec)? { - self.balances.current_epoch_boundary_attesters += attesting_balance; + self.total_balances.current_epoch_boundary_attesters += attesting_balance; status.is_current_epoch_boundary_attester = true; } } else if is_from_epoch(a, state.previous_epoch(spec), spec) { - self.balances.previous_epoch_attesters += attesting_balance; + self.total_balances.previous_epoch_attesters += attesting_balance; status.is_previous_epoch_attester = true; // The inclusion slot and distance are only required for previous epoch attesters. @@ -148,12 +155,12 @@ impl Attesters { }; if has_common_epoch_boundary_root(a, state, state.previous_epoch(spec), spec)? { - self.balances.previous_epoch_boundary_attesters += attesting_balance; + self.total_balances.previous_epoch_boundary_attesters += attesting_balance; status.is_previous_epoch_boundary_attester = true; } if has_common_beacon_block_root(a, state, spec)? { - self.balances.previous_epoch_head_attesters += attesting_balance; + self.total_balances.previous_epoch_head_attesters += attesting_balance; status.is_previous_epoch_head_attester = true; } }