@@ -345,8 +345,16 @@ func (r *Layer2Relayer) commitGenesisBatch(batchHash string, batchHeader []byte,
345345// - backlogCount > r.cfg.BatchSubmission.BacklogMax -> forceSubmit
346346// - we have at least minBatches AND price hits a desired target price
347347func (r * Layer2Relayer ) ProcessPendingBatches () {
348- // Get effective batch limits based on whether validium mode is enabled.
349- minBatches , maxBatches := r .getEffectiveBatchLimits ()
348+ // First, get the backlog count to determine batch submission strategy
349+ backlogCount , err := r .batchOrm .GetFailedAndPendingBatchesCount (r .ctx )
350+ if err != nil {
351+ log .Error ("Failed to fetch pending L2 batches count" , "err" , err )
352+ return
353+ }
354+ r .metrics .rollupL2RelayerBacklogCounts .Set (float64 (backlogCount ))
355+
356+ // Get effective batch limits based on validium mode and backlog size.
357+ minBatches , maxBatches := r .getEffectiveBatchLimits (backlogCount )
350358
351359 // get pending batches from database in ascending order by their index.
352360 dbBatches , err := r .batchOrm .GetFailedAndPendingBatches (r .ctx , maxBatches )
@@ -360,15 +368,6 @@ func (r *Layer2Relayer) ProcessPendingBatches() {
360368 return
361369 }
362370
363- // if backlog outgrow max size, force‐submit enough oldest batches
364- backlogCount , err := r .batchOrm .GetFailedAndPendingBatchesCount (r .ctx )
365- r .metrics .rollupL2RelayerBacklogCounts .Set (float64 (backlogCount ))
366-
367- if err != nil {
368- log .Error ("Failed to fetch pending L2 batches" , "err" , err )
369- return
370- }
371-
372371 var forceSubmit bool
373372
374373 startChunk , err := r .chunkOrm .GetChunkByIndex (r .ctx , dbBatches [0 ].StartChunkIndex )
@@ -563,12 +562,22 @@ func (r *Layer2Relayer) ProcessPendingBatches() {
563562 log .Info ("Sent the commitBatches tx to layer1" , "batches count" , len (batchesToSubmit ), "start index" , firstBatch .Index , "start hash" , firstBatch .Hash , "end index" , lastBatch .Index , "end hash" , lastBatch .Hash , "tx hash" , txHash .String ())
564563}
565564
566- // getEffectiveBatchLimits returns the effective min and max batch limits based on whether validium mode is enabled.
567- func (r * Layer2Relayer ) getEffectiveBatchLimits () (int , int ) {
565+ // getEffectiveBatchLimits returns the effective min and max batch limits based on whether validium mode is enabled
566+ // and the current backlog size.
567+ // When backlogCount >= backlog_max: submit min_batches for fast inclusion at slightly higher price.
568+ // When backlogCount < backlog_max: submit max_batches for better cost amortization.
569+ func (r * Layer2Relayer ) getEffectiveBatchLimits (backlogCount int64 ) (int , int ) {
568570 if r .cfg .ValidiumMode {
569571 return 1 , 1 // minBatches=1, maxBatches=1
570572 }
571- return r .cfg .BatchSubmission .MinBatches , r .cfg .BatchSubmission .MaxBatches
573+
574+ // If backlog is at or above max, prioritize fast inclusion by submitting min_batches
575+ if backlogCount >= r .cfg .BatchSubmission .BacklogMax {
576+ return r .cfg .BatchSubmission .MinBatches , r .cfg .BatchSubmission .MinBatches
577+ }
578+
579+ // Otherwise, prioritize cost efficiency by trying to submit max_batches
580+ return r .cfg .BatchSubmission .MaxBatches , r .cfg .BatchSubmission .MaxBatches
572581}
573582
574583func (r * Layer2Relayer ) contextIDFromBatches (codecVersion encoding.CodecVersion , batches []* dbBatchWithChunks ) string {
0 commit comments