Skip to content

Commit

Permalink
Address PR comment
Browse files Browse the repository at this point in the history
  • Loading branch information
PlasmaPower committed Jul 10, 2023
1 parent adc4f0c commit c7f0dc4
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 101 deletions.
90 changes: 4 additions & 86 deletions util/jsonapi/validation.go → util/jsonapi/preimages.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ import (
"io"

"github.com/ethereum/go-ethereum/common"

"github.com/offchainlabs/nitro/validator"
)

type PreimagesMapJson struct {
Map map[common.Hash][]byte
}

func NewPreimagesMapJson(inner map[common.Hash][]byte) PreimagesMapJson {
return PreimagesMapJson{inner}
}

func (m *PreimagesMapJson) MarshalJSON() ([]byte, error) {
encoding := base64.StdEncoding
size := 2 // {}
Expand Down Expand Up @@ -168,87 +170,3 @@ func (m *PreimagesMapJson) UnmarshalJSON(data []byte) error {
}
return nil
}

type BatchInfoJson struct {
Number uint64
DataB64 string
}

type ValidationInputJson struct {
Id uint64
HasDelayedMsg bool
DelayedMsgNr uint64
PreimagesB64 PreimagesMapJson
BatchInfo []BatchInfoJson
DelayedMsgB64 string
StartState validator.GoGlobalState
}

func ValidationInputToJson(entry *validator.ValidationInput) *ValidationInputJson {
res := &ValidationInputJson{
Id: entry.Id,
HasDelayedMsg: entry.HasDelayedMsg,
DelayedMsgNr: entry.DelayedMsgNr,
DelayedMsgB64: base64.StdEncoding.EncodeToString(entry.DelayedMsg),
StartState: entry.StartState,
PreimagesB64: PreimagesMapJson{entry.Preimages},
}
for _, binfo := range entry.BatchInfo {
encData := base64.StdEncoding.EncodeToString(binfo.Data)
res.BatchInfo = append(res.BatchInfo, BatchInfoJson{binfo.Number, encData})
}
return res
}

func ValidationInputFromJson(entry *ValidationInputJson) (*validator.ValidationInput, error) {
valInput := &validator.ValidationInput{
Id: entry.Id,
HasDelayedMsg: entry.HasDelayedMsg,
DelayedMsgNr: entry.DelayedMsgNr,
StartState: entry.StartState,
Preimages: entry.PreimagesB64.Map,
}
delayed, err := base64.StdEncoding.DecodeString(entry.DelayedMsgB64)
if err != nil {
return nil, err
}
valInput.DelayedMsg = delayed
for _, binfo := range entry.BatchInfo {
data, err := base64.StdEncoding.DecodeString(binfo.DataB64)
if err != nil {
return nil, err
}
decInfo := validator.BatchInfo{
Number: binfo.Number,
Data: data,
}
valInput.BatchInfo = append(valInput.BatchInfo, decInfo)
}
return valInput, nil
}

type MachineStepResultJson struct {
Hash common.Hash
Position uint64
Status uint8
GlobalState validator.GoGlobalState
}

func MachineStepResultToJson(result *validator.MachineStepResult) *MachineStepResultJson {
return &MachineStepResultJson{
Hash: result.Hash,
Position: result.Position,
Status: uint8(result.Status),
GlobalState: result.GlobalState,
}
}

func MachineStepResultFromJson(resultJson *MachineStepResultJson) (*validator.MachineStepResult, error) {

return &validator.MachineStepResult{
Hash: resultJson.Hash,
Position: resultJson.Position,
Status: validator.MachineStatus(resultJson.Status),
GlobalState: resultJson.GlobalState,
}, nil
}
File renamed without changes.
96 changes: 96 additions & 0 deletions validator/server_api/json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Copyright 2023, Offchain Labs, Inc.
// For license information, see https://github.com/OffchainLabs/nitro/blob/master/LICENSE

package server_api

import (
"encoding/base64"

"github.com/ethereum/go-ethereum/common"
"github.com/offchainlabs/nitro/util/jsonapi"
"github.com/offchainlabs/nitro/validator"
)

type BatchInfoJson struct {
Number uint64
DataB64 string
}

type ValidationInputJson struct {
Id uint64
HasDelayedMsg bool
DelayedMsgNr uint64
PreimagesB64 jsonapi.PreimagesMapJson
BatchInfo []BatchInfoJson
DelayedMsgB64 string
StartState validator.GoGlobalState
}

func ValidationInputToJson(entry *validator.ValidationInput) *ValidationInputJson {
res := &ValidationInputJson{
Id: entry.Id,
HasDelayedMsg: entry.HasDelayedMsg,
DelayedMsgNr: entry.DelayedMsgNr,
DelayedMsgB64: base64.StdEncoding.EncodeToString(entry.DelayedMsg),
StartState: entry.StartState,
PreimagesB64: jsonapi.NewPreimagesMapJson(entry.Preimages),
}
for _, binfo := range entry.BatchInfo {
encData := base64.StdEncoding.EncodeToString(binfo.Data)
res.BatchInfo = append(res.BatchInfo, BatchInfoJson{binfo.Number, encData})
}
return res
}

func ValidationInputFromJson(entry *ValidationInputJson) (*validator.ValidationInput, error) {
valInput := &validator.ValidationInput{
Id: entry.Id,
HasDelayedMsg: entry.HasDelayedMsg,
DelayedMsgNr: entry.DelayedMsgNr,
StartState: entry.StartState,
Preimages: entry.PreimagesB64.Map,
}
delayed, err := base64.StdEncoding.DecodeString(entry.DelayedMsgB64)
if err != nil {
return nil, err
}
valInput.DelayedMsg = delayed
for _, binfo := range entry.BatchInfo {
data, err := base64.StdEncoding.DecodeString(binfo.DataB64)
if err != nil {
return nil, err
}
decInfo := validator.BatchInfo{
Number: binfo.Number,
Data: data,
}
valInput.BatchInfo = append(valInput.BatchInfo, decInfo)
}
return valInput, nil
}

type MachineStepResultJson struct {
Hash common.Hash
Position uint64
Status uint8
GlobalState validator.GoGlobalState
}

func MachineStepResultToJson(result *validator.MachineStepResult) *MachineStepResultJson {
return &MachineStepResultJson{
Hash: result.Hash,
Position: result.Position,
Status: uint8(result.Status),
GlobalState: result.GlobalState,
}
}

func MachineStepResultFromJson(resultJson *MachineStepResultJson) (*validator.MachineStepResult, error) {

return &validator.MachineStepResult{
Hash: resultJson.Hash,
Position: resultJson.Position,
Status: validator.MachineStatus(resultJson.Status),
GlobalState: resultJson.GlobalState,
}, nil
}
17 changes: 8 additions & 9 deletions validator/server_api/valiation_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/ethereum/go-ethereum/common"

"github.com/offchainlabs/nitro/util/jsonapi"
"github.com/offchainlabs/nitro/util/stopwaiter"
"github.com/offchainlabs/nitro/validator"
"github.com/offchainlabs/nitro/validator/server_arb"
Expand All @@ -30,8 +29,8 @@ func (a *ValidationServerAPI) Room() int {
return a.spawner.Room()
}

func (a *ValidationServerAPI) Validate(ctx context.Context, entry *jsonapi.ValidationInputJson, moduleRoot common.Hash) (validator.GoGlobalState, error) {
valInput, err := jsonapi.ValidationInputFromJson(entry)
func (a *ValidationServerAPI) Validate(ctx context.Context, entry *ValidationInputJson, moduleRoot common.Hash) (validator.GoGlobalState, error) {
valInput, err := ValidationInputFromJson(entry)
if err != nil {
return validator.GoGlobalState{}, err
}
Expand Down Expand Up @@ -70,8 +69,8 @@ func NewExecutionServerAPI(valSpawner validator.ValidationSpawner, execution val
}
}

func (a *ExecServerAPI) CreateExecutionRun(ctx context.Context, wasmModuleRoot common.Hash, jsonInput *jsonapi.ValidationInputJson) (uint64, error) {
input, err := jsonapi.ValidationInputFromJson(jsonInput)
func (a *ExecServerAPI) CreateExecutionRun(ctx context.Context, wasmModuleRoot common.Hash, jsonInput *ValidationInputJson) (uint64, error) {
input, err := ValidationInputFromJson(jsonInput)
if err != nil {
return 0, err
}
Expand Down Expand Up @@ -108,8 +107,8 @@ func (a *ExecServerAPI) Start(ctx_in context.Context) {
a.CallIteratively(a.removeOldRuns)
}

func (a *ExecServerAPI) WriteToFile(ctx context.Context, jsonInput *jsonapi.ValidationInputJson, expOut validator.GoGlobalState, moduleRoot common.Hash) error {
input, err := jsonapi.ValidationInputFromJson(jsonInput)
func (a *ExecServerAPI) WriteToFile(ctx context.Context, jsonInput *ValidationInputJson, expOut validator.GoGlobalState, moduleRoot common.Hash) error {
input, err := ValidationInputFromJson(jsonInput)
if err != nil {
return err
}
Expand All @@ -130,7 +129,7 @@ func (a *ExecServerAPI) getRun(id uint64) (validator.ExecutionRun, error) {
return entry.run, nil
}

func (a *ExecServerAPI) GetStepAt(ctx context.Context, execid uint64, position uint64) (*jsonapi.MachineStepResultJson, error) {
func (a *ExecServerAPI) GetStepAt(ctx context.Context, execid uint64, position uint64) (*MachineStepResultJson, error) {
run, err := a.getRun(execid)
if err != nil {
return nil, err
Expand All @@ -140,7 +139,7 @@ func (a *ExecServerAPI) GetStepAt(ctx context.Context, execid uint64, position u
if err != nil {
return nil, err
}
return jsonapi.MachineStepResultToJson(res), nil
return MachineStepResultToJson(res), nil
}

func (a *ExecServerAPI) GetProofAt(ctx context.Context, execid uint64, position uint64) (string, error) {
Expand Down
11 changes: 5 additions & 6 deletions validator/server_api/validation_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/offchainlabs/nitro/validator"

"github.com/offchainlabs/nitro/util/containers"
"github.com/offchainlabs/nitro/util/jsonapi"
"github.com/offchainlabs/nitro/util/rpcclient"
"github.com/offchainlabs/nitro/util/stopwaiter"

Expand All @@ -35,7 +34,7 @@ func NewValidationClient(config rpcclient.ClientConfigFetcher, stack *node.Node)
func (c *ValidationClient) Launch(entry *validator.ValidationInput, moduleRoot common.Hash) validator.ValidationRun {
valrun := server_common.NewValRun(moduleRoot)
c.LaunchThread(func(ctx context.Context) {
input := jsonapi.ValidationInputToJson(entry)
input := ValidationInputToJson(entry)
var res validator.GoGlobalState
err := c.client.CallContext(ctx, &res, Namespace+"_validate", input, moduleRoot)
valrun.ConsumeResult(res, err)
Expand Down Expand Up @@ -99,7 +98,7 @@ func NewExecutionClient(config rpcclient.ClientConfigFetcher, stack *node.Node)
func (c *ExecutionClient) CreateExecutionRun(wasmModuleRoot common.Hash, input *validator.ValidationInput) containers.PromiseInterface[validator.ExecutionRun] {
return stopwaiter.LaunchPromiseThread[validator.ExecutionRun](c, func(ctx context.Context) (validator.ExecutionRun, error) {
var res uint64
err := c.client.CallContext(ctx, &res, Namespace+"_createExecutionRun", wasmModuleRoot, jsonapi.ValidationInputToJson(input))
err := c.client.CallContext(ctx, &res, Namespace+"_createExecutionRun", wasmModuleRoot, ValidationInputToJson(input))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -130,7 +129,7 @@ func (c *ExecutionClient) LatestWasmModuleRoot() containers.PromiseInterface[com
}

func (c *ExecutionClient) WriteToFile(input *validator.ValidationInput, expOut validator.GoGlobalState, moduleRoot common.Hash) containers.PromiseInterface[struct{}] {
jsonInput := jsonapi.ValidationInputToJson(input)
jsonInput := ValidationInputToJson(input)
return stopwaiter.LaunchPromiseThread[struct{}](c, func(ctx context.Context) (struct{}, error) {
err := c.client.CallContext(ctx, nil, Namespace+"_writeToFile", jsonInput, expOut, moduleRoot)
return struct{}{}, err
Expand All @@ -152,12 +151,12 @@ func (r *ExecutionClientRun) Start(ctx_in context.Context) {

func (r *ExecutionClientRun) GetStepAt(pos uint64) containers.PromiseInterface[*validator.MachineStepResult] {
return stopwaiter.LaunchPromiseThread[*validator.MachineStepResult](r, func(ctx context.Context) (*validator.MachineStepResult, error) {
var resJson jsonapi.MachineStepResultJson
var resJson MachineStepResultJson
err := r.client.client.CallContext(ctx, &resJson, Namespace+"_getStepAt", r.id, pos)
if err != nil {
return nil, err
}
res, err := jsonapi.MachineStepResultFromJson(&resJson)
res, err := MachineStepResultFromJson(&resJson)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit c7f0dc4

Please sign in to comment.