Skip to content

app/eth2wrap: derive epoch validators from cached records - #4874

Open
KaloyanTanev wants to merge 1 commit into
mainfrom
kalo/valcache-refresh-nonblocking
Open

KaloyanTanev wants to merge 1 commit into
mainfrom
kalo/valcache-refresh-nonblocking

Conversation

@KaloyanTanev

Copy link
Copy Markdown
Collaborator

Stops the validator cache refresh from blocking the validator API and the scheduler at every epoch start. The cache now derives each epoch's active set and statuses from the activation and exit epochs of the validators it last fetched, which the chain assigns at least MAX_SEED_LOOKAHEAD + 1 epochs ahead, so no beacon node call is needed at the boundary.

  • ValidatorCache.Refresh fetches the head state once an epoch, from the third slot on and late in the slot, without holding the lock. Fetched validators are trusted for MAX_SEED_LOOKAHEAD - 1 epochs ahead, since the head state may be an epoch behind.
  • A chain reorg through epoch boundaries shortens that window.
  • The validators endpoint serves only the head state from the cache; other state ids go to the beacon node.
  • Mock validators in testutil get consistent activation and exit epochs, as statuses are derived from them.

category: bug
ticket: none

@KaloyanTanev KaloyanTanev self-assigned this Dec 3, 2026
@sonarqubecloud

sonarqubecloud Bot commented Dec 3, 2026

Copy link
Copy Markdown

@codecov

codecov Bot commented Dec 3, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 83.84615% with 21 lines in your changes missing coverage. Please review.
✅ Project coverage is 70.54%. Comparing base (3d2a936) to head (58843d9).

Files with missing lines Patch % Lines
app/app.go 80.00% 9 Missing ⚠️
app/vmock.go 0.00% 7 Missing ⚠️
app/eth2wrap/utils.go 72.72% 3 Missing ⚠️
app/eth2wrap/cache.go 98.36% 1 Missing ⚠️
core/validatorapi/validatorapi.go 83.33% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4874      +/-   ##
==========================================
+ Coverage   70.47%   70.54%   +0.06%     
==========================================
  Files         284      284              
  Lines       37308    37346      +38     
==========================================
+ Hits        26293    26345      +52     
+ Misses      11014    11000      -14     
  Partials        1        1              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unresolved critical flaws in stale-head validity, concurrent epoch updates, and reorg handling can serve incorrect active validator sets or leave stale indices in use.

3 open issues
What changed in this PR

This PR reduces epoch-boundary blocking in Charon by deriving validator sets from cached activation and exit epochs.

Changes:

  • Refreshes validator records late in eligible slots and limits cache validity after reorgs.
  • Uses the validator cache only for head requests.
  • Adds cache lifecycle tests and consistent mock validator epochs.
File Description
testutil/​random.go Makes random validator epochs consistent.
testutil/​beaconmock/​options.go Updates mock activation and exit epochs.
core/​validatorapi/​validatorapi.go Bypasses the cache for non-head states.
core/​validatorapi/​validatorapi_test.go Tests non-head request forwarding.
app/​vmock.go Supplies epoch timing and lookahead to the cache.
app/​eth2wrap/​utils.go Adds network lookahead retrieval.
app/​eth2wrap/​utils_test.go Tests lookahead retrieval and validation.
app/​eth2wrap/​cache.go Derives validator sets and handles refreshes and reorgs.
app/​eth2wrap/​cache_test.go Tests derivation, concurrency, refreshes, and invalidation.
app/​app.go Wires cache refresh and duties updates separately.
app/​app_internal_test.go Tests refresh scheduling and duties updates.

Review effort: Balanced

Comment thread app/app.go
Comment on lines +979 to +983
var updated atomic.Bool

return func(ctx context.Context, slot core.Slot) error {
if !slot.FirstInEpoch() && updated.Load() {
return nil
Comment thread app/eth2wrap/cache.go
Comment on lines +135 to +136
// The head state may still be in the previous epoch.
c.validThrough = epoch + c.maxSeedLookahead - 1
Comment thread app/eth2wrap/cache.go
missedCacheCount.WithLabelValues("validators").Inc()

refreshedBySlot := true
return c.store(fetched, epoch)

@OisinKyne OisinKyne left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. I checked the lookahead argument against the spec (activation and exit epochs are always ≥ MAX_SEED_LOOKAHEAD + 1 ahead, electra exit queue and consolidations included), and the reorg bound of epoch + MAX_SEED_LOOKAHEAD is exactly right. Race tests pass locally. Small things, none blocking:

  1. A Refresh whose fetch starts before a reorg more than one epoch deep and stores after InvalidateCache restores the full window. Rare, but a counter bumped by InvalidateCache and checked in store would close it.
  2. Refresh increments the validators miss counter every epoch, so misses no longer mean a caller waited. Maybe a separate label?
  3. valCacheRefreshSlot never allows a refresh with fewer than 4 slots per epoch. That's only test configs, but worth a comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants