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: allow filtering of validators by top-level validator status #7143

Merged
Merged
Changes from 2 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
61 changes: 58 additions & 3 deletions packages/beacon-node/src/api/impl/beacon/state/utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {PubkeyIndexMap} from "@chainsafe/pubkey-index-map";
import {routes} from "@lodestar/api";
import {GENESIS_SLOT} from "@lodestar/params";
import {FAR_FUTURE_EPOCH, GENESIS_SLOT} from "@lodestar/params";
import {BeaconStateAllForks} from "@lodestar/state-transition";
import {BLSPubkey, Epoch, getValidatorStatus, phase0, RootHex, Slot, ValidatorIndex} from "@lodestar/types";
import {BLSPubkey, Epoch, phase0, RootHex, Slot, ValidatorIndex} from "@lodestar/types";
import {fromHex} from "@lodestar/utils";
import {CheckpointWithHex, IForkChoice} from "@lodestar/fork-choice";
import {IBeaconChain} from "../../../../chain/index.js";
Expand Down Expand Up @@ -83,6 +83,60 @@ export async function getStateResponseWithRegen(
return res;
}

export type GeneralValidatorStatus = routes.beacon.ValidatorStatus | "active" | "pending" | "exited" | "withdrawal";
vladmarusyk marked this conversation as resolved.
Show resolved Hide resolved

function mapToGeneralStatus(subStatus: routes.beacon.ValidatorStatus): GeneralValidatorStatus {
switch (subStatus) {
case "active_ongoing":
case "active_exiting":
case "active_slashed":
return "active";
case "pending_initialized":
case "pending_queued":
return "pending";
case "exited_slashed":
case "exited_unslashed":
return "exited";
case "withdrawal_possible":
case "withdrawal_done":
return "withdrawal";
default:
throw new Error(`Unknown substatus: ${subStatus}`);
}
}

/**
* Get the status of the validator
* based on conditions outlined in https://hackmd.io/ofFJ5gOmQpu1jjHilHbdQQ
*/
export function getValidatorStatus(validator: phase0.Validator, currentEpoch: Epoch): routes.beacon.ValidatorStatus {
Copy link
Member

Choose a reason for hiding this comment

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

why is this added here?

// pending
if (validator.activationEpoch > currentEpoch) {
if (validator.activationEligibilityEpoch === FAR_FUTURE_EPOCH) {
return "pending_initialized";
} else if (validator.activationEligibilityEpoch < FAR_FUTURE_EPOCH) {
return "pending_queued";
}
}
// active
if (validator.activationEpoch <= currentEpoch && currentEpoch < validator.exitEpoch) {
if (validator.exitEpoch === FAR_FUTURE_EPOCH) {
return "active_ongoing";
} else if (validator.exitEpoch < FAR_FUTURE_EPOCH) {
return validator.slashed ? "active_slashed" : "active_exiting";
}
}
// exited
if (validator.exitEpoch <= currentEpoch && currentEpoch < validator.withdrawableEpoch) {
return validator.slashed ? "exited_slashed" : "exited_unslashed";
}
// withdrawal
if (validator.withdrawableEpoch <= currentEpoch) {
return validator.effectiveBalance !== 0 ? "withdrawal_possible" : "withdrawal_done";
}
throw new Error("ValidatorStatus unknown");
}

export function toValidatorResponse(
index: ValidatorIndex,
validator: phase0.Validator,
Expand All @@ -109,9 +163,10 @@ export function filterStateValidatorsByStatus(

for (const validator of validatorsArr) {
const validatorStatus = getValidatorStatus(validator, currentEpoch);
const generalStatus = mapToGeneralStatus(validatorStatus);

const resp = getStateValidatorIndex(validator.pubkey, state, pubkey2index);
if (resp.valid && statusSet.has(validatorStatus)) {
if (resp.valid && (statusSet.has(validatorStatus) || statusSet.has(generalStatus))) {
responses.push(
toValidatorResponse(resp.validatorIndex, validator, state.balances.get(resp.validatorIndex), currentEpoch)
);
Expand Down