Skip to content

Commit

Permalink
Fix Calculations Relying on MAX_EB (#5983)
Browse files Browse the repository at this point in the history
* Fix Calculations Relying on MAX_EB

* Use spec fn for max_effective_balance
  • Loading branch information
ethDreamer authored Jun 26, 2024
1 parent f48b704 commit 95939dc
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
12 changes: 6 additions & 6 deletions consensus/types/src/beacon_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,8 @@ impl<E: EthSpec> BeaconState<E> {
return Err(Error::InsufficientValidators);
}

let max_effective_balance = spec.max_effective_balance_for_fork(self.fork_name_unchecked());

let mut i = 0;
loop {
let shuffled_index = compute_shuffled_index(
Expand All @@ -907,9 +909,7 @@ impl<E: EthSpec> BeaconState<E> {
let random_byte = Self::shuffling_random_byte(i, seed)?;
let effective_balance = self.get_effective_balance(candidate_index)?;
if effective_balance.safe_mul(MAX_RANDOM_BYTE)?
>= spec
.max_effective_balance
.safe_mul(u64::from(random_byte))?
>= max_effective_balance.safe_mul(u64::from(random_byte))?
{
return Ok(candidate_index);
}
Expand Down Expand Up @@ -1091,6 +1091,8 @@ impl<E: EthSpec> BeaconState<E> {

let seed = self.get_seed(epoch, Domain::SyncCommittee, spec)?;

let max_effective_balance = spec.max_effective_balance_for_fork(self.fork_name_unchecked());

let mut i = 0;
let mut sync_committee_indices = Vec::with_capacity(E::SyncCommitteeSize::to_usize());
while sync_committee_indices.len() < E::SyncCommitteeSize::to_usize() {
Expand All @@ -1107,9 +1109,7 @@ impl<E: EthSpec> BeaconState<E> {
let random_byte = Self::shuffling_random_byte(i, seed.as_bytes())?;
let effective_balance = self.get_validator(candidate_index)?.effective_balance;
if effective_balance.safe_mul(MAX_RANDOM_BYTE)?
>= spec
.max_effective_balance
.safe_mul(u64::from(random_byte))?
>= max_effective_balance.safe_mul(u64::from(random_byte))?
{
sync_committee_indices.push(candidate_index);
}
Expand Down
1 change: 1 addition & 0 deletions consensus/types/src/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ impl Validator {
}
}

/// TODO(electra): refactor these functions and make it simpler.. this is a mess
/// Returns `true` if the validator is partially withdrawable.
fn is_partially_withdrawable_validator_capella(&self, balance: u64, spec: &ChainSpec) -> bool {
self.has_eth1_withdrawal_credential(spec)
Expand Down

0 comments on commit 95939dc

Please sign in to comment.