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

feat: handle exited/exiting validators during top up #6880

Merged
merged 3 commits into from
Jun 21, 2024
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
@@ -1,26 +1,49 @@
import {FAR_FUTURE_EPOCH} from "@lodestar/params";
import {CachedBeaconStateElectra} from "../types.js";
import {increaseBalance} from "../util/balance.js";
import {getActivationExitChurnLimit} from "../util/validator.js";
import {getCurrentEpoch} from "../util/epoch.js";

/**
* Starting from Electra:
* Process pending balance deposits from state subject to churn limit and depsoitBalanceToConsume.
* For each eligible `deposit`, call `increaseBalance()`.
* Remove the processed deposits from `state.pendingBalanceDeposits`.
* Update `state.depositBalanceToConsume` for the next epoch
*
* TODO Electra: Update ssz library to support batch push to `pendingBalanceDeposits`
*/
export function processPendingBalanceDeposits(state: CachedBeaconStateElectra): void {
ensi321 marked this conversation as resolved.
Show resolved Hide resolved
const availableForProcessing = state.depositBalanceToConsume + BigInt(getActivationExitChurnLimit(state));
const currentEpoch = getCurrentEpoch(state);
let processedAmount = 0n;
let nextDepositIndex = 0;
const depositsToPostpone = [];

for (const deposit of state.pendingBalanceDeposits.getAllReadonly()) {
const {amount} = deposit;
if (processedAmount + amount > availableForProcessing) {
break;
const {amount, index: depositIndex} = deposit;
const validator = state.validators.get(depositIndex);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const validator = state.validators.get(depositIndex);
const validator = state.validators.getReadOnly(depositIndex);

Copy link
Contributor Author

@ensi321 ensi321 Jun 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@twoeths Caught this during spec testing. Fixed in #6892 👍


// Validator is exiting, postpone the deposit until after withdrawable epoch
if (validator.exitEpoch < FAR_FUTURE_EPOCH) {
if (currentEpoch <= validator.withdrawableEpoch) {
depositsToPostpone.push(deposit);
} else {
// Deposited balance will never become active. Increase balance but do not consume churn
increaseBalance(state, deposit.index, Number(amount));
}
} else {
// Validator is not exiting, attempt to process deposit
if (processedAmount + amount > availableForProcessing) {
// Deposit does not fit in the churn, no more deposit processing in this epoch.
break;
} else {
// Deposit fits in the churn, process it. Increase balance and consume churn.
increaseBalance(state, deposit.index, Number(amount));
processedAmount = processedAmount + amount;
}
}
increaseBalance(state, deposit.index, Number(amount));
processedAmount = processedAmount + amount;
// Regardless of how the deposit was handled, we move on in the queue.
nextDepositIndex++;
}

Expand All @@ -32,4 +55,9 @@ export function processPendingBalanceDeposits(state: CachedBeaconStateElectra):
} else {
state.depositBalanceToConsume = availableForProcessing - processedAmount;
}

// TODO Electra: add a function in ListCompositeTreeView to support batch push operation
for (const deposit of depositsToPostpone) {
state.pendingBalanceDeposits.push(deposit);
}
}
Loading