Skip to content

Commit

Permalink
mainnet and rollup exit roots (0xPolygonHermez#1975)
Browse files Browse the repository at this point in the history
* mainnet and rollup exit roots

* linter

* fix test e2e

* doc
  • Loading branch information
ARR552 authored Apr 4, 2023
1 parent 776e0f7 commit 6e47b4a
Showing 6 changed files with 83 additions and 9 deletions.
9 changes: 8 additions & 1 deletion jsonrpc/endpoints_zkevm.go
Original file line number Diff line number Diff line change
@@ -154,8 +154,15 @@ func (z *ZKEVMEndpoints) GetBatchByNumber(batchNumber types.BatchNumber, fullTx
return rpcErrorResponse(types.DefaultErrorCode, fmt.Sprintf("couldn't load virtual batch from state by number %v", batchNumber), err)
}

ger, err := z.state.GetExitRootByGlobalExitRoot(ctx, batch.GlobalExitRoot, dbTx)
if err != nil && !errors.Is(err, state.ErrNotFound) {
return rpcErrorResponse(types.DefaultErrorCode, fmt.Sprintf("couldn't load full GER from state by number %v", batchNumber), err)
} else if errors.Is(err, state.ErrNotFound) {
ger = &state.GlobalExitRoot{}
}

batch.Transactions = txs
rpcBatch := types.NewBatch(batch, virtualBatch, verifiedBatch, receipts, fullTx)
rpcBatch := types.NewBatch(batch, virtualBatch, verifiedBatch, receipts, fullTx, ger)

return rpcBatch, nil
})
6 changes: 6 additions & 0 deletions jsonrpc/endpoints_zkevm.openrpc.json
Original file line number Diff line number Diff line change
@@ -385,6 +385,12 @@
"globalExitRoot": {
"$ref": "#/components/schemas/Keccak"
},
"mainnetExitRoot": {
"$ref": "#/components/schemas/Keccak"
},
"rollupExitRoot": {
"$ref": "#/components/schemas/Keccak"
},
"accInputHash": {
"$ref": "#/components/schemas/Keccak"
},
30 changes: 30 additions & 0 deletions jsonrpc/endpoints_zkevm_test.go
Original file line number Diff line number Diff line change
@@ -693,6 +693,16 @@ func TestGetBatchByNumber(t *testing.T) {
Return(verifiedBatch, nil).
Once()

ger := state.GlobalExitRoot{
MainnetExitRoot: common.HexToHash("0x4"),
RollupExitRoot: common.HexToHash("0x4"),
GlobalExitRoot: common.HexToHash("0x4"),
}
m.State.
On("GetExitRootByGlobalExitRoot", context.Background(), batch.GlobalExitRoot, m.DbTx).
Return(&ger, nil).
Once()

txs := []*ethTypes.Transaction{
signTx(ethTypes.NewTransaction(1001, common.HexToAddress("0x1000"), big.NewInt(1000), 1001, big.NewInt(1002), []byte("1003")), s.Config.ChainID),
signTx(ethTypes.NewTransaction(1002, common.HexToAddress("0x1000"), big.NewInt(1000), 1001, big.NewInt(1002), []byte("1003")), s.Config.ChainID),
@@ -807,6 +817,16 @@ func TestGetBatchByNumber(t *testing.T) {
Return(verifiedBatch, nil).
Once()

ger := state.GlobalExitRoot{
MainnetExitRoot: common.HexToHash("0x4"),
RollupExitRoot: common.HexToHash("0x4"),
GlobalExitRoot: common.HexToHash("0x4"),
}
m.State.
On("GetExitRootByGlobalExitRoot", context.Background(), batch.GlobalExitRoot, m.DbTx).
Return(&ger, nil).
Once()

txs := []*ethTypes.Transaction{
signTx(ethTypes.NewTransaction(1001, common.HexToAddress("0x1000"), big.NewInt(1000), 1001, big.NewInt(1002), []byte("1003")), s.Config.ChainID),
signTx(ethTypes.NewTransaction(1002, common.HexToAddress("0x1000"), big.NewInt(1000), 1001, big.NewInt(1002), []byte("1003")), s.Config.ChainID),
@@ -906,6 +926,16 @@ func TestGetBatchByNumber(t *testing.T) {
Return(verifiedBatch, nil).
Once()

ger := state.GlobalExitRoot{
MainnetExitRoot: common.HexToHash("0x4"),
RollupExitRoot: common.HexToHash("0x4"),
GlobalExitRoot: common.HexToHash("0x4"),
}
m.State.
On("GetExitRootByGlobalExitRoot", context.Background(), batch.GlobalExitRoot, m.DbTx).
Return(&ger, nil).
Once()

txs := []*ethTypes.Transaction{
signTx(ethTypes.NewTransaction(1001, common.HexToAddress("0x1000"), big.NewInt(1000), 1001, big.NewInt(1002), []byte("1003")), s.Config.ChainID),
signTx(ethTypes.NewTransaction(1002, common.HexToAddress("0x1000"), big.NewInt(1000), 1001, big.NewInt(1002), []byte("1003")), s.Config.ChainID),
26 changes: 26 additions & 0 deletions jsonrpc/mocks/mock_state.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions jsonrpc/types/interfaces.go
Original file line number Diff line number Diff line change
@@ -61,4 +61,5 @@ type StateInterface interface {
GetTransactionsByBatchNumber(ctx context.Context, batchNumber uint64, dbTx pgx.Tx) (txs []types.Transaction, err error)
GetVirtualBatch(ctx context.Context, batchNumber uint64, dbTx pgx.Tx) (*state.VirtualBatch, error)
GetVerifiedBatch(ctx context.Context, batchNumber uint64, dbTx pgx.Tx) (*state.VerifiedBatch, error)
GetExitRootByGlobalExitRoot(ctx context.Context, ger common.Hash, dbTx pgx.Tx) (*state.GlobalExitRoot, error)
}
20 changes: 12 additions & 8 deletions jsonrpc/types/types.go
Original file line number Diff line number Diff line change
@@ -329,6 +329,8 @@ type Batch struct {
Coinbase common.Address `json:"coinbase"`
StateRoot common.Hash `json:"stateRoot"`
GlobalExitRoot common.Hash `json:"globalExitRoot"`
MainnetExitRoot common.Hash `json:"mainnetExitRoot"`
RollupExitRoot common.Hash `json:"rollupExitRoot"`
LocalExitRoot common.Hash `json:"localExitRoot"`
AccInputHash common.Hash `json:"accInputHash"`
Timestamp ArgUint64 `json:"timestamp"`
@@ -338,15 +340,17 @@ type Batch struct {
}

// NewBatch creates a Batch instance
func NewBatch(batch *state.Batch, virtualBatch *state.VirtualBatch, verifiedBatch *state.VerifiedBatch, receipts []types.Receipt, fullTx bool) *Batch {
func NewBatch(batch *state.Batch, virtualBatch *state.VirtualBatch, verifiedBatch *state.VerifiedBatch, receipts []types.Receipt, fullTx bool, ger *state.GlobalExitRoot) *Batch {
res := &Batch{
Number: ArgUint64(batch.BatchNumber),
GlobalExitRoot: batch.GlobalExitRoot,
AccInputHash: batch.AccInputHash,
Timestamp: ArgUint64(batch.Timestamp.Unix()),
StateRoot: batch.StateRoot,
Coinbase: batch.Coinbase,
LocalExitRoot: batch.LocalExitRoot,
Number: ArgUint64(batch.BatchNumber),
GlobalExitRoot: batch.GlobalExitRoot,
MainnetExitRoot: ger.MainnetExitRoot,
RollupExitRoot: ger.RollupExitRoot,
AccInputHash: batch.AccInputHash,
Timestamp: ArgUint64(batch.Timestamp.Unix()),
StateRoot: batch.StateRoot,
Coinbase: batch.Coinbase,
LocalExitRoot: batch.LocalExitRoot,
}

if virtualBatch != nil {

0 comments on commit 6e47b4a

Please sign in to comment.