Make add_validator_to_registry more in line with the spec

This commit is contained in:
Pawan Dhananjay
2024-10-29 12:27:15 -07:00
parent 061be34758
commit 37c765809a
4 changed files with 40 additions and 56 deletions

View File

@@ -470,7 +470,16 @@ pub fn apply_deposit<E: EthSpec>(
return Ok(()); 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] // [New in Electra:EIP7251]
if let Ok(pending_deposits) = state.pending_deposits_mut() { if let Ok(pending_deposits) = state.pending_deposits_mut() {

View File

@@ -362,47 +362,19 @@ pub fn process_epoch_single_pass<E: EthSpec>(
// Apply the new deposits to the state // Apply the new deposits to the state
for deposit in ctxt.new_validator_deposits.into_iter() { for deposit in ctxt.new_validator_deposits.into_iter() {
if is_valid_deposit_signature( let deposit_data = DepositData {
&DepositData { pubkey: deposit.pubkey,
pubkey: deposit.pubkey, withdrawal_credentials: deposit.withdrawal_credentials,
withdrawal_credentials: deposit.withdrawal_credentials, amount: deposit.amount,
amount: deposit.amount, signature: deposit.signature,
signature: deposit.signature, };
}, if is_valid_deposit_signature(&deposit_data, spec).is_ok() {
spec, state.add_validator_to_registry(
) deposit_data.pubkey,
.is_ok() deposit_data.withdrawal_credentials,
{ deposit_data.amount,
let mut validator = Validator { spec,
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)?;
}
} }
} }
} }

View File

@@ -1550,17 +1550,19 @@ impl<E: EthSpec> BeaconState<E> {
pub fn add_validator_to_registry( pub fn add_validator_to_registry(
&mut self, &mut self,
deposit_data: &DepositData, pubkey: PublicKeyBytes,
withdrawal_credentials: Hash256,
amount: u64,
spec: &ChainSpec, spec: &ChainSpec,
) -> Result<(), Error> { ) -> Result<(), Error> {
let fork = self.fork_name_unchecked(); let fork_name = self.fork_name_unchecked();
let amount = if fork.electra_enabled() { self.validators_mut().push(Validator::from_deposit(
0 pubkey,
} else { withdrawal_credentials,
deposit_data.amount amount,
}; fork_name,
self.validators_mut() spec,
.push(Validator::from_deposit(deposit_data, amount, fork, spec))?; ))?;
self.balances_mut().push(amount)?; self.balances_mut().push(amount)?;
// Altair or later initializations. // Altair or later initializations.

View File

@@ -1,6 +1,6 @@
use crate::{ use crate::{
test_utils::TestRandom, Address, BeaconState, ChainSpec, Checkpoint, DepositData, Epoch, test_utils::TestRandom, Address, BeaconState, ChainSpec, Checkpoint, Epoch, EthSpec,
EthSpec, FixedBytesExtended, ForkName, Hash256, PublicKeyBytes, FixedBytesExtended, ForkName, Hash256, PublicKeyBytes,
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use ssz_derive::{Decode, Encode}; use ssz_derive::{Decode, Encode};
@@ -37,14 +37,15 @@ pub struct Validator {
impl Validator { impl Validator {
#[allow(clippy::arithmetic_side_effects)] #[allow(clippy::arithmetic_side_effects)]
pub fn from_deposit( pub fn from_deposit(
deposit_data: &DepositData, pubkey: PublicKeyBytes,
withdrawal_credentials: Hash256,
amount: u64, amount: u64,
fork_name: ForkName, fork_name: ForkName,
spec: &ChainSpec, spec: &ChainSpec,
) -> Self { ) -> Self {
let mut validator = Validator { let mut validator = Validator {
pubkey: deposit_data.pubkey, pubkey,
withdrawal_credentials: deposit_data.withdrawal_credentials, withdrawal_credentials,
activation_eligibility_epoch: spec.far_future_epoch, activation_eligibility_epoch: spec.far_future_epoch,
activation_epoch: spec.far_future_epoch, activation_epoch: spec.far_future_epoch,
exit_epoch: spec.far_future_epoch, exit_epoch: spec.far_future_epoch,