Skip to content

Commit 5781d64

Browse files
authored
Merge of #6515
2 parents 6ad2c18 + 462b1eb commit 5781d64

File tree

3 files changed

+62
-42
lines changed

3 files changed

+62
-42
lines changed

consensus/state_processing/src/per_block_processing/process_operations.rs

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -475,50 +475,13 @@ pub fn apply_deposit<E: EthSpec>(
475475
return Ok(());
476476
}
477477

478-
let new_validator_index = state.validators().len();
479-
480-
// [Modified in Electra:EIP7251]
481-
let (effective_balance, state_balance) = if state.fork_name_unchecked() >= ForkName::Electra
482-
{
483-
(0, 0)
484-
} else {
485-
(
486-
std::cmp::min(
487-
amount.safe_sub(amount.safe_rem(spec.effective_balance_increment)?)?,
488-
spec.max_effective_balance,
489-
),
490-
amount,
491-
)
492-
};
493-
// Create a new validator.
494-
let validator = Validator {
495-
pubkey: deposit_data.pubkey,
496-
withdrawal_credentials: deposit_data.withdrawal_credentials,
497-
activation_eligibility_epoch: spec.far_future_epoch,
498-
activation_epoch: spec.far_future_epoch,
499-
exit_epoch: spec.far_future_epoch,
500-
withdrawable_epoch: spec.far_future_epoch,
501-
effective_balance,
502-
slashed: false,
503-
};
504-
state.validators_mut().push(validator)?;
505-
state.balances_mut().push(state_balance)?;
506-
507-
// Altair or later initializations.
508-
if let Ok(previous_epoch_participation) = state.previous_epoch_participation_mut() {
509-
previous_epoch_participation.push(ParticipationFlags::default())?;
510-
}
511-
if let Ok(current_epoch_participation) = state.current_epoch_participation_mut() {
512-
current_epoch_participation.push(ParticipationFlags::default())?;
513-
}
514-
if let Ok(inactivity_scores) = state.inactivity_scores_mut() {
515-
inactivity_scores.push(0)?;
516-
}
478+
state.add_validator_to_registry(&deposit_data, spec)?;
479+
let new_validator_index = state.validators().len().safe_sub(1)? as u64;
517480

518481
// [New in Electra:EIP7251]
519482
if let Ok(pending_balance_deposits) = state.pending_balance_deposits_mut() {
520483
pending_balance_deposits.push(PendingBalanceDeposit {
521-
index: new_validator_index as u64,
484+
index: new_validator_index,
522485
amount,
523486
})?;
524487
}

consensus/types/src/beacon_state.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1548,6 +1548,35 @@ impl<E: EthSpec> BeaconState<E> {
15481548
.ok_or(Error::UnknownValidator(validator_index))
15491549
}
15501550

1551+
pub fn add_validator_to_registry(
1552+
&mut self,
1553+
deposit_data: &DepositData,
1554+
spec: &ChainSpec,
1555+
) -> Result<(), Error> {
1556+
let fork = self.fork_name_unchecked();
1557+
let amount = if fork.electra_enabled() {
1558+
0
1559+
} else {
1560+
deposit_data.amount
1561+
};
1562+
self.validators_mut()
1563+
.push(Validator::from_deposit(deposit_data, amount, fork, spec))?;
1564+
self.balances_mut().push(amount)?;
1565+
1566+
// Altair or later initializations.
1567+
if let Ok(previous_epoch_participation) = self.previous_epoch_participation_mut() {
1568+
previous_epoch_participation.push(ParticipationFlags::default())?;
1569+
}
1570+
if let Ok(current_epoch_participation) = self.current_epoch_participation_mut() {
1571+
current_epoch_participation.push(ParticipationFlags::default())?;
1572+
}
1573+
if let Ok(inactivity_scores) = self.inactivity_scores_mut() {
1574+
inactivity_scores.push(0)?;
1575+
}
1576+
1577+
Ok(())
1578+
}
1579+
15511580
/// Safe copy-on-write accessor for the `validators` list.
15521581
pub fn get_validator_cow(
15531582
&mut self,

consensus/types/src/validator.rs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{
2-
test_utils::TestRandom, Address, BeaconState, ChainSpec, Checkpoint, Epoch, EthSpec,
3-
FixedBytesExtended, ForkName, Hash256, PublicKeyBytes,
2+
test_utils::TestRandom, Address, BeaconState, ChainSpec, Checkpoint, DepositData, Epoch,
3+
EthSpec, FixedBytesExtended, ForkName, Hash256, PublicKeyBytes,
44
};
55
use serde::{Deserialize, Serialize};
66
use ssz_derive::{Decode, Encode};
@@ -35,6 +35,34 @@ pub struct Validator {
3535
}
3636

3737
impl Validator {
38+
#[allow(clippy::arithmetic_side_effects)]
39+
pub fn from_deposit(
40+
deposit_data: &DepositData,
41+
amount: u64,
42+
fork_name: ForkName,
43+
spec: &ChainSpec,
44+
) -> Self {
45+
let mut validator = Validator {
46+
pubkey: deposit_data.pubkey,
47+
withdrawal_credentials: deposit_data.withdrawal_credentials,
48+
activation_eligibility_epoch: spec.far_future_epoch,
49+
activation_epoch: spec.far_future_epoch,
50+
exit_epoch: spec.far_future_epoch,
51+
withdrawable_epoch: spec.far_future_epoch,
52+
effective_balance: 0,
53+
slashed: false,
54+
};
55+
56+
let max_effective_balance = validator.get_max_effective_balance(spec, fork_name);
57+
// safe math is unnecessary here since the spec.effecive_balance_increment is never <= 0
58+
validator.effective_balance = std::cmp::min(
59+
amount - (amount % spec.effective_balance_increment),
60+
max_effective_balance,
61+
);
62+
63+
validator
64+
}
65+
3866
/// Returns `true` if the validator is considered active at some epoch.
3967
pub fn is_active_at(&self, epoch: Epoch) -> bool {
4068
self.activation_epoch <= epoch && epoch < self.exit_epoch

0 commit comments

Comments
 (0)