From 01cb502f3a79e3d3d410e2d98e82e355dea0942a Mon Sep 17 00:00:00 2001 From: Alonso Date: Tue, 3 Sep 2024 10:35:29 +0200 Subject: [PATCH] try to find the BatchData in db to avoid calculate it when it's possible --- cmd/rpcdaemon/commands/zkevm_api.go | 10 ++++++++++ rpc/types.go | 5 +++++ 2 files changed, 15 insertions(+) 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