Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated process_pending_balance_deposits #8407

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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