state_processing: get_attesting_indices
This commit is contained in:
parent
240e269f2f
commit
ca73fb72da
@ -1,38 +0,0 @@
|
|||||||
use crate::common::verify_bitfield_length;
|
|
||||||
use types::*;
|
|
||||||
|
|
||||||
/// Returns validator indices which participated in the attestation.
|
|
||||||
///
|
|
||||||
/// Spec v0.5.1
|
|
||||||
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 => participants.push(*validator_index),
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
participants.shrink_to_fit();
|
|
||||||
|
|
||||||
Ok(participants)
|
|
||||||
}
|
|
@ -0,0 +1,32 @@
|
|||||||
|
use crate::common::verify_bitfield_length;
|
||||||
|
use types::*;
|
||||||
|
|
||||||
|
/// Returns validator indices which participated in the attestation.
|
||||||
|
///
|
||||||
|
/// Spec v0.6.1
|
||||||
|
pub fn get_attesting_indices_unsorted(
|
||||||
|
state: &BeaconState,
|
||||||
|
attestation_data: &AttestationData,
|
||||||
|
bitfield: &Bitfield,
|
||||||
|
spec: &ChainSpec,
|
||||||
|
) -> Result<Vec<usize>, BeaconStateError> {
|
||||||
|
let committee = state.get_crosslink_committee(
|
||||||
|
attestation_data.target_epoch,
|
||||||
|
attestation_data.shard,
|
||||||
|
spec,
|
||||||
|
)?;
|
||||||
|
|
||||||
|
if !verify_bitfield_length(&bitfield, committee.committee.len()) {
|
||||||
|
return Err(BeaconStateError::InvalidBitfield);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(committee
|
||||||
|
.committee
|
||||||
|
.iter()
|
||||||
|
.enumerate()
|
||||||
|
.filter_map(|(i, validator_index)| match bitfield.get(i) {
|
||||||
|
Ok(true) => Some(*validator_index),
|
||||||
|
_ => None,
|
||||||
|
})
|
||||||
|
.collect())
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user