@@ -345,8 +345,16 @@ func (r *Layer2Relayer) commitGenesisBatch(batchHash string, batchHeader []byte,
345
345
// - backlogCount > r.cfg.BatchSubmission.BacklogMax -> forceSubmit
346
346
// - we have at least minBatches AND price hits a desired target price
347
347
func (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 )
350
358
351
359
// get pending batches from database in ascending order by their index.
352
360
dbBatches , err := r .batchOrm .GetFailedAndPendingBatches (r .ctx , maxBatches )
@@ -360,15 +368,6 @@ func (r *Layer2Relayer) ProcessPendingBatches() {
360
368
return
361
369
}
362
370
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
-
372
371
var forceSubmit bool
373
372
374
373
startChunk , err := r .chunkOrm .GetChunkByIndex (r .ctx , dbBatches [0 ].StartChunkIndex )
@@ -563,12 +562,22 @@ func (r *Layer2Relayer) ProcessPendingBatches() {
563
562
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 ())
564
563
}
565
564
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 ) {
568
570
if r .cfg .ValidiumMode {
569
571
return 1 , 1 // minBatches=1, maxBatches=1
570
572
}
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
572
581
}
573
582
574
583
func (r * Layer2Relayer ) contextIDFromBatches (codecVersion encoding.CodecVersion , batches []* dbBatchWithChunks ) string {
0 commit comments