From 64440c4043c6663516b8ddccca17c3949967588d Mon Sep 17 00:00:00 2001 From: Pawan Dhananjay Date: Thu, 2 May 2024 17:42:15 -0700 Subject: [PATCH] Make more functions fork aware --- .../src/per_block_processing.rs | 5 +- .../per_epoch_processing/registry_updates.rs | 4 +- .../src/per_epoch_processing/single_pass.rs | 2 +- consensus/types/src/validator.rs | 87 +++++++++++++++---- 4 files changed, 78 insertions(+), 20 deletions(-) diff --git a/consensus/state_processing/src/per_block_processing.rs b/consensus/state_processing/src/per_block_processing.rs index 2efa1218829..98671f82b9f 100644 --- a/consensus/state_processing/src/per_block_processing.rs +++ b/consensus/state_processing/src/per_block_processing.rs @@ -508,6 +508,7 @@ pub fn get_expected_withdrawals( let mut withdrawal_index = state.next_withdrawal_index()?; let mut validator_index = state.next_withdrawal_validator_index()?; let mut withdrawals = vec![]; + let fork_name = state.fork_name_unchecked(); let bound = std::cmp::min( state.validators().len() as u64, @@ -518,7 +519,7 @@ pub fn get_expected_withdrawals( let balance = *state.balances().get(validator_index as usize).ok_or( BeaconStateError::BalancesOutOfBounds(validator_index as usize), )?; - if validator.is_fully_withdrawable_at(balance, epoch, spec) { + if validator.is_fully_withdrawable_at(balance, epoch, spec, fork_name) { withdrawals.push(Withdrawal { index: withdrawal_index, validator_index, @@ -528,7 +529,7 @@ pub fn get_expected_withdrawals( amount: balance, }); withdrawal_index.safe_add_assign(1)?; - } else if validator.is_partially_withdrawable_validator(balance, spec) { + } else if validator.is_partially_withdrawable_validator(balance, spec, fork_name) { withdrawals.push(Withdrawal { index: withdrawal_index, validator_index, diff --git a/consensus/state_processing/src/per_epoch_processing/registry_updates.rs b/consensus/state_processing/src/per_epoch_processing/registry_updates.rs index a9881c219c6..3d02d797366 100644 --- a/consensus/state_processing/src/per_epoch_processing/registry_updates.rs +++ b/consensus/state_processing/src/per_epoch_processing/registry_updates.rs @@ -25,14 +25,14 @@ pub fn process_registry_updates( .iter() .enumerate() .filter(|(_, validator)| { - validator.is_eligible_for_activation_queue(&fork_name, spec) || is_ejectable(validator) + validator.is_eligible_for_activation_queue(spec, fork_name) || is_ejectable(validator) }) .map(|(idx, _)| idx) .collect(); for index in indices_to_update { let validator = state.get_validator_mut(index)?; - if validator.is_eligible_for_activation_queue(&fork_name, spec) { + if validator.is_eligible_for_activation_queue(spec, fork_name) { validator.activation_eligibility_epoch = current_epoch.safe_add(1)?; } if is_ejectable(validator) { diff --git a/consensus/state_processing/src/per_epoch_processing/single_pass.rs b/consensus/state_processing/src/per_epoch_processing/single_pass.rs index 46a30ac7331..a9629e73e40 100644 --- a/consensus/state_processing/src/per_epoch_processing/single_pass.rs +++ b/consensus/state_processing/src/per_epoch_processing/single_pass.rs @@ -466,7 +466,7 @@ fn process_single_registry_update( ) -> Result<(), Error> { let current_epoch = state_ctxt.current_epoch; - if validator.is_eligible_for_activation_queue(&state_ctxt.fork_name, spec) { + if validator.is_eligible_for_activation_queue(spec, state_ctxt.fork_name) { validator.make_mut()?.activation_eligibility_epoch = current_epoch.safe_add(1)?; } diff --git a/consensus/types/src/validator.rs b/consensus/types/src/validator.rs index 618f7b280ad..4e72bfadd1c 100644 --- a/consensus/types/src/validator.rs +++ b/consensus/types/src/validator.rs @@ -57,26 +57,23 @@ impl Validator { /// Returns `true` if the validator is eligible to join the activation queue. /// - /// Calls the appropriate function given the fork_name. + /// Calls the correct function depending on the provided `fork_name`. pub fn is_eligible_for_activation_queue( &self, - current_fork: &ForkName, spec: &ChainSpec, + current_fork: ForkName, ) -> bool { - match current_fork { - ForkName::Base - | ForkName::Altair - | ForkName::Bellatrix - | ForkName::Capella - | ForkName::Deneb => self.is_eligible_for_activation_queue_base(spec), - ForkName::Electra => self.is_eligible_for_activation_queue_electra(spec), + if current_fork >= ForkName::Electra { + self.is_eligible_for_activation_queue_electra(spec) + } else { + self.is_eligible_for_activation_queue_base(spec) } } /// Returns `true` if the validator is eligible to join the activation queue. /// /// Spec v0.12.1 - pub fn is_eligible_for_activation_queue_base(&self, spec: &ChainSpec) -> bool { + fn is_eligible_for_activation_queue_base(&self, spec: &ChainSpec) -> bool { self.activation_eligibility_epoch == spec.far_future_epoch && self.effective_balance == spec.max_effective_balance } @@ -84,7 +81,7 @@ impl Validator { /// Returns `true` if the validator is eligible to join the activation queue. /// /// Modified in electra as part of EIP 7251. - pub fn is_eligible_for_activation_queue_electra(&self, spec: &ChainSpec) -> bool { + fn is_eligible_for_activation_queue_electra(&self, spec: &ChainSpec) -> bool { self.activation_eligibility_epoch == spec.far_future_epoch && self.effective_balance >= spec.min_activation_balance } @@ -157,17 +154,77 @@ impl Validator { /// Returns `true` if the validator is fully withdrawable at some epoch. /// - /// Note: Modified in electra. - pub fn is_fully_withdrawable_at(&self, balance: u64, epoch: Epoch, spec: &ChainSpec) -> bool { + /// Calls the correct function depending on the provided `fork_name`. + pub fn is_fully_withdrawable_at( + &self, + balance: u64, + epoch: Epoch, + spec: &ChainSpec, + current_fork: ForkName, + ) -> bool { + if current_fork >= ForkName::Electra { + self.is_fully_withdrawable_at_electra(balance, epoch, spec) + } else { + self.is_fully_withdrawable_at_capella(balance, epoch, spec) + } + } + + /// Returns `true` if the validator is fully withdrawable at some epoch. + fn is_fully_withdrawable_at_capella( + &self, + balance: u64, + epoch: Epoch, + spec: &ChainSpec, + ) -> bool { + self.has_eth1_withdrawal_credential(spec) && self.withdrawable_epoch <= epoch && balance > 0 + } + + /// Returns `true` if the validator is fully withdrawable at some epoch. + /// + /// Modified in electra as part of EIP 7251. + fn is_fully_withdrawable_at_electra( + &self, + balance: u64, + epoch: Epoch, + spec: &ChainSpec, + ) -> bool { self.has_execution_withdrawal_credential(spec) && self.withdrawable_epoch <= epoch && balance > 0 } + /// Returns `true` if the validator is partially withdrawable. + /// + /// Calls the correct function depending on the provided `fork_name`. + pub fn is_partially_withdrawable_validator( + &self, + balance: u64, + spec: &ChainSpec, + current_fork: ForkName, + ) -> bool { + if current_fork >= ForkName::Electra { + self.is_partially_withdrawable_validator_electra(balance, spec) + } + else { + self.is_partially_withdrawable_validator_capella(balance, spec) + } + } + + /// 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) + && self.effective_balance == spec.max_effective_balance + && balance > spec.max_effective_balance + } + /// Returns `true` if the validator is partially withdrawable. /// - /// Note: Modified in electra. - pub fn is_partially_withdrawable_validator(&self, balance: u64, spec: &ChainSpec) -> bool { + /// Modified in electra as part of EIP 7251. + pub fn is_partially_withdrawable_validator_electra( + &self, + balance: u64, + spec: &ChainSpec, + ) -> bool { let max_effective_balance = self.get_validator_max_effective_balance(spec); let has_max_effective_balance = self.effective_balance == max_effective_balance; let has_excess_balance = balance > max_effective_balance;