Fix some BeaconState
API changes in state proc.
This commit is contained in:
parent
9eb8c7411f
commit
03849de319
@ -19,7 +19,7 @@ pub fn initiate_validator_exit<T: EthSpec>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Compute exit queue epoch
|
// Compute exit queue epoch
|
||||||
let delayed_epoch = state.get_delayed_activation_exit_epoch(state.current_epoch(spec), spec);
|
let delayed_epoch = state.get_delayed_activation_exit_epoch(state.current_epoch(), spec);
|
||||||
let mut exit_queue_epoch = state
|
let mut exit_queue_epoch = state
|
||||||
.exit_cache
|
.exit_cache
|
||||||
.max_epoch()
|
.max_epoch()
|
||||||
|
@ -12,7 +12,7 @@ pub fn exit_validator<T: EthSpec>(
|
|||||||
return Err(Error::UnknownValidator);
|
return Err(Error::UnknownValidator);
|
||||||
}
|
}
|
||||||
|
|
||||||
let delayed_epoch = state.get_delayed_activation_exit_epoch(state.current_epoch(spec), spec);
|
let delayed_epoch = state.get_delayed_activation_exit_epoch(state.current_epoch(), spec);
|
||||||
|
|
||||||
if state.validator_registry[validator_index].exit_epoch > delayed_epoch {
|
if state.validator_registry[validator_index].exit_epoch > delayed_epoch {
|
||||||
state.validator_registry[validator_index].exit_epoch = delayed_epoch;
|
state.validator_registry[validator_index].exit_epoch = delayed_epoch;
|
||||||
|
@ -14,7 +14,7 @@ pub fn slash_validator<T: EthSpec>(
|
|||||||
return Err(BeaconStateError::UnknownValidator);
|
return Err(BeaconStateError::UnknownValidator);
|
||||||
}
|
}
|
||||||
|
|
||||||
let current_epoch = state.current_epoch(spec);
|
let current_epoch = state.current_epoch();
|
||||||
|
|
||||||
initiate_validator_exit(state, slashed_index, spec)?;
|
initiate_validator_exit(state, slashed_index, spec)?;
|
||||||
|
|
||||||
|
@ -164,7 +164,7 @@ pub fn process_randao<T: EthSpec>(
|
|||||||
// Verify the RANDAO is a valid signature of the proposer.
|
// Verify the RANDAO is a valid signature of the proposer.
|
||||||
verify!(
|
verify!(
|
||||||
block.body.randao_reveal.verify(
|
block.body.randao_reveal.verify(
|
||||||
&state.current_epoch(spec).tree_hash_root()[..],
|
&state.current_epoch().tree_hash_root()[..],
|
||||||
spec.get_domain(
|
spec.get_domain(
|
||||||
block.slot.epoch(spec.slots_per_epoch),
|
block.slot.epoch(spec.slots_per_epoch),
|
||||||
Domain::Randao,
|
Domain::Randao,
|
||||||
@ -176,7 +176,7 @@ pub fn process_randao<T: EthSpec>(
|
|||||||
);
|
);
|
||||||
|
|
||||||
// Update the current epoch RANDAO mix.
|
// Update the current epoch RANDAO mix.
|
||||||
state.update_randao_mix(state.current_epoch(spec), &block.body.randao_reveal, spec)?;
|
state.update_randao_mix(state.current_epoch(), &block.body.randao_reveal, spec)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@ -330,7 +330,7 @@ pub fn process_attestations<T: EthSpec>(
|
|||||||
let pending_attestation = PendingAttestation::from_attestation(attestation, state.slot);
|
let pending_attestation = PendingAttestation::from_attestation(attestation, state.slot);
|
||||||
let attestation_epoch = attestation.data.slot.epoch(spec.slots_per_epoch);
|
let attestation_epoch = attestation.data.slot.epoch(spec.slots_per_epoch);
|
||||||
|
|
||||||
if attestation_epoch == state.current_epoch(spec) {
|
if attestation_epoch == state.current_epoch() {
|
||||||
state.current_epoch_attestations.push(pending_attestation)
|
state.current_epoch_attestations.push(pending_attestation)
|
||||||
} else if attestation_epoch == state.previous_epoch(spec) {
|
} else if attestation_epoch == state.previous_epoch(spec) {
|
||||||
state.previous_epoch_attestations.push(pending_attestation)
|
state.previous_epoch_attestations.push(pending_attestation)
|
||||||
|
@ -51,9 +51,9 @@ fn verify_exit_parametric<T: EthSpec>(
|
|||||||
|
|
||||||
// Exits must specify an epoch when they become valid; they are not valid before then.
|
// Exits must specify an epoch when they become valid; they are not valid before then.
|
||||||
verify!(
|
verify!(
|
||||||
time_independent_only || state.current_epoch(spec) >= exit.epoch,
|
time_independent_only || state.current_epoch() >= exit.epoch,
|
||||||
Invalid::FutureEpoch {
|
Invalid::FutureEpoch {
|
||||||
state: state.current_epoch(spec),
|
state: state.current_epoch(),
|
||||||
exit: exit.epoch
|
exit: exit.epoch
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
@ -88,12 +88,12 @@ pub fn process_justification_and_finalization<T: EthSpec>(
|
|||||||
total_balances: &TotalBalances,
|
total_balances: &TotalBalances,
|
||||||
spec: &ChainSpec,
|
spec: &ChainSpec,
|
||||||
) -> Result<(), Error> {
|
) -> Result<(), Error> {
|
||||||
if state.current_epoch(spec) == spec.genesis_epoch {
|
if state.current_epoch() == spec.genesis_epoch {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
let previous_epoch = state.previous_epoch(spec);
|
let previous_epoch = state.previous_epoch();
|
||||||
let current_epoch = state.current_epoch(spec);
|
let current_epoch = state.current_epoch();
|
||||||
|
|
||||||
let old_previous_justified_epoch = state.previous_justified_epoch;
|
let old_previous_justified_epoch = state.previous_justified_epoch;
|
||||||
let old_current_justified_epoch = state.current_justified_epoch;
|
let old_current_justified_epoch = state.current_justified_epoch;
|
||||||
@ -159,7 +159,7 @@ pub fn process_crosslinks<T: EthSpec>(
|
|||||||
|
|
||||||
state.previous_crosslinks = state.current_crosslinks.clone();
|
state.previous_crosslinks = state.current_crosslinks.clone();
|
||||||
|
|
||||||
for epoch in vec![state.previous_epoch(spec), state.current_epoch(spec)] {
|
for epoch in vec![state.previous_epoch(), state.current_epoch()] {
|
||||||
for offset in 0..state.get_epoch_committee_count(epoch, spec) {
|
for offset in 0..state.get_epoch_committee_count(epoch, spec) {
|
||||||
let shard = (state.get_epoch_start_shard(epoch, spec) + offset) % spec.shard_count;
|
let shard = (state.get_epoch_start_shard(epoch, spec) + offset) % spec.shard_count;
|
||||||
let crosslink_committee = state.get_crosslink_committee(epoch, shard, spec)?;
|
let crosslink_committee = state.get_crosslink_committee(epoch, shard, spec)?;
|
||||||
@ -188,8 +188,8 @@ pub fn process_final_updates<T: EthSpec>(
|
|||||||
state: &mut BeaconState<T>,
|
state: &mut BeaconState<T>,
|
||||||
spec: &ChainSpec,
|
spec: &ChainSpec,
|
||||||
) -> Result<(), Error> {
|
) -> Result<(), Error> {
|
||||||
let current_epoch = state.current_epoch(spec);
|
let current_epoch = state.current_epoch();
|
||||||
let next_epoch = state.next_epoch(spec);
|
let next_epoch = state.next_epoch();
|
||||||
|
|
||||||
// Reset eth1 data votes.
|
// Reset eth1 data votes.
|
||||||
if (state.slot + 1) % spec.slots_per_eth1_voting_period == 0 {
|
if (state.slot + 1) % spec.slots_per_eth1_voting_period == 0 {
|
||||||
@ -233,11 +233,7 @@ pub fn process_final_updates<T: EthSpec>(
|
|||||||
state.set_slashed_balance(next_epoch, state.get_slashed_balance(current_epoch)?)?;
|
state.set_slashed_balance(next_epoch, state.get_slashed_balance(current_epoch)?)?;
|
||||||
|
|
||||||
// Set randao mix
|
// Set randao mix
|
||||||
state.set_randao_mix(
|
state.set_randao_mix(next_epoch, *state.get_randao_mix(current_epoch)?)?;
|
||||||
next_epoch,
|
|
||||||
*state.get_randao_mix(current_epoch, spec)?,
|
|
||||||
spec,
|
|
||||||
)?;
|
|
||||||
|
|
||||||
state.slot -= 1;
|
state.slot -= 1;
|
||||||
}
|
}
|
||||||
|
@ -39,7 +39,7 @@ pub fn process_rewards_and_penalties<T: EthSpec>(
|
|||||||
winning_root_for_shards: &WinningRootHashSet,
|
winning_root_for_shards: &WinningRootHashSet,
|
||||||
spec: &ChainSpec,
|
spec: &ChainSpec,
|
||||||
) -> Result<(), Error> {
|
) -> Result<(), Error> {
|
||||||
if state.current_epoch(spec) == spec.genesis_epoch {
|
if state.current_epoch() == spec.genesis_epoch {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -118,7 +118,7 @@ fn get_attestation_deltas<T: EthSpec>(
|
|||||||
validator_statuses: &ValidatorStatuses,
|
validator_statuses: &ValidatorStatuses,
|
||||||
spec: &ChainSpec,
|
spec: &ChainSpec,
|
||||||
) -> Result<(), Error> {
|
) -> Result<(), Error> {
|
||||||
let finality_delay = (state.previous_epoch(spec) - state.finalized_epoch).as_u64();
|
let finality_delay = (state.previous_epoch() - state.finalized_epoch).as_u64();
|
||||||
|
|
||||||
for (index, validator) in validator_statuses.statuses.iter().enumerate() {
|
for (index, validator) in validator_statuses.statuses.iter().enumerate() {
|
||||||
let base_reward = get_base_reward(
|
let base_reward = get_base_reward(
|
||||||
|
@ -8,7 +8,7 @@ pub fn process_slashings<T: EthSpec>(
|
|||||||
current_total_balance: u64,
|
current_total_balance: u64,
|
||||||
spec: &ChainSpec,
|
spec: &ChainSpec,
|
||||||
) -> Result<(), Error> {
|
) -> Result<(), Error> {
|
||||||
let current_epoch = state.current_epoch(spec);
|
let current_epoch = state.current_epoch();
|
||||||
|
|
||||||
let total_at_start = state.get_slashed_balance(current_epoch + 1)?;
|
let total_at_start = state.get_slashed_balance(current_epoch + 1)?;
|
||||||
let total_at_end = state.get_slashed_balance(current_epoch)?;
|
let total_at_end = state.get_slashed_balance(current_epoch)?;
|
||||||
|
@ -14,7 +14,7 @@ pub fn process_registry_updates<T: EthSpec>(
|
|||||||
// Collect eligible and exiting validators (we need to avoid mutating the state while iterating).
|
// Collect eligible and exiting validators (we need to avoid mutating the state while iterating).
|
||||||
// We assume it's safe to re-order the change in eligibility and `initiate_validator_exit`.
|
// We assume it's safe to re-order the change in eligibility and `initiate_validator_exit`.
|
||||||
// Rest assured exiting validators will still be exited in the same order as in the spec.
|
// Rest assured exiting validators will still be exited in the same order as in the spec.
|
||||||
let current_epoch = state.current_epoch(spec);
|
let current_epoch = state.current_epoch();
|
||||||
let is_eligible = |validator: &Validator| {
|
let is_eligible = |validator: &Validator| {
|
||||||
validator.activation_eligibility_epoch == spec.far_future_epoch
|
validator.activation_eligibility_epoch == spec.far_future_epoch
|
||||||
&& validator.effective_balance >= spec.max_effective_balance
|
&& validator.effective_balance >= spec.max_effective_balance
|
||||||
|
@ -15,7 +15,7 @@ pub fn update_registry_and_shuffling_data<T: EthSpec>(
|
|||||||
state.previous_shuffling_start_shard = state.previous_shuffling_start_shard;
|
state.previous_shuffling_start_shard = state.previous_shuffling_start_shard;
|
||||||
state.previous_shuffling_seed = state.previous_shuffling_seed;
|
state.previous_shuffling_seed = state.previous_shuffling_seed;
|
||||||
|
|
||||||
let current_epoch = state.current_epoch(spec);
|
let current_epoch = state.current_epoch();
|
||||||
let next_epoch = current_epoch + 1;
|
let next_epoch = current_epoch + 1;
|
||||||
|
|
||||||
// Check we should update, and if so, update.
|
// Check we should update, and if so, update.
|
||||||
@ -84,7 +84,7 @@ pub fn update_validator_registry<T: EthSpec>(
|
|||||||
current_total_balance: u64,
|
current_total_balance: u64,
|
||||||
spec: &ChainSpec,
|
spec: &ChainSpec,
|
||||||
) -> Result<(), Error> {
|
) -> Result<(), Error> {
|
||||||
let current_epoch = state.current_epoch(spec);
|
let current_epoch = state.current_epoch();
|
||||||
|
|
||||||
let max_balance_churn = std::cmp::max(
|
let max_balance_churn = std::cmp::max(
|
||||||
spec.max_deposit_amount,
|
spec.max_deposit_amount,
|
||||||
@ -140,7 +140,7 @@ pub fn activate_validator<T: EthSpec>(
|
|||||||
is_genesis: bool,
|
is_genesis: bool,
|
||||||
spec: &ChainSpec,
|
spec: &ChainSpec,
|
||||||
) {
|
) {
|
||||||
let current_epoch = state.current_epoch(spec);
|
let current_epoch = state.current_epoch();
|
||||||
|
|
||||||
state.validator_registry[validator_index].activation_epoch = if is_genesis {
|
state.validator_registry[validator_index].activation_epoch = if is_genesis {
|
||||||
spec.genesis_epoch
|
spec.genesis_epoch
|
||||||
|
@ -175,17 +175,17 @@ impl ValidatorStatuses {
|
|||||||
let mut status = ValidatorStatus {
|
let mut status = ValidatorStatus {
|
||||||
is_slashed: validator.slashed,
|
is_slashed: validator.slashed,
|
||||||
is_withdrawable_in_current_epoch: validator
|
is_withdrawable_in_current_epoch: validator
|
||||||
.is_withdrawable_at(state.current_epoch(spec)),
|
.is_withdrawable_at(state.current_epoch()),
|
||||||
current_epoch_effective_balance: effective_balance,
|
current_epoch_effective_balance: effective_balance,
|
||||||
..ValidatorStatus::default()
|
..ValidatorStatus::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
if validator.is_active_at(state.current_epoch(spec)) {
|
if validator.is_active_at(state.current_epoch()) {
|
||||||
status.is_active_in_current_epoch = true;
|
status.is_active_in_current_epoch = true;
|
||||||
total_balances.current_epoch += effective_balance;
|
total_balances.current_epoch += effective_balance;
|
||||||
}
|
}
|
||||||
|
|
||||||
if validator.is_active_at(state.previous_epoch(spec)) {
|
if validator.is_active_at(state.previous_epoch()) {
|
||||||
status.is_active_in_previous_epoch = true;
|
status.is_active_in_previous_epoch = true;
|
||||||
total_balances.previous_epoch += effective_balance;
|
total_balances.previous_epoch += effective_balance;
|
||||||
}
|
}
|
||||||
@ -220,17 +220,17 @@ impl ValidatorStatuses {
|
|||||||
|
|
||||||
// Profile this attestation, updating the total balances and generating an
|
// Profile this attestation, updating the total balances and generating an
|
||||||
// `ValidatorStatus` 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)) {
|
if is_from_epoch(a, state.current_epoch()) {
|
||||||
status.is_current_epoch_attester = true;
|
status.is_current_epoch_attester = true;
|
||||||
|
|
||||||
if target_matches_epoch_start_block(a, state, state.current_epoch(spec), spec)? {
|
if target_matches_epoch_start_block(a, state, state.current_epoch(), spec)? {
|
||||||
status.is_current_epoch_target_attester = true;
|
status.is_current_epoch_target_attester = true;
|
||||||
}
|
}
|
||||||
} else if is_from_epoch(a, state.previous_epoch(spec)) {
|
} else if is_from_epoch(a, state.previous_epoch()) {
|
||||||
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.
|
||||||
let attestation_slot = state.get_attestation_slot(&a.data, spec)?;
|
let attestation_slot = state.get_attestation_slot(&a.data)?;
|
||||||
let inclusion_slot = attestation_slot + a.inclusion_delay;
|
let inclusion_slot = attestation_slot + a.inclusion_delay;
|
||||||
let relative_epoch = RelativeEpoch::from_slot(state.slot, inclusion_slot, spec)?;
|
let relative_epoch = RelativeEpoch::from_slot(state.slot, inclusion_slot, spec)?;
|
||||||
status.inclusion_info = Some(InclusionInfo {
|
status.inclusion_info = Some(InclusionInfo {
|
||||||
@ -243,7 +243,7 @@ impl ValidatorStatuses {
|
|||||||
)?,
|
)?,
|
||||||
});
|
});
|
||||||
|
|
||||||
if target_matches_epoch_start_block(a, state, state.previous_epoch(spec), spec)? {
|
if target_matches_epoch_start_block(a, state, state.previous_epoch(), spec)? {
|
||||||
status.is_previous_epoch_target_attester = true;
|
status.is_previous_epoch_target_attester = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -296,7 +296,7 @@ impl ValidatorStatuses {
|
|||||||
spec: &ChainSpec,
|
spec: &ChainSpec,
|
||||||
) -> Result<(), BeaconStateError> {
|
) -> Result<(), BeaconStateError> {
|
||||||
// Loop through each slot in the previous epoch.
|
// Loop through each slot in the previous epoch.
|
||||||
for slot in state.previous_epoch(spec).slot_iter(spec.slots_per_epoch) {
|
for slot in state.previous_epoch().slot_iter(spec.slots_per_epoch) {
|
||||||
let crosslink_committees_at_slot =
|
let crosslink_committees_at_slot =
|
||||||
state.get_crosslink_committees_at_slot(slot, spec)?;
|
state.get_crosslink_committees_at_slot(slot, spec)?;
|
||||||
|
|
||||||
@ -353,7 +353,7 @@ fn has_common_beacon_block_root<T: EthSpec>(
|
|||||||
state: &BeaconState<T>,
|
state: &BeaconState<T>,
|
||||||
spec: &ChainSpec,
|
spec: &ChainSpec,
|
||||||
) -> Result<bool, BeaconStateError> {
|
) -> Result<bool, BeaconStateError> {
|
||||||
let attestation_slot = state.get_attestation_slot(&a.data, spec)?;
|
let attestation_slot = state.get_attestation_slot(&a.data)?;
|
||||||
let state_block_root = *state.get_block_root(attestation_slot)?;
|
let state_block_root = *state.get_block_root(attestation_slot)?;
|
||||||
|
|
||||||
Ok(a.data.beacon_block_root == state_block_root)
|
Ok(a.data.beacon_block_root == state_block_root)
|
||||||
|
Loading…
Reference in New Issue
Block a user