Add progress on state_processing fixed-len update

This commit is contained in:
Paul Hauner
2019-05-08 15:36:02 +10:00
parent 7a67d34293
commit 8cefd20e9d
29 changed files with 275 additions and 268 deletions

View File

@@ -33,8 +33,8 @@ impl std::ops::AddAssign for Delta {
/// Apply attester and proposer rewards.
///
/// Spec v0.5.1
pub fn apply_rewards(
state: &mut BeaconState,
pub fn apply_rewards<T: BeaconStateTypes>(
state: &mut BeaconState<T>,
validator_statuses: &mut ValidatorStatuses,
winning_root_for_shards: &WinningRootHashSet,
spec: &ChainSpec,
@@ -80,9 +80,9 @@ pub fn apply_rewards(
/// attestation in the previous epoch.
///
/// Spec v0.5.1
fn get_proposer_deltas(
fn get_proposer_deltas<T: BeaconStateTypes>(
deltas: &mut Vec<Delta>,
state: &mut BeaconState,
state: &mut BeaconState<T>,
validator_statuses: &mut ValidatorStatuses,
winning_root_for_shards: &WinningRootHashSet,
spec: &ChainSpec,
@@ -121,9 +121,9 @@ fn get_proposer_deltas(
/// Apply rewards for participation in attestations during the previous epoch.
///
/// Spec v0.5.1
fn get_justification_and_finalization_deltas(
fn get_justification_and_finalization_deltas<T: BeaconStateTypes>(
deltas: &mut Vec<Delta>,
state: &BeaconState,
state: &BeaconState<T>,
validator_statuses: &ValidatorStatuses,
spec: &ChainSpec,
) -> Result<(), Error> {
@@ -262,9 +262,9 @@ fn compute_inactivity_leak_delta(
/// Calculate the deltas based upon the winning roots for attestations during the previous epoch.
///
/// Spec v0.5.1
fn get_crosslink_deltas(
fn get_crosslink_deltas<T: BeaconStateTypes>(
deltas: &mut Vec<Delta>,
state: &BeaconState,
state: &BeaconState<T>,
validator_statuses: &ValidatorStatuses,
spec: &ChainSpec,
) -> Result<(), Error> {
@@ -296,8 +296,8 @@ fn get_crosslink_deltas(
/// Returns the base reward for some validator.
///
/// Spec v0.5.1
fn get_base_reward(
state: &BeaconState,
fn get_base_reward<T: BeaconStateTypes>(
state: &BeaconState<T>,
index: usize,
previous_total_balance: u64,
spec: &ChainSpec,
@@ -313,8 +313,8 @@ fn get_base_reward(
/// Returns the inactivity penalty for some validator.
///
/// Spec v0.5.1
fn get_inactivity_penalty(
state: &BeaconState,
fn get_inactivity_penalty<T: BeaconStateTypes>(
state: &BeaconState<T>,
index: usize,
epochs_since_finality: u64,
previous_total_balance: u64,
@@ -329,6 +329,6 @@ fn get_inactivity_penalty(
/// Returns the epochs since the last finalized epoch.
///
/// Spec v0.5.1
fn epochs_since_finality(state: &BeaconState, spec: &ChainSpec) -> Epoch {
fn epochs_since_finality<T: BeaconStateTypes>(state: &BeaconState<T>, spec: &ChainSpec) -> Epoch {
state.current_epoch(spec) + 1 - state.finalized_epoch
}

View File

@@ -4,8 +4,8 @@ use types::*;
/// Returns validator indices which participated in the attestation.
///
/// Spec v0.5.1
pub fn get_attestation_participants(
state: &BeaconState,
pub fn get_attestation_participants<T: BeaconStateTypes>(
state: &BeaconState<T>,
attestation_data: &AttestationData,
bitfield: &Bitfield,
spec: &ChainSpec,

View File

@@ -6,8 +6,8 @@ use types::*;
/// slot.
///
/// Spec v0.5.1
pub fn inclusion_distance(
state: &BeaconState,
pub fn inclusion_distance<T: BeaconStateTypes>(
state: &BeaconState<T>,
attestations: &[&PendingAttestation],
validator_index: usize,
spec: &ChainSpec,
@@ -19,8 +19,8 @@ pub fn inclusion_distance(
/// Returns the slot of the earliest included attestation for some validator.
///
/// Spec v0.5.1
pub fn inclusion_slot(
state: &BeaconState,
pub fn inclusion_slot<T: BeaconStateTypes>(
state: &BeaconState<T>,
attestations: &[&PendingAttestation],
validator_index: usize,
spec: &ChainSpec,
@@ -32,8 +32,8 @@ pub fn inclusion_slot(
/// Finds the earliest included attestation for some validator.
///
/// Spec v0.5.1
fn earliest_included_attestation(
state: &BeaconState,
fn earliest_included_attestation<T: BeaconStateTypes>(
state: &BeaconState<T>,
attestations: &[&PendingAttestation],
validator_index: usize,
spec: &ChainSpec,

View File

@@ -5,7 +5,10 @@ use types::{BeaconStateError as Error, *};
/// ``EJECTION_BALANCE``.
///
/// Spec v0.5.1
pub fn process_ejections(state: &mut BeaconState, spec: &ChainSpec) -> Result<(), Error> {
pub fn process_ejections<T: BeaconStateTypes>(
state: &mut BeaconState<T>,
spec: &ChainSpec,
) -> Result<(), Error> {
// There is an awkward double (triple?) loop here because we can't loop across the borrowed
// active validator indices and mutate state in the one loop.
let exitable: Vec<usize> = state

View File

@@ -3,7 +3,7 @@ use types::*;
/// Process the exit queue.
///
/// Spec v0.5.1
pub fn process_exit_queue(state: &mut BeaconState, spec: &ChainSpec) {
pub fn process_exit_queue<T: BeaconStateTypes>(state: &mut BeaconState<T>, spec: &ChainSpec) {
let current_epoch = state.current_epoch(spec);
let eligible = |index: usize| {
@@ -32,8 +32,8 @@ pub fn process_exit_queue(state: &mut BeaconState, spec: &ChainSpec) {
/// Initiate an exit for the validator of the given `index`.
///
/// Spec v0.5.1
fn prepare_validator_for_withdrawal(
state: &mut BeaconState,
fn prepare_validator_for_withdrawal<T: BeaconStateTypes>(
state: &mut BeaconState<T>,
validator_index: usize,
spec: &ChainSpec,
) {

View File

@@ -3,20 +3,20 @@ use types::{BeaconStateError as Error, *};
/// Process slashings.
///
/// Spec v0.5.1
pub fn process_slashings(
state: &mut BeaconState,
pub fn process_slashings<T: BeaconStateTypes>(
state: &mut BeaconState<T>,
current_total_balance: u64,
spec: &ChainSpec,
) -> Result<(), Error> {
let current_epoch = state.current_epoch(spec);
let total_at_start = state.get_slashed_balance(current_epoch + 1, spec)?;
let total_at_end = state.get_slashed_balance(current_epoch, spec)?;
let total_at_start = state.get_slashed_balance(current_epoch + 1)?;
let total_at_end = state.get_slashed_balance(current_epoch)?;
let total_penalities = total_at_end - total_at_start;
for (index, validator) in state.validator_registry.iter().enumerate() {
let should_penalize = current_epoch.as_usize()
== validator.withdrawable_epoch.as_usize() - spec.latest_slashed_exit_length / 2;
== validator.withdrawable_epoch.as_usize() - T::LatestSlashedExitLength::to_usize() / 2;
if validator.slashed && should_penalize {
let effective_balance = state.get_effective_balance(index, spec)?;

View File

@@ -5,8 +5,8 @@ use types::*;
/// Peforms a validator registry update, if required.
///
/// Spec v0.5.1
pub fn update_registry_and_shuffling_data(
state: &mut BeaconState,
pub fn update_registry_and_shuffling_data<T: BeaconStateTypes>(
state: &mut BeaconState<T>,
current_total_balance: u64,
spec: &ChainSpec,
) -> Result<(), Error> {
@@ -50,8 +50,8 @@ pub fn update_registry_and_shuffling_data(
/// Returns `true` if the validator registry should be updated during an epoch processing.
///
/// Spec v0.5.1
pub fn should_update_validator_registry(
state: &BeaconState,
pub fn should_update_validator_registry<T: BeaconStateTypes>(
state: &BeaconState<T>,
spec: &ChainSpec,
) -> Result<bool, BeaconStateError> {
if state.finalized_epoch <= state.validator_registry_update_epoch {
@@ -79,8 +79,8 @@ pub fn should_update_validator_registry(
/// Note: Utilizes the cache and will fail if the appropriate cache is not initialized.
///
/// Spec v0.5.1
pub fn update_validator_registry(
state: &mut BeaconState,
pub fn update_validator_registry<T: BeaconStateTypes>(
state: &mut BeaconState<T>,
current_total_balance: u64,
spec: &ChainSpec,
) -> Result<(), Error> {
@@ -134,8 +134,8 @@ pub fn update_validator_registry(
/// Activate the validator of the given ``index``.
///
/// Spec v0.5.1
pub fn activate_validator(
state: &mut BeaconState,
pub fn activate_validator<T: BeaconStateTypes>(
state: &mut BeaconState<T>,
validator_index: usize,
is_genesis: bool,
spec: &ChainSpec,

View File

@@ -161,7 +161,10 @@ impl ValidatorStatuses {
/// - Total balances for the current and previous epochs.
///
/// Spec v0.5.1
pub fn new(state: &BeaconState, spec: &ChainSpec) -> Result<Self, BeaconStateError> {
pub fn new<T: BeaconStateTypes>(
state: &BeaconState<T>,
spec: &ChainSpec,
) -> Result<Self, BeaconStateError> {
let mut statuses = Vec::with_capacity(state.validator_registry.len());
let mut total_balances = TotalBalances::default();
@@ -196,9 +199,9 @@ impl ValidatorStatuses {
/// `total_balances` fields.
///
/// Spec v0.5.1
pub fn process_attestations(
pub fn process_attestations<T: BeaconStateTypes>(
&mut self,
state: &BeaconState,
state: &BeaconState<T>,
spec: &ChainSpec,
) -> Result<(), BeaconStateError> {
for a in state
@@ -262,9 +265,9 @@ impl ValidatorStatuses {
/// "winning" shard block root for the previous epoch.
///
/// Spec v0.5.1
pub fn process_winning_roots(
pub fn process_winning_roots<T: BeaconStateTypes>(
&mut self,
state: &BeaconState,
state: &BeaconState<T>,
winning_roots: &WinningRootHashSet,
spec: &ChainSpec,
) -> Result<(), BeaconStateError> {
@@ -313,14 +316,14 @@ fn is_from_epoch(a: &PendingAttestation, epoch: Epoch, spec: &ChainSpec) -> bool
/// the first slot of the given epoch.
///
/// Spec v0.5.1
fn has_common_epoch_boundary_root(
fn has_common_epoch_boundary_root<T: BeaconStateTypes>(
a: &PendingAttestation,
state: &BeaconState,
state: &BeaconState<T>,
epoch: Epoch,
spec: &ChainSpec,
) -> Result<bool, BeaconStateError> {
let slot = epoch.start_slot(spec.slots_per_epoch);
let state_boundary_root = *state.get_block_root(slot, spec)?;
let state_boundary_root = *state.get_block_root(slot)?;
Ok(a.data.target_root == state_boundary_root)
}
@@ -329,12 +332,12 @@ fn has_common_epoch_boundary_root(
/// the current slot of the `PendingAttestation`.
///
/// Spec v0.5.1
fn has_common_beacon_block_root(
fn has_common_beacon_block_root<T: BeaconStateTypes>(
a: &PendingAttestation,
state: &BeaconState,
state: &BeaconState<T>,
spec: &ChainSpec,
) -> Result<bool, BeaconStateError> {
let state_block_root = *state.get_block_root(a.data.slot, spec)?;
let state_block_root = *state.get_block_root(a.data.slot)?;
Ok(a.data.beacon_block_root == state_block_root)
}

View File

@@ -35,8 +35,8 @@ impl WinningRoot {
/// per-epoch processing.
///
/// Spec v0.5.1
pub fn winning_root(
state: &BeaconState,
pub fn winning_root<T: BeaconStateTypes>(
state: &BeaconState<T>,
shard: u64,
spec: &ChainSpec,
) -> Result<Option<WinningRoot>, BeaconStateError> {
@@ -90,7 +90,11 @@ pub fn winning_root(
/// Returns `true` if pending attestation `a` is eligible to become a winning root.
///
/// Spec v0.5.1
fn is_eligible_for_winning_root(state: &BeaconState, a: &PendingAttestation, shard: Shard) -> bool {
fn is_eligible_for_winning_root<T: BeaconStateTypes>(
state: &BeaconState<T>,
a: &PendingAttestation,
shard: Shard,
) -> bool {
if shard >= state.latest_crosslinks.len() as u64 {
return false;
}
@@ -101,8 +105,8 @@ fn is_eligible_for_winning_root(state: &BeaconState, a: &PendingAttestation, sha
/// Returns all indices which voted for a given crosslink. Does not contain duplicates.
///
/// Spec v0.5.1
fn get_attesting_validator_indices(
state: &BeaconState,
fn get_attesting_validator_indices<T: BeaconStateTypes>(
state: &BeaconState<T>,
shard: u64,
crosslink_data_root: &Hash256,
spec: &ChainSpec,