2019-05-13 07:32:06 +00:00
|
|
|
use apply_rewards::process_rewards_and_penalties;
|
2019-03-07 00:32:53 +00:00
|
|
|
use errors::EpochProcessingError as Error;
|
2019-03-18 10:34:42 +00:00
|
|
|
use process_slashings::process_slashings;
|
2019-05-13 07:32:06 +00:00
|
|
|
use registry_updates::process_registry_updates;
|
2019-03-08 23:37:41 +00:00
|
|
|
use std::collections::HashMap;
|
2019-04-16 04:14:38 +00:00
|
|
|
use tree_hash::TreeHash;
|
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-19 06:16:33 +00:00
|
|
|
pub mod apply_rewards;
|
2019-03-06 07:57:41 +00:00
|
|
|
pub mod errors;
|
2019-03-18 10:34:42 +00:00
|
|
|
pub mod process_slashings;
|
2019-05-13 07:32:06 +00:00
|
|
|
pub mod registry_updates;
|
2019-03-07 00:32:53 +00:00
|
|
|
pub mod tests;
|
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.
|
|
|
|
///
|
2019-06-17 06:25:57 +00:00
|
|
|
/// Spec v0.6.3
|
2019-05-10 04:47:09 +00:00
|
|
|
pub fn per_epoch_processing<T: EthSpec>(
|
2019-05-08 05:36:02 +00:00
|
|
|
state: &mut BeaconState<T>,
|
|
|
|
spec: &ChainSpec,
|
|
|
|
) -> Result<(), Error> {
|
2019-03-17 06:47:12 +00:00
|
|
|
// Ensure the previous and next epoch caches are built.
|
2019-05-20 04:36:54 +00:00
|
|
|
state.build_committee_cache(RelativeEpoch::Previous, spec)?;
|
|
|
|
state.build_committee_cache(RelativeEpoch::Current, spec)?;
|
2019-03-06 07:57:41 +00:00
|
|
|
|
2019-03-19 08:27:10 +00:00
|
|
|
// Load the struct we use to assign validators into sets based on their participation.
|
|
|
|
//
|
|
|
|
// E.g., attestation in the previous epoch, attested to the head, etc.
|
2019-03-19 08:43:31 +00:00
|
|
|
let mut validator_statuses = ValidatorStatuses::new(state, spec)?;
|
|
|
|
validator_statuses.process_attestations(&state, spec)?;
|
2019-03-06 07:57:41 +00:00
|
|
|
|
2019-05-14 05:00:18 +00:00
|
|
|
// Justification and finalization.
|
2019-06-09 10:26:34 +00:00
|
|
|
process_justification_and_finalization(state, &validator_statuses.total_balances)?;
|
2019-03-06 07:57:41 +00:00
|
|
|
|
2019-03-19 08:27:10 +00:00
|
|
|
// Crosslinks.
|
2019-03-06 07:57:41 +00:00
|
|
|
let winning_root_for_shards = process_crosslinks(state, spec)?;
|
|
|
|
|
2019-03-19 08:27:10 +00:00
|
|
|
// Rewards and Penalities.
|
2019-05-13 07:32:06 +00:00
|
|
|
process_rewards_and_penalties(
|
2019-03-19 08:43:31 +00:00
|
|
|
state,
|
|
|
|
&mut validator_statuses,
|
|
|
|
&winning_root_for_shards,
|
|
|
|
spec,
|
|
|
|
)?;
|
2019-03-06 07:57:41 +00:00
|
|
|
|
2019-05-14 05:00:18 +00:00
|
|
|
// Registry Updates.
|
|
|
|
process_registry_updates(state, spec)?;
|
2019-03-19 08:27:10 +00:00
|
|
|
|
2019-05-14 05:00:18 +00:00
|
|
|
// Slashings.
|
2019-03-19 08:43:31 +00:00
|
|
|
process_slashings(state, validator_statuses.total_balances.current_epoch, spec)?;
|
2019-03-06 07:57:41 +00:00
|
|
|
|
2019-03-19 08:27:10 +00:00
|
|
|
// Final updates.
|
2019-05-14 05:00:18 +00:00
|
|
|
process_final_updates(state, spec)?;
|
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
|
|
|
/// Update the following fields on the `BeaconState`:
|
|
|
|
///
|
|
|
|
/// - `justification_bitfield`.
|
|
|
|
/// - `previous_justified_epoch`
|
2019-05-06 05:19:27 +00:00
|
|
|
/// - `previous_justified_root`
|
|
|
|
/// - `current_justified_epoch`
|
|
|
|
/// - `current_justified_root`
|
|
|
|
/// - `finalized_epoch`
|
|
|
|
/// - `finalized_root`
|
2019-03-12 07:02:53 +00:00
|
|
|
///
|
2019-06-17 06:25:57 +00:00
|
|
|
/// Spec v0.6.3
|
2019-05-14 02:09:59 +00:00
|
|
|
pub fn process_justification_and_finalization<T: EthSpec>(
|
2019-05-08 05:36:02 +00:00
|
|
|
state: &mut BeaconState<T>,
|
2019-03-14 03:59:00 +00:00
|
|
|
total_balances: &TotalBalances,
|
2019-03-19 06:16:33 +00:00
|
|
|
) -> Result<(), Error> {
|
2019-06-08 12:17:42 +00:00
|
|
|
if state.current_epoch() == T::genesis_epoch() {
|
2019-05-06 05:19:27 +00:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2019-05-19 05:56:24 +00:00
|
|
|
let previous_epoch = state.previous_epoch();
|
|
|
|
let current_epoch = state.current_epoch();
|
2019-03-06 07:57:41 +00:00
|
|
|
|
2019-05-06 05:19:27 +00:00
|
|
|
let old_previous_justified_epoch = state.previous_justified_epoch;
|
|
|
|
let old_current_justified_epoch = state.current_justified_epoch;
|
2019-03-19 06:16:33 +00:00
|
|
|
|
2019-05-06 05:19:27 +00:00
|
|
|
// Process justifications
|
|
|
|
state.previous_justified_epoch = state.current_justified_epoch;
|
|
|
|
state.previous_justified_root = state.current_justified_root;
|
2019-03-06 07:57:41 +00:00
|
|
|
state.justification_bitfield <<= 1;
|
|
|
|
|
2019-05-06 05:19:27 +00:00
|
|
|
if total_balances.previous_epoch_target_attesters * 3 >= total_balances.previous_epoch * 2 {
|
|
|
|
state.current_justified_epoch = previous_epoch;
|
|
|
|
state.current_justified_root =
|
2019-06-09 10:26:34 +00:00
|
|
|
*state.get_block_root_at_epoch(state.current_justified_epoch)?;
|
2019-03-19 06:16:33 +00:00
|
|
|
state.justification_bitfield |= 2;
|
2019-03-06 07:57:41 +00:00
|
|
|
}
|
2019-03-19 06:16:33 +00:00
|
|
|
// If the current epoch gets justified, fill the last bit.
|
2019-05-06 05:19:27 +00:00
|
|
|
if total_balances.current_epoch_target_attesters * 3 >= total_balances.current_epoch * 2 {
|
|
|
|
state.current_justified_epoch = current_epoch;
|
|
|
|
state.current_justified_root =
|
2019-06-09 10:26:34 +00:00
|
|
|
*state.get_block_root_at_epoch(state.current_justified_epoch)?;
|
2019-03-19 06:16:33 +00:00
|
|
|
state.justification_bitfield |= 1;
|
2019-03-06 07:57:41 +00:00
|
|
|
}
|
2019-02-14 01:09:18 +00:00
|
|
|
|
2019-03-19 06:16:33 +00:00
|
|
|
let bitfield = state.justification_bitfield;
|
|
|
|
|
|
|
|
// The 2nd/3rd/4th most recent epochs are all justified, the 2nd using the 4th as source.
|
2019-05-06 05:19:27 +00:00
|
|
|
if (bitfield >> 1) % 8 == 0b111 && old_previous_justified_epoch == current_epoch - 3 {
|
|
|
|
state.finalized_epoch = old_previous_justified_epoch;
|
2019-06-09 10:26:34 +00:00
|
|
|
state.finalized_root = *state.get_block_root_at_epoch(state.finalized_epoch)?;
|
2019-03-06 07:57:41 +00:00
|
|
|
}
|
2019-03-19 06:16:33 +00:00
|
|
|
// The 2nd/3rd most recent epochs are both justified, the 2nd using the 3rd as source.
|
2019-06-18 17:40:30 +00:00
|
|
|
if (bitfield >> 1) % 4 == 0b11 && old_previous_justified_epoch == current_epoch - 2 {
|
2019-05-06 05:19:27 +00:00
|
|
|
state.finalized_epoch = old_previous_justified_epoch;
|
2019-06-09 10:26:34 +00:00
|
|
|
state.finalized_root = *state.get_block_root_at_epoch(state.finalized_epoch)?;
|
2019-03-06 07:57:41 +00:00
|
|
|
}
|
2019-03-19 06:16:33 +00:00
|
|
|
// The 1st/2nd/3rd most recent epochs are all justified, the 1st using the 2nd as source.
|
2019-06-18 17:40:30 +00:00
|
|
|
if bitfield % 8 == 0b111 && old_current_justified_epoch == current_epoch - 2 {
|
2019-05-06 05:19:27 +00:00
|
|
|
state.finalized_epoch = old_current_justified_epoch;
|
2019-06-09 10:26:34 +00:00
|
|
|
state.finalized_root = *state.get_block_root_at_epoch(state.finalized_epoch)?;
|
2019-03-06 07:57:41 +00:00
|
|
|
}
|
2019-03-19 06:16:33 +00:00
|
|
|
// The 1st/2nd most recent epochs are both justified, the 1st using the 2nd as source.
|
2019-06-18 17:40:30 +00:00
|
|
|
if bitfield % 4 == 0b11 && old_current_justified_epoch == current_epoch - 1 {
|
2019-05-06 05:19:27 +00:00
|
|
|
state.finalized_epoch = old_current_justified_epoch;
|
2019-06-09 10:26:34 +00:00
|
|
|
state.finalized_root = *state.get_block_root_at_epoch(state.finalized_epoch)?;
|
2019-03-19 06:16:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
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`:
|
|
|
|
///
|
2019-05-13 07:32:06 +00:00
|
|
|
/// - `previous_crosslinks`
|
|
|
|
/// - `current_crosslinks`
|
2019-03-12 07:02:53 +00:00
|
|
|
///
|
|
|
|
/// Also returns a `WinningRootHashSet` for later use during epoch processing.
|
|
|
|
///
|
2019-06-17 06:25:57 +00:00
|
|
|
/// Spec v0.6.3
|
2019-05-10 04:47:09 +00:00
|
|
|
pub fn process_crosslinks<T: EthSpec>(
|
2019-05-08 05:36:02 +00:00
|
|
|
state: &mut BeaconState<T>,
|
2019-03-06 07:57:41 +00:00
|
|
|
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-05-13 07:32:06 +00:00
|
|
|
state.previous_crosslinks = state.current_crosslinks.clone();
|
2019-03-07 00:32:53 +00:00
|
|
|
|
2019-06-10 15:01:25 +00:00
|
|
|
for &relative_epoch in &[RelativeEpoch::Previous, RelativeEpoch::Current] {
|
2019-05-19 06:56:39 +00:00
|
|
|
let epoch = relative_epoch.into_epoch(state.current_epoch());
|
|
|
|
for offset in 0..state.get_epoch_committee_count(relative_epoch)? {
|
2019-05-21 01:47:52 +00:00
|
|
|
let shard =
|
|
|
|
(state.get_epoch_start_shard(relative_epoch)? + offset) % T::ShardCount::to_u64();
|
2019-05-19 07:08:09 +00:00
|
|
|
let crosslink_committee =
|
|
|
|
state.get_crosslink_committee_for_shard(shard, relative_epoch)?;
|
2019-02-14 01:09:18 +00:00
|
|
|
|
2019-05-13 07:32:06 +00:00
|
|
|
let winning_root = winning_root(state, shard, epoch, 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-05-13 07:32:06 +00:00
|
|
|
let total_committee_balance =
|
|
|
|
state.get_total_balance(&crosslink_committee.committee, spec)?;
|
|
|
|
|
|
|
|
if 3 * winning_root.total_attesting_balance >= 2 * total_committee_balance {
|
|
|
|
state.current_crosslinks[shard as usize] = winning_root.crosslink.clone();
|
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-19 08:27:10 +00:00
|
|
|
/// Finish up an epoch update.
|
2019-03-12 07:02:53 +00:00
|
|
|
///
|
2019-06-17 06:25:57 +00:00
|
|
|
/// Spec v0.6.3
|
2019-05-14 05:00:18 +00:00
|
|
|
pub fn process_final_updates<T: EthSpec>(
|
2019-05-08 05:36:02 +00:00
|
|
|
state: &mut BeaconState<T>,
|
|
|
|
spec: &ChainSpec,
|
|
|
|
) -> Result<(), Error> {
|
2019-05-19 05:56:24 +00:00
|
|
|
let current_epoch = state.current_epoch();
|
|
|
|
let next_epoch = state.next_epoch();
|
2019-03-08 23:37:41 +00:00
|
|
|
|
2019-05-14 05:00:18 +00:00
|
|
|
// Reset eth1 data votes.
|
|
|
|
if (state.slot + 1) % spec.slots_per_eth1_voting_period == 0 {
|
|
|
|
state.eth1_data_votes = vec![];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update effective balances with hysteresis (lag).
|
|
|
|
for (index, validator) in state.validator_registry.iter_mut().enumerate() {
|
|
|
|
let balance = state.balances[index];
|
|
|
|
let half_increment = spec.effective_balance_increment / 2;
|
|
|
|
if balance < validator.effective_balance
|
|
|
|
|| validator.effective_balance + 3 * half_increment < balance
|
|
|
|
{
|
|
|
|
validator.effective_balance = std::cmp::min(
|
|
|
|
balance - balance % spec.effective_balance_increment,
|
|
|
|
spec.max_effective_balance,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update start shard.
|
2019-06-13 14:37:35 +00:00
|
|
|
state.latest_start_shard = state.next_epoch_start_shard(spec)?;
|
2019-05-14 05:00:18 +00:00
|
|
|
|
2019-03-19 08:27:10 +00:00
|
|
|
// This is a hack to allow us to update index roots and slashed balances for the next epoch.
|
|
|
|
//
|
|
|
|
// The indentation here is to make it obvious where the weird stuff happens.
|
|
|
|
{
|
|
|
|
state.slot += 1;
|
|
|
|
|
|
|
|
// Set active index root
|
|
|
|
let active_index_root = Hash256::from_slice(
|
|
|
|
&state
|
|
|
|
.get_active_validator_indices(next_epoch + spec.activation_exit_delay)
|
2019-04-16 04:14:38 +00:00
|
|
|
.tree_hash_root()[..],
|
2019-03-19 08:27:10 +00:00
|
|
|
);
|
2019-06-12 05:39:48 +00:00
|
|
|
state.set_active_index_root(
|
|
|
|
next_epoch + spec.activation_exit_delay,
|
|
|
|
active_index_root,
|
|
|
|
spec,
|
|
|
|
)?;
|
2019-03-19 08:27:10 +00:00
|
|
|
|
|
|
|
// Set total slashed balances
|
2019-05-08 05:36:02 +00:00
|
|
|
state.set_slashed_balance(next_epoch, state.get_slashed_balance(current_epoch)?)?;
|
2019-03-19 08:27:10 +00:00
|
|
|
|
|
|
|
// Set randao mix
|
2019-05-19 05:56:24 +00:00
|
|
|
state.set_randao_mix(next_epoch, *state.get_randao_mix(current_epoch)?)?;
|
2019-03-19 08:27:10 +00:00
|
|
|
|
|
|
|
state.slot -= 1;
|
|
|
|
}
|
2019-03-08 23:37:41 +00:00
|
|
|
|
2019-06-08 11:57:25 +00:00
|
|
|
if next_epoch.as_u64() % (T::SlotsPerHistoricalRoot::to_u64() / T::slots_per_epoch()) == 0 {
|
2019-05-08 05:36:02 +00:00
|
|
|
let historical_batch = state.historical_batch();
|
2019-03-19 08:27:10 +00:00
|
|
|
state
|
|
|
|
.historical_roots
|
2019-04-16 04:14:38 +00:00
|
|
|
.push(Hash256::from_slice(&historical_batch.tree_hash_root()[..]));
|
2019-03-19 08:27:10 +00:00
|
|
|
}
|
2019-03-08 23:37:41 +00:00
|
|
|
|
2019-05-14 05:00:18 +00:00
|
|
|
// Rotate current/previous epoch attestations
|
2019-05-15 07:14:07 +00:00
|
|
|
state.previous_epoch_attestations =
|
|
|
|
std::mem::replace(&mut state.current_epoch_attestations, vec![]);
|
2019-03-18 10:34:42 +00:00
|
|
|
|
|
|
|
Ok(())
|
2019-03-08 23:37:41 +00:00
|
|
|
}
|