Skip to content

Commit 2b2a029

Browse files
authored
core/scheduler: refactor builder registration (#4271)
Refactor builder registration to be asynchronous. category: feature ticket: #4254
1 parent d834ec2 commit 2b2a029

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
@@ -115,6 +117,20 @@ func (s *Scheduler) Stop() {
115117
close(s.quit)
116118
}
117119

120+
// getSubmittedRegistrationEpoch returns the last epoch for which registrations were submitted.
121+
func (s *Scheduler) getSubmittedRegistrationEpoch() uint64 {
122+
s.registrationMutex.Lock()
123+
defer s.registrationMutex.Unlock()
124+
return s.submittedRegistrationEpoch
125+
}
126+
127+
// setSubmittedRegistrationEpoch sets the last epoch for which registrations were submitted.
128+
func (s *Scheduler) setSubmittedRegistrationEpoch(epoch uint64) {
129+
s.registrationMutex.Lock()
130+
defer s.registrationMutex.Unlock()
131+
s.submittedRegistrationEpoch = epoch
132+
}
133+
118134
// Run blocks and runs the scheduler until Stop is called.
119135
func (s *Scheduler) Run() error {
120136
ctx := log.WithTopic(context.Background(), "sched")
@@ -125,6 +141,12 @@ func (s *Scheduler) Run() error {
125141
waitChainStart(ctx, s.eth2Cl, s.clock)
126142
waitBeaconSync(ctx, s.eth2Cl, s.clock)
127143

144+
// Submit validator registrations on startup if builder is enabled.
145+
// This ensures registrations are sent before the first proposal opportunity.
146+
if s.builderEnabled {
147+
go s.submitValidatorRegistrations(ctx, 0)
148+
}
149+
128150
slotTicker, err := newSlotTicker(ctx, s.eth2Cl, s.clock)
129151
if err != nil {
130152
return err
@@ -283,10 +305,11 @@ func (s *Scheduler) scheduleSlot(ctx context.Context, slot core.Slot) {
283305
}
284306
}
285307

286-
// Safe to access s.submittedRegistrationEpoch without lock because scheduleSlot is called by the same goroutine.
287-
if s.builderEnabled && s.submittedRegistrationEpoch != slot.Epoch() {
288-
if err := s.submitValidatorRegistrations(ctx); err == nil {
289-
s.submittedRegistrationEpoch = slot.Epoch()
308+
// Submit validator registrations asynchronously to avoid blocking duty triggering.
309+
// Only submit at slot 0 of each epoch, delayed to end of slot to reduce BN load.
310+
if s.builderEnabled && s.getSubmittedRegistrationEpoch() != slot.Epoch() {
311+
if slot.Slot%slot.SlotsPerEpoch == 0 {
312+
go s.submitValidatorRegistrationsDelayed(ctx, slot)
290313
}
291314
}
292315

@@ -780,20 +803,43 @@ func (s *Scheduler) trimEventTriggeredAttestations(epoch uint64) {
780803
})
781804
}
782805

806+
// submitValidatorRegistrationsDelayed delays submission until near the end of slot 0 to reduce BN load.
807+
func (s *Scheduler) submitValidatorRegistrationsDelayed(ctx context.Context, slot core.Slot) {
808+
slotDuration, _, err := eth2wrap.FetchSlotsConfig(ctx, s.eth2Cl)
809+
if err != nil {
810+
log.Warn(ctx, "Failed to fetch slot duration for delayed registration", err)
811+
// Fall back to immediate submission
812+
s.submitValidatorRegistrations(ctx, slot.Epoch())
813+
return
814+
}
815+
816+
// Wait for 75% of slot duration before submitting (end of slot 0)
817+
delay := (slotDuration * 3) / 4
818+
819+
select {
820+
case <-s.quit:
821+
return
822+
case <-s.clock.After(delay):
823+
s.submitValidatorRegistrations(ctx, slot.Epoch())
824+
}
825+
}
826+
783827
// submitValidatorRegistrations submits the validator registrations for all DVs.
784-
func (s *Scheduler) submitValidatorRegistrations(ctx context.Context) error {
828+
func (s *Scheduler) submitValidatorRegistrations(ctx context.Context, epoch uint64) {
829+
if s.getSubmittedRegistrationEpoch() == epoch {
830+
return
831+
}
832+
785833
submitRegistrationCounter.Add(1)
786834

787835
err := s.eth2Cl.SubmitValidatorRegistrations(ctx, s.builderRegistrations)
788836
if err != nil {
789837
submitRegistrationErrors.Add(1)
790-
791-
log.Error(ctx, "Failed to submit validator registrations", err)
838+
log.Error(ctx, "Failed to submit validator registrations", err, z.U64("epoch", epoch))
792839
} else {
793-
log.Info(ctx, "Submitted validator registrations", z.Int("count", len(s.builderRegistrations)))
840+
log.Info(ctx, "Submitted validator registrations", z.Int("count", len(s.builderRegistrations)), z.U64("epoch", epoch))
841+
s.setSubmittedRegistrationEpoch(epoch)
794842
}
795-
796-
return err
797843
}
798844

799845
// 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
@@ -550,6 +550,7 @@ func TestSubmitValidatorRegistrations(t *testing.T) {
550550
callCount atomic.Int64
551551
callMutex sync.Mutex
552552
registrations []*eth2api.VersionedSignedValidatorRegistration
553+
callDone = make(chan struct{}, 1)
553554
)
554555

555556
origFunc := eth2Cl.SubmitValidatorRegistrationsFunc
@@ -558,10 +559,13 @@ func TestSubmitValidatorRegistrations(t *testing.T) {
558559

559560
if registrations == nil {
560561
callMutex.Lock()
561-
562562
registrations = regs
563-
564563
callMutex.Unlock()
564+
565+
select {
566+
case callDone <- struct{}{}:
567+
default:
568+
}
565569
}
566570

567571
return origFunc(ctx, regs)
@@ -601,11 +605,11 @@ func TestSubmitValidatorRegistrations(t *testing.T) {
601605
epochsSeen[slot.Epoch()] = true
602606
}
603607

604-
// Stop after processing enough slots to see 3 epochs
608+
// Stop after processing enough slots to see 3 epochs and after registration completes
605609
// With 4 slots per epoch, we need at least 9 slots (slots 0-8 cover epochs 0, 1, 2)
606610
if slotCount >= 9 && !stopped {
611+
<-callDone
607612
stopped = true
608-
609613
sched.Stop()
610614
}
611615

0 commit comments

Comments
 (0)