-
-
Notifications
You must be signed in to change notification settings - Fork 410
fix: delayed broadcasting of Voluntary exits #8579
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
Open
pheobeayo
wants to merge
2
commits into
ChainSafe:unstable
Choose a base branch
from
pheobeayo:fix-#7431
base: unstable
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+341
−7
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
211 changes: 211 additions & 0 deletions
211
packages/beacon-node/src/chain/opPools/voluntaryExitBroadcaster.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 |
|---|---|---|
| @@ -0,0 +1,211 @@ | ||
| import {Logger} from "@lodestar/logger"; | ||
| import {phase0} from "@lodestar/types"; | ||
| import {INetwork} from "../../network/interface.js"; | ||
| import {VoluntaryExitError} from "../errors/index.js"; | ||
| import {IBeaconChain} from "../index.js"; | ||
| import {validateGossipVoluntaryExit} from "../validation/voluntaryExit.js"; | ||
|
|
||
| /** | ||
| * Cached voluntary exit with metadata | ||
| */ | ||
| interface CachedVoluntaryExit { | ||
| voluntaryExit: phase0.SignedVoluntaryExit; | ||
| receivedAt: number; // timestamp when received via API | ||
| } | ||
|
|
||
| /** | ||
| * Manages delayed broadcasting of voluntary exits. | ||
| * | ||
| * When a voluntary exit is submitted via API but doesn't yet meet transient conditions | ||
| * (e.g., validator not active, exit epoch not reached, pending withdrawals), it's cached | ||
| * here and periodically checked. Once conditions are met, it's broadcast to the network. | ||
| * | ||
| * This improves UX by accepting exits early and is more forgiving for DVT/multi-node setups. | ||
| */ | ||
| export class VoluntaryExitDelayedBroadcaster { | ||
| private readonly cachedExits = new Map<number, CachedVoluntaryExit>(); // validatorIndex -> exit | ||
| private readonly MAX_CACHE_AGE_MS = 7 * 24 * 60 * 60 * 1000; // 7 days | ||
|
|
||
| constructor( | ||
| private readonly chain: IBeaconChain, | ||
| private readonly network: INetwork, | ||
| private readonly logger: Logger | ||
| ) {} | ||
|
|
||
| /** | ||
| * Add a voluntary exit to the cache for delayed broadcasting. | ||
| * Called when a voluntary exit passes signature validation but doesn't yet meet | ||
| * transient conditions (validator active status, exit epoch timing, etc.) | ||
| */ | ||
| addToCacheForDelayedBroadcast(voluntaryExit: phase0.SignedVoluntaryExit): void { | ||
| const validatorIndex = voluntaryExit.message.validatorIndex; | ||
|
|
||
| // Don't cache if already exists | ||
| if (this.cachedExits.has(validatorIndex)) { | ||
| this.logger.debug("Voluntary exit already cached, skipping", {validatorIndex}); | ||
| return; | ||
| } | ||
|
|
||
| this.cachedExits.set(validatorIndex, { | ||
| voluntaryExit, | ||
| receivedAt: Date.now(), | ||
| }); | ||
|
|
||
| this.logger.info("Voluntary exit cached for delayed broadcasting", { | ||
| validatorIndex, | ||
| epoch: voluntaryExit.message.epoch, | ||
| cacheSize: this.cachedExits.size, | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Check cached voluntary exits and broadcast those that now meet transient conditions. | ||
| * Should be called periodically (e.g., every slot or every few seconds). | ||
| */ | ||
| async checkAndBroadcastCachedExits(): Promise<void> { | ||
| if (this.cachedExits.size === 0) { | ||
| return; | ||
| } | ||
|
|
||
| const currentTime = Date.now(); | ||
| const exitsToRemove: number[] = []; | ||
|
|
||
| for (const [validatorIndex, cached] of this.cachedExits.entries()) { | ||
| try { | ||
| // Check if exit has been in cache too long | ||
| const ageMs = currentTime - cached.receivedAt; | ||
| if (ageMs > this.MAX_CACHE_AGE_MS) { | ||
| this.logger.warn("Removing stale voluntary exit from cache", { | ||
| validatorIndex, | ||
| ageMs, | ||
| ageDays: Math.floor(ageMs / (24 * 60 * 60 * 1000)), | ||
| }); | ||
| exitsToRemove.push(validatorIndex); | ||
| continue; | ||
| } | ||
|
|
||
| // Use full gossip validation to check if all conditions (including transient) are now met | ||
| await validateGossipVoluntaryExit(this.chain, cached.voluntaryExit); | ||
|
|
||
| // If validation passes, broadcast to network | ||
| await this.network.publishVoluntaryExit(cached.voluntaryExit); | ||
|
|
||
| this.logger.info("Successfully broadcasted delayed voluntary exit", { | ||
| validatorIndex, | ||
| epoch: cached.voluntaryExit.message.epoch, | ||
| delayMs: ageMs, | ||
| delaySeconds: Math.floor(ageMs / 1000), | ||
| }); | ||
|
|
||
| // Remove from cache after successful broadcast | ||
| exitsToRemove.push(validatorIndex); | ||
| } catch (e) { | ||
| if (e instanceof VoluntaryExitError) { | ||
| // Check if this is a permanent failure or transient | ||
| if (this.isPermanentFailure(e)) { | ||
| this.logger.warn("Removing voluntary exit due to permanent validation failure", { | ||
| validatorIndex, | ||
| error: e.message, | ||
| errorCode: e.type.code, | ||
| }); | ||
| exitsToRemove.push(validatorIndex); | ||
| } else { | ||
| // Transient conditions not yet met, keep in cache | ||
| this.logger.debug("Voluntary exit not yet ready for broadcasting", { | ||
| validatorIndex, | ||
| error: e.message, | ||
| cacheSize: this.cachedExits.size, | ||
| }); | ||
| } | ||
| } else { | ||
| // Unexpected error, log and remove from cache | ||
| this.logger.error("Unexpected error checking voluntary exit, removing from cache", { | ||
| validatorIndex, | ||
| error: (e as Error).message, | ||
| }); | ||
| exitsToRemove.push(validatorIndex); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Clean up processed exits | ||
| for (const validatorIndex of exitsToRemove) { | ||
| this.cachedExits.delete(validatorIndex); | ||
| } | ||
|
|
||
| if (exitsToRemove.length > 0) { | ||
| this.logger.debug("Cleaned up voluntary exit cache", { | ||
| removed: exitsToRemove.length, | ||
| remaining: this.cachedExits.size, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Determine if a validation error is permanent (will never become valid) | ||
| * or transient (may become valid later). | ||
| * | ||
| * Transient errors: validator not active yet, exit epoch not reached, pending withdrawals | ||
| * Permanent errors: invalid signature, validator already exited, invalid index | ||
| */ | ||
| private isPermanentFailure(error: VoluntaryExitError): boolean { | ||
| const errorMessage = error.message.toLowerCase(); | ||
|
|
||
| // These are transient conditions that may resolve over time | ||
| const transientIndicators = [ | ||
| "not active", | ||
| "not_active_validator", | ||
| "validator_not_active", | ||
| "not withdrawable", | ||
| "withdrawable_epoch", | ||
| "exit epoch", | ||
| "epoch not current", | ||
| "pending withdrawal", // post-Electra | ||
| "pending_withdrawal", | ||
| "too early", | ||
| "future epoch", | ||
| ]; | ||
|
|
||
| // If any transient indicator is found, it's not a permanent failure | ||
| const isTransient = transientIndicators.some((indicator) => errorMessage.includes(indicator)); | ||
|
|
||
| return !isTransient; | ||
| } | ||
|
|
||
| /** | ||
| * Get the current size of the cache (for metrics/monitoring) | ||
| */ | ||
| getCacheSize(): number { | ||
| return this.cachedExits.size; | ||
| } | ||
|
|
||
| /** | ||
| * Get all cached voluntary exits (for debugging/inspection) | ||
| */ | ||
| getCachedExits(): phase0.SignedVoluntaryExit[] { | ||
| return Array.from(this.cachedExits.values()).map((cached) => cached.voluntaryExit); | ||
| } | ||
|
|
||
| /** | ||
| * Clear all cached exits (for testing or shutdown) | ||
| */ | ||
| clearCache(): void { | ||
| const size = this.cachedExits.size; | ||
| this.cachedExits.clear(); | ||
| if (size > 0) { | ||
| this.logger.info("Cleared voluntary exit cache", {clearedCount: size}); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Remove a specific exit from cache (for testing or manual intervention) | ||
| */ | ||
| removeFromCache(validatorIndex: number): boolean { | ||
| const existed = this.cachedExits.has(validatorIndex); | ||
| this.cachedExits.delete(validatorIndex); | ||
| if (existed) { | ||
| this.logger.debug("Manually removed voluntary exit from cache", {validatorIndex}); | ||
| } | ||
| return existed; | ||
| } | ||
| } |
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.
I don't see those changes implemented in the PR