Skip to content

Commit

Permalink
[RPC] Add Fork endpoints (#3782)
Browse files Browse the repository at this point in the history
  • Loading branch information
tclemos authored Sep 13, 2024
1 parent f9111f4 commit 2499ae9
Show file tree
Hide file tree
Showing 11 changed files with 859 additions and 3 deletions.
67 changes: 67 additions & 0 deletions jsonrpc/endpoints_zkevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"math/big"
"slices"
"time"

"github.com/0xPolygonHermez/zkevm-node/hex"
Expand Down Expand Up @@ -638,3 +639,69 @@ func (z *ZKEVMEndpoints) GetLatestGlobalExitRoot() (interface{}, types.Error) {

return ger.String(), nil
}

// GetForkId returns the network's current fork ID
func (z *ZKEVMEndpoints) GetForkId() (interface{}, types.Error) {
ctx := context.Background()
forkID, err := z.state.GetCurrentForkID(ctx, nil)
if err != nil {
return "0x0", types.NewRPCError(types.DefaultErrorCode, "failed to get the current fork id from state")
}

return hex.EncodeUint64(forkID), nil
}

// GetForkById returns the network fork ID interval given the provided fork id
func (z *ZKEVMEndpoints) GetForkById(forkID types.ArgUint64) (interface{}, types.Error) {
ctx := context.Background()
forkIDInterval, err := z.state.GetForkByID(ctx, uint64(forkID), nil)
if errors.Is(err, state.ErrNotFound) {
return nil, nil
} else if err != nil {
return nil, types.NewRPCError(types.DefaultErrorCode, "failed to get the fork interval by id from state")
}

res := types.NewForkIDInterval(*forkIDInterval)
return res, nil
}

// GetForkIdByBatchNumber returns the fork ID given the provided batch number
func (z *ZKEVMEndpoints) GetForkIdByBatchNumber(batchNumber types.BatchNumber) (interface{}, types.Error) {
ctx := context.Background()

numericBatchNumber, rpcErr := batchNumber.GetNumericBatchNumber(ctx, z.state, z.etherman, nil)
if rpcErr != nil {
return nil, rpcErr
}

forkID := z.state.GetForkIDByBatchNumber(numericBatchNumber)
return hex.EncodeUint64(forkID), nil
}

// GetForks returns the network fork ID intervals
func (z *ZKEVMEndpoints) GetForks() (interface{}, types.Error) {
ctx := context.Background()
forkIDIntervals, err := z.state.GetForkIDIntervals(ctx, nil)
if errors.Is(err, state.ErrStateNotSynchronized) {
return nil, nil
} else if err != nil {
return nil, types.NewRPCError(types.DefaultErrorCode, "failed to get the fork id intervals from state")
}

res := make([]*types.ForkIDInterval, 0, len(forkIDIntervals))
for _, forkIDInterval := range forkIDIntervals {
res = append(res, types.NewForkIDInterval(forkIDInterval))
}

slices.SortFunc(res, func(a *types.ForkIDInterval, b *types.ForkIDInterval) int {
if a.ForkId == b.ForkId {
return 0
} else if a.ForkId > b.ForkId {
return 1
} else {
return -1
}
})

return res, nil
}
165 changes: 165 additions & 0 deletions jsonrpc/endpoints_zkevm.openrpc.json
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,126 @@
"$ref": "#/components/schemas/Integer"
}
}
},
{
"name": "zkevm_forkId",
"summary": "Returns the network's current fork ID.",
"params": [],
"result": {
"$ref": "#/components/contentDescriptors/ForkID"
},
"examples": [
{
"name": "example",
"description": "",
"params": [],
"result": {
"name": "exampleResult",
"description": "",
"value": "0x1"
}
}
]
},
{
"name": "zkevm_getForkById",
"summary": "returns the network fork ID interval given the provided fork id",
"params": [
{
"$ref": "#/components/contentDescriptors/ForkID"
}
],
"result": {
"$ref": "#/components/contentDescriptors/Fork"
},
"examples": [
{
"name": "example",
"description": "",
"params": [
{
"name": "fork id",
"value": "0x1"
}
],
"result": {
"name": "Fork",
"value": {
"forkId": "0x8",
"fromBatchNumber": "0x1",
"toBatchNumber": "0xffffffffffffffff",
"version": "",
"blockNumber": "0x88"
}
}
}
]
},
{
"name": "zkevm_getForkIdByBatchNumber",
"summary": "returns the fork ID given the provided batch number",
"params": [
{
"$ref": "#/components/contentDescriptors/BatchNumber"
}
],
"result": {
"$ref": "#/components/contentDescriptors/ForkID"
},
"examples": [
{
"name": "example",
"description": "",
"params": [],
"result": {
"name": "exampleResult",
"description": "",
"value": "0x1"
}
}
]
},
{
"name": "zkevm_getForks",
"summary": "returns the network fork ID interval given the provided fork id",
"params": [],
"result": {
"name": "result",
"schema": {
"description": "Array of forks",
"type": "array",
"items": {
"title": "fork",
"$ref": "#/components/contentDescriptors/Fork"
}
}
},
"examples": [
{
"name": "example",
"description": "",
"params": [],
"result": {
"name": "Fork",
"value": [
{
"forkId": "0x8",
"fromBatchNumber": "0x1",
"toBatchNumber": "0xa",
"version": "",
"blockNumber": "0x88"
},
{
"forkId": "0x9",
"fromBatchNumber": "0xb",
"toBatchNumber": "0xffffffffffffffff",
"version": "",
"blockNumber": "0x188"
}
]
}
}
]
}
],
"components": {
Expand Down Expand Up @@ -520,6 +640,14 @@
"$ref": "#/components/schemas/Block"
}
},
"Fork": {
"name": "fork",
"description": "fork",
"required": true,
"schema": {
"$ref": "#/components/schemas/Fork"
}
},
"Transaction": {
"required": true,
"name": "transaction",
Expand Down Expand Up @@ -548,6 +676,13 @@
}
]
}
},
"ForkID": {
"name": "forkID",
"required": true,
"schema": {
"$ref": "#/components/schemas/ForkID"
}
}
},
"schemas": {
Expand Down Expand Up @@ -1437,6 +1572,36 @@
"$ref": "#/components/schemas/Integer"
}
}
},
"ForkID": {
"title": "forkID",
"type": "string",
"description": "The hex representation of the fork's id",
"$ref": "#/components/schemas/Integer"
},
"Fork": {
"title": "Fork",
"type": "object",
"readOnly": true,
"properties": {
"forkId": {
"$ref": "#/components/schemas/ForkID"
},
"fromBatchNumber": {
"$ref": "#/components/schemas/BatchNumber"
},
"toBatchNumber": {
"$ref": "#/components/schemas/BatchNumber"
},
"Version": {
"title": "batchNumberTag",
"type": "string",
"description": "fork version"
},
"BlockNumber": {
"$ref": "#/components/schemas/BlockNumber"
}
}
}
}
}
Expand Down
Loading

0 comments on commit 2499ae9

Please sign in to comment.