mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-03 00:31:50 +00:00
Electra alpha8 spec updates (#6496)
* Fix partial withdrawals count * Remove get_active_balance * Remove queue_entire_balance_and_reset_validator * Switch to compounding when consolidating with source==target * Queue deposit requests and apply them during epoch processing * Fix ef tests * Clear todos * Fix engine api formatting issues * Merge branch 'unstable' into electra-alpha7 * Make add_validator_to_registry more in line with the spec * Address some review comments * Cleanup * Update initialize_beacon_state_from_eth1 * Merge branch 'unstable' into electra-alpha7 * Fix rpc decoding for blobs by range/root * Fix block hash computation * Fix process_deposits bug * Merge branch 'unstable' into electra-alpha7 * Fix topup deposit processing bug * Update builder api for electra * Refactor mock builder to separate functionality * Merge branch 'unstable' into electra-alpha7 * Address review comments * Use copied for reference rather than cloned * Optimise and simplify PendingDepositsContext::new * Merge remote-tracking branch 'origin/unstable' into electra-alpha7 * Fix processing of deposits with invalid signatures * Remove redundant code in genesis init * Revert "Refactor mock builder to separate functionality" This reverts commit6d10456912. * Revert "Update builder api for electra" This reverts commitc5c9aca6db. * Simplify pre-activation sorting * Fix stale validators used in upgrade_to_electra * Merge branch 'unstable' into electra-alpha7
This commit is contained in:
@@ -514,6 +514,7 @@ pub fn get_expected_withdrawals<E: EthSpec>(
|
||||
// Consume pending partial withdrawals
|
||||
let partial_withdrawals_count =
|
||||
if let Ok(partial_withdrawals) = state.pending_partial_withdrawals() {
|
||||
let mut partial_withdrawals_count = 0;
|
||||
for withdrawal in partial_withdrawals {
|
||||
if withdrawal.withdrawable_epoch > epoch
|
||||
|| withdrawals.len() == spec.max_pending_partials_per_withdrawals_sweep as usize
|
||||
@@ -546,8 +547,9 @@ pub fn get_expected_withdrawals<E: EthSpec>(
|
||||
});
|
||||
withdrawal_index.safe_add_assign(1)?;
|
||||
}
|
||||
partial_withdrawals_count.safe_add_assign(1)?;
|
||||
}
|
||||
Some(withdrawals.len())
|
||||
Some(partial_withdrawals_count)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
@@ -7,7 +7,6 @@ use crate::per_block_processing::errors::{BlockProcessingError, IntoWithIndex};
|
||||
use crate::VerifySignatures;
|
||||
use types::consts::altair::{PARTICIPATION_FLAG_WEIGHTS, PROPOSER_WEIGHT, WEIGHT_DENOMINATOR};
|
||||
use types::typenum::U33;
|
||||
use types::validator::is_compounding_withdrawal_credential;
|
||||
|
||||
pub fn process_operations<E: EthSpec, Payload: AbstractExecPayload<E>>(
|
||||
state: &mut BeaconState<E>,
|
||||
@@ -378,7 +377,7 @@ pub fn process_deposits<E: EthSpec>(
|
||||
if state.eth1_deposit_index() < eth1_deposit_index_limit {
|
||||
let expected_deposit_len = std::cmp::min(
|
||||
E::MaxDeposits::to_u64(),
|
||||
state.get_outstanding_deposit_len()?,
|
||||
eth1_deposit_index_limit.safe_sub(state.eth1_deposit_index())?,
|
||||
);
|
||||
block_verify!(
|
||||
deposits.len() as u64 == expected_deposit_len,
|
||||
@@ -450,39 +449,46 @@ pub fn apply_deposit<E: EthSpec>(
|
||||
|
||||
if let Some(index) = validator_index {
|
||||
// [Modified in Electra:EIP7251]
|
||||
if let Ok(pending_balance_deposits) = state.pending_balance_deposits_mut() {
|
||||
pending_balance_deposits.push(PendingBalanceDeposit { index, amount })?;
|
||||
|
||||
let validator = state
|
||||
.validators()
|
||||
.get(index as usize)
|
||||
.ok_or(BeaconStateError::UnknownValidator(index as usize))?;
|
||||
|
||||
if is_compounding_withdrawal_credential(deposit_data.withdrawal_credentials, spec)
|
||||
&& validator.has_eth1_withdrawal_credential(spec)
|
||||
&& is_valid_deposit_signature(&deposit_data, spec).is_ok()
|
||||
{
|
||||
state.switch_to_compounding_validator(index as usize, spec)?;
|
||||
}
|
||||
if let Ok(pending_deposits) = state.pending_deposits_mut() {
|
||||
pending_deposits.push(PendingDeposit {
|
||||
pubkey: deposit_data.pubkey,
|
||||
withdrawal_credentials: deposit_data.withdrawal_credentials,
|
||||
amount,
|
||||
signature: deposit_data.signature,
|
||||
slot: spec.genesis_slot, // Use `genesis_slot` to distinguish from a pending deposit request
|
||||
})?;
|
||||
} else {
|
||||
// Update the existing validator balance.
|
||||
increase_balance(state, index as usize, amount)?;
|
||||
}
|
||||
} else {
|
||||
}
|
||||
// New validator
|
||||
else {
|
||||
// The signature should be checked for new validators. Return early for a bad
|
||||
// signature.
|
||||
if is_valid_deposit_signature(&deposit_data, spec).is_err() {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
state.add_validator_to_registry(&deposit_data, spec)?;
|
||||
let new_validator_index = state.validators().len().safe_sub(1)? as u64;
|
||||
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_balance_deposits) = state.pending_balance_deposits_mut() {
|
||||
pending_balance_deposits.push(PendingBalanceDeposit {
|
||||
index: new_validator_index,
|
||||
if let Ok(pending_deposits) = state.pending_deposits_mut() {
|
||||
pending_deposits.push(PendingDeposit {
|
||||
pubkey: deposit_data.pubkey,
|
||||
withdrawal_credentials: deposit_data.withdrawal_credentials,
|
||||
amount,
|
||||
signature: deposit_data.signature,
|
||||
slot: spec.genesis_slot, // Use `genesis_slot` to distinguish from a pending deposit request
|
||||
})?;
|
||||
}
|
||||
}
|
||||
@@ -596,13 +602,18 @@ pub fn process_deposit_requests<E: EthSpec>(
|
||||
if state.deposit_requests_start_index()? == spec.unset_deposit_requests_start_index {
|
||||
*state.deposit_requests_start_index_mut()? = request.index
|
||||
}
|
||||
let deposit_data = DepositData {
|
||||
pubkey: request.pubkey,
|
||||
withdrawal_credentials: request.withdrawal_credentials,
|
||||
amount: request.amount,
|
||||
signature: request.signature.clone().into(),
|
||||
};
|
||||
apply_deposit(state, deposit_data, None, false, spec)?
|
||||
let slot = state.slot();
|
||||
|
||||
// [New in Electra:EIP7251]
|
||||
if let Ok(pending_deposits) = state.pending_deposits_mut() {
|
||||
pending_deposits.push(PendingDeposit {
|
||||
pubkey: request.pubkey,
|
||||
withdrawal_credentials: request.withdrawal_credentials,
|
||||
amount: request.amount,
|
||||
signature: request.signature.clone(),
|
||||
slot,
|
||||
})?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
@@ -621,11 +632,84 @@ pub fn process_consolidation_requests<E: EthSpec>(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn is_valid_switch_to_compounding_request<E: EthSpec>(
|
||||
state: &BeaconState<E>,
|
||||
consolidation_request: &ConsolidationRequest,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<bool, BlockProcessingError> {
|
||||
// Switch to compounding requires source and target be equal
|
||||
if consolidation_request.source_pubkey != consolidation_request.target_pubkey {
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
// Verify pubkey exists
|
||||
let Some(source_index) = state
|
||||
.pubkey_cache()
|
||||
.get(&consolidation_request.source_pubkey)
|
||||
else {
|
||||
// source validator doesn't exist
|
||||
return Ok(false);
|
||||
};
|
||||
|
||||
let source_validator = state.get_validator(source_index)?;
|
||||
// Verify the source withdrawal credentials
|
||||
// Note: We need to specifically check for eth1 withdrawal credentials here
|
||||
// If the validator is already compounding, the compounding request is not valid.
|
||||
if let Some(withdrawal_address) = source_validator
|
||||
.has_eth1_withdrawal_credential(spec)
|
||||
.then(|| {
|
||||
source_validator
|
||||
.withdrawal_credentials
|
||||
.as_slice()
|
||||
.get(12..)
|
||||
.map(Address::from_slice)
|
||||
})
|
||||
.flatten()
|
||||
{
|
||||
if withdrawal_address != consolidation_request.source_address {
|
||||
return Ok(false);
|
||||
}
|
||||
} else {
|
||||
// Source doesn't have eth1 withdrawal credentials
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
// Verify the source is active
|
||||
let current_epoch = state.current_epoch();
|
||||
if !source_validator.is_active_at(current_epoch) {
|
||||
return Ok(false);
|
||||
}
|
||||
// Verify exits for source has not been initiated
|
||||
if source_validator.exit_epoch != spec.far_future_epoch {
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
pub fn process_consolidation_request<E: EthSpec>(
|
||||
state: &mut BeaconState<E>,
|
||||
consolidation_request: &ConsolidationRequest,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), BlockProcessingError> {
|
||||
if is_valid_switch_to_compounding_request(state, consolidation_request, spec)? {
|
||||
let Some(source_index) = state
|
||||
.pubkey_cache()
|
||||
.get(&consolidation_request.source_pubkey)
|
||||
else {
|
||||
// source validator doesn't exist. This is unreachable as `is_valid_switch_to_compounding_request`
|
||||
// will return false in that case.
|
||||
return Ok(());
|
||||
};
|
||||
state.switch_to_compounding_validator(source_index, spec)?;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// Verify that source != target, so a consolidation cannot be used as an exit.
|
||||
if consolidation_request.source_pubkey == consolidation_request.target_pubkey {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// If the pending consolidations queue is full, consolidation requests are ignored
|
||||
if state.pending_consolidations()?.len() == E::PendingConsolidationsLimit::to_usize() {
|
||||
return Ok(());
|
||||
@@ -649,10 +733,6 @@ pub fn process_consolidation_request<E: EthSpec>(
|
||||
// target validator doesn't exist
|
||||
return Ok(());
|
||||
};
|
||||
// Verify that source != target, so a consolidation cannot be used as an exit.
|
||||
if source_index == target_index {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let source_validator = state.get_validator(source_index)?;
|
||||
// Verify the source withdrawal credentials
|
||||
@@ -699,5 +779,10 @@ pub fn process_consolidation_request<E: EthSpec>(
|
||||
target_index: target_index as u64,
|
||||
})?;
|
||||
|
||||
let target_validator = state.get_validator(target_index)?;
|
||||
// Churn any target excess active balance of target and raise its max
|
||||
if target_validator.has_eth1_withdrawal_credential(spec) {
|
||||
state.switch_to_compounding_validator(target_index, spec)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -28,6 +28,7 @@ pub enum EpochProcessingError {
|
||||
SinglePassMissingActivationQueue,
|
||||
MissingEarliestExitEpoch,
|
||||
MissingExitBalanceToConsume,
|
||||
PendingDepositsLogicError,
|
||||
}
|
||||
|
||||
impl From<InclusionError> for EpochProcessingError {
|
||||
|
||||
@@ -4,6 +4,7 @@ use crate::{
|
||||
update_progressive_balances_cache::initialize_progressive_balances_cache,
|
||||
},
|
||||
epoch_cache::{initialize_epoch_cache, PreEpochCache},
|
||||
per_block_processing::is_valid_deposit_signature,
|
||||
per_epoch_processing::{Delta, Error, ParticipationEpochSummary},
|
||||
};
|
||||
use itertools::izip;
|
||||
@@ -16,9 +17,9 @@ use types::{
|
||||
TIMELY_TARGET_FLAG_INDEX, WEIGHT_DENOMINATOR,
|
||||
},
|
||||
milhouse::Cow,
|
||||
ActivationQueue, BeaconState, BeaconStateError, ChainSpec, Checkpoint, Epoch, EthSpec,
|
||||
ExitCache, ForkName, List, ParticipationFlags, PendingBalanceDeposit, ProgressiveBalancesCache,
|
||||
RelativeEpoch, Unsigned, Validator,
|
||||
ActivationQueue, BeaconState, BeaconStateError, ChainSpec, Checkpoint, DepositData, Epoch,
|
||||
EthSpec, ExitCache, ForkName, List, ParticipationFlags, PendingDeposit,
|
||||
ProgressiveBalancesCache, RelativeEpoch, Unsigned, Validator,
|
||||
};
|
||||
|
||||
pub struct SinglePassConfig {
|
||||
@@ -26,7 +27,7 @@ pub struct SinglePassConfig {
|
||||
pub rewards_and_penalties: bool,
|
||||
pub registry_updates: bool,
|
||||
pub slashings: bool,
|
||||
pub pending_balance_deposits: bool,
|
||||
pub pending_deposits: bool,
|
||||
pub pending_consolidations: bool,
|
||||
pub effective_balance_updates: bool,
|
||||
}
|
||||
@@ -44,7 +45,7 @@ impl SinglePassConfig {
|
||||
rewards_and_penalties: true,
|
||||
registry_updates: true,
|
||||
slashings: true,
|
||||
pending_balance_deposits: true,
|
||||
pending_deposits: true,
|
||||
pending_consolidations: true,
|
||||
effective_balance_updates: true,
|
||||
}
|
||||
@@ -56,7 +57,7 @@ impl SinglePassConfig {
|
||||
rewards_and_penalties: false,
|
||||
registry_updates: false,
|
||||
slashings: false,
|
||||
pending_balance_deposits: false,
|
||||
pending_deposits: false,
|
||||
pending_consolidations: false,
|
||||
effective_balance_updates: false,
|
||||
}
|
||||
@@ -85,15 +86,17 @@ struct SlashingsContext {
|
||||
penalty_per_effective_balance_increment: u64,
|
||||
}
|
||||
|
||||
struct PendingBalanceDepositsContext {
|
||||
struct PendingDepositsContext {
|
||||
/// The value to set `next_deposit_index` to *after* processing completes.
|
||||
next_deposit_index: usize,
|
||||
/// The value to set `deposit_balance_to_consume` to *after* processing completes.
|
||||
deposit_balance_to_consume: u64,
|
||||
/// Total balance increases for each validator due to pending balance deposits.
|
||||
validator_deposits_to_process: HashMap<usize, u64>,
|
||||
/// The deposits to append to `pending_balance_deposits` after processing all applicable deposits.
|
||||
deposits_to_postpone: Vec<PendingBalanceDeposit>,
|
||||
/// The deposits to append to `pending_deposits` after processing all applicable deposits.
|
||||
deposits_to_postpone: Vec<PendingDeposit>,
|
||||
/// New validators to be added to the state *after* processing completes.
|
||||
new_validator_deposits: Vec<PendingDeposit>,
|
||||
}
|
||||
|
||||
struct EffectiveBalancesContext {
|
||||
@@ -138,6 +141,7 @@ pub fn process_epoch_single_pass<E: EthSpec>(
|
||||
state.build_exit_cache(spec)?;
|
||||
state.build_committee_cache(RelativeEpoch::Previous, spec)?;
|
||||
state.build_committee_cache(RelativeEpoch::Current, spec)?;
|
||||
state.update_pubkey_cache()?;
|
||||
|
||||
let previous_epoch = state.previous_epoch();
|
||||
let current_epoch = state.current_epoch();
|
||||
@@ -163,12 +167,11 @@ pub fn process_epoch_single_pass<E: EthSpec>(
|
||||
let slashings_ctxt = &SlashingsContext::new(state, state_ctxt, spec)?;
|
||||
let mut next_epoch_cache = PreEpochCache::new_for_next_epoch(state)?;
|
||||
|
||||
let pending_balance_deposits_ctxt =
|
||||
if fork_name.electra_enabled() && conf.pending_balance_deposits {
|
||||
Some(PendingBalanceDepositsContext::new(state, spec)?)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let pending_deposits_ctxt = if fork_name.electra_enabled() && conf.pending_deposits {
|
||||
Some(PendingDepositsContext::new(state, spec, &conf)?)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let mut earliest_exit_epoch = state.earliest_exit_epoch().ok();
|
||||
let mut exit_balance_to_consume = state.exit_balance_to_consume().ok();
|
||||
@@ -303,9 +306,9 @@ pub fn process_epoch_single_pass<E: EthSpec>(
|
||||
process_single_slashing(&mut balance, &validator, slashings_ctxt, state_ctxt, spec)?;
|
||||
}
|
||||
|
||||
// `process_pending_balance_deposits`
|
||||
if let Some(pending_balance_deposits_ctxt) = &pending_balance_deposits_ctxt {
|
||||
process_pending_balance_deposits_for_validator(
|
||||
// `process_pending_deposits`
|
||||
if let Some(pending_balance_deposits_ctxt) = &pending_deposits_ctxt {
|
||||
process_pending_deposits_for_validator(
|
||||
&mut balance,
|
||||
validator_info,
|
||||
pending_balance_deposits_ctxt,
|
||||
@@ -342,20 +345,84 @@ pub fn process_epoch_single_pass<E: EthSpec>(
|
||||
// Finish processing pending balance deposits if relevant.
|
||||
//
|
||||
// This *could* be reordered after `process_pending_consolidations` which pushes only to the end
|
||||
// of the `pending_balance_deposits` list. But we may as well preserve the write ordering used
|
||||
// of the `pending_deposits` list. But we may as well preserve the write ordering used
|
||||
// by the spec and do this first.
|
||||
if let Some(ctxt) = pending_balance_deposits_ctxt {
|
||||
let mut new_pending_balance_deposits = List::try_from_iter(
|
||||
if let Some(ctxt) = pending_deposits_ctxt {
|
||||
let mut new_balance_deposits = List::try_from_iter(
|
||||
state
|
||||
.pending_balance_deposits()?
|
||||
.pending_deposits()?
|
||||
.iter_from(ctxt.next_deposit_index)?
|
||||
.cloned(),
|
||||
)?;
|
||||
for deposit in ctxt.deposits_to_postpone {
|
||||
new_pending_balance_deposits.push(deposit)?;
|
||||
new_balance_deposits.push(deposit)?;
|
||||
}
|
||||
*state.pending_balance_deposits_mut()? = new_pending_balance_deposits;
|
||||
*state.pending_deposits_mut()? = new_balance_deposits;
|
||||
*state.deposit_balance_to_consume_mut()? = ctxt.deposit_balance_to_consume;
|
||||
|
||||
// `new_validator_deposits` may contain multiple deposits with the same pubkey where
|
||||
// the first deposit creates the new validator and the others are topups.
|
||||
// Each item in the vec is a (pubkey, validator_index)
|
||||
let mut added_validators = Vec::new();
|
||||
for deposit in ctxt.new_validator_deposits {
|
||||
let deposit_data = DepositData {
|
||||
pubkey: deposit.pubkey,
|
||||
withdrawal_credentials: deposit.withdrawal_credentials,
|
||||
amount: deposit.amount,
|
||||
signature: deposit.signature,
|
||||
};
|
||||
// Only check the signature if this is the first deposit for the validator,
|
||||
// following the logic from `apply_pending_deposit` in the spec.
|
||||
if let Some(validator_index) = state.get_validator_index(&deposit_data.pubkey)? {
|
||||
state
|
||||
.get_balance_mut(validator_index)?
|
||||
.safe_add_assign(deposit_data.amount)?;
|
||||
} else if is_valid_deposit_signature(&deposit_data, spec).is_ok() {
|
||||
// Apply the new deposit to the state
|
||||
let validator_index = state.add_validator_to_registry(
|
||||
deposit_data.pubkey,
|
||||
deposit_data.withdrawal_credentials,
|
||||
deposit_data.amount,
|
||||
spec,
|
||||
)?;
|
||||
added_validators.push((deposit_data.pubkey, validator_index));
|
||||
}
|
||||
}
|
||||
if conf.effective_balance_updates {
|
||||
// Re-process effective balance updates for validators affected by top-up of new validators.
|
||||
let (
|
||||
validators,
|
||||
balances,
|
||||
_,
|
||||
current_epoch_participation,
|
||||
_,
|
||||
progressive_balances,
|
||||
_,
|
||||
_,
|
||||
) = state.mutable_validator_fields()?;
|
||||
for (_, validator_index) in added_validators.iter() {
|
||||
let balance = *balances
|
||||
.get(*validator_index)
|
||||
.ok_or(BeaconStateError::UnknownValidator(*validator_index))?;
|
||||
let mut validator = validators
|
||||
.get_cow(*validator_index)
|
||||
.ok_or(BeaconStateError::UnknownValidator(*validator_index))?;
|
||||
let validator_current_epoch_participation = *current_epoch_participation
|
||||
.get(*validator_index)
|
||||
.ok_or(BeaconStateError::UnknownValidator(*validator_index))?;
|
||||
process_single_effective_balance_update(
|
||||
*validator_index,
|
||||
balance,
|
||||
&mut validator,
|
||||
validator_current_epoch_participation,
|
||||
&mut next_epoch_cache,
|
||||
progressive_balances,
|
||||
effective_balances_ctxt,
|
||||
state_ctxt,
|
||||
spec,
|
||||
)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Process consolidations outside the single-pass loop, as they depend on balances for multiple
|
||||
@@ -819,8 +886,12 @@ fn process_single_slashing(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
impl PendingBalanceDepositsContext {
|
||||
fn new<E: EthSpec>(state: &BeaconState<E>, spec: &ChainSpec) -> Result<Self, Error> {
|
||||
impl PendingDepositsContext {
|
||||
fn new<E: EthSpec>(
|
||||
state: &BeaconState<E>,
|
||||
spec: &ChainSpec,
|
||||
config: &SinglePassConfig,
|
||||
) -> Result<Self, Error> {
|
||||
let available_for_processing = state
|
||||
.deposit_balance_to_consume()?
|
||||
.safe_add(state.get_activation_exit_churn_limit(spec)?)?;
|
||||
@@ -830,10 +901,31 @@ impl PendingBalanceDepositsContext {
|
||||
let mut next_deposit_index = 0;
|
||||
let mut validator_deposits_to_process = HashMap::new();
|
||||
let mut deposits_to_postpone = vec![];
|
||||
let mut new_validator_deposits = vec![];
|
||||
let mut is_churn_limit_reached = false;
|
||||
let finalized_slot = state
|
||||
.finalized_checkpoint()
|
||||
.epoch
|
||||
.start_slot(E::slots_per_epoch());
|
||||
|
||||
let pending_balance_deposits = state.pending_balance_deposits()?;
|
||||
let pending_deposits = state.pending_deposits()?;
|
||||
|
||||
for deposit in pending_balance_deposits.iter() {
|
||||
for deposit in pending_deposits.iter() {
|
||||
// Do not process deposit requests if the Eth1 bridge deposits are not yet applied.
|
||||
if deposit.slot > spec.genesis_slot
|
||||
&& state.eth1_deposit_index() < state.deposit_requests_start_index()?
|
||||
{
|
||||
break;
|
||||
}
|
||||
// Do not process is deposit slot has not been finalized.
|
||||
if deposit.slot > finalized_slot {
|
||||
break;
|
||||
}
|
||||
// Do not process if we have reached the limit for the number of deposits
|
||||
// processed in an epoch.
|
||||
if next_deposit_index >= E::max_pending_deposits_per_epoch() {
|
||||
break;
|
||||
}
|
||||
// We have to do a bit of indexing into `validators` here, but I can't see any way
|
||||
// around that without changing the spec.
|
||||
//
|
||||
@@ -844,48 +936,70 @@ impl PendingBalanceDepositsContext {
|
||||
// take, just whether it is non-default. Nor do we need to know the value of
|
||||
// `withdrawable_epoch`, because `next_epoch <= withdrawable_epoch` will evaluate to
|
||||
// `true` both for the actual value & the default placeholder value (`FAR_FUTURE_EPOCH`).
|
||||
let validator = state.get_validator(deposit.index as usize)?;
|
||||
let already_exited = validator.exit_epoch < spec.far_future_epoch;
|
||||
// In the spec process_registry_updates is called before process_pending_balance_deposits
|
||||
// so we must account for process_registry_updates ejecting the validator for low balance
|
||||
// and setting the exit_epoch to < far_future_epoch. Note that in the spec the effective
|
||||
// balance update does not happen until *after* the registry update, so we don't need to
|
||||
// account for changes to the effective balance that would push it below the ejection
|
||||
// balance here.
|
||||
let will_be_exited = validator.is_active_at(current_epoch)
|
||||
&& validator.effective_balance <= spec.ejection_balance;
|
||||
if already_exited || will_be_exited {
|
||||
if next_epoch <= validator.withdrawable_epoch {
|
||||
deposits_to_postpone.push(deposit.clone());
|
||||
} else {
|
||||
// Deposited balance will never become active. Increase balance but do not
|
||||
// consume churn.
|
||||
validator_deposits_to_process
|
||||
.entry(deposit.index as usize)
|
||||
.or_insert(0)
|
||||
.safe_add_assign(deposit.amount)?;
|
||||
}
|
||||
} else {
|
||||
// Deposit does not fit in the churn, no more deposit processing in this epoch.
|
||||
if processed_amount.safe_add(deposit.amount)? > available_for_processing {
|
||||
break;
|
||||
}
|
||||
// Deposit fits in the churn, process it. Increase balance and consume churn.
|
||||
let mut is_validator_exited = false;
|
||||
let mut is_validator_withdrawn = false;
|
||||
let opt_validator_index = state.pubkey_cache().get(&deposit.pubkey);
|
||||
if let Some(validator_index) = opt_validator_index {
|
||||
let validator = state.get_validator(validator_index)?;
|
||||
let already_exited = validator.exit_epoch < spec.far_future_epoch;
|
||||
// In the spec process_registry_updates is called before process_pending_deposits
|
||||
// so we must account for process_registry_updates ejecting the validator for low balance
|
||||
// and setting the exit_epoch to < far_future_epoch. Note that in the spec the effective
|
||||
// balance update does not happen until *after* the registry update, so we don't need to
|
||||
// account for changes to the effective balance that would push it below the ejection
|
||||
// balance here.
|
||||
// Note: we only consider this if registry_updates are enabled in the config.
|
||||
// EF tests require us to run epoch_processing functions in isolation.
|
||||
let will_be_exited = config.registry_updates
|
||||
&& (validator.is_active_at(current_epoch)
|
||||
&& validator.effective_balance <= spec.ejection_balance);
|
||||
is_validator_exited = already_exited || will_be_exited;
|
||||
is_validator_withdrawn = validator.withdrawable_epoch < next_epoch;
|
||||
}
|
||||
|
||||
if is_validator_withdrawn {
|
||||
// Deposited balance will never become active. Queue a balance increase but do not
|
||||
// consume churn. Validator index must be known if the validator is known to be
|
||||
// withdrawn (see calculation of `is_validator_withdrawn` above).
|
||||
let validator_index =
|
||||
opt_validator_index.ok_or(Error::PendingDepositsLogicError)?;
|
||||
validator_deposits_to_process
|
||||
.entry(deposit.index as usize)
|
||||
.entry(validator_index)
|
||||
.or_insert(0)
|
||||
.safe_add_assign(deposit.amount)?;
|
||||
} else if is_validator_exited {
|
||||
// Validator is exiting, postpone the deposit until after withdrawable epoch
|
||||
deposits_to_postpone.push(deposit.clone());
|
||||
} else {
|
||||
// Check if deposit fits in the churn, otherwise, do no more deposit processing in this epoch.
|
||||
is_churn_limit_reached =
|
||||
processed_amount.safe_add(deposit.amount)? > available_for_processing;
|
||||
if is_churn_limit_reached {
|
||||
break;
|
||||
}
|
||||
processed_amount.safe_add_assign(deposit.amount)?;
|
||||
|
||||
// Deposit fits in the churn, process it. Increase balance and consume churn.
|
||||
if let Some(validator_index) = state.pubkey_cache().get(&deposit.pubkey) {
|
||||
validator_deposits_to_process
|
||||
.entry(validator_index)
|
||||
.or_insert(0)
|
||||
.safe_add_assign(deposit.amount)?;
|
||||
} else {
|
||||
// The `PendingDeposit` is for a new validator
|
||||
new_validator_deposits.push(deposit.clone());
|
||||
}
|
||||
}
|
||||
|
||||
// Regardless of how the deposit was handled, we move on in the queue.
|
||||
next_deposit_index.safe_add_assign(1)?;
|
||||
}
|
||||
|
||||
let deposit_balance_to_consume = if next_deposit_index == pending_balance_deposits.len() {
|
||||
0
|
||||
} else {
|
||||
// Accumulate churn only if the churn limit has been hit.
|
||||
let deposit_balance_to_consume = if is_churn_limit_reached {
|
||||
available_for_processing.safe_sub(processed_amount)?
|
||||
} else {
|
||||
0
|
||||
};
|
||||
|
||||
Ok(Self {
|
||||
@@ -893,14 +1007,15 @@ impl PendingBalanceDepositsContext {
|
||||
deposit_balance_to_consume,
|
||||
validator_deposits_to_process,
|
||||
deposits_to_postpone,
|
||||
new_validator_deposits,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn process_pending_balance_deposits_for_validator(
|
||||
fn process_pending_deposits_for_validator(
|
||||
balance: &mut Cow<u64>,
|
||||
validator_info: &ValidatorInfo,
|
||||
pending_balance_deposits_ctxt: &PendingBalanceDepositsContext,
|
||||
pending_balance_deposits_ctxt: &PendingDepositsContext,
|
||||
) -> Result<(), Error> {
|
||||
if let Some(deposit_amount) = pending_balance_deposits_ctxt
|
||||
.validator_deposits_to_process
|
||||
@@ -941,21 +1056,20 @@ fn process_pending_consolidations<E: EthSpec>(
|
||||
break;
|
||||
}
|
||||
|
||||
// Calculate the active balance while we have the source validator loaded. This is a safe
|
||||
// reordering.
|
||||
let source_balance = *state
|
||||
.balances()
|
||||
.get(source_index)
|
||||
.ok_or(BeaconStateError::UnknownValidator(source_index))?;
|
||||
let active_balance =
|
||||
source_validator.get_active_balance(source_balance, spec, state_ctxt.fork_name);
|
||||
|
||||
// Churn any target excess active balance of target and raise its max.
|
||||
state.switch_to_compounding_validator(target_index, spec)?;
|
||||
// Calculate the consolidated balance
|
||||
let max_effective_balance =
|
||||
source_validator.get_max_effective_balance(spec, state_ctxt.fork_name);
|
||||
let source_effective_balance = std::cmp::min(
|
||||
*state
|
||||
.balances()
|
||||
.get(source_index)
|
||||
.ok_or(BeaconStateError::UnknownValidator(source_index))?,
|
||||
max_effective_balance,
|
||||
);
|
||||
|
||||
// Move active balance to target. Excess balance is withdrawable.
|
||||
decrease_balance(state, source_index, active_balance)?;
|
||||
increase_balance(state, target_index, active_balance)?;
|
||||
decrease_balance(state, source_index, source_effective_balance)?;
|
||||
increase_balance(state, target_index, source_effective_balance)?;
|
||||
|
||||
affected_validators.insert(source_index);
|
||||
affected_validators.insert(target_index);
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
use bls::Signature;
|
||||
use itertools::Itertools;
|
||||
use safe_arith::SafeArith;
|
||||
use std::mem;
|
||||
use types::{
|
||||
BeaconState, BeaconStateElectra, BeaconStateError as Error, ChainSpec, Epoch, EpochCache,
|
||||
EthSpec, Fork,
|
||||
EthSpec, Fork, PendingDeposit,
|
||||
};
|
||||
|
||||
/// Transform a `Deneb` state into an `Electra` state.
|
||||
@@ -38,29 +40,44 @@ pub fn upgrade_to_electra<E: EthSpec>(
|
||||
|
||||
// Add validators that are not yet active to pending balance deposits
|
||||
let validators = post.validators().clone();
|
||||
let mut pre_activation = validators
|
||||
let pre_activation = validators
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(_, validator)| validator.activation_epoch == spec.far_future_epoch)
|
||||
.sorted_by_key(|(index, validator)| (validator.activation_eligibility_epoch, *index))
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
// Sort the indices by activation_eligibility_epoch and then by index
|
||||
pre_activation.sort_by(|(index_a, val_a), (index_b, val_b)| {
|
||||
if val_a.activation_eligibility_epoch == val_b.activation_eligibility_epoch {
|
||||
index_a.cmp(index_b)
|
||||
} else {
|
||||
val_a
|
||||
.activation_eligibility_epoch
|
||||
.cmp(&val_b.activation_eligibility_epoch)
|
||||
}
|
||||
});
|
||||
|
||||
// Process validators to queue entire balance and reset them
|
||||
for (index, _) in pre_activation {
|
||||
post.queue_entire_balance_and_reset_validator(index, spec)?;
|
||||
let balance = post
|
||||
.balances_mut()
|
||||
.get_mut(index)
|
||||
.ok_or(Error::UnknownValidator(index))?;
|
||||
let balance_copy = *balance;
|
||||
*balance = 0_u64;
|
||||
|
||||
let validator = post
|
||||
.validators_mut()
|
||||
.get_mut(index)
|
||||
.ok_or(Error::UnknownValidator(index))?;
|
||||
validator.effective_balance = 0;
|
||||
validator.activation_eligibility_epoch = spec.far_future_epoch;
|
||||
let pubkey = validator.pubkey;
|
||||
let withdrawal_credentials = validator.withdrawal_credentials;
|
||||
|
||||
post.pending_deposits_mut()?
|
||||
.push(PendingDeposit {
|
||||
pubkey,
|
||||
withdrawal_credentials,
|
||||
amount: balance_copy,
|
||||
signature: Signature::infinity()?.into(),
|
||||
slot: spec.genesis_slot,
|
||||
})
|
||||
.map_err(Error::MilhouseError)?;
|
||||
}
|
||||
|
||||
// Ensure early adopters of compounding credentials go through the activation churn
|
||||
let validators = post.validators().clone();
|
||||
for (index, validator) in validators.iter().enumerate() {
|
||||
if validator.has_compounding_withdrawal_credential(spec) {
|
||||
post.queue_excess_active_balance(index, spec)?;
|
||||
@@ -137,7 +154,7 @@ pub fn upgrade_state_to_electra<E: EthSpec>(
|
||||
earliest_exit_epoch,
|
||||
consolidation_balance_to_consume: 0,
|
||||
earliest_consolidation_epoch,
|
||||
pending_balance_deposits: Default::default(),
|
||||
pending_deposits: Default::default(),
|
||||
pending_partial_withdrawals: Default::default(),
|
||||
pending_consolidations: Default::default(),
|
||||
// Caches
|
||||
|
||||
Reference in New Issue
Block a user