mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-06 10:11:44 +00:00
Added Capella Epoch Processing Logic (#3666)
This commit is contained in:
@@ -4,6 +4,7 @@ mod get_attesting_indices;
|
||||
mod get_indexed_attestation;
|
||||
mod initiate_validator_exit;
|
||||
mod slash_validator;
|
||||
mod withdraw_balance;
|
||||
|
||||
pub mod altair;
|
||||
pub mod base;
|
||||
@@ -14,6 +15,7 @@ pub use get_attesting_indices::{get_attesting_indices, get_attesting_indices_fro
|
||||
pub use get_indexed_attestation::get_indexed_attestation;
|
||||
pub use initiate_validator_exit::initiate_validator_exit;
|
||||
pub use slash_validator::slash_validator;
|
||||
pub use withdraw_balance::withdraw_balance;
|
||||
|
||||
use safe_arith::SafeArith;
|
||||
use types::{BeaconState, BeaconStateError, EthSpec};
|
||||
|
||||
28
consensus/state_processing/src/common/withdraw_balance.rs
Normal file
28
consensus/state_processing/src/common/withdraw_balance.rs
Normal file
@@ -0,0 +1,28 @@
|
||||
use crate::common::decrease_balance;
|
||||
use safe_arith::SafeArith;
|
||||
use types::{BeaconStateError as Error, *};
|
||||
|
||||
pub fn withdraw_balance<T: EthSpec>(
|
||||
state: &mut BeaconState<T>,
|
||||
validator_index: usize,
|
||||
amount: u64,
|
||||
) -> Result<(), Error> {
|
||||
decrease_balance(state, validator_index as usize, amount)?;
|
||||
|
||||
let withdrawal_address = Address::from_slice(
|
||||
&state
|
||||
.get_validator(validator_index)?
|
||||
.withdrawal_credentials
|
||||
.as_bytes()[12..],
|
||||
);
|
||||
let withdrawal = Withdrawal {
|
||||
index: *state.next_withdrawal_index()?,
|
||||
validator_index: validator_index as u64,
|
||||
address: withdrawal_address,
|
||||
amount,
|
||||
};
|
||||
state.next_withdrawal_index_mut()?.safe_add_assign(1)?;
|
||||
state.withdrawal_queue_mut()?.push(withdrawal)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -66,9 +66,9 @@ pub fn process_epoch<T: EthSpec>(
|
||||
altair::process_sync_committee_updates(state, spec)?;
|
||||
|
||||
// Withdrawals
|
||||
process_full_withdrawals(state)?;
|
||||
process_full_withdrawals(state, spec)?;
|
||||
|
||||
process_partial_withdrawals(state)?;
|
||||
process_partial_withdrawals(state, spec)?;
|
||||
|
||||
// Rotate the epoch caches to suit the epoch transition.
|
||||
state.advance_caches(spec)?;
|
||||
|
||||
@@ -1,10 +1,23 @@
|
||||
use crate::common::withdraw_balance;
|
||||
use crate::EpochProcessingError;
|
||||
use types::beacon_state::BeaconState;
|
||||
use types::eth_spec::EthSpec;
|
||||
use types::{beacon_state::BeaconState, eth_spec::EthSpec, ChainSpec};
|
||||
|
||||
pub fn process_full_withdrawals<T: EthSpec>(
|
||||
_state: &mut BeaconState<T>,
|
||||
state: &mut BeaconState<T>,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), EpochProcessingError> {
|
||||
todo!("implement this");
|
||||
let current_epoch = state.current_epoch();
|
||||
// FIXME: is this the most efficient way to do this?
|
||||
for validator_index in 0..state.validators().len() {
|
||||
// TODO: is this the correct way to handle validators not existing?
|
||||
if let (Some(validator), Some(balance)) = (
|
||||
state.validators().get(validator_index),
|
||||
state.balances().get(validator_index),
|
||||
) {
|
||||
if validator.is_fully_withdrawable_at(*balance, current_epoch, spec) {
|
||||
withdraw_balance(state, validator_index, *balance)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1,10 +1,39 @@
|
||||
use crate::common::withdraw_balance;
|
||||
use crate::EpochProcessingError;
|
||||
use types::beacon_state::BeaconState;
|
||||
use types::eth_spec::EthSpec;
|
||||
use safe_arith::SafeArith;
|
||||
use types::{beacon_state::BeaconState, eth_spec::EthSpec, ChainSpec};
|
||||
|
||||
pub fn process_partial_withdrawals<T: EthSpec>(
|
||||
_state: &mut BeaconState<T>,
|
||||
state: &mut BeaconState<T>,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), EpochProcessingError> {
|
||||
todo!("implement this");
|
||||
let mut partial_withdrawals_count = 0;
|
||||
let mut validator_index = *state.next_partial_withdrawal_validator_index()? as usize;
|
||||
|
||||
let n_validators = state.validators().len();
|
||||
// FIXME: is this the most efficient way to do this?
|
||||
for _ in 0..n_validators {
|
||||
// TODO: is this the correct way to handle validators not existing?
|
||||
if let (Some(validator), Some(balance)) = (
|
||||
state.validators().get(validator_index),
|
||||
state.balances().get(validator_index),
|
||||
) {
|
||||
if validator.is_partially_withdrawable_validator(*balance, spec) {
|
||||
withdraw_balance(
|
||||
state,
|
||||
validator_index,
|
||||
*balance - spec.max_effective_balance,
|
||||
)?;
|
||||
partial_withdrawals_count.safe_add_assign(1)?;
|
||||
|
||||
validator_index = validator_index.safe_add(1)? % n_validators;
|
||||
if partial_withdrawals_count == T::max_partial_withdrawals_per_epoch() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
*state.next_partial_withdrawal_validator_index_mut()? = validator_index as u64;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user