mirror of
https://github.com/sigp/lighthouse.git
synced 2026-04-28 02:03:32 +00:00
Simplify Validator Creation and Align with Spec (#6515)
* Simplify Validator Creation and Align with Spec * clippy * Bug Fix
This commit is contained in:
@@ -1548,6 +1548,35 @@ impl<E: EthSpec> BeaconState<E> {
|
||||
.ok_or(Error::UnknownValidator(validator_index))
|
||||
}
|
||||
|
||||
pub fn add_validator_to_registry(
|
||||
&mut self,
|
||||
deposit_data: &DepositData,
|
||||
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))?;
|
||||
self.balances_mut().push(amount)?;
|
||||
|
||||
// Altair or later initializations.
|
||||
if let Ok(previous_epoch_participation) = self.previous_epoch_participation_mut() {
|
||||
previous_epoch_participation.push(ParticipationFlags::default())?;
|
||||
}
|
||||
if let Ok(current_epoch_participation) = self.current_epoch_participation_mut() {
|
||||
current_epoch_participation.push(ParticipationFlags::default())?;
|
||||
}
|
||||
if let Ok(inactivity_scores) = self.inactivity_scores_mut() {
|
||||
inactivity_scores.push(0)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Safe copy-on-write accessor for the `validators` list.
|
||||
pub fn get_validator_cow(
|
||||
&mut self,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use crate::{
|
||||
test_utils::TestRandom, Address, BeaconState, ChainSpec, Checkpoint, Epoch, EthSpec,
|
||||
FixedBytesExtended, ForkName, Hash256, PublicKeyBytes,
|
||||
test_utils::TestRandom, Address, BeaconState, ChainSpec, Checkpoint, DepositData, Epoch,
|
||||
EthSpec, FixedBytesExtended, ForkName, Hash256, PublicKeyBytes,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use ssz_derive::{Decode, Encode};
|
||||
@@ -35,6 +35,34 @@ pub struct Validator {
|
||||
}
|
||||
|
||||
impl Validator {
|
||||
#[allow(clippy::arithmetic_side_effects)]
|
||||
pub fn from_deposit(
|
||||
deposit_data: &DepositData,
|
||||
amount: u64,
|
||||
fork_name: ForkName,
|
||||
spec: &ChainSpec,
|
||||
) -> Self {
|
||||
let mut validator = Validator {
|
||||
pubkey: deposit_data.pubkey,
|
||||
withdrawal_credentials: deposit_data.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 max_effective_balance = validator.get_max_effective_balance(spec, fork_name);
|
||||
// safe math is unnecessary here since the spec.effecive_balance_increment is never <= 0
|
||||
validator.effective_balance = std::cmp::min(
|
||||
amount - (amount % spec.effective_balance_increment),
|
||||
max_effective_balance,
|
||||
);
|
||||
|
||||
validator
|
||||
}
|
||||
|
||||
/// Returns `true` if the validator is considered active at some epoch.
|
||||
pub fn is_active_at(&self, epoch: Epoch) -> bool {
|
||||
self.activation_epoch <= epoch && epoch < self.exit_epoch
|
||||
|
||||
Reference in New Issue
Block a user