-
-
Notifications
You must be signed in to change notification settings - Fork 311
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
wemeetagain
merged 6 commits into
ChainSafe:unstable
from
vladmarusyk:feature/fix-validator-status
Oct 10, 2024
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
46b8c63
Fix ValidatorStatus type issue
vladmarusyk 7302aaa
refactor: use switch-case and typed general validator status mapping
vladmarusyk f702f91
Update packages/beacon-node/src/api/impl/beacon/state/utils.ts
vladmarusyk e07a82b
refactor: deleted unrelated code
vladmarusyk 361ad8d
refactor: deleted unrelated code
vladmarusyk 59febed
Update packages/beacon-node/src/api/impl/beacon/state/utils.ts
vladmarusyk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
|
@@ -83,6 +83,51 @@ export async function getStateResponseWithRegen( | |
return res; | ||
} | ||
|
||
function mapToGeneralStatus(subStatus: string): string { | ||
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
@@ -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) | ||
); | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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