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(());
}
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() {

View File

@@ -362,47 +362,19 @@ pub fn process_epoch_single_pass<E: EthSpec>(
// 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,
)?;
}
}
}

View File

@@ -1550,17 +1550,19 @@ impl<E: EthSpec> BeaconState<E> {
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.

View File

@@ -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,