From ffcc61bae95385f157848243856ec6b521283482 Mon Sep 17 00:00:00 2001 From: Mark Mackey Date: Fri, 21 Jun 2024 15:15:35 -0500 Subject: [PATCH 1/2] Fix Calculations Relying on MAX_EB --- consensus/types/src/beacon_state.rs | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/consensus/types/src/beacon_state.rs b/consensus/types/src/beacon_state.rs index 295aaade1cf..5504d82ec7b 100644 --- a/consensus/types/src/beacon_state.rs +++ b/consensus/types/src/beacon_state.rs @@ -892,6 +892,12 @@ impl BeaconState { return Err(Error::InsufficientValidators); } + let max_effective_balance = if self.fork_name_unchecked() >= ForkName::Electra { + spec.max_effective_balance_electra + } else { + spec.max_effective_balance + }; + let mut i = 0; loop { let shuffled_index = compute_shuffled_index( @@ -907,9 +913,7 @@ impl BeaconState { 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); } @@ -1091,6 +1095,12 @@ impl BeaconState { let seed = self.get_seed(epoch, Domain::SyncCommittee, spec)?; + let max_effective_balance = if self.fork_name_unchecked() >= ForkName::Electra { + spec.max_effective_balance_electra + } else { + spec.max_effective_balance + }; + 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() { @@ -1107,9 +1117,7 @@ impl BeaconState { 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); } From 7deb40d586d3c573c10037d3d0abe43b2a6b962d Mon Sep 17 00:00:00 2001 From: Mark Mackey Date: Wed, 26 Jun 2024 14:49:01 -0500 Subject: [PATCH 2/2] Use spec fn for max_effective_balance --- consensus/types/src/beacon_state.rs | 12 ++---------- consensus/types/src/validator.rs | 1 + 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/consensus/types/src/beacon_state.rs b/consensus/types/src/beacon_state.rs index 5504d82ec7b..8642036ad61 100644 --- a/consensus/types/src/beacon_state.rs +++ b/consensus/types/src/beacon_state.rs @@ -892,11 +892,7 @@ impl BeaconState { return Err(Error::InsufficientValidators); } - let max_effective_balance = if self.fork_name_unchecked() >= ForkName::Electra { - spec.max_effective_balance_electra - } else { - spec.max_effective_balance - }; + let max_effective_balance = spec.max_effective_balance_for_fork(self.fork_name_unchecked()); let mut i = 0; loop { @@ -1095,11 +1091,7 @@ impl BeaconState { let seed = self.get_seed(epoch, Domain::SyncCommittee, spec)?; - let max_effective_balance = if self.fork_name_unchecked() >= ForkName::Electra { - spec.max_effective_balance_electra - } else { - spec.max_effective_balance - }; + 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()); diff --git a/consensus/types/src/validator.rs b/consensus/types/src/validator.rs index b5e92d1f5d8..4173e18526e 100644 --- a/consensus/types/src/validator.rs +++ b/consensus/types/src/validator.rs @@ -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)