-
-
Notifications
You must be signed in to change notification settings - Fork 410
fix : Delayed broadcasting of voluntary exits #8496
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
Closed
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
fccb5bc
fix:Delayed broadcasting of voluntary exits
pheobeayo d08d2fc
fix suggestions
pheobeayo 09a563d
fix formatting
pheobeayo f2f9e08
fix:vscode settings
pheobeayo 9166df8
fix: formatting
pheobeayo 834d8cb
Update packages/beacon-node/src/chain/validation/voluntaryExit.ts
pheobeayo 974bada
fix: cleanup and testing
pheobeayo 0783024
Merge branch 'unstable' into fix/#7431
pheobeayo a1fce04
Merge branch 'fix/#7431' of https://github.com/pheobeayo/lodestar int…
pheobeayo 1189235
still cleaning
pheobeayo 1f961d9
fixing failing tests
pheobeayo 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 hidden or 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 hidden or 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
286 changes: 241 additions & 45 deletions
286
packages/beacon-node/src/chain/validation/voluntaryExit.ts
This file contains hidden or 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,68 +1,264 @@ | ||
| import { | ||
| VoluntaryExitValidity, | ||
| getVoluntaryExitSignatureSet, | ||
| getVoluntaryExitValidity, | ||
| } from "@lodestar/state-transition"; | ||
| import {getVoluntaryExitSignatureSet, isValidVoluntaryExit} from "@lodestar/state-transition"; | ||
| import {phase0} from "@lodestar/types"; | ||
| import { | ||
| GossipAction, | ||
| VoluntaryExitError, | ||
| VoluntaryExitErrorCode, | ||
| voluntaryExitValidityToErrorCode, | ||
| } from "../errors/index.js"; | ||
| import {GossipAction, VoluntaryExitError, VoluntaryExitErrorCode} from "../errors/index.js"; | ||
| import {IBeaconChain} from "../index.js"; | ||
| import {RegenCaller} from "../regen/index.js"; | ||
|
|
||
| /** | ||
| * Helper to get human-readable error code name | ||
| */ | ||
| function getVoluntaryExitErrorCodeName(code: VoluntaryExitErrorCode): string { | ||
| switch (code) { | ||
| case VoluntaryExitErrorCode.ALREADY_EXISTS: | ||
| return "ALREADY_EXISTS"; | ||
| case VoluntaryExitErrorCode.INVALID: | ||
| return "INVALID"; | ||
| case VoluntaryExitErrorCode.INVALID_SIGNATURE: | ||
| return "INVALID_SIGNATURE"; | ||
| default: | ||
| return `UNKNOWN_CODE_${code}`; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Validation result that distinguishes between permanent and transient failures | ||
| */ | ||
| interface ValidationResult { | ||
| isValid: boolean; | ||
| error?: { | ||
| action: GossipAction; | ||
| code: | ||
| | VoluntaryExitErrorCode.ALREADY_EXISTS | ||
| | VoluntaryExitErrorCode.INACTIVE | ||
| | VoluntaryExitErrorCode.ALREADY_EXITED | ||
| | VoluntaryExitErrorCode.EARLY_EPOCH | ||
| | VoluntaryExitErrorCode.SHORT_TIME_ACTIVE | ||
| | VoluntaryExitErrorCode.PENDING_WITHDRAWALS | ||
| | VoluntaryExitErrorCode.INVALID_SIGNATURE; | ||
| isTransient: boolean; // True if the error might resolve over time | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Cached voluntary exit awaiting transient conditions to be met | ||
| */ | ||
| interface CachedVoluntaryExit { | ||
| exit: phase0.SignedVoluntaryExit; | ||
| submittedAt: number; // Timestamp | ||
| lastCheckedEpoch: number; | ||
| failureReason: string; | ||
| } | ||
|
|
||
| /** | ||
| * Pool for managing pending voluntary exits that failed transient checks | ||
| */ | ||
| class PendingVoluntaryExitPool { | ||
| private pending = new Map<number, CachedVoluntaryExit>(); // validatorIndex -> exit | ||
| private readonly MAX_CACHE_TIME_MS = 7 * 24 * 60 * 60 * 1000; // 7 days | ||
pheobeayo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| add(validatorIndex: number, exit: phase0.SignedVoluntaryExit, reason: string, epoch: number): void { | ||
| this.pending.set(validatorIndex, { | ||
| exit, | ||
| submittedAt: Date.now(), | ||
| lastCheckedEpoch: epoch, | ||
| failureReason: reason, | ||
| }); | ||
| } | ||
|
|
||
| get(validatorIndex: number): CachedVoluntaryExit | undefined { | ||
| return this.pending.get(validatorIndex); | ||
| } | ||
|
|
||
| delete(validatorIndex: number): void { | ||
| this.pending.delete(validatorIndex); | ||
| } | ||
|
|
||
| /** | ||
| * Clean up stale cached exits | ||
| */ | ||
| prune(): void { | ||
| const now = Date.now(); | ||
| for (const [validatorIndex, cached] of this.pending.entries()) { | ||
| if (now - cached.submittedAt > this.MAX_CACHE_TIME_MS) { | ||
| this.pending.delete(validatorIndex); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| getAllPending(): Map<number, CachedVoluntaryExit> { | ||
| return new Map(this.pending); | ||
| } | ||
|
|
||
| size(): number { | ||
| return this.pending.size; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Validates a voluntary exit with detailed error classification | ||
| */ | ||
| async function validateVoluntaryExitDetailed( | ||
| chain: IBeaconChain, | ||
| voluntaryExit: phase0.SignedVoluntaryExit, | ||
| prioritizeBls = false | ||
| ): Promise<ValidationResult> { | ||
| const validatorIndex = voluntaryExit.message.validatorIndex; | ||
|
|
||
| // Check if already seen (this is always permanent) | ||
| if (chain.opPool.hasSeenVoluntaryExit(validatorIndex)) { | ||
| return { | ||
| isValid: false, | ||
| error: { | ||
| action: GossipAction.IGNORE, | ||
| code: VoluntaryExitErrorCode.ALREADY_EXISTS, | ||
| isTransient: false, | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| const state = await chain.getHeadStateAtCurrentEpoch(RegenCaller.validateGossipVoluntaryExit); | ||
|
|
||
| // First verify signature (permanent failure if invalid) | ||
| const signatureSet = getVoluntaryExitSignatureSet(state, voluntaryExit); | ||
| if (!(await chain.bls.verifySignatureSets([signatureSet], {batchable: true, priority: prioritizeBls}))) { | ||
| return { | ||
| isValid: false, | ||
| error: { | ||
| action: GossipAction.REJECT, | ||
| code: VoluntaryExitErrorCode.INVALID_SIGNATURE, | ||
| isTransient: false, | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| if (!isValidVoluntaryExit(chain.config.getForkSeq(state.slot), state, voluntaryExit, false)) { | ||
| // Determine if the failure is transient and map to a specific VoluntaryExitErrorCode | ||
| const validator = state.validators.get(validatorIndex); | ||
| const isTransient = | ||
| validator !== undefined && | ||
| // Validator not yet active (could become active) | ||
| (!validator.activationEpoch || | ||
| // Validator has initiated exit but epoch hasn't passed (time-based) | ||
| validator.exitEpoch !== Infinity || | ||
| false); | ||
|
|
||
| // Map general INVALID reason to a more specific code expected by VoluntaryExitError | ||
| let code: VoluntaryExitErrorCode; | ||
| if (validator === undefined || !validator.activationEpoch) { | ||
| code = VoluntaryExitErrorCode.INACTIVE; | ||
| } else if (validator.exitEpoch !== Infinity) { | ||
| code = VoluntaryExitErrorCode.ALREADY_EXITED; | ||
| } else { | ||
| // Fallback to EARLY_EPOCH for time-based invalid reasons | ||
| code = VoluntaryExitErrorCode.EARLY_EPOCH; | ||
| } | ||
|
|
||
| return { | ||
| isValid: false, | ||
| error: { | ||
| action: GossipAction.REJECT, | ||
| code, | ||
| isTransient, | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| return {isValid: true}; | ||
| } | ||
|
|
||
| /** | ||
| * API validation that accepts and caches transient failures | ||
| */ | ||
| export async function validateApiVoluntaryExit( | ||
| chain: IBeaconChain, | ||
| voluntaryExit: phase0.SignedVoluntaryExit | ||
| ): Promise<void> { | ||
| voluntaryExit: phase0.SignedVoluntaryExit, | ||
| pendingPool?: PendingVoluntaryExitPool | ||
| ): Promise<{shouldPublish: boolean; isCached: boolean}> { | ||
| const prioritizeBls = true; | ||
| return validateVoluntaryExit(chain, voluntaryExit, prioritizeBls); | ||
| const result = await validateVoluntaryExitDetailed(chain, voluntaryExit, prioritizeBls); | ||
|
|
||
| if (result.isValid) { | ||
| return {shouldPublish: true, isCached: false}; | ||
| } | ||
|
|
||
| // If we have a transient error and a pending pool, cache it | ||
| if (result.error?.isTransient && pendingPool) { | ||
| const state = await chain.getHeadStateAtCurrentEpoch(RegenCaller.validateGossipVoluntaryExit); | ||
| const errorCodeName = getVoluntaryExitErrorCodeName(result.error.code); | ||
| pendingPool.add( | ||
| voluntaryExit.message.validatorIndex, | ||
| voluntaryExit, | ||
| `Transient failure: ${errorCodeName}`, | ||
| state.epochCtx.epoch | ||
| ); | ||
| return {shouldPublish: false, isCached: true}; | ||
| } | ||
| //biome-ignore lint/style/noNonNullAssertion: error is guaranteed to exist when isValid is false | ||
| throw new VoluntaryExitError(result.error!.action, { | ||
| //biome-ignore lint/style/noNonNullAssertion: error is guaranteed to exist when isValid is false | ||
| code: result.error!.code, | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Gossip validation (strict, no caching) | ||
| */ | ||
| export async function validateGossipVoluntaryExit( | ||
| chain: IBeaconChain, | ||
| voluntaryExit: phase0.SignedVoluntaryExit | ||
| ): Promise<void> { | ||
| return validateVoluntaryExit(chain, voluntaryExit); | ||
| } | ||
| const result = await validateVoluntaryExitDetailed(chain, voluntaryExit); | ||
|
|
||
| async function validateVoluntaryExit( | ||
| chain: IBeaconChain, | ||
| voluntaryExit: phase0.SignedVoluntaryExit, | ||
| prioritizeBls = false | ||
| ): Promise<void> { | ||
| // [IGNORE] The voluntary exit is the first valid voluntary exit received for the validator with index | ||
| // signed_voluntary_exit.message.validator_index. | ||
| if (chain.opPool.hasSeenVoluntaryExit(voluntaryExit.message.validatorIndex)) { | ||
| throw new VoluntaryExitError(GossipAction.IGNORE, { | ||
| code: VoluntaryExitErrorCode.ALREADY_EXISTS, | ||
| if (!result.isValid) { | ||
| //biome-ignore lint/style/noNonNullAssertion: error is guaranteed to exist when isValid is false | ||
| throw new VoluntaryExitError(result.error!.action, { | ||
| //biome-ignore lint/style/noNonNullAssertion: error is guaranteed to exist when isValid is false | ||
| code: result.error!.code, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| // What state should the voluntaryExit validate against? | ||
| // | ||
| // The only condition that is time sensitive and may require a non-head state is | ||
| // -> Validator is active && validator has not initiated exit | ||
| // The voluntaryExit.epoch must be in the past but the validator's status may change in recent epochs. | ||
| // We dial the head state to the current epoch to get the current status of the validator. This is | ||
| // relevant on periods of many skipped slots. | ||
| /** | ||
| * Process pending voluntary exits at each epoch | ||
| * Should be called by the beacon chain on epoch transitions | ||
| */ | ||
| export async function processPendingVoluntaryExits( | ||
| chain: IBeaconChain, | ||
| pendingPool: PendingVoluntaryExitPool, | ||
| network: {publishVoluntaryExit(exit: phase0.SignedVoluntaryExit): Promise<void>} | ||
| ): Promise<void> { | ||
| const state = await chain.getHeadStateAtCurrentEpoch(RegenCaller.validateGossipVoluntaryExit); | ||
| const currentEpoch = state.epochCtx.epoch; | ||
|
|
||
| // [REJECT] All of the conditions within process_voluntary_exit pass validation. | ||
| // verifySignature = false, verified in batch below | ||
| const validity = getVoluntaryExitValidity(chain.config.getForkSeq(state.slot), state, voluntaryExit, false); | ||
| if (validity !== VoluntaryExitValidity.valid) { | ||
| throw new VoluntaryExitError(GossipAction.REJECT, { | ||
| code: voluntaryExitValidityToErrorCode(validity), | ||
| }); | ||
| const toRemove: number[] = []; | ||
|
|
||
| for (const [validatorIndex, cached] of pendingPool.getAllPending()) { | ||
| if (cached.lastCheckedEpoch === currentEpoch) { | ||
| continue; | ||
| } | ||
|
|
||
| try { | ||
| const result = await validateVoluntaryExitDetailed(chain, cached.exit); | ||
|
|
||
| if (result.isValid) { | ||
| chain.opPool.insertVoluntaryExit(cached.exit); | ||
| await network.publishVoluntaryExit(cached.exit); | ||
| toRemove.push(validatorIndex); | ||
| } else if (!result.error?.isTransient) { | ||
| toRemove.push(validatorIndex); | ||
| } | ||
| } catch { | ||
| toRemove.push(validatorIndex); | ||
| } | ||
| } | ||
|
|
||
| const signatureSet = getVoluntaryExitSignatureSet(state, voluntaryExit); | ||
| if (!(await chain.bls.verifySignatureSets([signatureSet], {batchable: true, priority: prioritizeBls}))) { | ||
| throw new VoluntaryExitError(GossipAction.REJECT, { | ||
| code: VoluntaryExitErrorCode.INVALID_SIGNATURE, | ||
| }); | ||
| // Remove processed exits | ||
| for (const validatorIndex of toRemove) { | ||
| pendingPool.delete(validatorIndex); | ||
| } | ||
|
|
||
| // Clean up old entries | ||
| pendingPool.prune(); | ||
| } | ||
|
|
||
| export {PendingVoluntaryExitPool}; | ||
This file contains hidden or 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
Oops, something went wrong.
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.
unrelated change