From 37c765809a57f0e5a663636aa84bec5e3d10838a Mon Sep 17 00:00:00 2001 From: Pawan Dhananjay Date: Tue, 29 Oct 2024 12:27:15 -0700 Subject: [PATCH] Make add_validator_to_registry more in line with the spec --- .../process_operations.rs | 11 +++- .../src/per_epoch_processing/single_pass.rs | 54 +++++-------------- consensus/types/src/beacon_state.rs | 20 +++---- consensus/types/src/validator.rs | 11 ++-- 4 files changed, 40 insertions(+), 56 deletions(-) diff --git a/consensus/state_processing/src/per_block_processing/process_operations.rs b/consensus/state_processing/src/per_block_processing/process_operations.rs index 5d862899bc..a405156acb 100644 --- a/consensus/state_processing/src/per_block_processing/process_operations.rs +++ b/consensus/state_processing/src/per_block_processing/process_operations.rs @@ -470,7 +470,16 @@ pub fn apply_deposit( return Ok(()); } - state.add_validator_to_registry(&deposit_data, spec)?; + state.add_validator_to_registry( + deposit_data.pubkey, + deposit_data.withdrawal_credentials, + if state.fork_name_unchecked() >= ForkName::Electra { + 0 + } else { + amount + }, + spec, + )?; // [New in Electra:EIP7251] if let Ok(pending_deposits) = state.pending_deposits_mut() { 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 e49b98700f..e79a6fb6fb 100644 --- a/consensus/state_processing/src/per_epoch_processing/single_pass.rs +++ b/consensus/state_processing/src/per_epoch_processing/single_pass.rs @@ -362,47 +362,19 @@ pub fn process_epoch_single_pass( // Apply the new deposits to the state for deposit in ctxt.new_validator_deposits.into_iter() { - if is_valid_deposit_signature( - &DepositData { - pubkey: deposit.pubkey, - withdrawal_credentials: deposit.withdrawal_credentials, - amount: deposit.amount, - signature: deposit.signature, - }, - spec, - ) - .is_ok() - { - let mut validator = Validator { - pubkey: deposit.pubkey, - withdrawal_credentials: deposit.withdrawal_credentials, - activation_eligibility_epoch: spec.far_future_epoch, - activation_epoch: spec.far_future_epoch, - exit_epoch: spec.far_future_epoch, - withdrawable_epoch: spec.far_future_epoch, - effective_balance: 0, - slashed: false, - }; - let amount = deposit.amount; - let max_effective_balance = - validator.get_max_effective_balance(spec, state.fork_name_unchecked()); - validator.effective_balance = std::cmp::min( - amount.safe_sub(amount.safe_rem(spec.effective_balance_increment)?)?, - max_effective_balance, - ); - - state.validators_mut().push(validator)?; - state.balances_mut().push(amount)?; - // Altair or later initializations. - if let Ok(previous_epoch_participation) = state.previous_epoch_participation_mut() { - previous_epoch_participation.push(ParticipationFlags::default())?; - } - if let Ok(current_epoch_participation) = state.current_epoch_participation_mut() { - current_epoch_participation.push(ParticipationFlags::default())?; - } - if let Ok(inactivity_scores) = state.inactivity_scores_mut() { - inactivity_scores.push(0)?; - } + let deposit_data = DepositData { + pubkey: deposit.pubkey, + withdrawal_credentials: deposit.withdrawal_credentials, + amount: deposit.amount, + signature: deposit.signature, + }; + if is_valid_deposit_signature(&deposit_data, spec).is_ok() { + state.add_validator_to_registry( + deposit_data.pubkey, + deposit_data.withdrawal_credentials, + deposit_data.amount, + spec, + )?; } } } diff --git a/consensus/types/src/beacon_state.rs b/consensus/types/src/beacon_state.rs index 99f5ea7756..eddd80e268 100644 --- a/consensus/types/src/beacon_state.rs +++ b/consensus/types/src/beacon_state.rs @@ -1550,17 +1550,19 @@ impl BeaconState { pub fn add_validator_to_registry( &mut self, - deposit_data: &DepositData, + pubkey: PublicKeyBytes, + withdrawal_credentials: Hash256, + amount: u64, spec: &ChainSpec, ) -> Result<(), Error> { - let fork = self.fork_name_unchecked(); - let amount = if fork.electra_enabled() { - 0 - } else { - deposit_data.amount - }; - self.validators_mut() - .push(Validator::from_deposit(deposit_data, amount, fork, spec))?; + let fork_name = self.fork_name_unchecked(); + self.validators_mut().push(Validator::from_deposit( + pubkey, + withdrawal_credentials, + amount, + fork_name, + spec, + ))?; self.balances_mut().push(amount)?; // Altair or later initializations. diff --git a/consensus/types/src/validator.rs b/consensus/types/src/validator.rs index f2a16bacd5..3717297abc 100644 --- a/consensus/types/src/validator.rs +++ b/consensus/types/src/validator.rs @@ -1,6 +1,6 @@ use crate::{ - test_utils::TestRandom, Address, BeaconState, ChainSpec, Checkpoint, DepositData, Epoch, - EthSpec, FixedBytesExtended, ForkName, Hash256, PublicKeyBytes, + test_utils::TestRandom, Address, BeaconState, ChainSpec, Checkpoint, Epoch, EthSpec, + FixedBytesExtended, ForkName, Hash256, PublicKeyBytes, }; use serde::{Deserialize, Serialize}; use ssz_derive::{Decode, Encode}; @@ -37,14 +37,15 @@ pub struct Validator { impl Validator { #[allow(clippy::arithmetic_side_effects)] pub fn from_deposit( - deposit_data: &DepositData, + pubkey: PublicKeyBytes, + withdrawal_credentials: Hash256, amount: u64, fork_name: ForkName, spec: &ChainSpec, ) -> Self { let mut validator = Validator { - pubkey: deposit_data.pubkey, - withdrawal_credentials: deposit_data.withdrawal_credentials, + pubkey, + withdrawal_credentials, activation_eligibility_epoch: spec.far_future_epoch, activation_epoch: spec.far_future_epoch, exit_epoch: spec.far_future_epoch,