@@ -53,13 +53,14 @@ func NewForT(t *testing.T, clock clockwork.Clock, delayFunc delayFunc, builderRe
5353// New returns a new scheduler.
5454func 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.
119135func (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.
0 commit comments