Skip to content

Commit

Permalink
Working single-loop refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
paulhauner committed Jul 15, 2021
1 parent 1f85249 commit 9f08360
Showing 1 changed file with 26 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,43 @@ use types::{
TIMELY_TARGET_FLAG_INDEX,
},
BeaconState, BeaconStateError, ChainSpec, Epoch, EthSpec, ParticipationFlags, RelativeEpoch,
Validator,
};

#[derive(PartialEq, Debug, Clone, Copy)]
struct Balance {
balance: u64,
raw: u64,
minimum: u64,
}

impl Balance {
pub fn balance(&self) -> u64 {
std::cmp::max(self.balance, self.minimum)
pub fn zero(minimum: u64) -> Self {
Self { raw: 0, minimum }
}

pub fn get(&self) -> u64 {
std::cmp::max(self.raw, self.minimum)
}

pub fn safe_add_assign(&mut self, other: u64) -> Result<(), ArithError> {
self.raw.safe_add_assign(other)
}
}

#[derive(PartialEq, Debug)]
struct EpochParticipation {
unslashed_participating_indices: HashMap<usize, ParticipationFlags>,
total_flag_balances: [u64; NUM_FLAG_INDICES],
total_active_balance: u64,
total_flag_balances: [Balance; NUM_FLAG_INDICES],
total_active_balance: Balance,
}

impl EpochParticipation {
pub fn new(hashmap_len: usize) -> Self {
pub fn new(hashmap_len: usize, spec: &ChainSpec) -> Self {
let zero_balance = Balance::zero(spec.effective_balance_increment);

Self {
unslashed_participating_indices: HashMap::with_capacity(hashmap_len),
total_flag_balances: <_>::default(),
total_active_balance: <_>::default(),
total_flag_balances: [zero_balance; NUM_FLAG_INDICES],
total_active_balance: zero_balance,
}
}

Expand Down Expand Up @@ -88,24 +97,6 @@ pub struct ParticipationCache {
}

impl ParticipationCache {
/*
pub fn new<T: EthSpec>(
state: &BeaconState<T>,
spec: &ChainSpec,
) -> Result<Self, BeaconStateError> {
let current_epoch = state.current_epoch();
let previous_epoch = state.previous_epoch();
Ok(Self {
current_epoch,
current_epoch_participation: get_epoch_participation(state, current_epoch, spec)?,
previous_epoch,
previous_epoch_participation: get_epoch_participation(state, previous_epoch, spec)?,
eligible_indices: state.get_eligible_validator_indices()?,
})
}
*/

pub fn new<T: EthSpec>(
state: &BeaconState<T>,
spec: &ChainSpec,
Expand All @@ -121,9 +112,9 @@ impl ParticipationCache {
.len();

let mut current_epoch_participation =
EpochParticipation::new(num_current_epoch_active_vals);
EpochParticipation::new(num_current_epoch_active_vals, spec);
let mut previous_epoch_participation =
EpochParticipation::new(num_previous_epoch_active_vals);
EpochParticipation::new(num_previous_epoch_active_vals, spec);
let mut eligible_indices = Vec::with_capacity(state.validators().len());

for (val_index, val) in state.validators().iter().enumerate() {
Expand All @@ -148,6 +139,8 @@ impl ParticipationCache {
}
}

eligible_indices.shrink_to_fit();

Ok(Self {
current_epoch,
current_epoch_participation,
Expand All @@ -162,7 +155,7 @@ impl ParticipationCache {
}

pub fn previous_epoch_total_active_balance(&self) -> u64 {
self.previous_epoch_participation.total_active_balance
self.previous_epoch_participation.total_active_balance.get()
}

fn previous_epoch_flag_attesting_balance(
Expand All @@ -172,7 +165,7 @@ impl ParticipationCache {
self.previous_epoch_participation
.total_flag_balances
.get(flag_index)
.copied()
.map(Balance::get)
// FIXME(paul): inconsistent use of ParticipationOutOfBounds?
.ok_or(BeaconStateError::ParticipationOutOfBounds(flag_index))
}
Expand All @@ -186,7 +179,7 @@ impl ParticipationCache {
}

pub fn current_epoch_total_active_balance(&self) -> u64 {
self.current_epoch_participation.total_active_balance
self.current_epoch_participation.total_active_balance.get()
}

pub fn get_unslashed_participating_indices(
Expand Down Expand Up @@ -279,81 +272,8 @@ impl<'a> UnslashedParticipatingIndices<'a> {
self.participation
.total_flag_balances
.get(self.flag_index)
.copied()
.map(Balance::get)
// FIXME(paul): inconsistent use of ParticipationOutOfBounds?
.ok_or(BeaconStateError::ParticipationOutOfBounds(self.flag_index))
}
}

/*
fn get_epoch_participation<T: EthSpec>(
state: &BeaconState<T>,
epoch: Epoch,
spec: &ChainSpec,
) -> Result<EpochParticipation, BeaconStateError> {
let epoch_participation;
let active_validator_indices;
if epoch == state.current_epoch() {
active_validator_indices =
state.get_cached_active_validator_indices(RelativeEpoch::Current)?;
epoch_participation = state.current_epoch_participation()?;
} else if epoch == state.previous_epoch() {
active_validator_indices =
state.get_cached_active_validator_indices(RelativeEpoch::Previous)?;
epoch_participation = state.previous_epoch_participation()?;
} else {
return Err(BeaconStateError::EpochOutOfBounds);
};
// It's possible this Vec is larger than necessary due to slashed validators.
let mut unslashed_participating_indices =
HashMap::with_capacity(active_validator_indices.len());
let mut total_flag_balances = [0; NUM_FLAG_INDICES];
let mut total_active_balance = 0;
for &val_index in active_validator_indices {
let val_balance = state.get_effective_balance(val_index)?;
total_active_balance.safe_add_assign(val_balance)?;
if !state.get_validator(val_index)?.slashed {
// Iterate through all the flags and increment total balances.
total_flag_balances
.iter_mut()
.enumerate()
.try_for_each(|(flag, balance)| {
if epoch_participation
.get(val_index)
.ok_or(BeaconStateError::ParticipationOutOfBounds(val_index))?
.has_flag(flag)?
{
balance.safe_add_assign(val_balance)?;
}
Ok::<_, BeaconStateError>(())
})?;
// The validator is active an unslashed, add their `ParticipationFlags` to the map.
unslashed_participating_indices.insert(
val_index,
*epoch_participation
.get(val_index)
.ok_or(BeaconStateError::ParticipationOutOfBounds(val_index))?,
);
}
}
total_active_balance = std::cmp::max(total_active_balance, spec.effective_balance_increment);
for balance in &mut total_flag_balances {
*balance = std::cmp::max(*balance, spec.effective_balance_increment)
}
unslashed_participating_indices.shrink_to_fit();
Ok(EpochParticipation {
unslashed_participating_indices,
total_flag_balances,
total_active_balance,
})
}
*/

0 comments on commit 9f08360

Please sign in to comment.