44 "context"
55 "fmt"
66 "math/big"
7+ "time"
78
89 "github.com/scroll-tech/da-codec/encoding"
910 "github.com/scroll-tech/go-ethereum/common"
@@ -252,6 +253,11 @@ func (e *L1EventParser) ParseL1BatchEventLogs(ctx context.Context, logs []types.
252253 // Key: commit transaction hash
253254 // Value: parent batch hashes (in order) for each processed CommitBatch event in the transaction
254255 txBlobIndexMap := make (map [common.Hash ][]common.Hash )
256+
257+ // Cache for the previous transaction to avoid duplicate fetches
258+ var lastTxHash common.Hash
259+ var lastTx * types.Transaction
260+
255261 var l1BatchEvents []* orm.BatchEvent
256262 for _ , vlog := range logs {
257263 switch vlog .Topics [0 ] {
@@ -261,11 +267,28 @@ func (e *L1EventParser) ParseL1BatchEventLogs(ctx context.Context, logs []types.
261267 log .Error ("Failed to unpack CommitBatch event" , "err" , err )
262268 return nil , err
263269 }
264- commitTx , isPending , err := client .TransactionByHash (ctx , vlog .TxHash )
265- if err != nil || isPending {
266- log .Error ("Failed to get commit batch tx or the tx is still pending" , "err" , err , "isPending" , isPending )
267- return nil , err
270+
271+ // Get transaction, reuse if it's the same as previous
272+ var commitTx * types.Transaction
273+ if lastTxHash == vlog .TxHash && lastTx != nil {
274+ commitTx = lastTx
275+ } else {
276+ log .Debug ("Fetching commit batch transaction" , "txHash" , vlog .TxHash .String ())
277+
278+ // Create 10-second timeout context for transaction fetch
279+ txCtx , txCancel := context .WithTimeout (ctx , 10 * time .Second )
280+ fetchedTx , isPending , err := client .TransactionByHash (txCtx , vlog .TxHash )
281+ txCancel ()
282+
283+ if err != nil || isPending {
284+ log .Error ("Failed to get commit batch tx or the tx is still pending" , "err" , err , "isPending" , isPending )
285+ return nil , err
286+ }
287+ commitTx = fetchedTx
288+ lastTxHash = vlog .TxHash
289+ lastTx = commitTx
268290 }
291+
269292 version , startBlock , endBlock , err := utils .GetBatchVersionAndBlockRangeFromCalldata (commitTx .Data ())
270293 if err != nil {
271294 log .Error ("Failed to get batch range from calldata" , "hash" , commitTx .Hash ().String (), "height" , vlog .BlockNumber )
@@ -305,7 +328,13 @@ func (e *L1EventParser) ParseL1BatchEventLogs(ctx context.Context, logs []types.
305328 return nil , fmt .Errorf ("batch hash mismatch for batch %d, expected: %s, got: %s" , event .BatchIndex , event .BatchHash .String (), calculatedBatch .Hash ().String ())
306329 }
307330
308- blocks , err := e .getBatchBlockRangeFromBlob (ctx , codec , blobVersionedHash , blockTimestampsMap [vlog .BlockNumber ])
331+ log .Debug ("Processing blob data" , "blobVersionedHash" , blobVersionedHash .String (), "batchIndex" , event .BatchIndex .Uint64 (), "currentIndex" , currentIndex )
332+
333+ // Create 20-second timeout context for blob processing
334+ blobCtx , blobCancel := context .WithTimeout (ctx , 20 * time .Second )
335+ blocks , err := e .getBatchBlockRangeFromBlob (blobCtx , codec , blobVersionedHash , blockTimestampsMap [vlog .BlockNumber ])
336+ blobCancel ()
337+
309338 if err != nil {
310339 return nil , fmt .Errorf ("failed to process versioned blob, blobVersionedHash: %s, block number: %d, blob index: %d, err: %w" ,
311340 blobVersionedHash .String (), vlog .BlockNumber , currentIndex , err )
0 commit comments