Skip to content

Commit

Permalink
feat: zkevm_getBatchByNumber batch closed based on datastream
Browse files Browse the repository at this point in the history
  • Loading branch information
V-Staykov committed Oct 28, 2024
1 parent 9239ac3 commit 0638fdb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
15 changes: 11 additions & 4 deletions turbo/jsonrpc/zkevm_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

zktypes "github.com/ledgerwatch/erigon/zk/types"

"github.com/0xPolygonHermez/zkevm-data-streamer/datastreamer"
"github.com/holiman/uint256"
"github.com/ledgerwatch/erigon-lib/common/hexutil"
"github.com/ledgerwatch/erigon-lib/kv/membatchwithdb"
Expand All @@ -32,6 +33,7 @@ import (
"github.com/ledgerwatch/erigon/smt/pkg/smt"
smtUtils "github.com/ledgerwatch/erigon/smt/pkg/utils"
"github.com/ledgerwatch/erigon/turbo/rpchelper"
"github.com/ledgerwatch/erigon/zk/datastream/server"
"github.com/ledgerwatch/erigon/zk/hermez_db"
"github.com/ledgerwatch/erigon/zk/legacy_executor_verifier"
types "github.com/ledgerwatch/erigon/zk/rpcdaemon"
Expand All @@ -44,8 +46,6 @@ import (
"github.com/ledgerwatch/erigon/zk/witness"
"github.com/ledgerwatch/erigon/zkevm/hex"
"github.com/ledgerwatch/erigon/zkevm/jsonrpc/client"
"github.com/ledgerwatch/erigon/zk/datastream/server"
"github.com/0xPolygonHermez/zkevm-data-streamer/datastreamer"
)

var sha3UncleHash = common.HexToHash("0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347")
Expand Down Expand Up @@ -557,10 +557,17 @@ func (api *ZkEvmAPIImpl) GetBatchByNumber(ctx context.Context, batchNumber rpc.B
// sequenced, genesis or injected batch 1 - special batches 0,1 will always be closed, if next batch has blocks, bn must be closed
batch.Closed = batchNo == 0 || batchNo == 1 || batchNo <= highestClosed
} else {
if _, found, err = hermezDb.GetLowestBlockInBatch(batchNo + 1); err != nil {
latestClosedBlock, err := hermezDb.GetLatestBatchEndBlock()
if err != nil {
return nil, err
}
batch.Closed = batchNo == 0 || batchNo == 1 || found

latestClosedbatchNum, err := hermezDb.GetBatchNoByL2Block(latestClosedBlock)
if err != nil {
return nil, err
}

batch.Closed = batchNo <= latestClosedbatchNum
}

// verification - if we can't find one, maybe this batch was verified along with a higher batch number
Expand Down
15 changes: 15 additions & 0 deletions zk/hermez_db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -1782,6 +1782,21 @@ func (db *HermezDbReader) GetBatchEnd(blockNo uint64) (bool, error) {
return len(v) > 0, nil
}

func (db *HermezDbReader) GetLatestBatchEndBlock() (uint64, error) {
c, err := db.tx.Cursor(BATCH_ENDS)
if err != nil {
return 0, nil
}
defer c.Close()

k, _, err := c.Last()
if err != nil {
return 0, err
}

return BytesToUint64(k), nil
}

func (db *HermezDb) DeleteBatchEnds(from, to uint64) error {
return db.deleteFromBucketWithUintKeysRange(BATCH_ENDS, from, to)
}
Expand Down

0 comments on commit 0638fdb

Please sign in to comment.