Skip to content

Commit 49ecee6

Browse files
authored
*: cherry pick async builder (#4292)
body... category: feature ticket: none
1 parent de16495 commit 49ecee6

2 files changed

Lines changed: 71 additions & 21 deletions

File tree

‎core/scheduler/scheduler.go‎

Lines changed: 63 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,14 @@ func NewForT(t *testing.T, clock clockwork.Clock, delayFunc delayFunc, builderRe
5353
// New returns a new scheduler.
5454
func New(builderRegistrations []*eth2api.VersionedSignedValidatorRegistration, eth2Cl eth2wrap.Client, builderEnabled bool) (*Scheduler, error) {
5555
return &Scheduler{
56-
eth2Cl: eth2Cl,
57-
builderRegistrations: builderRegistrations,
58-
quit: make(chan struct{}),
59-
duties: make(map[core.Duty]core.DutyDefinitionSet),
60-
dutiesByEpoch: make(map[uint64][]core.Duty),
61-
epochResolved: make(map[uint64]chan struct{}),
62-
clock: clockwork.NewRealClock(),
56+
eth2Cl: eth2Cl,
57+
builderRegistrations: builderRegistrations,
58+
submittedRegistrationEpoch: math.MaxUint64,
59+
quit: make(chan struct{}),
60+
duties: make(map[core.Duty]core.DutyDefinitionSet),
61+
dutiesByEpoch: make(map[uint64][]core.Duty),
62+
epochResolved: make(map[uint64]chan struct{}),
63+
clock: clockwork.NewRealClock(),
6364
delayFunc: func(_ core.Duty, deadline time.Time) <-chan time.Time {
6465
return time.After(time.Until(deadline))
6566
},
@@ -74,6 +75,7 @@ type Scheduler struct {
7475
eth2Cl eth2wrap.Client
7576
builderRegistrations []*eth2api.VersionedSignedValidatorRegistration
7677
submittedRegistrationEpoch uint64
78+
registrationMutex sync.Mutex
7779
quit chan struct{}
7880
clock clockwork.Clock
7981
delayFunc delayFunc
@@ -107,6 +109,20 @@ func (s *Scheduler) Stop() {
107109
close(s.quit)
108110
}
109111

112+
// getSubmittedRegistrationEpoch returns the last epoch for which registrations were submitted.
113+
func (s *Scheduler) getSubmittedRegistrationEpoch() uint64 {
114+
s.registrationMutex.Lock()
115+
defer s.registrationMutex.Unlock()
116+
return s.submittedRegistrationEpoch
117+
}
118+
119+
// setSubmittedRegistrationEpoch sets the last epoch for which registrations were submitted.
120+
func (s *Scheduler) setSubmittedRegistrationEpoch(epoch uint64) {
121+
s.registrationMutex.Lock()
122+
defer s.registrationMutex.Unlock()
123+
s.submittedRegistrationEpoch = epoch
124+
}
125+
110126
// Run blocks and runs the scheduler until Stop is called.
111127
func (s *Scheduler) Run() error {
112128
ctx := log.WithTopic(context.Background(), "sched")
@@ -117,6 +133,12 @@ func (s *Scheduler) Run() error {
117133
waitChainStart(ctx, s.eth2Cl, s.clock)
118134
waitBeaconSync(ctx, s.eth2Cl, s.clock)
119135

136+
// Submit validator registrations on startup if builder is enabled.
137+
// This ensures registrations are sent before the first proposal opportunity.
138+
if s.builderEnabled {
139+
go s.submitValidatorRegistrations(ctx, 0)
140+
}
141+
120142
slotTicker, err := newSlotTicker(ctx, s.eth2Cl, s.clock)
121143
if err != nil {
122144
return err
@@ -228,10 +250,11 @@ func (s *Scheduler) scheduleSlot(ctx context.Context, slot core.Slot) {
228250
}
229251
}
230252

231-
// Safe to access s.submittedRegistrationEpoch without lock because scheduleSlot is called by the same goroutine.
232-
if s.builderEnabled && s.submittedRegistrationEpoch != slot.Epoch() {
233-
if err := s.submitValidatorRegistrations(ctx); err == nil {
234-
s.submittedRegistrationEpoch = slot.Epoch()
253+
// Submit validator registrations asynchronously to avoid blocking duty triggering.
254+
// Only submit at slot 0 of each epoch, delayed to end of slot to reduce BN load.
255+
if s.builderEnabled && s.getSubmittedRegistrationEpoch() != slot.Epoch() {
256+
if slot.Slot%slot.SlotsPerEpoch == 0 {
257+
go s.submitValidatorRegistrationsDelayed(ctx, slot)
235258
}
236259
}
237260

@@ -665,20 +688,43 @@ func (s *Scheduler) trimDuties(epoch uint64) {
665688
delete(s.dutiesByEpoch, epoch)
666689
}
667690

691+
// submitValidatorRegistrationsDelayed delays submission until near the end of slot 0 to reduce BN load.
692+
func (s *Scheduler) submitValidatorRegistrationsDelayed(ctx context.Context, slot core.Slot) {
693+
slotDuration, _, err := eth2wrap.FetchSlotsConfig(ctx, s.eth2Cl)
694+
if err != nil {
695+
log.Warn(ctx, "Failed to fetch slot duration for delayed registration", err)
696+
// Fall back to immediate submission
697+
s.submitValidatorRegistrations(ctx, slot.Epoch())
698+
return
699+
}
700+
701+
// Wait for 75% of slot duration before submitting (end of slot 0)
702+
delay := (slotDuration * 3) / 4
703+
704+
select {
705+
case <-s.quit:
706+
return
707+
case <-s.clock.After(delay):
708+
s.submitValidatorRegistrations(ctx, slot.Epoch())
709+
}
710+
}
711+
668712
// submitValidatorRegistrations submits the validator registrations for all DVs.
669-
func (s *Scheduler) submitValidatorRegistrations(ctx context.Context) error {
713+
func (s *Scheduler) submitValidatorRegistrations(ctx context.Context, epoch uint64) {
714+
if s.getSubmittedRegistrationEpoch() == epoch {
715+
return
716+
}
717+
670718
submitRegistrationCounter.Add(1)
671719

672720
err := s.eth2Cl.SubmitValidatorRegistrations(ctx, s.builderRegistrations)
673721
if err != nil {
674722
submitRegistrationErrors.Add(1)
675-
676-
log.Error(ctx, "Failed to submit validator registrations", err)
723+
log.Error(ctx, "Failed to submit validator registrations", err, z.U64("epoch", epoch))
677724
} else {
678-
log.Info(ctx, "Submitted validator registrations", z.Int("count", len(s.builderRegistrations)))
725+
log.Info(ctx, "Submitted validator registrations", z.Int("count", len(s.builderRegistrations)), z.U64("epoch", epoch))
726+
s.setSubmittedRegistrationEpoch(epoch)
679727
}
680-
681-
return err
682728
}
683729

684730
// newSlotTicker returns a blocking channel that will be populated with new slots in real time.

‎core/scheduler/scheduler_test.go‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,7 @@ func TestSubmitValidatorRegistrations(t *testing.T) {
534534
callCount atomic.Int64
535535
callMutex sync.Mutex
536536
registrations []*eth2api.VersionedSignedValidatorRegistration
537+
callDone = make(chan struct{}, 1)
537538
)
538539

539540
origFunc := eth2Cl.SubmitValidatorRegistrationsFunc
@@ -542,10 +543,13 @@ func TestSubmitValidatorRegistrations(t *testing.T) {
542543

543544
if registrations == nil {
544545
callMutex.Lock()
545-
546546
registrations = regs
547-
548547
callMutex.Unlock()
548+
549+
select {
550+
case callDone <- struct{}{}:
551+
default:
552+
}
549553
}
550554

551555
return origFunc(ctx, regs)
@@ -585,11 +589,11 @@ func TestSubmitValidatorRegistrations(t *testing.T) {
585589
epochsSeen[slot.Epoch()] = true
586590
}
587591

588-
// Stop after processing enough slots to see 3 epochs
592+
// Stop after processing enough slots to see 3 epochs and after registration completes
589593
// With 4 slots per epoch, we need at least 9 slots (slots 0-8 cover epochs 0, 1, 2)
590594
if slotCount >= 9 && !stopped {
595+
<-callDone
591596
stopped = true
592-
593597
sched.Stop()
594598
}
595599

0 commit comments

Comments
 (0)