-
-
Notifications
You must be signed in to change notification settings - Fork 289
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
refactor: move validator status type and util to @lodestar/types #7140
Merged
Merged
Changes from all commits
Commits
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
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
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
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
100 changes: 1 addition & 99 deletions
100
packages/beacon-node/test/unit/api/impl/beacon/state/utils.test.ts
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
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
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import {FAR_FUTURE_EPOCH} from "@lodestar/params"; | ||
import {Epoch, phase0} from "../types.js"; | ||
|
||
export type ValidatorStatus = | ||
| "active" | ||
| "pending_initialized" | ||
| "pending_queued" | ||
| "active_ongoing" | ||
| "active_exiting" | ||
| "active_slashed" | ||
| "exited_unslashed" | ||
| "exited_slashed" | ||
| "withdrawal_possible" | ||
| "withdrawal_done"; | ||
|
||
/** | ||
* Get the status of the validator | ||
* based on conditions outlined in https://hackmd.io/ofFJ5gOmQpu1jjHilHbdQQ | ||
*/ | ||
export function getValidatorStatus(validator: phase0.Validator, currentEpoch: Epoch): ValidatorStatus { | ||
// 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"); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import {describe, it, expect} from "vitest"; | ||
import {getValidatorStatus} from "../../src/utils/validatorStatus.js"; | ||
import {phase0} from "../../src/types.js"; | ||
|
||
describe("getValidatorStatus", function () { | ||
it("should return PENDING_INITIALIZED", function () { | ||
const validator = { | ||
activationEpoch: 1, | ||
activationEligibilityEpoch: Infinity, | ||
} as phase0.Validator; | ||
const currentEpoch = 0; | ||
const status = getValidatorStatus(validator, currentEpoch); | ||
expect(status).toBe("pending_initialized"); | ||
}); | ||
it("should return PENDING_QUEUED", function () { | ||
const validator = { | ||
activationEpoch: 1, | ||
activationEligibilityEpoch: 101010101101010, | ||
} as phase0.Validator; | ||
const currentEpoch = 0; | ||
const status = getValidatorStatus(validator, currentEpoch); | ||
expect(status).toBe("pending_queued"); | ||
}); | ||
it("should return ACTIVE_ONGOING", function () { | ||
const validator = { | ||
activationEpoch: 1, | ||
exitEpoch: Infinity, | ||
} as phase0.Validator; | ||
const currentEpoch = 1; | ||
const status = getValidatorStatus(validator, currentEpoch); | ||
expect(status).toBe("active_ongoing"); | ||
}); | ||
it("should return ACTIVE_SLASHED", function () { | ||
const validator = { | ||
activationEpoch: 1, | ||
exitEpoch: 101010101101010, | ||
slashed: true, | ||
} as phase0.Validator; | ||
const currentEpoch = 1; | ||
const status = getValidatorStatus(validator, currentEpoch); | ||
expect(status).toBe("active_slashed"); | ||
}); | ||
it("should return ACTIVE_EXITING", function () { | ||
const validator = { | ||
activationEpoch: 1, | ||
exitEpoch: 101010101101010, | ||
slashed: false, | ||
} as phase0.Validator; | ||
const currentEpoch = 1; | ||
const status = getValidatorStatus(validator, currentEpoch); | ||
expect(status).toBe("active_exiting"); | ||
}); | ||
it("should return EXITED_SLASHED", function () { | ||
const validator = { | ||
exitEpoch: 1, | ||
withdrawableEpoch: 3, | ||
slashed: true, | ||
} as phase0.Validator; | ||
const currentEpoch = 2; | ||
const status = getValidatorStatus(validator, currentEpoch); | ||
expect(status).toBe("exited_slashed"); | ||
}); | ||
it("should return EXITED_UNSLASHED", function () { | ||
const validator = { | ||
exitEpoch: 1, | ||
withdrawableEpoch: 3, | ||
slashed: false, | ||
} as phase0.Validator; | ||
const currentEpoch = 2; | ||
const status = getValidatorStatus(validator, currentEpoch); | ||
expect(status).toBe("exited_unslashed"); | ||
}); | ||
it("should return WITHDRAWAL_POSSIBLE", function () { | ||
const validator = { | ||
withdrawableEpoch: 1, | ||
effectiveBalance: 32, | ||
} as phase0.Validator; | ||
const currentEpoch = 1; | ||
const status = getValidatorStatus(validator, currentEpoch); | ||
expect(status).toBe("withdrawal_possible"); | ||
}); | ||
it("should return WITHDRAWAL_DONE", function () { | ||
const validator = { | ||
withdrawableEpoch: 1, | ||
effectiveBalance: 0, | ||
} as phase0.Validator; | ||
const currentEpoch = 1; | ||
const status = getValidatorStatus(validator, currentEpoch); | ||
expect(status).toBe("withdrawal_done"); | ||
}); | ||
it("should error", function () { | ||
const validator = {} as phase0.Validator; | ||
const currentEpoch = 0; | ||
try { | ||
getValidatorStatus(validator, currentEpoch); | ||
} catch (error) { | ||
expect(error).toHaveProperty("message", "ValidatorStatus unknown"); | ||
} | ||
}); | ||
}); |
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.
re-export for backward compatibility