Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mainnet and rollup exit roots #1975

Merged
merged 4 commits into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion jsonrpc/endpoints_zkevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,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
})
Expand Down
6 changes: 6 additions & 0 deletions jsonrpc/endpoints_zkevm.openrpc.json
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,12 @@
"globalExitRoot": {
"$ref": "#/components/schemas/Keccak"
},
"mainnetExitRoot": {
"$ref": "#/components/schemas/Keccak"
},
"rollupExitRoot": {
"$ref": "#/components/schemas/Keccak"
},
"accInputHash": {
"$ref": "#/components/schemas/Keccak"
},
Expand Down
30 changes: 30 additions & 0 deletions jsonrpc/endpoints_zkevm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,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),
Expand Down Expand Up @@ -775,6 +785,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),
Expand Down Expand Up @@ -874,6 +894,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),
Expand Down
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
Expand Up @@ -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
Expand Up @@ -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"`
Expand All @@ -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 {
Expand Down