2019-03-07 00:32:53 +00:00
|
|
|
use errors::EpochProcessingError as Error;
|
2019-02-14 01:09:18 +00:00
|
|
|
use integer_sqrt::IntegerSquareRoot;
|
2019-03-18 10:34:42 +00:00
|
|
|
use process_ejections::process_ejections;
|
|
|
|
use process_exit_queue::process_exit_queue;
|
|
|
|
use process_slashings::process_slashings;
|
2019-03-17 06:47:12 +00:00
|
|
|
use process_validator_registry::process_validator_registry;
|
2019-02-14 01:09:18 +00:00
|
|
|
use rayon::prelude::*;
|
|
|
|
use ssz::TreeHash;
|
2019-03-08 23:37:41 +00:00
|
|
|
use std::collections::HashMap;
|
2019-03-18 22:09:57 +00:00
|
|
|
use types::*;
|
2019-03-14 03:59:00 +00:00
|
|
|
use validator_statuses::{TotalBalances, ValidatorStatuses};
|
2019-03-06 07:57:41 +00:00
|
|
|
use winning_root::{winning_root, WinningRoot};
|
2019-02-14 01:09:18 +00:00
|
|
|
|
2019-03-06 07:57:41 +00:00
|
|
|
pub mod errors;
|
2019-03-17 06:47:12 +00:00
|
|
|
pub mod get_attestation_participants;
|
2019-03-07 00:32:53 +00:00
|
|
|
pub mod inclusion_distance;
|
2019-03-18 10:34:42 +00:00
|
|
|
pub mod process_ejections;
|
|
|
|
pub mod process_exit_queue;
|
|
|
|
pub mod process_slashings;
|
2019-03-17 06:47:12 +00:00
|
|
|
pub mod process_validator_registry;
|
2019-03-07 00:32:53 +00:00
|
|
|
pub mod tests;
|
2019-03-18 10:34:42 +00:00
|
|
|
pub mod update_validator_registry;
|
2019-03-14 03:59:00 +00:00
|
|
|
pub mod validator_statuses;
|
2019-03-07 00:32:53 +00:00
|
|
|
pub mod winning_root;
|
2019-02-14 01:09:18 +00:00
|
|
|
|
2019-03-12 07:02:53 +00:00
|
|
|
/// Maps a shard to a winning root.
|
|
|
|
///
|
|
|
|
/// It is generated during crosslink processing and later used to reward/penalize validators.
|
|
|
|
pub type WinningRootHashSet = HashMap<u64, WinningRoot>;
|
|
|
|
|
|
|
|
/// Performs per-epoch processing on some BeaconState.
|
|
|
|
///
|
|
|
|
/// Mutates the given `BeaconState`, returning early if an error is encountered. If an error is
|
|
|
|
/// returned, a state might be "half-processed" and therefore in an invalid state.
|
|
|
|
///
|
|
|
|
/// Spec v0.4.0
|
2019-03-06 07:57:41 +00:00
|
|
|
pub fn per_epoch_processing(state: &mut BeaconState, spec: &ChainSpec) -> Result<(), Error> {
|
2019-03-17 06:47:12 +00:00
|
|
|
// Ensure the previous and next epoch caches are built.
|
2019-03-06 07:57:41 +00:00
|
|
|
state.build_epoch_cache(RelativeEpoch::Previous, spec)?;
|
|
|
|
state.build_epoch_cache(RelativeEpoch::Current, spec)?;
|
|
|
|
|
2019-03-14 03:59:00 +00:00
|
|
|
let mut statuses = initialize_validator_statuses(&state, spec)?;
|
2019-03-14 00:53:50 +00:00
|
|
|
|
2019-03-06 07:57:41 +00:00
|
|
|
process_eth1_data(state, spec);
|
|
|
|
|
2019-03-14 03:59:00 +00:00
|
|
|
process_justification(state, &statuses.total_balances, spec);
|
2019-03-06 07:57:41 +00:00
|
|
|
|
|
|
|
// Crosslinks
|
|
|
|
let winning_root_for_shards = process_crosslinks(state, spec)?;
|
|
|
|
|
|
|
|
// Rewards and Penalities
|
2019-03-14 03:59:00 +00:00
|
|
|
process_rewards_and_penalities(state, &mut statuses, &winning_root_for_shards, spec)?;
|
2019-03-06 07:57:41 +00:00
|
|
|
|
|
|
|
// Ejections
|
2019-03-18 10:34:42 +00:00
|
|
|
process_ejections(state, spec)?;
|
2019-03-06 07:57:41 +00:00
|
|
|
|
|
|
|
// Validator Registry
|
|
|
|
process_validator_registry(state, spec)?;
|
2019-03-18 10:34:42 +00:00
|
|
|
process_slashings(state, spec)?;
|
|
|
|
process_exit_queue(state, spec);
|
2019-03-06 07:57:41 +00:00
|
|
|
|
|
|
|
// Final updates
|
2019-03-08 23:37:41 +00:00
|
|
|
update_active_tree_index_roots(state, spec)?;
|
2019-03-18 10:34:42 +00:00
|
|
|
update_latest_slashed_balances(state, spec)?;
|
2019-03-17 12:11:07 +00:00
|
|
|
clean_attestations(state);
|
2019-03-06 07:57:41 +00:00
|
|
|
|
|
|
|
// Rotate the epoch caches to suit the epoch transition.
|
|
|
|
state.advance_caches();
|
|
|
|
|
|
|
|
Ok(())
|
2019-02-14 01:09:18 +00:00
|
|
|
}
|
|
|
|
|
2019-03-12 07:02:53 +00:00
|
|
|
/// Calculates various sets of attesters, including:
|
|
|
|
///
|
|
|
|
/// - current epoch attesters
|
|
|
|
/// - current epoch boundary attesters
|
|
|
|
/// - previous epoch attesters
|
|
|
|
/// - etc.
|
|
|
|
///
|
2019-03-17 06:47:12 +00:00
|
|
|
/// Spec v0.5.0
|
2019-03-14 03:59:00 +00:00
|
|
|
pub fn initialize_validator_statuses(
|
2019-03-08 23:37:41 +00:00
|
|
|
state: &BeaconState,
|
|
|
|
spec: &ChainSpec,
|
2019-03-14 03:59:00 +00:00
|
|
|
) -> Result<ValidatorStatuses, BeaconStateError> {
|
2019-03-17 06:47:12 +00:00
|
|
|
let mut statuses = ValidatorStatuses::new(state, spec)?;
|
2019-03-14 01:49:48 +00:00
|
|
|
|
2019-03-17 06:47:12 +00:00
|
|
|
statuses.process_attestations(&state, spec)?;
|
2019-03-14 01:49:48 +00:00
|
|
|
|
2019-03-14 03:59:00 +00:00
|
|
|
Ok(statuses)
|
2019-03-08 23:37:41 +00:00
|
|
|
}
|
|
|
|
|
2019-03-17 06:47:12 +00:00
|
|
|
/// Maybe resets the eth1 period.
|
|
|
|
///
|
|
|
|
/// Spec v0.5.0
|
2019-03-08 23:37:41 +00:00
|
|
|
pub fn process_eth1_data(state: &mut BeaconState, spec: &ChainSpec) {
|
2019-03-06 07:57:41 +00:00
|
|
|
let next_epoch = state.next_epoch(spec);
|
|
|
|
let voting_period = spec.epochs_per_eth1_voting_period;
|
2019-02-14 01:09:18 +00:00
|
|
|
|
2019-03-06 07:57:41 +00:00
|
|
|
if next_epoch % voting_period == 0 {
|
|
|
|
for eth1_data_vote in &state.eth1_data_votes {
|
2019-03-17 06:47:12 +00:00
|
|
|
if eth1_data_vote.vote_count * 2 > voting_period * spec.slots_per_epoch {
|
2019-03-06 07:57:41 +00:00
|
|
|
state.latest_eth1_data = eth1_data_vote.eth1_data.clone();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
state.eth1_data_votes = vec![];
|
|
|
|
}
|
|
|
|
}
|
2019-02-14 01:09:18 +00:00
|
|
|
|
2019-03-12 07:02:53 +00:00
|
|
|
/// Update the following fields on the `BeaconState`:
|
|
|
|
///
|
|
|
|
/// - `justification_bitfield`.
|
|
|
|
/// - `finalized_epoch`
|
|
|
|
/// - `justified_epoch`
|
|
|
|
/// - `previous_justified_epoch`
|
|
|
|
///
|
2019-03-06 07:57:41 +00:00
|
|
|
/// Spec v0.4.0
|
2019-03-08 23:37:41 +00:00
|
|
|
pub fn process_justification(
|
2019-03-06 07:57:41 +00:00
|
|
|
state: &mut BeaconState,
|
2019-03-14 03:59:00 +00:00
|
|
|
total_balances: &TotalBalances,
|
2019-03-06 07:57:41 +00:00
|
|
|
spec: &ChainSpec,
|
|
|
|
) {
|
|
|
|
let previous_epoch = state.previous_epoch(spec);
|
|
|
|
let current_epoch = state.current_epoch(spec);
|
|
|
|
|
2019-03-17 06:47:12 +00:00
|
|
|
let mut new_justified_epoch = state.current_justified_epoch;
|
2019-03-06 07:57:41 +00:00
|
|
|
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.
|
2019-03-14 03:59:00 +00:00
|
|
|
if (3 * total_balances.previous_epoch_boundary_attesters) >= (2 * total_balances.previous_epoch)
|
|
|
|
{
|
2019-03-06 07:57:41 +00:00
|
|
|
state.justification_bitfield |= 2;
|
|
|
|
new_justified_epoch = previous_epoch;
|
|
|
|
}
|
|
|
|
// 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.
|
2019-03-14 03:59:00 +00:00
|
|
|
if (3 * total_balances.current_epoch_boundary_attesters) >= (2 * total_balances.current_epoch) {
|
2019-03-06 07:57:41 +00:00
|
|
|
state.justification_bitfield |= 1;
|
|
|
|
new_justified_epoch = current_epoch;
|
|
|
|
}
|
2019-02-14 01:09:18 +00:00
|
|
|
|
2019-03-06 07:57:41 +00:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
// 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.
|
2019-03-17 06:47:12 +00:00
|
|
|
if (state.justification_bitfield % 8 == 0b111)
|
|
|
|
& (state.current_justified_epoch == previous_epoch - 1)
|
|
|
|
{
|
|
|
|
state.finalized_epoch = state.current_justified_epoch;
|
2019-03-06 07:57:41 +00:00
|
|
|
}
|
|
|
|
// 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.
|
2019-03-17 06:47:12 +00:00
|
|
|
if (state.justification_bitfield % 4 == 0b11)
|
|
|
|
& (state.current_justified_epoch == previous_epoch)
|
|
|
|
{
|
|
|
|
state.finalized_epoch = state.current_justified_epoch;
|
2019-03-06 07:57:41 +00:00
|
|
|
}
|
2019-02-14 01:09:18 +00:00
|
|
|
|
2019-03-17 06:47:12 +00:00
|
|
|
state.previous_justified_epoch = state.current_justified_epoch;
|
|
|
|
state.current_justified_epoch = new_justified_epoch;
|
2019-03-06 07:57:41 +00:00
|
|
|
}
|
2019-02-14 01:09:18 +00:00
|
|
|
|
2019-03-12 07:02:53 +00:00
|
|
|
/// Updates the following fields on the `BeaconState`:
|
|
|
|
///
|
|
|
|
/// - `latest_crosslinks`
|
|
|
|
///
|
|
|
|
/// Also returns a `WinningRootHashSet` for later use during epoch processing.
|
|
|
|
///
|
2019-03-17 06:47:12 +00:00
|
|
|
/// Spec v0.5.0
|
2019-03-08 23:37:41 +00:00
|
|
|
pub fn process_crosslinks(
|
2019-03-06 07:57:41 +00:00
|
|
|
state: &mut BeaconState,
|
|
|
|
spec: &ChainSpec,
|
|
|
|
) -> Result<WinningRootHashSet, Error> {
|
2019-03-07 00:32:53 +00:00
|
|
|
let mut winning_root_for_shards: WinningRootHashSet = HashMap::new();
|
2019-02-14 01:09:18 +00:00
|
|
|
|
2019-03-07 00:32:53 +00:00
|
|
|
let previous_and_current_epoch_slots: Vec<Slot> = state
|
|
|
|
.previous_epoch(spec)
|
|
|
|
.slot_iter(spec.slots_per_epoch)
|
|
|
|
.chain(state.current_epoch(spec).slot_iter(spec.slots_per_epoch))
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
for slot in previous_and_current_epoch_slots {
|
|
|
|
// Clone removes the borrow which becomes an issue when mutating `state.balances`.
|
2019-03-06 07:57:41 +00:00
|
|
|
let crosslink_committees_at_slot =
|
|
|
|
state.get_crosslink_committees_at_slot(slot, spec)?.clone();
|
2019-02-14 01:09:18 +00:00
|
|
|
|
2019-03-17 06:47:12 +00:00
|
|
|
for c in crosslink_committees_at_slot {
|
|
|
|
let shard = c.shard as u64;
|
2019-02-14 01:09:18 +00:00
|
|
|
|
2019-03-17 06:47:12 +00:00
|
|
|
let winning_root = winning_root(state, shard, spec)?;
|
2019-03-06 07:57:41 +00:00
|
|
|
|
2019-03-07 00:32:53 +00:00
|
|
|
if let Some(winning_root) = winning_root {
|
2019-03-17 06:47:12 +00:00
|
|
|
let total_committee_balance = state.get_total_balance(&c.committee, spec)?;
|
2019-02-14 01:09:18 +00:00
|
|
|
|
2019-03-07 00:32:53 +00:00
|
|
|
// TODO: I think this has a bug.
|
2019-03-06 07:57:41 +00:00
|
|
|
if (3 * winning_root.total_attesting_balance) >= (2 * total_committee_balance) {
|
|
|
|
state.latest_crosslinks[shard as usize] = Crosslink {
|
2019-03-17 06:47:12 +00:00
|
|
|
epoch: slot.epoch(spec.slots_per_epoch),
|
2019-03-06 07:57:41 +00:00
|
|
|
crosslink_data_root: winning_root.crosslink_data_root,
|
|
|
|
}
|
2019-02-14 01:09:18 +00:00
|
|
|
}
|
2019-03-07 00:32:53 +00:00
|
|
|
winning_root_for_shards.insert(shard, winning_root);
|
2019-02-14 01:09:18 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-06 07:57:41 +00:00
|
|
|
}
|
2019-02-14 01:09:18 +00:00
|
|
|
|
2019-03-06 07:57:41 +00:00
|
|
|
Ok(winning_root_for_shards)
|
|
|
|
}
|
2019-02-14 01:09:18 +00:00
|
|
|
|
2019-03-12 07:02:53 +00:00
|
|
|
/// Updates the following fields on the BeaconState:
|
|
|
|
///
|
|
|
|
/// - `validator_balances`
|
|
|
|
///
|
2019-03-07 00:32:53 +00:00
|
|
|
/// Spec v0.4.0
|
2019-03-08 23:37:41 +00:00
|
|
|
pub fn process_rewards_and_penalities(
|
2019-03-06 07:57:41 +00:00
|
|
|
state: &mut BeaconState,
|
2019-03-14 03:59:00 +00:00
|
|
|
statuses: &mut ValidatorStatuses,
|
2019-03-07 00:32:53 +00:00
|
|
|
winning_root_for_shards: &WinningRootHashSet,
|
2019-03-06 07:57:41 +00:00
|
|
|
spec: &ChainSpec,
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
let next_epoch = state.next_epoch(spec);
|
|
|
|
|
2019-03-14 03:59:00 +00:00
|
|
|
statuses.process_winning_roots(state, winning_root_for_shards, spec)?;
|
|
|
|
|
|
|
|
let total_balances = &statuses.total_balances;
|
2019-03-14 01:49:48 +00:00
|
|
|
|
2019-03-14 03:59:00 +00:00
|
|
|
let base_reward_quotient =
|
|
|
|
total_balances.previous_epoch.integer_sqrt() / spec.base_reward_quotient;
|
2019-03-07 00:32:53 +00:00
|
|
|
|
2019-03-14 05:00:22 +00:00
|
|
|
// Guard against a divide-by-zero during the validator balance update.
|
2019-03-06 07:57:41 +00:00
|
|
|
if base_reward_quotient == 0 {
|
|
|
|
return Err(Error::BaseRewardQuotientIsZero);
|
|
|
|
}
|
2019-03-14 05:00:22 +00:00
|
|
|
// Guard against a divide-by-zero during the validator balance update.
|
2019-03-14 03:59:00 +00:00
|
|
|
if total_balances.previous_epoch == 0 {
|
2019-03-08 23:37:41 +00:00
|
|
|
return Err(Error::PreviousTotalBalanceIsZero);
|
|
|
|
}
|
2019-03-14 05:00:22 +00:00
|
|
|
// Guard against an out-of-bounds during the validator balance update.
|
|
|
|
if statuses.statuses.len() != state.validator_balances.len() {
|
|
|
|
return Err(Error::ValidatorStatusesInconsistent);
|
|
|
|
}
|
2019-02-14 01:09:18 +00:00
|
|
|
|
2019-03-07 00:32:53 +00:00
|
|
|
// Justification and finalization
|
2019-02-22 05:14:16 +00:00
|
|
|
|
2019-03-07 00:32:53 +00:00
|
|
|
let epochs_since_finality = next_epoch - state.finalized_epoch;
|
2019-02-14 01:09:18 +00:00
|
|
|
|
2019-03-14 00:53:50 +00:00
|
|
|
state.validator_balances = state
|
|
|
|
.validator_balances
|
|
|
|
.par_iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(index, &balance)| {
|
|
|
|
let mut balance = balance;
|
2019-03-14 05:00:22 +00:00
|
|
|
let status = &statuses.statuses[index];
|
2019-03-17 06:47:12 +00:00
|
|
|
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.",
|
|
|
|
);
|
2019-03-14 00:53:50 +00:00
|
|
|
|
|
|
|
if epochs_since_finality <= 4 {
|
2019-03-08 23:37:41 +00:00
|
|
|
// Expected FFG source
|
2019-03-14 01:49:48 +00:00
|
|
|
if status.is_previous_epoch_attester {
|
2019-03-08 23:37:41 +00:00
|
|
|
safe_add_assign!(
|
|
|
|
balance,
|
2019-03-14 03:59:00 +00:00
|
|
|
base_reward * total_balances.previous_epoch_attesters
|
|
|
|
/ total_balances.previous_epoch
|
2019-03-08 23:37:41 +00:00
|
|
|
);
|
2019-03-14 01:49:48 +00:00
|
|
|
} else if status.is_active_in_previous_epoch {
|
2019-03-08 23:37:41 +00:00
|
|
|
safe_sub_assign!(balance, base_reward);
|
|
|
|
}
|
2019-02-14 01:09:18 +00:00
|
|
|
|
2019-03-08 23:37:41 +00:00
|
|
|
// Expected FFG target
|
2019-03-14 01:49:48 +00:00
|
|
|
if status.is_previous_epoch_boundary_attester {
|
2019-03-08 23:37:41 +00:00
|
|
|
safe_add_assign!(
|
|
|
|
balance,
|
2019-03-14 03:59:00 +00:00
|
|
|
base_reward * total_balances.previous_epoch_boundary_attesters
|
|
|
|
/ total_balances.previous_epoch
|
2019-03-08 23:37:41 +00:00
|
|
|
);
|
2019-03-14 01:49:48 +00:00
|
|
|
} else if status.is_active_in_previous_epoch {
|
2019-03-08 23:37:41 +00:00
|
|
|
safe_sub_assign!(balance, base_reward);
|
|
|
|
}
|
2019-02-14 01:09:18 +00:00
|
|
|
|
2019-03-08 23:37:41 +00:00
|
|
|
// Expected beacon chain head
|
2019-03-14 01:49:48 +00:00
|
|
|
if status.is_previous_epoch_head_attester {
|
2019-03-08 23:37:41 +00:00
|
|
|
safe_add_assign!(
|
|
|
|
balance,
|
2019-03-14 03:59:00 +00:00
|
|
|
base_reward * total_balances.previous_epoch_head_attesters
|
|
|
|
/ total_balances.previous_epoch
|
2019-03-08 23:37:41 +00:00
|
|
|
);
|
2019-03-14 01:49:48 +00:00
|
|
|
} else if status.is_active_in_previous_epoch {
|
2019-03-08 23:37:41 +00:00
|
|
|
safe_sub_assign!(balance, base_reward);
|
|
|
|
};
|
2019-03-14 00:53:50 +00:00
|
|
|
} else {
|
2019-03-17 06:47:12 +00:00
|
|
|
let inactivity_penalty = get_inactivity_penalty(
|
|
|
|
state,
|
2019-03-08 23:37:41 +00:00
|
|
|
index,
|
2019-03-17 06:47:12 +00:00
|
|
|
epochs_since_finality.as_u64(),
|
|
|
|
total_balances.previous_epoch,
|
2019-03-08 23:37:41 +00:00
|
|
|
spec,
|
2019-03-17 06:47:12 +00:00
|
|
|
)
|
|
|
|
.expect(
|
|
|
|
"Cannot fail to access a validator balance when iterating validator balances.",
|
2019-03-08 23:37:41 +00:00
|
|
|
);
|
2019-03-07 00:32:53 +00:00
|
|
|
|
2019-03-14 01:49:48 +00:00
|
|
|
if status.is_active_in_previous_epoch {
|
|
|
|
if !status.is_previous_epoch_attester {
|
2019-03-08 23:37:41 +00:00
|
|
|
safe_sub_assign!(balance, inactivity_penalty);
|
|
|
|
}
|
2019-03-14 01:49:48 +00:00
|
|
|
if !status.is_previous_epoch_boundary_attester {
|
2019-03-08 23:37:41 +00:00
|
|
|
safe_sub_assign!(balance, inactivity_penalty);
|
|
|
|
}
|
2019-03-14 01:49:48 +00:00
|
|
|
if !status.is_previous_epoch_head_attester {
|
2019-03-08 23:37:41 +00:00
|
|
|
safe_sub_assign!(balance, inactivity_penalty);
|
|
|
|
}
|
|
|
|
|
|
|
|
if state.validator_registry[index].slashed {
|
2019-03-17 06:47:12 +00:00
|
|
|
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.",
|
|
|
|
);
|
2019-03-08 23:37:41 +00:00
|
|
|
safe_sub_assign!(balance, 2 * inactivity_penalty + base_reward);
|
|
|
|
}
|
2019-02-14 01:09:18 +00:00
|
|
|
}
|
2019-03-14 00:53:50 +00:00
|
|
|
}
|
2019-02-14 01:09:18 +00:00
|
|
|
|
2019-03-14 01:17:43 +00:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2019-03-14 00:53:50 +00:00
|
|
|
balance
|
|
|
|
})
|
|
|
|
.collect();
|
2019-02-14 01:09:18 +00:00
|
|
|
|
2019-03-07 00:32:53 +00:00
|
|
|
// Attestation inclusion
|
2019-03-06 07:57:41 +00:00
|
|
|
|
2019-03-14 05:00:22 +00:00
|
|
|
// Guard against an out-of-bounds during the attester inclusion balance update.
|
|
|
|
if statuses.statuses.len() != state.validator_registry.len() {
|
|
|
|
return Err(Error::ValidatorStatusesInconsistent);
|
|
|
|
}
|
|
|
|
|
2019-03-14 00:53:50 +00:00
|
|
|
for (index, _validator) in state.validator_registry.iter().enumerate() {
|
2019-03-14 05:00:22 +00:00
|
|
|
let status = &statuses.statuses[index];
|
2019-03-07 00:32:53 +00:00
|
|
|
|
2019-03-14 01:49:48 +00:00
|
|
|
if status.is_previous_epoch_attester {
|
2019-03-14 00:53:50 +00:00
|
|
|
let proposer_index = status.inclusion_info.proposer_index;
|
|
|
|
let inclusion_distance = status.inclusion_info.distance;
|
2019-03-07 00:32:53 +00:00
|
|
|
|
2019-03-17 06:47:12 +00:00
|
|
|
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.",
|
|
|
|
);
|
2019-03-14 00:53:50 +00:00
|
|
|
|
|
|
|
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()
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2019-03-06 07:57:41 +00:00
|
|
|
}
|
2019-02-14 01:09:18 +00:00
|
|
|
|
2019-03-06 07:57:41 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2019-02-16 04:08:33 +00:00
|
|
|
|
2019-03-17 06:47:12 +00:00
|
|
|
/// Returns the base reward for some validator.
|
2019-03-12 07:02:53 +00:00
|
|
|
///
|
2019-03-17 06:47:12 +00:00
|
|
|
/// 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)
|
2019-03-06 07:57:41 +00:00
|
|
|
} else {
|
2019-03-17 06:47:12 +00:00
|
|
|
let adjusted_quotient = previous_total_balance.integer_sqrt() / spec.base_reward_quotient;
|
|
|
|
Ok(state.get_effective_balance(index, spec)? / adjusted_quotient / 5)
|
2019-02-14 01:09:18 +00:00
|
|
|
}
|
2019-03-17 06:47:12 +00:00
|
|
|
}
|
2019-02-14 01:09:18 +00:00
|
|
|
|
2019-03-17 06:47:12 +00:00
|
|
|
/// 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)
|
2019-02-14 01:09:18 +00:00
|
|
|
}
|
2019-03-08 23:37:41 +00:00
|
|
|
|
2019-03-12 07:02:53 +00:00
|
|
|
/// Updates the state's `latest_active_index_roots` field with a tree hash the active validator
|
|
|
|
/// indices for the next epoch.
|
|
|
|
///
|
|
|
|
/// Spec v0.4.0
|
2019-03-08 23:37:41 +00:00
|
|
|
pub fn update_active_tree_index_roots(
|
|
|
|
state: &mut BeaconState,
|
|
|
|
spec: &ChainSpec,
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
let next_epoch = state.next_epoch(spec);
|
|
|
|
|
2019-03-18 22:09:57 +00:00
|
|
|
let active_tree_root = state
|
|
|
|
.get_active_validator_indices(next_epoch + Epoch::from(spec.activation_exit_delay))
|
|
|
|
.to_vec()
|
|
|
|
.hash_tree_root();
|
2019-03-08 23:37:41 +00:00
|
|
|
|
2019-03-18 10:34:42 +00:00
|
|
|
state.set_active_index_root(next_epoch, Hash256::from_slice(&active_tree_root[..]), spec)?;
|
2019-03-08 23:37:41 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-03-12 07:02:53 +00:00
|
|
|
/// Advances the state's `latest_slashed_balances` field.
|
|
|
|
///
|
|
|
|
/// Spec v0.4.0
|
2019-03-18 10:34:42 +00:00
|
|
|
pub fn update_latest_slashed_balances(
|
|
|
|
state: &mut BeaconState,
|
|
|
|
spec: &ChainSpec,
|
|
|
|
) -> Result<(), Error> {
|
2019-03-08 23:37:41 +00:00
|
|
|
let current_epoch = state.current_epoch(spec);
|
|
|
|
let next_epoch = state.next_epoch(spec);
|
|
|
|
|
2019-03-18 10:34:42 +00:00
|
|
|
state.set_slashed_balance(
|
|
|
|
next_epoch,
|
|
|
|
state.get_slashed_balance(current_epoch, spec)?,
|
|
|
|
spec,
|
|
|
|
)?;
|
|
|
|
|
|
|
|
Ok(())
|
2019-03-08 23:37:41 +00:00
|
|
|
}
|
|
|
|
|
2019-03-12 07:02:53 +00:00
|
|
|
/// Removes all pending attestations from the previous epoch.
|
|
|
|
///
|
|
|
|
/// Spec v0.4.0
|
2019-03-17 12:11:07 +00:00
|
|
|
pub fn clean_attestations(state: &mut BeaconState) {
|
2019-03-17 06:47:12 +00:00
|
|
|
state.previous_epoch_attestations = vec![];
|
2019-03-08 23:37:41 +00:00
|
|
|
}
|