-
-
Notifications
You must be signed in to change notification settings - Fork 311
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move validator status type and util to @lodestar/types (#7140)
- Loading branch information
Showing
8 changed files
with
155 additions
and
153 deletions.
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"); | ||
} | ||
}); | ||
}); |
068fbae
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.
Possible performance regression was detected for some benchmarks.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold.
Full benchmark results