Skip to content

Commit

Permalink
new zkevm_getExitRootTable RPC endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
hexoscott committed Jul 29, 2024
1 parent 83e84b8 commit e6e5996
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
54 changes: 54 additions & 0 deletions cmd/rpcdaemon/commands/zkevm_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ type ZkEvmAPI interface {
EstimateCounters(ctx context.Context, argsOrNil *zkevmRPCTransaction) (json.RawMessage, error)
TraceTransactionCounters(ctx context.Context, hash common.Hash, config *tracers.TraceConfig_ZkEvm, stream *jsoniter.Stream) error
GetBatchCountersByNumber(ctx context.Context, batchNumRpc rpc.BlockNumber) (res json.RawMessage, err error)
GetExitRootTable(ctx context.Context) ([]l1InfoTreeData, error)
}

// APIImpl is implementation of the ZkEvmAPI interface based on remote Db access
Expand Down Expand Up @@ -1020,6 +1021,59 @@ func (api *ZkEvmAPIImpl) GetLatestGlobalExitRoot(ctx context.Context) (common.Ha
return ger, nil
}

type l1InfoTreeData struct {
Index uint64 `json:"index"`
Ger common.Hash `json:"ger"`
InfoRoot common.Hash `json:"info_root"`
MainnetExitRoot common.Hash `json:"mainnet_exit_root"`
RollupExitRoot common.Hash `json:"rollup_exit_root"`
ParentHash common.Hash `json:"parent_hash"`
MinTimestamp uint64 `json:"min_timestamp"`
BlockNumber uint64 `json:"block_number"`
}

func (api *ZkEvmAPIImpl) GetExitRootTable(ctx context.Context) ([]l1InfoTreeData, error) {
tx, err := api.db.BeginRo(ctx)
if err != nil {
return nil, err
}
defer tx.Rollback()

hermezDb := hermez_db.NewHermezDbReader(tx)

indexToRoots, err := hermezDb.GetL1InfoTreeIndexToRoots()
if err != nil {
return nil, err
}

var result []l1InfoTreeData

var idx uint64 = 1
for {
info, err := hermezDb.GetL1InfoTreeUpdate(idx)
if err != nil {
return nil, err
}
if info == nil || info.Index == 0 {
break
}
data := l1InfoTreeData{
Index: info.Index,
Ger: info.GER,
MainnetExitRoot: info.MainnetExitRoot,
RollupExitRoot: info.RollupExitRoot,
ParentHash: info.ParentHash,
MinTimestamp: info.Timestamp,
BlockNumber: info.BlockNumber,
InfoRoot: indexToRoots[info.Index],
}
result = append(result, data)
idx++
}

return result, nil
}

func (api *ZkEvmAPIImpl) sendGetBatchWitness(rpcUrl string, batchNumber uint64, mode *WitnessMode) (json.RawMessage, error) {
res, err := client.JSONRPCCall(rpcUrl, "zkevm_getBatchWitness", batchNumber, mode)
if err != nil {
Expand Down
20 changes: 20 additions & 0 deletions zk/hermez_db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -1463,6 +1463,26 @@ func (db *HermezDb) GetL1InfoTreeIndexByRoot(hash common.Hash) (uint64, bool, er
return BytesToUint64(data), data != nil, nil
}

func (db *HermezDbReader) GetL1InfoTreeIndexToRoots() (map[uint64]common.Hash, error) {
c, err := db.tx.Cursor(L1_INFO_ROOTS)
if err != nil {
return nil, err
}
defer c.Close()

indexToRoot := make(map[uint64]common.Hash)
for k, v, err := c.First(); k != nil; k, v, err = c.Next() {
if err != nil {
return nil, err
}
index := BytesToUint64(v)
root := common.BytesToHash(k)
indexToRoot[index] = root
}

return indexToRoot, nil
}

func (db *HermezDbReader) GetForkIdByBlockNum(blockNum uint64) (uint64, error) {
blockbatch, err := db.GetBatchNoByL2Block(blockNum)
if err != nil {
Expand Down

0 comments on commit e6e5996

Please sign in to comment.