@@ -371,25 +371,25 @@ func (s *Syncer) processHeightEvent(event *common.DAHeightEvent) {
371
371
// If this is not the next block in sequence, store as pending event
372
372
// This check is crucial as trySyncNextBlock simply attempts to sync the next block
373
373
if height != currentHeight + 1 {
374
- // Create a DAHeightEvent that matches the cache interface
375
- pendingEvent := & common.DAHeightEvent {
376
- Header : event .Header ,
377
- Data : event .Data ,
378
- DaHeight : event .DaHeight ,
379
- HeaderDaIncludedHeight : event .HeaderDaIncludedHeight ,
380
- }
381
- s .cache .SetPendingEvent (height , pendingEvent )
374
+ s .cache .SetPendingEvent (height , event )
382
375
s .logger .Debug ().Uint64 ("height" , height ).Uint64 ("current_height" , currentHeight ).Msg ("stored as pending event" )
383
376
return
384
377
}
385
378
386
379
// Try to sync the next block
387
380
if err := s .trySyncNextBlock (event ); err != nil {
388
381
s .logger .Error ().Err (err ).Msg ("failed to sync next block" )
382
+ // If the error is not due to an validation error, re-store the event as pending
383
+ if ! errors .Is (err , errInvalidBlock ) {
384
+ s .cache .SetPendingEvent (height , event )
385
+ }
389
386
return
390
387
}
391
388
}
392
389
390
+ // errInvalidBlock is returned when a block is failing validation
391
+ var errInvalidBlock = errors .New ("invalid block" )
392
+
393
393
// trySyncNextBlock attempts to sync the next available block
394
394
// the event is always the next block in sequence as processHeightEvent ensures it.
395
395
func (s * Syncer ) trySyncNextBlock (event * common.DAHeightEvent ) error {
@@ -410,7 +410,7 @@ func (s *Syncer) trySyncNextBlock(event *common.DAHeightEvent) error {
410
410
// here only the previous block needs to be applied to proceed to the verification.
411
411
// The header validation must be done before applying the block to avoid executing gibberish
412
412
if err := s .validateBlock (currentState , header , data ); err != nil {
413
- return fmt .Errorf ("failed to validate block: %w" , err )
413
+ return errors . Join ( errInvalidBlock , fmt .Errorf ("failed to validate block: %w" , err ) )
414
414
}
415
415
416
416
// Mark as DA included
@@ -551,34 +551,30 @@ func (s *Syncer) isHeightFromFutureError(err error) bool {
551
551
552
552
// processPendingEvents fetches and processes pending events from cache
553
553
func (s * Syncer ) processPendingEvents () {
554
- pendingEvents := s .cache .GetPendingEvents ()
554
+ currentHeight , err := s .store .Height (s .ctx )
555
+ if err != nil {
556
+ s .logger .Error ().Err (err ).Msg ("failed to get current height for pending events" )
557
+ return
558
+ }
555
559
556
- for height , event := range pendingEvents {
557
- currentHeight , err := s .store .Height (s .ctx )
558
- if err != nil {
559
- s .logger .Error ().Err (err ).Msg ("failed to get current height for pending events" )
560
- continue
560
+ // Try to get the next processable event (currentHeight + 1)
561
+ nextHeight := currentHeight + 1
562
+ if event := s .cache .GetNextPendingEvent (nextHeight ); event != nil {
563
+ heightEvent := common.DAHeightEvent {
564
+ Header : event .Header ,
565
+ Data : event .Data ,
566
+ DaHeight : event .DaHeight ,
567
+ HeaderDaIncludedHeight : event .HeaderDaIncludedHeight ,
561
568
}
562
569
563
- // Only process events for blocks we haven't synced yet
564
- if height > currentHeight {
565
- heightEvent := common.DAHeightEvent {
566
- Header : event .Header ,
567
- Data : event .Data ,
568
- DaHeight : event .DaHeight ,
569
- HeaderDaIncludedHeight : event .HeaderDaIncludedHeight ,
570
- }
571
-
572
- select {
573
- case s .heightInCh <- heightEvent :
574
- // Remove from pending events once sent
575
- s .cache .DeletePendingEvent (height )
576
- case <- s .ctx .Done ():
577
- return
578
- }
579
- } else {
580
- // Clean up events for blocks we've already processed
581
- s .cache .DeletePendingEvent (height )
570
+ select {
571
+ case s .heightInCh <- heightEvent :
572
+ // Event was successfully sent and already removed by GetNextPendingEvent
573
+ s .logger .Debug ().Uint64 ("height" , nextHeight ).Msg ("sent pending event to processing" )
574
+ case <- s .ctx .Done ():
575
+ s .cache .SetPendingEvent (nextHeight , event )
576
+ default :
577
+ s .cache .SetPendingEvent (nextHeight , event )
582
578
}
583
579
}
584
580
}
0 commit comments