Finish Modifications to get_attesting_indices()

This commit is contained in:
Mark Mackey
2024-09-11 16:18:35 -05:00
parent 330863d970
commit 4c9e12cfb6
14 changed files with 143 additions and 72 deletions

View File

@@ -1843,40 +1843,12 @@ impl<E: EthSpec> BeaconState<E> {
/// Get the PTC
/// Requires the committee cache to be initialized.
/// TODO(EIP-7732): is it easier to return u64 or usize?
/// TODO(EIP-7732): definitely gonna have to cache this..
pub fn get_ptc(&self, slot: Slot) -> Result<FixedVector<usize, E::PTCSize>, Error> {
// this function is only used here and
// I have no idea where else to put it
fn bit_floor(n: u64) -> u64 {
if n == 0 {
0
} else {
1 << (n.leading_zeros() as u64 ^ 63)
}
}
pub fn get_ptc(&self, slot: Slot) -> Result<PTC<E>, Error> {
let committee_cache = self.committee_cache_at_slot(slot)?;
let committee_count_per_slot = committee_cache.committees_per_slot();
let committees_per_slot = bit_floor(std::cmp::min(
committee_count_per_slot as u64,
E::PTCSize::to_u64(),
));
let members_per_committee =
committee_count_per_slot.safe_div(committees_per_slot)? as usize;
let committees = committee_cache.get_beacon_committees_at_slot(slot)?;
let mut validator_indices = Vec::with_capacity(committees_per_slot as usize);
for idx in 0..committees_per_slot {
let beacon_committee = committees
.get(idx as usize)
.ok_or_else(|| Error::InvalidCommitteeIndex(idx))?;
validator_indices
.extend_from_slice(&beacon_committee.committee[..members_per_committee]);
}
Ok(FixedVector::new(validator_indices)?)
PTC::from_committees(&committees)
}
/// Build all caches (except the tree hash cache), if they need to be built.

View File

@@ -67,6 +67,7 @@ pub mod pending_consolidation;
pub mod pending_partial_withdrawal;
pub mod proposer_preparation_data;
pub mod proposer_slashing;
pub mod ptc;
pub mod relative_epoch;
pub mod selection_proof;
pub mod shuffling_id;
@@ -236,6 +237,7 @@ pub use crate::preset::{
};
pub use crate::proposer_preparation_data::ProposerPreparationData;
pub use crate::proposer_slashing::ProposerSlashing;
pub use crate::ptc::PTC;
pub use crate::relative_epoch::{Error as RelativeEpochError, RelativeEpoch};
pub use crate::runtime_var_list::RuntimeVariableList;
pub use crate::selection_proof::SelectionProof;

View File

@@ -0,0 +1,55 @@
use crate::*;
use safe_arith::SafeArith;
/// TODO(EIP-7732): is it easier to return u64 or usize?
#[derive(Clone, Debug, PartialEq)]
pub struct PTC<E: EthSpec>(FixedVector<usize, E::PTCSize>);
impl<E: EthSpec> PTC<E> {
pub fn from_committees(committees: &[BeaconCommittee]) -> Result<Self, BeaconStateError> {
// this function is only used here and
// I have no idea where else to put it
fn bit_floor(n: u64) -> u64 {
if n == 0 {
0
} else {
1 << (n.leading_zeros() as u64 ^ 63)
}
}
let committee_count_per_slot = committees.len() as u64;
let committees_per_slot = bit_floor(std::cmp::min(
committee_count_per_slot,
E::PTCSize::to_u64(),
)) as usize;
let members_per_committee = E::PTCSize::to_usize().safe_div(committees_per_slot)?;
let mut ptc = Vec::with_capacity(E::PTCSize::to_usize());
for idx in 0..committees_per_slot {
let beacon_committee = committees
.get(idx as usize)
.ok_or_else(|| Error::InvalidCommitteeIndex(idx as u64))?;
ptc.extend_from_slice(&beacon_committee.committee[..members_per_committee]);
}
Ok(Self(FixedVector::from(ptc)))
}
}
impl<'a, E: EthSpec> IntoIterator for &'a PTC<E> {
type Item = &'a usize;
type IntoIter = std::slice::Iter<'a, usize>;
fn into_iter(self) -> Self::IntoIter {
self.0.iter()
}
}
impl<E: EthSpec> IntoIterator for PTC<E> {
type Item = usize;
type IntoIter = std::vec::IntoIter<usize>;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}