diff --git a/cmd/rpcdaemon/commands/zkevm_api.go b/cmd/rpcdaemon/commands/zkevm_api.go index e9788ca89e9..369bc55b78b 100644 --- a/cmd/rpcdaemon/commands/zkevm_api.go +++ b/cmd/rpcdaemon/commands/zkevm_api.go @@ -320,6 +320,16 @@ func (api *ZkEvmAPIImpl) GetBatchDataByNumbers(ctx context.Context, batchNumbers continue } + // try to find the BatchData in db to avoid calculate it when it is possible + batchData, err := hermezDb.GetL1BatchData(batchNumber.Uint64()) + if err != nil { + return nil, err + } else if len(batchData) != 0 { + bd.BatchL2Data = batchData + bds = append(bds, bd) + continue + } + // looks weird but we're using the rpc.BlockNumber type to represent the batch number, LatestBlockNumber represents latest batch if batchNumber == rpc.LatestBlockNumber { batchNumber = rpc.BlockNumber(highestBatchNo) diff --git a/rpc/types.go b/rpc/types.go index a62a89c168e..b99383cc12d 100644 --- a/rpc/types.go +++ b/rpc/types.go @@ -80,6 +80,11 @@ const ( EarliestBlockNumber = BlockNumber(0) ) +// Returns the block number as type uint64 +func (bn *BlockNumber) Uint64() uint64 { + return uint64(bn.Int64()) +} + // UnmarshalJSON parses the given JSON fragment into a BlockNumber. It supports: // - "latest", "earliest", "pending", "safe", or "finalized" as string arguments // - the block number