Update rewards processing to v0.5.0
This commit is contained in:
parent
37b8e9f39a
commit
d20fb93f0c
@ -1,16 +1,16 @@
|
||||
use apply_rewards::apply_rewards;
|
||||
use errors::EpochProcessingError as Error;
|
||||
use integer_sqrt::IntegerSquareRoot;
|
||||
use process_ejections::process_ejections;
|
||||
use process_exit_queue::process_exit_queue;
|
||||
use process_slashings::process_slashings;
|
||||
use process_validator_registry::process_validator_registry;
|
||||
use rayon::prelude::*;
|
||||
use ssz::TreeHash;
|
||||
use std::collections::HashMap;
|
||||
use types::*;
|
||||
use validator_statuses::{TotalBalances, ValidatorStatuses};
|
||||
use winning_root::{winning_root, WinningRoot};
|
||||
|
||||
pub mod apply_rewards;
|
||||
pub mod errors;
|
||||
pub mod get_attestation_participants;
|
||||
pub mod inclusion_distance;
|
||||
@ -43,13 +43,13 @@ pub fn per_epoch_processing(state: &mut BeaconState, spec: &ChainSpec) -> Result
|
||||
|
||||
process_eth1_data(state, spec);
|
||||
|
||||
process_justification(state, &statuses.total_balances, spec);
|
||||
update_justification_and_finalization(state, &statuses.total_balances, spec)?;
|
||||
|
||||
// Crosslinks
|
||||
let winning_root_for_shards = process_crosslinks(state, spec)?;
|
||||
|
||||
// Rewards and Penalities
|
||||
process_rewards_and_penalities(state, &mut statuses, &winning_root_for_shards, spec)?;
|
||||
apply_rewards(state, &mut statuses, &winning_root_for_shards, spec)?;
|
||||
|
||||
// Ejections
|
||||
process_ejections(state, spec)?;
|
||||
@ -62,7 +62,7 @@ pub fn per_epoch_processing(state: &mut BeaconState, spec: &ChainSpec) -> Result
|
||||
// Final updates
|
||||
update_active_tree_index_roots(state, spec)?;
|
||||
update_latest_slashed_balances(state, spec)?;
|
||||
clean_attestations(state);
|
||||
state.previous_epoch_attestations = vec![];
|
||||
|
||||
// Rotate the epoch caches to suit the epoch transition.
|
||||
state.advance_caches();
|
||||
@ -113,83 +113,68 @@ pub fn process_eth1_data(state: &mut BeaconState, spec: &ChainSpec) {
|
||||
/// - `justified_epoch`
|
||||
/// - `previous_justified_epoch`
|
||||
///
|
||||
/// Spec v0.4.0
|
||||
pub fn process_justification(
|
||||
/// Spec v0.5.0
|
||||
pub fn update_justification_and_finalization(
|
||||
state: &mut BeaconState,
|
||||
total_balances: &TotalBalances,
|
||||
spec: &ChainSpec,
|
||||
) {
|
||||
) -> Result<(), Error> {
|
||||
let previous_epoch = state.previous_epoch(spec);
|
||||
let current_epoch = state.current_epoch(spec);
|
||||
|
||||
let mut new_justified_epoch = state.current_justified_epoch;
|
||||
let mut new_finalized_epoch = state.finalized_epoch;
|
||||
|
||||
// Rotate the justification bitfield up one epoch to make room for the current epoch.
|
||||
state.justification_bitfield <<= 1;
|
||||
|
||||
// If > 2/3 of the total balance attested to the previous epoch boundary
|
||||
//
|
||||
// - Set the 2nd bit of the bitfield.
|
||||
// - Set the previous epoch to be justified.
|
||||
if (3 * total_balances.previous_epoch_boundary_attesters) >= (2 * total_balances.previous_epoch)
|
||||
// If the previous epoch gets justified, full the second last bit.
|
||||
if (total_balances.previous_epoch_boundary_attesters * 3) >= (total_balances.previous_epoch * 2)
|
||||
{
|
||||
state.justification_bitfield |= 2;
|
||||
new_justified_epoch = previous_epoch;
|
||||
state.justification_bitfield |= 2;
|
||||
}
|
||||
// If > 2/3 of the total balance attested to the previous epoch boundary
|
||||
//
|
||||
// - Set the 1st bit of the bitfield.
|
||||
// - Set the current epoch to be justified.
|
||||
if (3 * total_balances.current_epoch_boundary_attesters) >= (2 * total_balances.current_epoch) {
|
||||
state.justification_bitfield |= 1;
|
||||
// If the current epoch gets justified, fill the last bit.
|
||||
if (total_balances.current_epoch_boundary_attesters * 3) >= (total_balances.current_epoch * 2) {
|
||||
new_justified_epoch = current_epoch;
|
||||
state.justification_bitfield |= 1;
|
||||
}
|
||||
|
||||
// If:
|
||||
//
|
||||
// - All three epochs prior to this epoch have been justified.
|
||||
// - The previous justified justified epoch was three epochs ago.
|
||||
//
|
||||
// Then, set the finalized epoch to be three epochs ago.
|
||||
if ((state.justification_bitfield >> 1) % 8 == 0b111)
|
||||
& (state.previous_justified_epoch == previous_epoch - 2)
|
||||
{
|
||||
state.finalized_epoch = state.previous_justified_epoch;
|
||||
let bitfield = state.justification_bitfield;
|
||||
|
||||
// The 2nd/3rd/4th most recent epochs are all justified, the 2nd using the 4th as source.
|
||||
if ((bitfield >> 1) % 8 == 0b111) & (state.previous_justified_epoch == current_epoch - 3) {
|
||||
new_finalized_epoch = state.previous_justified_epoch;
|
||||
}
|
||||
// If:
|
||||
//
|
||||
// - Both two epochs prior to this epoch have been justified.
|
||||
// - The previous justified epoch was two epochs ago.
|
||||
//
|
||||
// Then, set the finalized epoch to two epochs ago.
|
||||
if ((state.justification_bitfield >> 1) % 4 == 0b11)
|
||||
& (state.previous_justified_epoch == previous_epoch - 1)
|
||||
{
|
||||
state.finalized_epoch = state.previous_justified_epoch;
|
||||
// The 2nd/3rd most recent epochs are both justified, the 2nd using the 3rd as source.
|
||||
if ((bitfield >> 1) % 4 == 0b11) & (state.previous_justified_epoch == current_epoch - 2) {
|
||||
new_finalized_epoch = state.previous_justified_epoch;
|
||||
}
|
||||
// If:
|
||||
//
|
||||
// - This epoch and the two prior have been justified.
|
||||
// - The presently justified epoch was two epochs ago.
|
||||
//
|
||||
// Then, set the finalized epoch to two epochs ago.
|
||||
if (state.justification_bitfield % 8 == 0b111)
|
||||
& (state.current_justified_epoch == previous_epoch - 1)
|
||||
{
|
||||
state.finalized_epoch = state.current_justified_epoch;
|
||||
// The 1st/2nd/3rd most recent epochs are all justified, the 1st using the 2nd as source.
|
||||
if (bitfield % 8 == 0b111) & (state.current_justified_epoch == current_epoch - 2) {
|
||||
new_finalized_epoch = state.current_justified_epoch;
|
||||
}
|
||||
// If:
|
||||
//
|
||||
// - This epoch and the epoch prior to it have been justified.
|
||||
// - Set the previous epoch to be justified.
|
||||
//
|
||||
// Then, set the finalized epoch to be the previous epoch.
|
||||
if (state.justification_bitfield % 4 == 0b11)
|
||||
& (state.current_justified_epoch == previous_epoch)
|
||||
{
|
||||
state.finalized_epoch = state.current_justified_epoch;
|
||||
// The 1st/2nd most recent epochs are both justified, the 1st using the 2nd as source.
|
||||
if (bitfield % 4 == 0b11) & (state.current_justified_epoch == current_epoch - 1) {
|
||||
new_finalized_epoch = state.current_justified_epoch;
|
||||
}
|
||||
|
||||
state.previous_justified_epoch = state.current_justified_epoch;
|
||||
state.previous_justified_root = state.current_justified_root;
|
||||
|
||||
if new_justified_epoch != state.current_justified_epoch {
|
||||
state.current_justified_epoch = new_justified_epoch;
|
||||
state.current_justified_root =
|
||||
*state.get_block_root(new_justified_epoch.start_slot(spec.slots_per_epoch), spec)?;
|
||||
}
|
||||
|
||||
if new_finalized_epoch != state.finalized_epoch {
|
||||
state.finalized_epoch = new_finalized_epoch;
|
||||
state.finalized_root =
|
||||
*state.get_block_root(new_finalized_epoch.start_slot(spec.slots_per_epoch), spec)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Updates the following fields on the `BeaconState`:
|
||||
@ -239,201 +224,6 @@ pub fn process_crosslinks(
|
||||
Ok(winning_root_for_shards)
|
||||
}
|
||||
|
||||
/// Updates the following fields on the BeaconState:
|
||||
///
|
||||
/// - `validator_balances`
|
||||
///
|
||||
/// Spec v0.4.0
|
||||
pub fn process_rewards_and_penalities(
|
||||
state: &mut BeaconState,
|
||||
statuses: &mut ValidatorStatuses,
|
||||
winning_root_for_shards: &WinningRootHashSet,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), Error> {
|
||||
let next_epoch = state.next_epoch(spec);
|
||||
|
||||
statuses.process_winning_roots(state, winning_root_for_shards, spec)?;
|
||||
|
||||
let total_balances = &statuses.total_balances;
|
||||
|
||||
let base_reward_quotient =
|
||||
total_balances.previous_epoch.integer_sqrt() / spec.base_reward_quotient;
|
||||
|
||||
// Guard against a divide-by-zero during the validator balance update.
|
||||
if base_reward_quotient == 0 {
|
||||
return Err(Error::BaseRewardQuotientIsZero);
|
||||
}
|
||||
// Guard against a divide-by-zero during the validator balance update.
|
||||
if total_balances.previous_epoch == 0 {
|
||||
return Err(Error::PreviousTotalBalanceIsZero);
|
||||
}
|
||||
// Guard against an out-of-bounds during the validator balance update.
|
||||
if statuses.statuses.len() != state.validator_balances.len() {
|
||||
return Err(Error::ValidatorStatusesInconsistent);
|
||||
}
|
||||
|
||||
// Justification and finalization
|
||||
|
||||
let epochs_since_finality = next_epoch - state.finalized_epoch;
|
||||
|
||||
state.validator_balances = state
|
||||
.validator_balances
|
||||
.par_iter()
|
||||
.enumerate()
|
||||
.map(|(index, &balance)| {
|
||||
let mut balance = balance;
|
||||
let status = &statuses.statuses[index];
|
||||
let base_reward = get_base_reward(state, index, total_balances.previous_epoch, spec)
|
||||
.expect(
|
||||
"Cannot fail to access a validator balance when iterating validator balances.",
|
||||
);
|
||||
|
||||
if epochs_since_finality <= 4 {
|
||||
// Expected FFG source
|
||||
if status.is_previous_epoch_attester {
|
||||
safe_add_assign!(
|
||||
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);
|
||||
}
|
||||
|
||||
// Expected FFG target
|
||||
if status.is_previous_epoch_boundary_attester {
|
||||
safe_add_assign!(
|
||||
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);
|
||||
}
|
||||
|
||||
// Expected beacon chain head
|
||||
if status.is_previous_epoch_head_attester {
|
||||
safe_add_assign!(
|
||||
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);
|
||||
};
|
||||
} else {
|
||||
let inactivity_penalty = get_inactivity_penalty(
|
||||
state,
|
||||
index,
|
||||
epochs_since_finality.as_u64(),
|
||||
total_balances.previous_epoch,
|
||||
spec,
|
||||
)
|
||||
.expect(
|
||||
"Cannot fail to access a validator balance when iterating validator balances.",
|
||||
);
|
||||
|
||||
if status.is_active_in_previous_epoch {
|
||||
if !status.is_previous_epoch_attester {
|
||||
safe_sub_assign!(balance, inactivity_penalty);
|
||||
}
|
||||
if !status.is_previous_epoch_boundary_attester {
|
||||
safe_sub_assign!(balance, inactivity_penalty);
|
||||
}
|
||||
if !status.is_previous_epoch_head_attester {
|
||||
safe_sub_assign!(balance, inactivity_penalty);
|
||||
}
|
||||
|
||||
if state.validator_registry[index].slashed {
|
||||
let base_reward =
|
||||
get_base_reward(state, index, total_balances.previous_epoch, spec).expect(
|
||||
"Cannot fail to access a validator balance when iterating validator balances.",
|
||||
);
|
||||
safe_sub_assign!(balance, 2 * inactivity_penalty + base_reward);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Crosslinks
|
||||
|
||||
if let Some(ref info) = status.winning_root_info {
|
||||
safe_add_assign!(
|
||||
balance,
|
||||
base_reward * info.total_attesting_balance / info.total_committee_balance
|
||||
);
|
||||
} else {
|
||||
safe_sub_assign!(balance, base_reward);
|
||||
}
|
||||
|
||||
balance
|
||||
})
|
||||
.collect();
|
||||
|
||||
// Attestation inclusion
|
||||
|
||||
// Guard against an out-of-bounds during the attester inclusion balance update.
|
||||
if statuses.statuses.len() != state.validator_registry.len() {
|
||||
return Err(Error::ValidatorStatusesInconsistent);
|
||||
}
|
||||
|
||||
for (index, _validator) in state.validator_registry.iter().enumerate() {
|
||||
let status = &statuses.statuses[index];
|
||||
|
||||
if status.is_previous_epoch_attester {
|
||||
let proposer_index = status.inclusion_info.proposer_index;
|
||||
let inclusion_distance = status.inclusion_info.distance;
|
||||
|
||||
let base_reward =
|
||||
get_base_reward(state, proposer_index, total_balances.previous_epoch, spec).expect(
|
||||
"Cannot fail to access a validator balance when iterating validator balances.",
|
||||
);
|
||||
|
||||
if inclusion_distance > 0 && inclusion_distance < Slot::max_value() {
|
||||
safe_add_assign!(
|
||||
state.validator_balances[proposer_index],
|
||||
base_reward * spec.min_attestation_inclusion_delay
|
||||
/ inclusion_distance.as_u64()
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Returns the base reward for some validator.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
pub fn get_base_reward(
|
||||
state: &BeaconState,
|
||||
index: usize,
|
||||
previous_total_balance: u64,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<u64, BeaconStateError> {
|
||||
if previous_total_balance == 0 {
|
||||
Ok(0)
|
||||
} else {
|
||||
let adjusted_quotient = previous_total_balance.integer_sqrt() / spec.base_reward_quotient;
|
||||
Ok(state.get_effective_balance(index, spec)? / adjusted_quotient / 5)
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the inactivity penalty for some validator.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
pub fn get_inactivity_penalty(
|
||||
state: &BeaconState,
|
||||
index: usize,
|
||||
epochs_since_finality: u64,
|
||||
previous_total_balance: u64,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<u64, BeaconStateError> {
|
||||
Ok(get_base_reward(state, index, previous_total_balance, spec)?
|
||||
+ state.get_effective_balance(index, spec)? * epochs_since_finality
|
||||
/ spec.inactivity_penalty_quotient
|
||||
/ 2)
|
||||
}
|
||||
|
||||
/// Updates the state's `latest_active_index_roots` field with a tree hash the active validator
|
||||
/// indices for the next epoch.
|
||||
///
|
||||
@ -472,10 +262,3 @@ pub fn update_latest_slashed_balances(
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Removes all pending attestations from the previous epoch.
|
||||
///
|
||||
/// Spec v0.4.0
|
||||
pub fn clean_attestations(state: &mut BeaconState) {
|
||||
state.previous_epoch_attestations = vec![];
|
||||
}
|
||||
|
317
eth2/state_processing/src/per_epoch_processing/apply_rewards.rs
Normal file
317
eth2/state_processing/src/per_epoch_processing/apply_rewards.rs
Normal file
@ -0,0 +1,317 @@
|
||||
use super::validator_statuses::{TotalBalances, ValidatorStatus, ValidatorStatuses};
|
||||
use super::{Error, WinningRootHashSet};
|
||||
use integer_sqrt::IntegerSquareRoot;
|
||||
use types::*;
|
||||
|
||||
#[derive(Default, Clone)]
|
||||
pub struct Delta {
|
||||
pub rewards: u64,
|
||||
pub penalties: u64,
|
||||
}
|
||||
|
||||
impl std::ops::AddAssign for Delta {
|
||||
/// Use wrapping addition as that is how it's defined in the spec.
|
||||
fn add_assign(&mut self, other: Delta) {
|
||||
self.rewards += other.rewards;
|
||||
self.penalties += other.penalties;
|
||||
}
|
||||
}
|
||||
|
||||
/// Apply attester and proposer rewards.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
pub fn apply_rewards(
|
||||
state: &mut BeaconState,
|
||||
validator_statuses: &mut ValidatorStatuses,
|
||||
winning_root_for_shards: &WinningRootHashSet,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), Error> {
|
||||
// Guard against an out-of-bounds during the validator balance update.
|
||||
if validator_statuses.statuses.len() != state.validator_balances.len() {
|
||||
return Err(Error::ValidatorStatusesInconsistent);
|
||||
}
|
||||
// Guard against an out-of-bounds during the attester inclusion balance update.
|
||||
if validator_statuses.statuses.len() != state.validator_registry.len() {
|
||||
return Err(Error::ValidatorStatusesInconsistent);
|
||||
}
|
||||
|
||||
let mut deltas = vec![Delta::default(); state.validator_balances.len()];
|
||||
|
||||
get_justification_and_finalization_deltas(&mut deltas, state, &validator_statuses, spec)?;
|
||||
get_crosslink_deltas(&mut deltas, state, &validator_statuses, spec)?;
|
||||
|
||||
// Apply the proposer deltas if we are finalizing normally.
|
||||
//
|
||||
// This is executed slightly differently to the spec because of the way our functions are
|
||||
// structured. It should be functionally equivalent.
|
||||
if epochs_since_finality(state, spec) <= 4 {
|
||||
get_proposer_deltas(
|
||||
&mut deltas,
|
||||
state,
|
||||
validator_statuses,
|
||||
winning_root_for_shards,
|
||||
spec,
|
||||
)?;
|
||||
}
|
||||
|
||||
// Apply the deltas, over-flowing but not under-flowing (saturating at 0 instead).
|
||||
for (i, delta) in deltas.iter().enumerate() {
|
||||
state.validator_balances[i] += delta.rewards;
|
||||
state.validator_balances[i] = state.validator_balances[i].saturating_sub(delta.penalties);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Applies the attestation inclusion reward to each proposer for every validator who included an
|
||||
/// attestation in the previous epoch.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
fn get_proposer_deltas(
|
||||
deltas: &mut Vec<Delta>,
|
||||
state: &mut BeaconState,
|
||||
validator_statuses: &mut ValidatorStatuses,
|
||||
winning_root_for_shards: &WinningRootHashSet,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), Error> {
|
||||
// Update statuses with the information from winning roots.
|
||||
validator_statuses.process_winning_roots(state, winning_root_for_shards, spec)?;
|
||||
|
||||
for (index, validator) in validator_statuses.statuses.iter().enumerate() {
|
||||
let mut delta = Delta::default();
|
||||
|
||||
if validator.is_previous_epoch_attester {
|
||||
let inclusion = validator
|
||||
.inclusion_info
|
||||
.expect("It is a logic error for an attester not to have an inclusion distance.");
|
||||
|
||||
let base_reward = get_base_reward(
|
||||
state,
|
||||
inclusion.proposer_index,
|
||||
validator_statuses.total_balances.previous_epoch,
|
||||
spec,
|
||||
)?;
|
||||
|
||||
if inclusion.proposer_index >= deltas.len() {
|
||||
return Err(Error::ValidatorStatusesInconsistent);
|
||||
}
|
||||
|
||||
delta.rewards += base_reward / spec.attestation_inclusion_reward_quotient;
|
||||
}
|
||||
|
||||
deltas[index] += delta;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Apply rewards for participation in attestations during the previous epoch.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
fn get_justification_and_finalization_deltas(
|
||||
deltas: &mut Vec<Delta>,
|
||||
state: &BeaconState,
|
||||
validator_statuses: &ValidatorStatuses,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), Error> {
|
||||
let epochs_since_finality = epochs_since_finality(state, spec);
|
||||
|
||||
for (index, validator) in validator_statuses.statuses.iter().enumerate() {
|
||||
let base_reward = get_base_reward(
|
||||
state,
|
||||
index,
|
||||
validator_statuses.total_balances.previous_epoch,
|
||||
spec,
|
||||
)?;
|
||||
let inactivity_penalty = get_inactivity_penalty(
|
||||
state,
|
||||
index,
|
||||
epochs_since_finality.as_u64(),
|
||||
validator_statuses.total_balances.previous_epoch,
|
||||
spec,
|
||||
)?;
|
||||
|
||||
let delta = if epochs_since_finality <= 4 {
|
||||
compute_normal_justification_and_finalization_delta(
|
||||
&validator,
|
||||
&validator_statuses.total_balances,
|
||||
base_reward,
|
||||
spec,
|
||||
)
|
||||
} else {
|
||||
compute_inactivity_leak_delta(&validator, base_reward, inactivity_penalty, spec)
|
||||
};
|
||||
|
||||
deltas[index] += delta;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Determine the delta for a single validator, if the chain is finalizing normally.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
fn compute_normal_justification_and_finalization_delta(
|
||||
validator: &ValidatorStatus,
|
||||
total_balances: &TotalBalances,
|
||||
base_reward: u64,
|
||||
spec: &ChainSpec,
|
||||
) -> Delta {
|
||||
let mut delta = Delta::default();
|
||||
|
||||
let boundary_attesting_balance = total_balances.previous_epoch_boundary_attesters;
|
||||
let total_balance = total_balances.previous_epoch;
|
||||
let total_attesting_balance = total_balances.previous_epoch_attesters;
|
||||
let matching_head_balance = total_balances.previous_epoch_boundary_attesters;
|
||||
|
||||
// Expected FFG source.
|
||||
if validator.is_previous_epoch_attester {
|
||||
delta.rewards += base_reward * total_attesting_balance / total_balance;
|
||||
// Inclusion speed bonus
|
||||
let inclusion = validator
|
||||
.inclusion_info
|
||||
.expect("It is a logic error for an attester not to have an inclusion distance.");
|
||||
delta.rewards +=
|
||||
base_reward * spec.min_attestation_inclusion_delay / inclusion.distance.as_u64();
|
||||
} else if validator.is_active_in_previous_epoch {
|
||||
delta.penalties += base_reward;
|
||||
}
|
||||
|
||||
// Expected FFG target.
|
||||
if validator.is_previous_epoch_boundary_attester {
|
||||
delta.rewards += base_reward / boundary_attesting_balance / total_balance;
|
||||
} else if validator.is_active_in_previous_epoch {
|
||||
delta.penalties += base_reward;
|
||||
}
|
||||
|
||||
// Expected head.
|
||||
if validator.is_previous_epoch_head_attester {
|
||||
delta.rewards += base_reward * matching_head_balance / total_balance;
|
||||
} else if validator.is_active_in_previous_epoch {
|
||||
delta.penalties += base_reward;
|
||||
};
|
||||
|
||||
// Proposer bonus is handled in `apply_proposer_deltas`.
|
||||
//
|
||||
// This function only computes the delta for a single validator, so it cannot also return a
|
||||
// delta for a validator.
|
||||
|
||||
delta
|
||||
}
|
||||
|
||||
/// Determine the delta for a single delta, assuming the chain is _not_ finalizing normally.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
fn compute_inactivity_leak_delta(
|
||||
validator: &ValidatorStatus,
|
||||
base_reward: u64,
|
||||
inactivity_penalty: u64,
|
||||
spec: &ChainSpec,
|
||||
) -> Delta {
|
||||
let mut delta = Delta::default();
|
||||
|
||||
if validator.is_active_in_previous_epoch {
|
||||
if !validator.is_previous_epoch_attester {
|
||||
delta.penalties += inactivity_penalty;
|
||||
} else {
|
||||
// If a validator did attest, apply a small penalty for getting attestations included
|
||||
// late.
|
||||
let inclusion = validator
|
||||
.inclusion_info
|
||||
.expect("It is a logic error for an attester not to have an inclusion distance.");
|
||||
delta.rewards +=
|
||||
base_reward * spec.min_attestation_inclusion_delay / inclusion.distance.as_u64();
|
||||
delta.penalties += base_reward;
|
||||
}
|
||||
|
||||
if !validator.is_previous_epoch_boundary_attester {
|
||||
delta.penalties += inactivity_penalty;
|
||||
}
|
||||
|
||||
if !validator.is_previous_epoch_head_attester {
|
||||
delta.penalties += inactivity_penalty;
|
||||
}
|
||||
}
|
||||
|
||||
// Penalize slashed-but-inactive validators as though they were active but offline.
|
||||
if !validator.is_active_in_previous_epoch
|
||||
& validator.is_slashed
|
||||
& !validator.is_withdrawable_in_current_epoch
|
||||
{
|
||||
delta.penalties += 2 * inactivity_penalty + base_reward;
|
||||
}
|
||||
|
||||
delta
|
||||
}
|
||||
|
||||
/// Calculate the deltas based upon the winning roots for attestations during the previous epoch.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
fn get_crosslink_deltas(
|
||||
deltas: &mut Vec<Delta>,
|
||||
state: &BeaconState,
|
||||
validator_statuses: &ValidatorStatuses,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), Error> {
|
||||
for (index, validator) in validator_statuses.statuses.iter().enumerate() {
|
||||
let mut delta = Delta::default();
|
||||
|
||||
let base_reward = get_base_reward(
|
||||
state,
|
||||
index,
|
||||
validator_statuses.total_balances.previous_epoch,
|
||||
spec,
|
||||
)?;
|
||||
|
||||
if let Some(ref winning_root) = validator.winning_root_info {
|
||||
delta.rewards += base_reward * winning_root.total_attesting_balance
|
||||
/ winning_root.total_committee_balance
|
||||
} else {
|
||||
delta.penalties += base_reward;
|
||||
}
|
||||
|
||||
deltas[index] += delta;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Returns the base reward for some validator.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
fn get_base_reward(
|
||||
state: &BeaconState,
|
||||
index: usize,
|
||||
previous_total_balance: u64,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<u64, BeaconStateError> {
|
||||
if previous_total_balance == 0 {
|
||||
Ok(0)
|
||||
} else {
|
||||
let adjusted_quotient = previous_total_balance.integer_sqrt() / spec.base_reward_quotient;
|
||||
Ok(state.get_effective_balance(index, spec)? / adjusted_quotient / 5)
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the inactivity penalty for some validator.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
fn get_inactivity_penalty(
|
||||
state: &BeaconState,
|
||||
index: usize,
|
||||
epochs_since_finality: u64,
|
||||
previous_total_balance: u64,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<u64, BeaconStateError> {
|
||||
Ok(get_base_reward(state, index, previous_total_balance, spec)?
|
||||
+ state.get_effective_balance(index, spec)? * epochs_since_finality
|
||||
/ spec.inactivity_penalty_quotient
|
||||
/ 2)
|
||||
}
|
||||
|
||||
/// Returns the epochs since the last finalized epoch.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
fn epochs_since_finality(state: &BeaconState, spec: &ChainSpec) -> Epoch {
|
||||
state.current_epoch(spec) + 1 - state.finalized_epoch
|
||||
}
|
@ -9,6 +9,7 @@ pub enum EpochProcessingError {
|
||||
PreviousTotalBalanceIsZero,
|
||||
InclusionDistanceZero,
|
||||
ValidatorStatusesInconsistent,
|
||||
DeltasInconsistent,
|
||||
/// Unable to get the inclusion distance for a validator that should have an inclusion
|
||||
/// distance. This indicates an internal inconsistency.
|
||||
///
|
||||
|
@ -23,7 +23,7 @@ pub struct WinningRootInfo {
|
||||
}
|
||||
|
||||
/// The information required to reward a block producer for including an attestation in a block.
|
||||
#[derive(Clone)]
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct InclusionInfo {
|
||||
/// The earliest slot a validator had an attestation included in the previous epoch.
|
||||
pub slot: Slot,
|
||||
@ -59,7 +59,11 @@ impl InclusionInfo {
|
||||
|
||||
/// Information required to reward some validator during the current and previous epoch.
|
||||
#[derive(Default, Clone)]
|
||||
pub struct AttesterStatus {
|
||||
pub struct ValidatorStatus {
|
||||
/// True if the validator has been slashed, ever.
|
||||
pub is_slashed: bool,
|
||||
/// True if the validator can withdraw in the current epoch.
|
||||
pub is_withdrawable_in_current_epoch: bool,
|
||||
/// True if the validator was active in the state's _current_ epoch.
|
||||
pub is_active_in_current_epoch: bool,
|
||||
/// True if the validator was active in the state's _previous_ epoch.
|
||||
@ -81,14 +85,14 @@ pub struct AttesterStatus {
|
||||
|
||||
/// Information used to reward the block producer of this validators earliest-included
|
||||
/// attestation.
|
||||
pub inclusion_info: InclusionInfo,
|
||||
pub inclusion_info: Option<InclusionInfo>,
|
||||
/// Information used to reward/penalize the validator if they voted in the super-majority for
|
||||
/// some shard block.
|
||||
pub winning_root_info: Option<WinningRootInfo>,
|
||||
}
|
||||
|
||||
impl AttesterStatus {
|
||||
/// Accepts some `other` `AttesterStatus` and updates `self` if required.
|
||||
impl ValidatorStatus {
|
||||
/// Accepts some `other` `ValidatorStatus` and updates `self` if required.
|
||||
///
|
||||
/// Will never set one of the `bool` fields to `false`, it will only set it to `true` if other
|
||||
/// contains a `true` field.
|
||||
@ -97,6 +101,8 @@ impl AttesterStatus {
|
||||
pub fn update(&mut self, other: &Self) {
|
||||
// Update all the bool fields, only updating `self` if `other` is true (never setting
|
||||
// `self` to false).
|
||||
set_self_if_other_is_true!(self, other, is_slashed);
|
||||
set_self_if_other_is_true!(self, other, is_withdrawable_in_current_epoch);
|
||||
set_self_if_other_is_true!(self, other, is_active_in_current_epoch);
|
||||
set_self_if_other_is_true!(self, other, is_active_in_previous_epoch);
|
||||
set_self_if_other_is_true!(self, other, is_current_epoch_attester);
|
||||
@ -105,7 +111,13 @@ impl AttesterStatus {
|
||||
set_self_if_other_is_true!(self, other, is_previous_epoch_boundary_attester);
|
||||
set_self_if_other_is_true!(self, other, is_previous_epoch_head_attester);
|
||||
|
||||
self.inclusion_info.update(&other.inclusion_info);
|
||||
if let Some(other_info) = other.inclusion_info {
|
||||
if let Some(self_info) = self.inclusion_info.as_mut() {
|
||||
self_info.update(&other_info);
|
||||
} else {
|
||||
self.inclusion_info = other.inclusion_info;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -137,7 +149,7 @@ pub struct TotalBalances {
|
||||
#[derive(Clone)]
|
||||
pub struct ValidatorStatuses {
|
||||
/// Information about each individual validator from the state's validator registy.
|
||||
pub statuses: Vec<AttesterStatus>,
|
||||
pub statuses: Vec<ValidatorStatus>,
|
||||
/// Summed balances for various sets of validators.
|
||||
pub total_balances: TotalBalances,
|
||||
}
|
||||
@ -154,7 +166,12 @@ impl ValidatorStatuses {
|
||||
let mut total_balances = TotalBalances::default();
|
||||
|
||||
for (i, validator) in state.validator_registry.iter().enumerate() {
|
||||
let mut status = AttesterStatus::default();
|
||||
let mut status = ValidatorStatus {
|
||||
is_slashed: validator.slashed,
|
||||
is_withdrawable_in_current_epoch: validator
|
||||
.is_withdrawable_at(state.current_epoch(spec)),
|
||||
..ValidatorStatus::default()
|
||||
};
|
||||
|
||||
if validator.is_active_at(state.current_epoch(spec)) {
|
||||
status.is_active_in_current_epoch = true;
|
||||
@ -193,10 +210,10 @@ impl ValidatorStatuses {
|
||||
get_attestation_participants(state, &a.data, &a.aggregation_bitfield, spec)?;
|
||||
let attesting_balance = state.get_total_balance(&attesting_indices, spec)?;
|
||||
|
||||
let mut status = AttesterStatus::default();
|
||||
let mut status = ValidatorStatus::default();
|
||||
|
||||
// Profile this attestation, updating the total balances and generating an
|
||||
// `AttesterStatus` object that applies to all participants in the attestation.
|
||||
// `ValidatorStatus` object that applies to all participants in the attestation.
|
||||
if is_from_epoch(a, state.current_epoch(spec), spec) {
|
||||
self.total_balances.current_epoch_attesters += attesting_balance;
|
||||
status.is_current_epoch_attester = true;
|
||||
@ -211,7 +228,7 @@ impl ValidatorStatuses {
|
||||
|
||||
// The inclusion slot and distance are only required for previous epoch attesters.
|
||||
let relative_epoch = RelativeEpoch::from_slot(state.slot, a.data.slot, spec)?;
|
||||
status.inclusion_info = InclusionInfo {
|
||||
status.inclusion_info = Some(InclusionInfo {
|
||||
slot: a.inclusion_slot,
|
||||
distance: inclusion_distance(a),
|
||||
proposer_index: state.get_beacon_proposer_index(
|
||||
@ -219,7 +236,7 @@ impl ValidatorStatuses {
|
||||
relative_epoch,
|
||||
spec,
|
||||
)?,
|
||||
};
|
||||
});
|
||||
|
||||
if has_common_epoch_boundary_root(a, state, state.previous_epoch(spec), spec)? {
|
||||
self.total_balances.previous_epoch_boundary_attesters += attesting_balance;
|
||||
|
Loading…
Reference in New Issue
Block a user