@@ -164,7 +164,8 @@ func (s *Syncer) SetBlockSyncer(bs BlockSyncer) {
164164
165165// Start begins the syncing component
166166func (s * Syncer ) Start (ctx context.Context ) error {
167- s .ctx , s .cancel = context .WithCancel (ctx )
167+ ctx , cancel := context .WithCancel (ctx )
168+ s .ctx , s .cancel = ctx , cancel
168169
169170 if err := s .initializeState (); err != nil {
170171 return fmt .Errorf ("failed to initialize syncer state: %w" , err )
@@ -195,7 +196,7 @@ func (s *Syncer) Start(ctx context.Context) error {
195196 }
196197
197198 // Start main processing loop
198- s .wg .Go (s .processLoop )
199+ s .wg .Go (func () { s .processLoop ( ctx ) } )
199200
200201 // Start dedicated workers for DA, and pending processing
201202 s .startSyncWorkers (ctx )
@@ -342,38 +343,37 @@ func (s *Syncer) initializeState() error {
342343}
343344
344345// processLoop is the main coordination loop for processing events
345- func (s * Syncer ) processLoop () {
346+ func (s * Syncer ) processLoop (ctx context. Context ) {
346347 s .logger .Info ().Msg ("starting process loop" )
347348 defer s .logger .Info ().Msg ("process loop stopped" )
348349
349350 for {
350351 select {
351- case <- s . ctx .Done ():
352+ case <- ctx .Done ():
352353 return
353354 case heightEvent , ok := <- s .heightInCh :
354355 if ok {
355- s .processHeightEvent (s . ctx , & heightEvent )
356+ s .processHeightEvent (ctx , & heightEvent )
356357 }
357358 }
358359 }
359360}
360361
361362func (s * Syncer ) startSyncWorkers (ctx context.Context ) {
362- _ = ctx
363363 s .wg .Add (3 )
364- go s .daWorkerLoop ()
365- go s .pendingWorkerLoop ()
364+ go s .daWorkerLoop (ctx )
365+ go s .pendingWorkerLoop (ctx )
366366 go s .p2pWorkerLoop (ctx )
367367}
368368
369- func (s * Syncer ) daWorkerLoop () {
369+ func (s * Syncer ) daWorkerLoop (ctx context. Context ) {
370370 defer s .wg .Done ()
371371
372372 s .logger .Info ().Msg ("starting DA worker" )
373373 defer s .logger .Info ().Msg ("DA worker stopped" )
374374
375375 for {
376- err := s .fetchDAUntilCaughtUp ()
376+ err := s .fetchDAUntilCaughtUp (ctx )
377377
378378 var backoff time.Duration
379379 if err == nil {
@@ -389,7 +389,7 @@ func (s *Syncer) daWorkerLoop() {
389389 }
390390
391391 select {
392- case <- s . ctx .Done ():
392+ case <- ctx .Done ():
393393 return
394394 case <- time .After (backoff ):
395395 }
@@ -402,11 +402,11 @@ func (s *Syncer) HasReachedDAHead() bool {
402402 return s .daHeadReached .Load ()
403403}
404404
405- func (s * Syncer ) fetchDAUntilCaughtUp () error {
405+ func (s * Syncer ) fetchDAUntilCaughtUp (ctx context. Context ) error {
406406 for {
407407 select {
408- case <- s . ctx .Done ():
409- return s . ctx .Err ()
408+ case <- ctx .Done ():
409+ return ctx .Err ()
410410 default :
411411 }
412412
@@ -424,7 +424,7 @@ func (s *Syncer) fetchDAUntilCaughtUp() error {
424424 daHeight = max (s .daRetrieverHeight .Load (), s .cache .DaHeight ())
425425 }
426426
427- events , err := s .daRetriever .RetrieveFromDA (s . ctx , daHeight )
427+ events , err := s .daRetriever .RetrieveFromDA (ctx , daHeight )
428428 if err != nil {
429429 switch {
430430 case errors .Is (err , datypes .ErrBlobNotFound ):
@@ -446,7 +446,7 @@ func (s *Syncer) fetchDAUntilCaughtUp() error {
446446
447447 // Process DA events
448448 for _ , event := range events {
449- if err := s .pipeEvent (s . ctx , event ); err != nil {
449+ if err := s .pipeEvent (ctx , event ); err != nil {
450450 return err
451451 }
452452 }
@@ -467,7 +467,7 @@ func (s *Syncer) fetchDAUntilCaughtUp() error {
467467 }
468468}
469469
470- func (s * Syncer ) pendingWorkerLoop () {
470+ func (s * Syncer ) pendingWorkerLoop (ctx context. Context ) {
471471 defer s .wg .Done ()
472472
473473 s .logger .Info ().Msg ("starting pending worker" )
@@ -478,10 +478,10 @@ func (s *Syncer) pendingWorkerLoop() {
478478
479479 for {
480480 select {
481- case <- s . ctx .Done ():
481+ case <- ctx .Done ():
482482 return
483483 case <- ticker .C :
484- s .processPendingEvents ()
484+ s .processPendingEvents (ctx )
485485 }
486486 }
487487}
@@ -503,7 +503,7 @@ func (s *Syncer) p2pWorkerLoop(ctx context.Context) {
503503 currentHeight , err := s .store .Height (ctx )
504504 if err != nil {
505505 logger .Error ().Err (err ).Msg ("failed to get current height for P2P worker" )
506- if ! s .sleepOrDone (50 * time .Millisecond ) {
506+ if ! s .sleepOrDone (ctx , 50 * time .Millisecond ) {
507507 return
508508 }
509509 continue
@@ -525,13 +525,13 @@ func (s *Syncer) p2pWorkerLoop(ctx context.Context) {
525525 logger .Warn ().Err (err ).Uint64 ("height" , targetHeight ).Msg ("P2P handler failed to process height" )
526526 }
527527
528- if ! s .sleepOrDone (50 * time .Millisecond ) {
528+ if ! s .sleepOrDone (ctx , 50 * time .Millisecond ) {
529529 return
530530 }
531531 continue
532532 }
533533
534- if err := s .waitForStoreHeight (targetHeight ); err != nil {
534+ if err := s .waitForStoreHeight (ctx , targetHeight ); err != nil {
535535 if errors .Is (err , context .Canceled ) {
536536 return
537537 }
@@ -1078,8 +1078,8 @@ func (s *Syncer) sendCriticalError(err error) {
10781078
10791079// processPendingEvents fetches and processes pending events from cache
10801080// optimistically fetches the next events from cache until no matching heights are found
1081- func (s * Syncer ) processPendingEvents () {
1082- currentHeight , err := s .store .Height (s . ctx )
1081+ func (s * Syncer ) processPendingEvents (ctx context. Context ) {
1082+ currentHeight , err := s .store .Height (ctx )
10831083 if err != nil {
10841084 s .logger .Error ().Err (err ).Msg ("failed to get current height for pending events" )
10851085 return
@@ -1104,7 +1104,7 @@ func (s *Syncer) processPendingEvents() {
11041104 case s .heightInCh <- heightEvent :
11051105 // Event was successfully sent and already removed by GetNextPendingEvent
11061106 s .logger .Debug ().Uint64 ("height" , nextHeight ).Msg ("sent pending event to processing" )
1107- case <- s . ctx .Done ():
1107+ case <- ctx .Done ():
11081108 s .cache .SetPendingEvent (nextHeight , event )
11091109 return
11101110 default :
@@ -1116,9 +1116,9 @@ func (s *Syncer) processPendingEvents() {
11161116 }
11171117}
11181118
1119- func (s * Syncer ) waitForStoreHeight (target uint64 ) error {
1119+ func (s * Syncer ) waitForStoreHeight (ctx context. Context , target uint64 ) error {
11201120 for {
1121- currentHeight , err := s .store .Height (s . ctx )
1121+ currentHeight , err := s .store .Height (ctx )
11221122 if err != nil {
11231123 return err
11241124 }
@@ -1127,20 +1127,20 @@ func (s *Syncer) waitForStoreHeight(target uint64) error {
11271127 return nil
11281128 }
11291129
1130- if ! s .sleepOrDone (10 * time .Millisecond ) {
1131- if s . ctx .Err () != nil {
1132- return s . ctx .Err ()
1130+ if ! s .sleepOrDone (ctx , 10 * time .Millisecond ) {
1131+ if ctx .Err () != nil {
1132+ return ctx .Err ()
11331133 }
11341134 }
11351135 }
11361136}
11371137
1138- func (s * Syncer ) sleepOrDone (duration time.Duration ) bool {
1138+ func (s * Syncer ) sleepOrDone (ctx context. Context , duration time.Duration ) bool {
11391139 timer := time .NewTimer (duration )
11401140 defer timer .Stop ()
11411141
11421142 select {
1143- case <- s . ctx .Done ():
1143+ case <- ctx .Done ():
11441144 return false
11451145 case <- timer .C :
11461146 return true
0 commit comments