From c700cdcbeac13ae18ea6375daa3bbc1d9db1442b Mon Sep 17 00:00:00 2001 From: Diogo Santos Date: Tue, 3 Feb 2026 10:11:59 +0000 Subject: [PATCH 1/3] async registration --- core/scheduler/scheduler.go | 39 ++++++++++++++++++++++---------- core/scheduler/scheduler_test.go | 12 ++++++---- 2 files changed, 35 insertions(+), 16 deletions(-) diff --git a/core/scheduler/scheduler.go b/core/scheduler/scheduler.go index f27c2f6cf8..162634a334 100644 --- a/core/scheduler/scheduler.go +++ b/core/scheduler/scheduler.go @@ -74,6 +74,7 @@ type Scheduler struct { eth2Cl eth2wrap.Client builderRegistrations []*eth2api.VersionedSignedValidatorRegistration submittedRegistrationEpoch uint64 + registrationMutex sync.Mutex quit chan struct{} clock clockwork.Clock delayFunc delayFunc @@ -115,6 +116,20 @@ func (s *Scheduler) Stop() { close(s.quit) } +// getSubmittedRegistrationEpoch returns the last epoch for which registrations were submitted. +func (s *Scheduler) getSubmittedRegistrationEpoch() uint64 { + s.registrationMutex.Lock() + defer s.registrationMutex.Unlock() + return s.submittedRegistrationEpoch +} + +// setSubmittedRegistrationEpoch sets the last epoch for which registrations were submitted. +func (s *Scheduler) setSubmittedRegistrationEpoch(epoch uint64) { + s.registrationMutex.Lock() + defer s.registrationMutex.Unlock() + s.submittedRegistrationEpoch = epoch +} + // Run blocks and runs the scheduler until Stop is called. func (s *Scheduler) Run() error { ctx := log.WithTopic(context.Background(), "sched") @@ -283,11 +298,9 @@ func (s *Scheduler) scheduleSlot(ctx context.Context, slot core.Slot) { } } - // Safe to access s.submittedRegistrationEpoch without lock because scheduleSlot is called by the same goroutine. - if s.builderEnabled && s.submittedRegistrationEpoch != slot.Epoch() { - if err := s.submitValidatorRegistrations(ctx); err == nil { - s.submittedRegistrationEpoch = slot.Epoch() - } + // Submit validator registrations asynchronously to avoid blocking duty triggering. + if s.builderEnabled && s.getSubmittedRegistrationEpoch() != slot.Epoch() { + go s.submitValidatorRegistrationsAsync(ctx, slot.Epoch()) } for _, dutyType := range core.AllDutyTypes() { @@ -780,20 +793,22 @@ func (s *Scheduler) trimEventTriggeredAttestations(epoch uint64) { }) } -// submitValidatorRegistrations submits the validator registrations for all DVs. -func (s *Scheduler) submitValidatorRegistrations(ctx context.Context) error { +// submitValidatorRegistrationsAsync submits validator registrations asynchronously and updates the epoch tracker on success. +func (s *Scheduler) submitValidatorRegistrationsAsync(ctx context.Context, epoch uint64) { + if s.getSubmittedRegistrationEpoch() == epoch { + return + } + submitRegistrationCounter.Add(1) err := s.eth2Cl.SubmitValidatorRegistrations(ctx, s.builderRegistrations) if err != nil { submitRegistrationErrors.Add(1) - - log.Error(ctx, "Failed to submit validator registrations", err) + log.Error(ctx, "Failed to submit validator registrations", err, z.U64("epoch", epoch)) } else { - log.Info(ctx, "Submitted validator registrations", z.Int("count", len(s.builderRegistrations))) + log.Info(ctx, "Submitted validator registrations", z.Int("count", len(s.builderRegistrations)), z.U64("epoch", epoch)) + s.setSubmittedRegistrationEpoch(epoch) } - - return err } // newSlotTicker returns a blocking channel that will be populated with new slots in real time. diff --git a/core/scheduler/scheduler_test.go b/core/scheduler/scheduler_test.go index b83bfa9c8d..d81cd131fe 100644 --- a/core/scheduler/scheduler_test.go +++ b/core/scheduler/scheduler_test.go @@ -543,6 +543,7 @@ func TestSubmitValidatorRegistrations(t *testing.T) { callCount atomic.Int64 callMutex sync.Mutex registrations []*eth2api.VersionedSignedValidatorRegistration + callDone = make(chan struct{}, 1) ) origFunc := eth2Cl.SubmitValidatorRegistrationsFunc @@ -551,10 +552,13 @@ func TestSubmitValidatorRegistrations(t *testing.T) { if registrations == nil { callMutex.Lock() - registrations = regs - callMutex.Unlock() + + select { + case callDone <- struct{}{}: + default: + } } return origFunc(ctx, regs) @@ -594,11 +598,11 @@ func TestSubmitValidatorRegistrations(t *testing.T) { epochsSeen[slot.Epoch()] = true } - // Stop after processing enough slots to see 3 epochs + // Stop after processing enough slots to see 3 epochs and after registration completes // With 4 slots per epoch, we need at least 9 slots (slots 0-8 cover epochs 0, 1, 2) if slotCount >= 9 && !stopped { + <-callDone stopped = true - sched.Stop() } From 97ccfb696f8d56f918337da6ae5aac85d4ee0d4f Mon Sep 17 00:00:00 2001 From: Diogo Santos Date: Tue, 3 Feb 2026 10:52:41 +0000 Subject: [PATCH 2/3] add delay and initial submission --- core/scheduler/scheduler.go | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/core/scheduler/scheduler.go b/core/scheduler/scheduler.go index 162634a334..2c02db6356 100644 --- a/core/scheduler/scheduler.go +++ b/core/scheduler/scheduler.go @@ -140,6 +140,12 @@ func (s *Scheduler) Run() error { waitChainStart(ctx, s.eth2Cl, s.clock) waitBeaconSync(ctx, s.eth2Cl, s.clock) + // Submit validator registrations on startup if builder is enabled. + // This ensures registrations are sent before the first proposal opportunity. + if s.builderEnabled { + go s.submitValidatorRegistrations(ctx, math.MaxUint64) + } + slotTicker, err := newSlotTicker(ctx, s.eth2Cl, s.clock) if err != nil { return err @@ -299,8 +305,11 @@ func (s *Scheduler) scheduleSlot(ctx context.Context, slot core.Slot) { } // Submit validator registrations asynchronously to avoid blocking duty triggering. + // Only submit at slot 0 of each epoch, delayed to end of slot to reduce BN load. if s.builderEnabled && s.getSubmittedRegistrationEpoch() != slot.Epoch() { - go s.submitValidatorRegistrationsAsync(ctx, slot.Epoch()) + if slot.Slot%slot.SlotsPerEpoch == 0 { + go s.submitValidatorRegistrationsDelayed(ctx, slot) + } } for _, dutyType := range core.AllDutyTypes() { @@ -793,8 +802,29 @@ func (s *Scheduler) trimEventTriggeredAttestations(epoch uint64) { }) } -// submitValidatorRegistrationsAsync submits validator registrations asynchronously and updates the epoch tracker on success. -func (s *Scheduler) submitValidatorRegistrationsAsync(ctx context.Context, epoch uint64) { +// submitValidatorRegistrationsDelayed delays submission until near the end of slot 0 to reduce BN load. +func (s *Scheduler) submitValidatorRegistrationsDelayed(ctx context.Context, slot core.Slot) { + slotDuration, _, err := eth2wrap.FetchSlotsConfig(ctx, s.eth2Cl) + if err != nil { + log.Warn(ctx, "Failed to fetch slot duration for delayed registration", err) + // Fall back to immediate submission + s.submitValidatorRegistrations(ctx, slot.Epoch()) + return + } + + // Wait for 75% of slot duration before submitting (end of slot 0) + delay := (slotDuration * 3) / 4 + + select { + case <-s.quit: + return + case <-s.clock.After(delay): + s.submitValidatorRegistrations(ctx, slot.Epoch()) + } +} + +// submitValidatorRegistrations submits the validator registrations for all DVs. +func (s *Scheduler) submitValidatorRegistrations(ctx context.Context, epoch uint64) { if s.getSubmittedRegistrationEpoch() == epoch { return } From cef4714b3b2c4cad4f52d7af0c70e6ab2ca9b84a Mon Sep 17 00:00:00 2001 From: Diogo Santos Date: Tue, 3 Feb 2026 15:30:56 +0000 Subject: [PATCH 3/3] first run with zero epoch --- core/scheduler/scheduler.go | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/core/scheduler/scheduler.go b/core/scheduler/scheduler.go index 2c02db6356..2dd0dc3d4c 100644 --- a/core/scheduler/scheduler.go +++ b/core/scheduler/scheduler.go @@ -53,13 +53,14 @@ func NewForT(t *testing.T, clock clockwork.Clock, delayFunc delayFunc, builderRe // New returns a new scheduler. func New(builderRegistrations []*eth2api.VersionedSignedValidatorRegistration, eth2Cl eth2wrap.Client, builderEnabled bool) (*Scheduler, error) { return &Scheduler{ - eth2Cl: eth2Cl, - builderRegistrations: builderRegistrations, - quit: make(chan struct{}), - duties: make(map[core.Duty]core.DutyDefinitionSet), - dutiesByEpoch: make(map[uint64][]core.Duty), - epochResolved: make(map[uint64]chan struct{}), - clock: clockwork.NewRealClock(), + eth2Cl: eth2Cl, + builderRegistrations: builderRegistrations, + submittedRegistrationEpoch: math.MaxUint64, + quit: make(chan struct{}), + duties: make(map[core.Duty]core.DutyDefinitionSet), + dutiesByEpoch: make(map[uint64][]core.Duty), + epochResolved: make(map[uint64]chan struct{}), + clock: clockwork.NewRealClock(), delayFunc: func(_ core.Duty, deadline time.Time) <-chan time.Time { return time.After(time.Until(deadline)) }, @@ -143,7 +144,7 @@ func (s *Scheduler) Run() error { // Submit validator registrations on startup if builder is enabled. // This ensures registrations are sent before the first proposal opportunity. if s.builderEnabled { - go s.submitValidatorRegistrations(ctx, math.MaxUint64) + go s.submitValidatorRegistrations(ctx, 0) } slotTicker, err := newSlotTicker(ctx, s.eth2Cl, s.clock)