mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-10 04:01:51 +00:00
Allow state processing to compile under v0.5.0
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
use types::{beacon_state::helpers::verify_bitfield_length, *};
|
||||
|
||||
/// Returns validator indices which participated in the attestation.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
pub fn get_attestation_participants(
|
||||
state: &BeaconState,
|
||||
attestation_data: &AttestationData,
|
||||
bitfield: &Bitfield,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<Vec<usize>, BeaconStateError> {
|
||||
let epoch = attestation_data.slot.epoch(spec.slots_per_epoch);
|
||||
|
||||
let crosslink_committee =
|
||||
state.get_crosslink_committee_for_shard(epoch, attestation_data.shard, spec)?;
|
||||
|
||||
if crosslink_committee.slot != attestation_data.slot {
|
||||
return Err(BeaconStateError::NoCommitteeForShard);
|
||||
}
|
||||
|
||||
let committee = &crosslink_committee.committee;
|
||||
|
||||
if !verify_bitfield_length(&bitfield, committee.len()) {
|
||||
return Err(BeaconStateError::InvalidBitfield);
|
||||
}
|
||||
|
||||
let mut participants = Vec::with_capacity(committee.len());
|
||||
for (i, validator_index) in committee.iter().enumerate() {
|
||||
match bitfield.get(i) {
|
||||
Ok(bit) if bit == true => participants.push(*validator_index),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
participants.shrink_to_fit();
|
||||
|
||||
Ok(participants)
|
||||
}
|
||||
@@ -1,12 +1,11 @@
|
||||
use super::errors::InclusionError;
|
||||
use super::get_attestation_participants::get_attestation_participants;
|
||||
use types::*;
|
||||
|
||||
/// Returns the distance between the first included attestation for some validator and this
|
||||
/// slot.
|
||||
///
|
||||
/// Note: In the spec this is defined "inline", not as a helper function.
|
||||
///
|
||||
/// Spec v0.4.0
|
||||
/// Spec v0.5.0
|
||||
pub fn inclusion_distance(
|
||||
state: &BeaconState,
|
||||
attestations: &[&PendingAttestation],
|
||||
@@ -19,9 +18,7 @@ pub fn inclusion_distance(
|
||||
|
||||
/// Returns the slot of the earliest included attestation for some validator.
|
||||
///
|
||||
/// Note: In the spec this is defined "inline", not as a helper function.
|
||||
///
|
||||
/// Spec v0.4.0
|
||||
/// Spec v0.5.0
|
||||
pub fn inclusion_slot(
|
||||
state: &BeaconState,
|
||||
attestations: &[&PendingAttestation],
|
||||
@@ -34,9 +31,7 @@ pub fn inclusion_slot(
|
||||
|
||||
/// Finds the earliest included attestation for some validator.
|
||||
///
|
||||
/// Note: In the spec this is defined "inline", not as a helper function.
|
||||
///
|
||||
/// Spec v0.4.0
|
||||
/// Spec v0.5.0
|
||||
fn earliest_included_attestation(
|
||||
state: &BeaconState,
|
||||
attestations: &[&PendingAttestation],
|
||||
@@ -47,7 +42,7 @@ fn earliest_included_attestation(
|
||||
|
||||
for (i, a) in attestations.iter().enumerate() {
|
||||
let participants =
|
||||
state.get_attestation_participants(&a.data, &a.aggregation_bitfield, spec)?;
|
||||
get_attestation_participants(state, &a.data, &a.aggregation_bitfield, spec)?;
|
||||
if participants.iter().any(|i| *i == validator_index) {
|
||||
included_attestations.push(i);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
use super::Error;
|
||||
use types::*;
|
||||
|
||||
/// Peforms a validator registry update, if required.
|
||||
///
|
||||
/// Spec v0.4.0
|
||||
pub fn process_validator_registry(state: &mut BeaconState, spec: &ChainSpec) -> Result<(), Error> {
|
||||
let current_epoch = state.current_epoch(spec);
|
||||
let next_epoch = state.next_epoch(spec);
|
||||
|
||||
state.previous_shuffling_epoch = state.current_shuffling_epoch;
|
||||
state.previous_shuffling_start_shard = state.current_shuffling_start_shard;
|
||||
|
||||
state.previous_shuffling_seed = state.current_shuffling_seed;
|
||||
|
||||
if should_update_validator_registry(state, spec)? {
|
||||
state.update_validator_registry(spec);
|
||||
|
||||
state.current_shuffling_epoch = next_epoch;
|
||||
state.current_shuffling_start_shard = (state.current_shuffling_start_shard
|
||||
+ spec.get_epoch_committee_count(
|
||||
state
|
||||
.get_active_validator_indices(current_epoch, spec)?
|
||||
.len(),
|
||||
) as u64)
|
||||
% spec.shard_count;
|
||||
state.current_shuffling_seed = state.generate_seed(state.current_shuffling_epoch, spec)?
|
||||
} else {
|
||||
let epochs_since_last_registry_update =
|
||||
current_epoch - state.validator_registry_update_epoch;
|
||||
if (epochs_since_last_registry_update > 1)
|
||||
& epochs_since_last_registry_update.is_power_of_two()
|
||||
{
|
||||
state.current_shuffling_epoch = next_epoch;
|
||||
state.current_shuffling_seed =
|
||||
state.generate_seed(state.current_shuffling_epoch, spec)?
|
||||
}
|
||||
}
|
||||
|
||||
state.process_slashings(spec);
|
||||
state.process_exit_queue(spec);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Returns `true` if the validator registry should be updated during an epoch processing.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
pub fn should_update_validator_registry(
|
||||
state: &BeaconState,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<bool, BeaconStateError> {
|
||||
if state.finalized_epoch <= state.validator_registry_update_epoch {
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
let num_active_validators = state
|
||||
.get_active_validator_indices(state.current_epoch(spec), spec)?
|
||||
.len();
|
||||
let current_epoch_committee_count = spec.get_epoch_committee_count(num_active_validators);
|
||||
|
||||
for shard in (0..current_epoch_committee_count)
|
||||
.into_iter()
|
||||
.map(|i| (state.current_shuffling_start_shard + i as u64) % spec.shard_count)
|
||||
{
|
||||
if state.latest_crosslinks[shard as usize].epoch <= state.validator_registry_update_epoch {
|
||||
return Ok(false);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(true)
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
use super::get_attestation_participants::get_attestation_participants;
|
||||
use super::WinningRootHashSet;
|
||||
use types::*;
|
||||
|
||||
@@ -147,8 +148,8 @@ impl ValidatorStatuses {
|
||||
/// - Active validators
|
||||
/// - Total balances for the current and previous epochs.
|
||||
///
|
||||
/// Spec v0.4.0
|
||||
pub fn new(state: &BeaconState, spec: &ChainSpec) -> Self {
|
||||
/// Spec v0.5.0
|
||||
pub fn new(state: &BeaconState, spec: &ChainSpec) -> Result<Self, BeaconStateError> {
|
||||
let mut statuses = Vec::with_capacity(state.validator_registry.len());
|
||||
let mut total_balances = TotalBalances::default();
|
||||
|
||||
@@ -157,37 +158,40 @@ impl ValidatorStatuses {
|
||||
|
||||
if validator.is_active_at(state.current_epoch(spec)) {
|
||||
status.is_active_in_current_epoch = true;
|
||||
total_balances.current_epoch += state.get_effective_balance(i, spec);
|
||||
total_balances.current_epoch += state.get_effective_balance(i, spec)?;
|
||||
}
|
||||
|
||||
if validator.is_active_at(state.previous_epoch(spec)) {
|
||||
status.is_active_in_previous_epoch = true;
|
||||
total_balances.previous_epoch += state.get_effective_balance(i, spec);
|
||||
total_balances.previous_epoch += state.get_effective_balance(i, spec)?;
|
||||
}
|
||||
|
||||
statuses.push(status);
|
||||
}
|
||||
|
||||
Self {
|
||||
Ok(Self {
|
||||
statuses,
|
||||
total_balances,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Process some attestations from the given `state` updating the `statuses` and
|
||||
/// `total_balances` fields.
|
||||
///
|
||||
/// Spec v0.4.0
|
||||
/// Spec v0.5.0
|
||||
pub fn process_attestations(
|
||||
&mut self,
|
||||
state: &BeaconState,
|
||||
attestations: &[PendingAttestation],
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), BeaconStateError> {
|
||||
for a in attestations {
|
||||
for a in state
|
||||
.previous_epoch_attestations
|
||||
.iter()
|
||||
.chain(state.current_epoch_attestations.iter())
|
||||
{
|
||||
let attesting_indices =
|
||||
state.get_attestation_participants(&a.data, &a.aggregation_bitfield, spec)?;
|
||||
let attesting_balance = state.get_total_balance(&attesting_indices, spec);
|
||||
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();
|
||||
|
||||
@@ -206,10 +210,15 @@ impl ValidatorStatuses {
|
||||
status.is_previous_epoch_attester = true;
|
||||
|
||||
// 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 {
|
||||
slot: a.inclusion_slot,
|
||||
distance: inclusion_distance(a),
|
||||
proposer_index: state.get_beacon_proposer_index(a.inclusion_slot, spec)?,
|
||||
proposer_index: state.get_beacon_proposer_index(
|
||||
a.inclusion_slot,
|
||||
relative_epoch,
|
||||
spec,
|
||||
)?,
|
||||
};
|
||||
|
||||
if has_common_epoch_boundary_root(a, state, state.previous_epoch(spec), spec)? {
|
||||
@@ -235,7 +244,7 @@ impl ValidatorStatuses {
|
||||
/// Update the `statuses` for each validator based upon whether or not they attested to the
|
||||
/// "winning" shard block root for the previous epoch.
|
||||
///
|
||||
/// Spec v0.4.0
|
||||
/// Spec v0.5.0
|
||||
pub fn process_winning_roots(
|
||||
&mut self,
|
||||
state: &BeaconState,
|
||||
@@ -248,11 +257,10 @@ impl ValidatorStatuses {
|
||||
state.get_crosslink_committees_at_slot(slot, spec)?;
|
||||
|
||||
// Loop through each committee in the slot.
|
||||
for (crosslink_committee, shard) in crosslink_committees_at_slot {
|
||||
for c in crosslink_committees_at_slot {
|
||||
// If there was some winning crosslink root for the committee's shard.
|
||||
if let Some(winning_root) = winning_roots.get(&shard) {
|
||||
let total_committee_balance =
|
||||
state.get_total_balance(&crosslink_committee, spec);
|
||||
if let Some(winning_root) = winning_roots.get(&c.shard) {
|
||||
let total_committee_balance = state.get_total_balance(&c.committee, spec)?;
|
||||
for &validator_index in &winning_root.attesting_validator_indices {
|
||||
// Take note of the balance information for the winning root, it will be
|
||||
// used later to calculate rewards for that validator.
|
||||
@@ -272,14 +280,14 @@ impl ValidatorStatuses {
|
||||
/// Returns the distance between when the attestation was created and when it was included in a
|
||||
/// block.
|
||||
///
|
||||
/// Spec v0.4.0
|
||||
/// Spec v0.5.0
|
||||
fn inclusion_distance(a: &PendingAttestation) -> Slot {
|
||||
a.inclusion_slot - a.data.slot
|
||||
}
|
||||
|
||||
/// Returns `true` if some `PendingAttestation` is from the supplied `epoch`.
|
||||
///
|
||||
/// Spec v0.4.0
|
||||
/// Spec v0.5.0
|
||||
fn is_from_epoch(a: &PendingAttestation, epoch: Epoch, spec: &ChainSpec) -> bool {
|
||||
a.data.slot.epoch(spec.slots_per_epoch) == epoch
|
||||
}
|
||||
@@ -287,7 +295,7 @@ fn is_from_epoch(a: &PendingAttestation, epoch: Epoch, spec: &ChainSpec) -> bool
|
||||
/// Returns `true` if a `PendingAttestation` and `BeaconState` share the same beacon block hash for
|
||||
/// the first slot of the given epoch.
|
||||
///
|
||||
/// Spec v0.4.0
|
||||
/// Spec v0.5.0
|
||||
fn has_common_epoch_boundary_root(
|
||||
a: &PendingAttestation,
|
||||
state: &BeaconState,
|
||||
@@ -295,25 +303,21 @@ fn has_common_epoch_boundary_root(
|
||||
spec: &ChainSpec,
|
||||
) -> Result<bool, BeaconStateError> {
|
||||
let slot = epoch.start_slot(spec.slots_per_epoch);
|
||||
let state_boundary_root = *state
|
||||
.get_block_root(slot, spec)
|
||||
.ok_or_else(|| BeaconStateError::InsufficientBlockRoots)?;
|
||||
let state_boundary_root = *state.get_block_root(slot, spec)?;
|
||||
|
||||
Ok(a.data.epoch_boundary_root == state_boundary_root)
|
||||
Ok(a.data.target_root == state_boundary_root)
|
||||
}
|
||||
|
||||
/// Returns `true` if a `PendingAttestation` and `BeaconState` share the same beacon block hash for
|
||||
/// the current slot of the `PendingAttestation`.
|
||||
///
|
||||
/// Spec v0.4.0
|
||||
/// Spec v0.5.0
|
||||
fn has_common_beacon_block_root(
|
||||
a: &PendingAttestation,
|
||||
state: &BeaconState,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<bool, BeaconStateError> {
|
||||
let state_block_root = *state
|
||||
.get_block_root(a.data.slot, spec)
|
||||
.ok_or_else(|| BeaconStateError::InsufficientBlockRoots)?;
|
||||
let state_block_root = *state.get_block_root(a.data.slot, spec)?;
|
||||
|
||||
Ok(a.data.beacon_block_root == state_block_root)
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use super::get_attestation_participants::get_attestation_participants;
|
||||
use std::collections::HashSet;
|
||||
use std::iter::FromIterator;
|
||||
use types::*;
|
||||
@@ -13,14 +14,14 @@ impl WinningRoot {
|
||||
/// Returns `true` if `self` is a "better" candidate than `other`.
|
||||
///
|
||||
/// A winning root is "better" than another if it has a higher `total_attesting_balance`. Ties
|
||||
/// are broken by favouring the lower `crosslink_data_root` value.
|
||||
/// are broken by favouring the higher `crosslink_data_root` value.
|
||||
///
|
||||
/// Spec v0.4.0
|
||||
/// Spec v0.5.0
|
||||
pub fn is_better_than(&self, other: &Self) -> bool {
|
||||
if self.total_attesting_balance > other.total_attesting_balance {
|
||||
true
|
||||
} else if self.total_attesting_balance == other.total_attesting_balance {
|
||||
self.crosslink_data_root < other.crosslink_data_root
|
||||
self.crosslink_data_root > other.crosslink_data_root
|
||||
} else {
|
||||
false
|
||||
}
|
||||
@@ -33,22 +34,21 @@ impl WinningRoot {
|
||||
/// The `WinningRoot` object also contains additional fields that are useful in later stages of
|
||||
/// per-epoch processing.
|
||||
///
|
||||
/// Spec v0.4.0
|
||||
/// Spec v0.5.0
|
||||
pub fn winning_root(
|
||||
state: &BeaconState,
|
||||
shard: u64,
|
||||
current_epoch_attestations: &[&PendingAttestation],
|
||||
previous_epoch_attestations: &[&PendingAttestation],
|
||||
spec: &ChainSpec,
|
||||
) -> Result<Option<WinningRoot>, BeaconStateError> {
|
||||
let mut winning_root: Option<WinningRoot> = None;
|
||||
|
||||
let crosslink_data_roots: HashSet<Hash256> = HashSet::from_iter(
|
||||
previous_epoch_attestations
|
||||
state
|
||||
.previous_epoch_attestations
|
||||
.iter()
|
||||
.chain(current_epoch_attestations.iter())
|
||||
.chain(state.current_epoch_attestations.iter())
|
||||
.filter_map(|a| {
|
||||
if a.data.shard == shard {
|
||||
if is_eligible_for_winning_root(state, a, shard) {
|
||||
Some(a.data.crosslink_data_root)
|
||||
} else {
|
||||
None
|
||||
@@ -57,18 +57,17 @@ pub fn winning_root(
|
||||
);
|
||||
|
||||
for crosslink_data_root in crosslink_data_roots {
|
||||
let attesting_validator_indices = get_attesting_validator_indices(
|
||||
state,
|
||||
shard,
|
||||
current_epoch_attestations,
|
||||
previous_epoch_attestations,
|
||||
&crosslink_data_root,
|
||||
spec,
|
||||
)?;
|
||||
let attesting_validator_indices =
|
||||
get_attesting_validator_indices(state, shard, &crosslink_data_root, spec)?;
|
||||
|
||||
let total_attesting_balance: u64 = attesting_validator_indices
|
||||
.iter()
|
||||
.fold(0, |acc, i| acc + state.get_effective_balance(*i, spec));
|
||||
let total_attesting_balance: u64 =
|
||||
attesting_validator_indices
|
||||
.iter()
|
||||
.try_fold(0_u64, |acc, i| {
|
||||
state
|
||||
.get_effective_balance(*i, spec)
|
||||
.and_then(|bal| Ok(acc + bal))
|
||||
})?;
|
||||
|
||||
let candidate = WinningRoot {
|
||||
crosslink_data_root,
|
||||
@@ -88,25 +87,36 @@ pub fn winning_root(
|
||||
Ok(winning_root)
|
||||
}
|
||||
|
||||
/// Returns all indices which voted for a given crosslink. May contain duplicates.
|
||||
/// Returns `true` if pending attestation `a` is eligible to become a winning root.
|
||||
///
|
||||
/// Spec v0.4.0
|
||||
/// Spec v0.5.0
|
||||
fn is_eligible_for_winning_root(state: &BeaconState, a: &PendingAttestation, shard: Shard) -> bool {
|
||||
if shard >= state.latest_crosslinks.len() as u64 {
|
||||
return false;
|
||||
}
|
||||
|
||||
a.data.previous_crosslink == state.latest_crosslinks[shard as usize]
|
||||
}
|
||||
|
||||
/// Returns all indices which voted for a given crosslink. Does not contain duplicates.
|
||||
///
|
||||
/// Spec v0.5.0
|
||||
fn get_attesting_validator_indices(
|
||||
state: &BeaconState,
|
||||
shard: u64,
|
||||
current_epoch_attestations: &[&PendingAttestation],
|
||||
previous_epoch_attestations: &[&PendingAttestation],
|
||||
crosslink_data_root: &Hash256,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<Vec<usize>, BeaconStateError> {
|
||||
let mut indices = vec![];
|
||||
|
||||
for a in current_epoch_attestations
|
||||
for a in state
|
||||
.current_epoch_attestations
|
||||
.iter()
|
||||
.chain(previous_epoch_attestations.iter())
|
||||
.chain(state.previous_epoch_attestations.iter())
|
||||
{
|
||||
if (a.data.shard == shard) && (a.data.crosslink_data_root == *crosslink_data_root) {
|
||||
indices.append(&mut state.get_attestation_participants(
|
||||
indices.append(&mut get_attestation_participants(
|
||||
state,
|
||||
&a.data,
|
||||
&a.aggregation_bitfield,
|
||||
spec,
|
||||
@@ -114,5 +124,41 @@ fn get_attesting_validator_indices(
|
||||
}
|
||||
}
|
||||
|
||||
// Sort the list (required for dedup). "Unstable" means the sort may re-order equal elements,
|
||||
// this causes no issue here.
|
||||
//
|
||||
// These sort + dedup ops are potentially good CPU time optimisation targets.
|
||||
indices.sort_unstable();
|
||||
// Remove all duplicate indices (requires a sorted list).
|
||||
indices.dedup();
|
||||
|
||||
Ok(indices)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn is_better_than() {
|
||||
let worse = WinningRoot {
|
||||
crosslink_data_root: Hash256::from_slice(&[1; 32]),
|
||||
attesting_validator_indices: vec![],
|
||||
total_attesting_balance: 42,
|
||||
};
|
||||
|
||||
let better = WinningRoot {
|
||||
crosslink_data_root: Hash256::from_slice(&[2; 32]),
|
||||
..worse.clone()
|
||||
};
|
||||
|
||||
assert!(better.is_better_than(&worse));
|
||||
|
||||
let better = WinningRoot {
|
||||
total_attesting_balance: worse.total_attesting_balance + 1,
|
||||
..worse.clone()
|
||||
};
|
||||
|
||||
assert!(better.is_better_than(&worse));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user