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 1 commit
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
52 changes: 49 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,51 @@ export async function getStateResponseWithRegen(
return res;
}

function mapToGeneralStatus(subStatus: string): string {
Copy link
Member

Choose a reason for hiding this comment

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

switch-case would be nicer here, we can also reuse ValidatorStatus type, and potentially a extended type which includes top-level / general statuses as well

if (["active_ongoing", "active_exiting", "active_slashed"].includes(subStatus)) {
return "active";
} else if (["pending_initialized", "pending_queued"].includes(subStatus)) {
return "pending";
} else if (["exited_slashed", "exited_unslashed"].includes(subStatus)) {
return "exited";
} else if (["withdrawal_possible", "withdrawal_done"].includes(subStatus)) {
return "withdrawal";
}
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 +154,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
Loading