Skip to content

Commit

Permalink
Optimise and simplify PendingDepositsContext::new
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelsproul committed Dec 13, 2024
1 parent da33bd6 commit 0af6b0f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub enum EpochProcessingError {
SinglePassMissingActivationQueue,
MissingEarliestExitEpoch,
MissingExitBalanceToConsume,
PendingDepositsLogicError,
}

impl From<InclusionError> for EpochProcessingError {
Expand Down
26 changes: 11 additions & 15 deletions consensus/state_processing/src/per_epoch_processing/single_pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,8 @@ impl PendingDepositsContext {
// `true` both for the actual value & the default placeholder value (`FAR_FUTURE_EPOCH`).
let mut is_validator_exited = false;
let mut is_validator_withdrawn = false;
if let Some(validator_index) = state.pubkey_cache().get(&deposit.pubkey) {
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
Expand All @@ -964,20 +965,15 @@ impl PendingDepositsContext {
}

if is_validator_withdrawn {
// Deposited balance will never become active. Queue a balance increase
// but do not 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
// We add the new validator to the state at the end of all processing.
// Note that the new validator will not be eligible for activation until
// next epoch, so none of the operations will affect a newly added validator.
new_validator_deposits.push(deposit.clone());
}
// 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(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());
Expand Down

0 comments on commit 0af6b0f

Please sign in to comment.