Skip to content

Commit

Permalink
Make add_validator_to_registry more in line with the spec
Browse files Browse the repository at this point in the history
  • Loading branch information
pawanjay176 committed Oct 29, 2024
1 parent 061be34 commit 37c7658
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
54 changes: 13 additions & 41 deletions consensus/state_processing/src/per_epoch_processing/single_pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)?;
}
}
}
Expand Down
20 changes: 11 additions & 9 deletions consensus/types/src/beacon_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
11 changes: 6 additions & 5 deletions consensus/types/src/validator.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 37c7658

Please sign in to comment.