Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 55 additions & 10 deletions core/scheduler/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand All @@ -125,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)
Comment thread
DiogoSantoss marked this conversation as resolved.
Resolved
}

slotTicker, err := newSlotTicker(ctx, s.eth2Cl, s.clock)
if err != nil {
return err
Expand Down Expand Up @@ -283,10 +304,11 @@ 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.
// Only submit at slot 0 of each epoch, delayed to end of slot to reduce BN load.
if s.builderEnabled && s.getSubmittedRegistrationEpoch() != slot.Epoch() {
Comment thread
DiogoSantoss marked this conversation as resolved.
Resolved
if slot.Slot%slot.SlotsPerEpoch == 0 {
go s.submitValidatorRegistrationsDelayed(ctx, slot)
}
}

Expand Down Expand Up @@ -780,20 +802,43 @@ func (s *Scheduler) trimEventTriggeredAttestations(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())
Comment on lines +811 to +812

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nice!

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) error {
func (s *Scheduler) submitValidatorRegistrations(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.
Expand Down
12 changes: 8 additions & 4 deletions core/scheduler/scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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()
}

Expand Down
Loading