Skip to content

Commit

Permalink
feat: allow filtering of validators by top-level validator status (#7143
Browse files Browse the repository at this point in the history
)

* Fix ValidatorStatus type issue

* refactor: use switch-case and typed general validator status mapping

* Update packages/beacon-node/src/api/impl/beacon/state/utils.ts

Co-authored-by: Nico Flaig <[email protected]>

* refactor: deleted unrelated code

* refactor: deleted unrelated code

* Update packages/beacon-node/src/api/impl/beacon/state/utils.ts

Co-authored-by: Nico Flaig <[email protected]>

---------

Co-authored-by: Nico Flaig <[email protected]>
  • Loading branch information
vladmarusyk and nflaig authored Oct 10, 2024
1 parent 5c359f9 commit 38f54ab
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion packages/beacon-node/src/api/impl/beacon/state/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,28 @@ export async function getStateResponseWithRegen(
return res;
}

type GeneralValidatorStatus = "active" | "pending" | "exited" | "withdrawal";

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}`);
}
}

export function toValidatorResponse(
index: ValidatorIndex,
validator: phase0.Validator,
Expand All @@ -109,9 +131,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

0 comments on commit 38f54ab

Please sign in to comment.