Added Capella Epoch Processing Logic (#3666)

This commit is contained in:
ethDreamer
2022-10-27 16:41:39 -05:00
committed by GitHub
parent 137f230344
commit f1a3b3b01c
9 changed files with 113 additions and 12 deletions

View File

@@ -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};

View 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(())
}

View File

@@ -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)?;

View File

@@ -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(())
}

View File

@@ -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(())
}