@@ -243,11 +243,11 @@ func (s *Syncer) syncLoop() {
243243 return
244244 }
245245
246- lastHeaderHeight := initialHeight
247- lastDataHeight := initialHeight
246+ lastHeaderHeight := & initialHeight
247+ lastDataHeight := & initialHeight
248248
249249 // Backoff control when DA replies with errors
250- var nextDARequestAt time.Time
250+ nextDARequestAt := & time.Time {}
251251
252252 blockTicker := time .NewTicker (s .config .Node .BlockTime .Duration )
253253 defer blockTicker .Stop ()
@@ -261,44 +261,27 @@ func (s *Syncer) syncLoop() {
261261 // Process pending events from cache on every iteration
262262 s .processPendingEvents ()
263263
264- // Try fetching from both DA and P2P in configurable order
265- if s .tryFetchStrategies (& nextDARequestAt , & lastHeaderHeight , & lastDataHeight , blockTicker .C ) {
266- continue // events were processed, restart loop immediately
264+ // Fetch events from DA layer first and optimistically p2p when necessary.
265+ // This is the default behavior.
266+ if ! s .config .Sync .PreferP2P {
267+ if s .tryFetchFromDA (nextDARequestAt ) {
268+ continue
269+ }
270+ if s .tryFetchFromP2P (lastHeaderHeight , lastDataHeight , blockTicker .C ) {
271+ continue
272+ }
273+ } else {
274+ if s .tryFetchFromP2P (lastHeaderHeight , lastDataHeight , blockTicker .C ) {
275+ continue
276+ }
277+ if s .tryFetchFromDA (nextDARequestAt ) {
278+ continue
279+ }
267280 }
268281
269282 // Prevent busy-waiting when no events are available
270- waitTime := min (10 * time .Millisecond , s .config .Node .BlockTime .Duration )
271- time .Sleep (waitTime )
272- }
273- }
274-
275- // tryFetchStrategies attempts to fetch from both DA and P2P based on configuration priority.
276- // The order of fetching depends on the PreferP2P configuration setting:
277- // - PreferP2P=false (default): DA first, then P2P (original and recommended behavior)
278- // - PreferP2P=true: P2P first, then DA
279- //
280- // Both strategies are always attempted on each call, but the order affects which
281- // events are processed first and may impact overall sync performance.
282- func (s * Syncer ) tryFetchStrategies (nextDARequestAt * time.Time , lastHeaderHeight , lastDataHeight * uint64 , blockTicker <- chan time.Time ) bool {
283- eventsProcessed := false
284-
285- if s .config .PreferP2P {
286- if s .tryFetchFromP2P (lastHeaderHeight , lastDataHeight , blockTicker ) {
287- eventsProcessed = true
288- }
289- if s .tryFetchFromDA (nextDARequestAt ) {
290- eventsProcessed = true
291- }
292- } else {
293- if s .tryFetchFromDA (nextDARequestAt ) {
294- eventsProcessed = true
295- }
296- if s .tryFetchFromP2P (lastHeaderHeight , lastDataHeight , blockTicker ) {
297- eventsProcessed = true
298- }
283+ time .Sleep (min (10 * time .Millisecond , s .config .Node .BlockTime .Duration ))
299284 }
300-
301- return eventsProcessed
302285}
303286
304287// tryFetchFromDA attempts to fetch events from the DA layer.
@@ -474,15 +457,17 @@ func (s *Syncer) trySyncNextBlock(event *common.DAHeightEvent) error {
474457 return errors .Join (errInvalidBlock , fmt .Errorf ("failed to validate block: %w" , err ))
475458 }
476459
477- // Mark as DA included
478- headerHash := header .Hash ().String ()
479- s .cache .SetHeaderDAIncluded (headerHash , event .HeaderDaIncludedHeight )
460+ // Mark as DA included (only if set, p2p sync does not set it)
461+ if event .HeaderDaIncludedHeight > 0 {
462+ headerHash := header .Hash ().String ()
463+ s .cache .SetHeaderDAIncluded (headerHash , event .HeaderDaIncludedHeight )
480464
481- s .logger .Info ().
482- Str ("header_hash" , headerHash ).
483- Uint64 ("da_height" , event .HeaderDaIncludedHeight ).
484- Uint64 ("height" , header .Height ()).
485- Msg ("header marked as DA included" )
465+ s .logger .Info ().
466+ Str ("header_hash" , headerHash ).
467+ Uint64 ("da_height" , event .HeaderDaIncludedHeight ).
468+ Uint64 ("height" , header .Height ()).
469+ Msg ("header marked as DA included" )
470+ }
486471
487472 // Apply block
488473 newState , err := s .applyBlock (header .Header , data , currentState )
0 commit comments