Tidy per_epoch_processing
Mainly renaming variables and files for readability.
This commit is contained in:
parent
95599ddc66
commit
10aee6214c
@ -4,7 +4,7 @@ use ssz::TreeHash;
|
|||||||
use state_processing::{
|
use state_processing::{
|
||||||
per_epoch_processing,
|
per_epoch_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,
|
process_justification, process_rewards_and_penalities, process_validator_registry,
|
||||||
update_active_tree_index_roots, update_latest_slashed_balances,
|
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();
|
let spec_clone = spec.clone();
|
||||||
c.bench(
|
c.bench(
|
||||||
&format!("{}/epoch_processing", desc),
|
&format!("{}/epoch_processing", desc),
|
||||||
Benchmark::new("calculate_attester_sets", move |b| {
|
Benchmark::new("initialize_validator_statuses", move |b| {
|
||||||
b.iter_batched(
|
b.iter_batched(
|
||||||
|| state_clone.clone(),
|
|| state_clone.clone(),
|
||||||
|mut state| {
|
|mut state| {
|
||||||
calculate_attester_sets(&mut state, &spec_clone).unwrap();
|
initialize_validator_statuses(&mut state, &spec_clone).unwrap();
|
||||||
state
|
state
|
||||||
},
|
},
|
||||||
criterion::BatchSize::SmallInput,
|
criterion::BatchSize::SmallInput,
|
||||||
@ -108,21 +108,14 @@ fn bench_epoch_processing(c: &mut Criterion, state: &BeaconState, spec: &ChainSp
|
|||||||
|
|
||||||
let state_clone = state.clone();
|
let state_clone = state.clone();
|
||||||
let spec_clone = spec.clone();
|
let spec_clone = spec.clone();
|
||||||
let attesters = calculate_attester_sets(&state, &spec).unwrap();
|
let attesters = initialize_validator_statuses(&state, &spec).unwrap();
|
||||||
c.bench(
|
c.bench(
|
||||||
&format!("{}/epoch_processing", desc),
|
&format!("{}/epoch_processing", desc),
|
||||||
Benchmark::new("process_justification", move |b| {
|
Benchmark::new("process_justification", move |b| {
|
||||||
b.iter_batched(
|
b.iter_batched(
|
||||||
|| state_clone.clone(),
|
|| state_clone.clone(),
|
||||||
|mut state| {
|
|mut state| {
|
||||||
process_justification(
|
process_justification(&mut state, &attesters.total_balances, &spec_clone);
|
||||||
&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,
|
|
||||||
);
|
|
||||||
state
|
state
|
||||||
},
|
},
|
||||||
criterion::BatchSize::SmallInput,
|
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 mut state_clone = state.clone();
|
||||||
let spec_clone = spec.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();
|
let winning_root_for_shards = process_crosslinks(&mut state_clone, &spec).unwrap();
|
||||||
c.bench(
|
c.bench(
|
||||||
&format!("{}/epoch_processing", desc),
|
&format!("{}/epoch_processing", desc),
|
||||||
|
@ -1,17 +1,17 @@
|
|||||||
use attesters::Attesters;
|
|
||||||
use errors::EpochProcessingError as Error;
|
use errors::EpochProcessingError as Error;
|
||||||
use integer_sqrt::IntegerSquareRoot;
|
use integer_sqrt::IntegerSquareRoot;
|
||||||
use rayon::prelude::*;
|
use rayon::prelude::*;
|
||||||
use ssz::TreeHash;
|
use ssz::TreeHash;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use types::{validator_registry::get_active_validator_indices, *};
|
use types::{validator_registry::get_active_validator_indices, *};
|
||||||
|
use validator_statuses::{TotalBalances, ValidatorStatuses};
|
||||||
use winning_root::{winning_root, WinningRoot};
|
use winning_root::{winning_root, WinningRoot};
|
||||||
|
|
||||||
pub mod attester_sets;
|
pub mod attester_sets;
|
||||||
pub mod attesters;
|
|
||||||
pub mod errors;
|
pub mod errors;
|
||||||
pub mod inclusion_distance;
|
pub mod inclusion_distance;
|
||||||
pub mod tests;
|
pub mod tests;
|
||||||
|
pub mod validator_statuses;
|
||||||
pub mod winning_root;
|
pub mod winning_root;
|
||||||
|
|
||||||
/// Maps a shard to a 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::Current, spec)?;
|
||||||
state.build_epoch_cache(RelativeEpoch::Next, 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_eth1_data(state, spec);
|
||||||
|
|
||||||
process_justification(
|
process_justification(state, &statuses.total_balances, spec);
|
||||||
state,
|
|
||||||
attesters.balances.current_epoch_total,
|
|
||||||
attesters.balances.previous_epoch_total,
|
|
||||||
attesters.balances.previous_epoch_boundary_attesters,
|
|
||||||
attesters.balances.current_epoch_boundary_attesters,
|
|
||||||
spec,
|
|
||||||
);
|
|
||||||
|
|
||||||
// Crosslinks
|
// Crosslinks
|
||||||
let winning_root_for_shards = process_crosslinks(state, spec)?;
|
let winning_root_for_shards = process_crosslinks(state, spec)?;
|
||||||
|
|
||||||
// Rewards and Penalities
|
// 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
|
// Ejections
|
||||||
state.process_ejections(spec);
|
state.process_ejections(spec);
|
||||||
@ -85,15 +78,15 @@ pub fn calculate_active_validator_indices(state: &BeaconState, spec: &ChainSpec)
|
|||||||
/// - etc.
|
/// - etc.
|
||||||
///
|
///
|
||||||
/// Spec v0.4.0
|
/// Spec v0.4.0
|
||||||
pub fn calculate_attester_sets(
|
pub fn initialize_validator_statuses(
|
||||||
state: &BeaconState,
|
state: &BeaconState,
|
||||||
spec: &ChainSpec,
|
spec: &ChainSpec,
|
||||||
) -> Result<Attesters, BeaconStateError> {
|
) -> Result<ValidatorStatuses, BeaconStateError> {
|
||||||
let mut attesters = Attesters::new(state, spec);
|
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
|
/// Spec v0.4.0
|
||||||
@ -121,10 +114,7 @@ pub fn process_eth1_data(state: &mut BeaconState, spec: &ChainSpec) {
|
|||||||
/// Spec v0.4.0
|
/// Spec v0.4.0
|
||||||
pub fn process_justification(
|
pub fn process_justification(
|
||||||
state: &mut BeaconState,
|
state: &mut BeaconState,
|
||||||
current_total_balance: u64,
|
total_balances: &TotalBalances,
|
||||||
previous_total_balance: u64,
|
|
||||||
previous_epoch_boundary_attesting_balance: u64,
|
|
||||||
current_epoch_boundary_attesting_balance: u64,
|
|
||||||
spec: &ChainSpec,
|
spec: &ChainSpec,
|
||||||
) {
|
) {
|
||||||
let previous_epoch = state.previous_epoch(spec);
|
let previous_epoch = state.previous_epoch(spec);
|
||||||
@ -137,7 +127,8 @@ pub fn process_justification(
|
|||||||
//
|
//
|
||||||
// - Set the 2nd bit of the bitfield.
|
// - Set the 2nd bit of the bitfield.
|
||||||
// - Set the previous epoch to be justified.
|
// - 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;
|
state.justification_bitfield |= 2;
|
||||||
new_justified_epoch = previous_epoch;
|
new_justified_epoch = previous_epoch;
|
||||||
}
|
}
|
||||||
@ -145,7 +136,7 @@ pub fn process_justification(
|
|||||||
//
|
//
|
||||||
// - Set the 1st bit of the bitfield.
|
// - Set the 1st bit of the bitfield.
|
||||||
// - Set the current epoch to be justified.
|
// - 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;
|
state.justification_bitfield |= 1;
|
||||||
new_justified_epoch = current_epoch;
|
new_justified_epoch = current_epoch;
|
||||||
}
|
}
|
||||||
@ -267,25 +258,26 @@ pub fn process_crosslinks(
|
|||||||
/// Spec v0.4.0
|
/// Spec v0.4.0
|
||||||
pub fn process_rewards_and_penalities(
|
pub fn process_rewards_and_penalities(
|
||||||
state: &mut BeaconState,
|
state: &mut BeaconState,
|
||||||
attesters: &mut Attesters,
|
statuses: &mut ValidatorStatuses,
|
||||||
winning_root_for_shards: &WinningRootHashSet,
|
winning_root_for_shards: &WinningRootHashSet,
|
||||||
spec: &ChainSpec,
|
spec: &ChainSpec,
|
||||||
) -> Result<(), Error> {
|
) -> Result<(), Error> {
|
||||||
let next_epoch = state.next_epoch(spec);
|
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 {
|
if base_reward_quotient == 0 {
|
||||||
return Err(Error::BaseRewardQuotientIsZero);
|
return Err(Error::BaseRewardQuotientIsZero);
|
||||||
}
|
}
|
||||||
if previous_total_balance == 0 {
|
if total_balances.previous_epoch == 0 {
|
||||||
return Err(Error::PreviousTotalBalanceIsZero);
|
return Err(Error::PreviousTotalBalanceIsZero);
|
||||||
}
|
}
|
||||||
|
|
||||||
attesters.process_winning_roots(state, winning_root_for_shards, spec)?;
|
|
||||||
|
|
||||||
// Justification and finalization
|
// Justification and finalization
|
||||||
|
|
||||||
let epochs_since_finality = next_epoch - state.finalized_epoch;
|
let epochs_since_finality = next_epoch - state.finalized_epoch;
|
||||||
@ -296,7 +288,7 @@ pub fn process_rewards_and_penalities(
|
|||||||
.enumerate()
|
.enumerate()
|
||||||
.map(|(index, &balance)| {
|
.map(|(index, &balance)| {
|
||||||
let mut balance = 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);
|
let base_reward = state.base_reward(index, base_reward_quotient, spec);
|
||||||
|
|
||||||
if epochs_since_finality <= 4 {
|
if epochs_since_finality <= 4 {
|
||||||
@ -304,8 +296,8 @@ pub fn process_rewards_and_penalities(
|
|||||||
if status.is_previous_epoch_attester {
|
if status.is_previous_epoch_attester {
|
||||||
safe_add_assign!(
|
safe_add_assign!(
|
||||||
balance,
|
balance,
|
||||||
base_reward * attesters.balances.previous_epoch_attesters
|
base_reward * total_balances.previous_epoch_attesters
|
||||||
/ previous_total_balance
|
/ total_balances.previous_epoch
|
||||||
);
|
);
|
||||||
} else if status.is_active_in_previous_epoch {
|
} else if status.is_active_in_previous_epoch {
|
||||||
safe_sub_assign!(balance, base_reward);
|
safe_sub_assign!(balance, base_reward);
|
||||||
@ -315,8 +307,8 @@ pub fn process_rewards_and_penalities(
|
|||||||
if status.is_previous_epoch_boundary_attester {
|
if status.is_previous_epoch_boundary_attester {
|
||||||
safe_add_assign!(
|
safe_add_assign!(
|
||||||
balance,
|
balance,
|
||||||
base_reward * attesters.balances.previous_epoch_boundary_attesters
|
base_reward * total_balances.previous_epoch_boundary_attesters
|
||||||
/ previous_total_balance
|
/ total_balances.previous_epoch
|
||||||
);
|
);
|
||||||
} else if status.is_active_in_previous_epoch {
|
} else if status.is_active_in_previous_epoch {
|
||||||
safe_sub_assign!(balance, base_reward);
|
safe_sub_assign!(balance, base_reward);
|
||||||
@ -326,8 +318,8 @@ pub fn process_rewards_and_penalities(
|
|||||||
if status.is_previous_epoch_head_attester {
|
if status.is_previous_epoch_head_attester {
|
||||||
safe_add_assign!(
|
safe_add_assign!(
|
||||||
balance,
|
balance,
|
||||||
base_reward * attesters.balances.previous_epoch_head_attesters
|
base_reward * total_balances.previous_epoch_head_attesters
|
||||||
/ previous_total_balance
|
/ total_balances.previous_epoch
|
||||||
);
|
);
|
||||||
} else if status.is_active_in_previous_epoch {
|
} else if status.is_active_in_previous_epoch {
|
||||||
safe_sub_assign!(balance, base_reward);
|
safe_sub_assign!(balance, base_reward);
|
||||||
@ -376,7 +368,7 @@ pub fn process_rewards_and_penalities(
|
|||||||
// Attestation inclusion
|
// Attestation inclusion
|
||||||
|
|
||||||
for (index, _validator) in state.validator_registry.iter().enumerate() {
|
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 {
|
if status.is_previous_epoch_attester {
|
||||||
let proposer_index = status.inclusion_info.proposer_index;
|
let proposer_index = status.inclusion_info.proposer_index;
|
||||||
|
@ -74,8 +74,8 @@ impl AttesterStatus {
|
|||||||
|
|
||||||
#[derive(Default, Clone)]
|
#[derive(Default, Clone)]
|
||||||
pub struct TotalBalances {
|
pub struct TotalBalances {
|
||||||
pub current_epoch_total: u64,
|
pub current_epoch: u64,
|
||||||
pub previous_epoch_total: u64,
|
pub previous_epoch: u64,
|
||||||
pub current_epoch_attesters: u64,
|
pub current_epoch_attesters: u64,
|
||||||
pub current_epoch_boundary_attesters: u64,
|
pub current_epoch_boundary_attesters: u64,
|
||||||
pub previous_epoch_attesters: u64,
|
pub previous_epoch_attesters: u64,
|
||||||
@ -84,33 +84,40 @@ pub struct TotalBalances {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Attesters {
|
pub struct ValidatorStatuses {
|
||||||
pub statuses: Vec<AttesterStatus>,
|
statuses: Vec<AttesterStatus>,
|
||||||
pub balances: TotalBalances,
|
pub total_balances: TotalBalances,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Attesters {
|
impl ValidatorStatuses {
|
||||||
pub fn new(state: &BeaconState, spec: &ChainSpec) -> Self {
|
pub fn new(state: &BeaconState, spec: &ChainSpec) -> Self {
|
||||||
let mut statuses = Vec::with_capacity(state.validator_registry.len());
|
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() {
|
for (i, validator) in state.validator_registry.iter().enumerate() {
|
||||||
let mut status = AttesterStatus::default();
|
let mut status = AttesterStatus::default();
|
||||||
|
|
||||||
if validator.is_active_at(state.current_epoch(spec)) {
|
if validator.is_active_at(state.current_epoch(spec)) {
|
||||||
status.is_active_in_current_epoch = true;
|
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)) {
|
if validator.is_active_at(state.previous_epoch(spec)) {
|
||||||
status.is_active_in_previous_epoch = true;
|
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);
|
statuses.push(status);
|
||||||
}
|
}
|
||||||
|
|
||||||
Self { statuses, balances }
|
Self {
|
||||||
|
statuses,
|
||||||
|
total_balances,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get(&self, i: usize) -> &AttesterStatus {
|
||||||
|
&self.statuses[i]
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn process_attestations(
|
pub fn process_attestations(
|
||||||
@ -129,15 +136,15 @@ impl Attesters {
|
|||||||
// Profile this attestation, updating the total balances and generating an
|
// Profile this attestation, updating the total balances and generating an
|
||||||
// `AttesterStatus` object that applies to all participants in the attestation.
|
// `AttesterStatus` object that applies to all participants in the attestation.
|
||||||
if is_from_epoch(a, state.current_epoch(spec), spec) {
|
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;
|
status.is_current_epoch_attester = true;
|
||||||
|
|
||||||
if has_common_epoch_boundary_root(a, state, state.current_epoch(spec), spec)? {
|
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;
|
status.is_current_epoch_boundary_attester = true;
|
||||||
}
|
}
|
||||||
} else if is_from_epoch(a, state.previous_epoch(spec), spec) {
|
} 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;
|
status.is_previous_epoch_attester = true;
|
||||||
|
|
||||||
// The inclusion slot and distance are only required for previous epoch attesters.
|
// 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)? {
|
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;
|
status.is_previous_epoch_boundary_attester = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if has_common_beacon_block_root(a, state, spec)? {
|
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;
|
status.is_previous_epoch_head_attester = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user