Skip to content

Commit

Permalink
Updated process_pending_balance_deposits (#8407)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucassaldanha committed Jun 27, 2024
1 parent e85a853 commit d5056b0
Showing 1 changed file with 42 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import static tech.pegasys.teku.spec.config.SpecConfig.FAR_FUTURE_EPOCH;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;
import tech.pegasys.teku.infrastructure.ssz.SszList;
Expand Down Expand Up @@ -78,13 +79,7 @@ public EpochProcessorElectra(
this.schemaDefinitionsElectra = SchemaDefinitionsElectra.required(schemaDefinitions);
}

/**
* process_registry_updates
*
* @param state
* @param statuses
* @throws EpochProcessingException
*/
/** process_registry_updates */
@Override
public void processRegistryUpdates(
final MutableBeaconState state, final List<ValidatorStatus> statuses)
Expand Down Expand Up @@ -138,7 +133,6 @@ protected boolean isEligibleForActivation(
* is_eligible_for_activation_queue
*
* @param status - Validator status
* @return
*/
@Override
protected boolean isEligibleForActivationQueue(final ValidatorStatus status) {
Expand Down Expand Up @@ -190,11 +184,7 @@ public void processEffectiveBalanceUpdates(
}
}

/**
* process_pending_balance_deposits
*
* @param state
*/
/** process_pending_balance_deposits */
@Override
public void processPendingBalanceDeposits(final MutableBeaconState state) {
final MutableBeaconStateElectra stateElectra = MutableBeaconStateElectra.required(state);
Expand All @@ -205,37 +195,67 @@ public void processPendingBalanceDeposits(final MutableBeaconState state) {

UInt64 processedAmount = UInt64.ZERO;
int nextDepositIndex = 0;
final List<PendingBalanceDeposit> depositsToPostpone = new ArrayList<>();

final SszList<PendingBalanceDeposit> pendingBalanceDeposits =
stateElectra.getPendingBalanceDeposits();
for (final PendingBalanceDeposit deposit : pendingBalanceDeposits) {
if (processedAmount.plus(deposit.getAmount()).isGreaterThan(availableForProcessing)) {
break;
final Validator validator = state.getValidators().get(deposit.getIndex());

if (validator.getExitEpoch().isLessThan(FAR_FUTURE_EPOCH)) {
// Validator is exiting, postpone the deposit until after withdrawable epoch
if (stateAccessorsElectra
.getCurrentEpoch(state)
.isLessThanOrEqualTo(validator.getWithdrawableEpoch())) {
depositsToPostpone.add(deposit);
} else {
// Deposited balance will never become active. Increase balance but do not consume churn
stateMutatorsElectra.increaseBalance(state, deposit.getIndex(), deposit.getAmount());
}
} else {
// Validator is not exiting, attempt to process deposit
if (processedAmount.plus(deposit.getAmount()).isGreaterThan(availableForProcessing)) {
break;
}

// Deposit fits in the churn, process it. Increase balance and consume churn.
stateMutatorsElectra.increaseBalance(state, deposit.getIndex(), deposit.getAmount());
processedAmount = processedAmount.plus(deposit.getAmount());
}
stateMutatorsElectra.increaseBalance(state, deposit.getIndex(), deposit.getAmount());
processedAmount = processedAmount.plus(deposit.getAmount());

// Regardless of how the deposit was handled, we move on in the queue.
nextDepositIndex++;
}

// Updating state.pending_balance_deposits (removing processed deposits)
if (!pendingBalanceDeposits.isEmpty()) {
final List<PendingBalanceDeposit> newList =
pendingBalanceDeposits.asList().subList(nextDepositIndex, pendingBalanceDeposits.size());
stateElectra.setPendingBalanceDeposits(
schemaDefinitionsElectra.getPendingBalanceDepositsSchema().createFromElements(newList));
stateElectra.setDepositBalanceToConsume(availableForProcessing.minusMinZero(processedAmount));
}

// Updating deposit_balance_to_consume
if (stateElectra.getPendingBalanceDeposits().isEmpty()) {
stateElectra.setDepositBalanceToConsume(UInt64.ZERO);
} else {
stateElectra.setDepositBalanceToConsume(availableForProcessing.minusMinZero(processedAmount));
}

// Adding postponed deposits to pending_balance_deposits
if (!depositsToPostpone.isEmpty()) {
final ArrayList<PendingBalanceDeposit> newPendingDeposits = new ArrayList<>();
newPendingDeposits.addAll(stateElectra.getPendingBalanceDeposits().asList());
newPendingDeposits.addAll(depositsToPostpone);
stateElectra.setPendingBalanceDeposits(
schemaDefinitionsElectra
.getPendingBalanceDepositsSchema()
.createFromElements(newPendingDeposits));
}
}

/**
* process_pending_consolidations
*
* @param state
*/
/** process_pending_consolidations */
@Override
public void processPendingConsolidations(final MutableBeaconState state) {

Expand Down

0 comments on commit d5056b0

Please sign in to comment.