Skip to content

Commit

Permalink
validate rpc sub-configs, move out ValidationInputToJson and Validati…
Browse files Browse the repository at this point in the history
…onInputFromJson into server api package
  • Loading branch information
anodar committed Apr 29, 2024
1 parent 82988d5 commit 82a68c6
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 64 deletions.
9 changes: 9 additions & 0 deletions staker/block_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@ func (c *BlockValidatorConfig) Validate() error {
c.ValidationServerConfigs = validationServersConfigs
}
}
for _, vc := range c.ValidationServerConfigs {
if err := vc.Validate(); err != nil {
return fmt.Errorf("validating validation server configs: %w", err)
}
}

if len(c.ValidationServerConfigs) == 0 && !streamsEnabled {
return fmt.Errorf("block-validator validation-server-configs is empty, need at least one validation server config")
}
Expand All @@ -133,6 +139,9 @@ func (c *BlockValidatorConfig) Validate() error {
if err := c.ExecutionServerConfig.Validate(); err != nil {
return fmt.Errorf("validating execution server config: %w", err)
}
if err := c.ValidationServer.Validate(); err != nil {
return fmt.Errorf("validating validation server config: %w", err)
}
return nil
}

Expand Down
33 changes: 4 additions & 29 deletions validator/client/validation_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ import (
"sync/atomic"
"time"

"github.com/offchainlabs/nitro/arbutil"
"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 Down Expand Up @@ -39,7 +37,7 @@ func NewValidationClient(config rpcclient.ClientConfigFetcher, stack *node.Node)
func (c *ValidationClient) Launch(entry *validator.ValidationInput, moduleRoot common.Hash) validator.ValidationRun {
atomic.AddInt32(&c.room, -1)
promise := stopwaiter.LaunchPromiseThread[validator.GoGlobalState](c, func(ctx context.Context) (validator.GoGlobalState, error) {
input := ValidationInputToJson(entry)
input := server_api.ValidationInputToJson(entry)
var res validator.GoGlobalState
err := c.client.CallContext(ctx, &res, server_api.Namespace+"_validate", input, moduleRoot)
atomic.AddInt32(&c.room, 1)
Expand Down Expand Up @@ -84,10 +82,7 @@ func (c *ValidationClient) Stop() {
}

func (c *ValidationClient) Name() string {
if c.Started() {
return c.name
}
return "(not started)"
return c.name
}

func (c *ValidationClient) Room() int {
Expand All @@ -111,7 +106,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, server_api.Namespace+"_createExecutionRun", wasmModuleRoot, ValidationInputToJson(input))
err := c.client.CallContext(ctx, &res, server_api.Namespace+"_createExecutionRun", wasmModuleRoot, server_api.ValidationInputToJson(input))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -142,7 +137,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 := ValidationInputToJson(input)
jsonInput := server_api.ValidationInputToJson(input)
return stopwaiter.LaunchPromiseThread[struct{}](c, func(ctx context.Context) (struct{}, error) {
err := c.client.CallContext(ctx, nil, server_api.Namespace+"_writeToFile", jsonInput, expOut, moduleRoot)
return struct{}{}, err
Expand Down Expand Up @@ -211,23 +206,3 @@ func (r *ExecutionClientRun) Close() {
}
})
}

func ValidationInputToJson(entry *validator.ValidationInput) *server_api.InputJSON {
jsonPreimagesMap := make(map[arbutil.PreimageType]*jsonapi.PreimagesMapJson)
for ty, preimages := range entry.Preimages {
jsonPreimagesMap[ty] = jsonapi.NewPreimagesMapJson(preimages)
}
res := &server_api.InputJSON{
Id: entry.Id,
HasDelayedMsg: entry.HasDelayedMsg,
DelayedMsgNr: entry.DelayedMsgNr,
DelayedMsgB64: base64.StdEncoding.EncodeToString(entry.DelayedMsg),
StartState: entry.StartState,
PreimagesB64: jsonPreimagesMap,
}
for _, binfo := range entry.BatchInfo {
encData := base64.StdEncoding.EncodeToString(binfo.Data)
res.BatchInfo = append(res.BatchInfo, server_api.BatchInfoJson{Number: binfo.Number, DataB64: encData})
}
return res
}
52 changes: 52 additions & 0 deletions validator/server_api/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package server_api

import (
"encoding/base64"
"fmt"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -63,3 +64,54 @@ type BatchInfoJson struct {
Number uint64
DataB64 string
}

func ValidationInputToJson(entry *validator.ValidationInput) *InputJSON {
jsonPreimagesMap := make(map[arbutil.PreimageType]*jsonapi.PreimagesMapJson)
for ty, preimages := range entry.Preimages {
jsonPreimagesMap[ty] = jsonapi.NewPreimagesMapJson(preimages)
}
res := &InputJSON{
Id: entry.Id,
HasDelayedMsg: entry.HasDelayedMsg,
DelayedMsgNr: entry.DelayedMsgNr,
DelayedMsgB64: base64.StdEncoding.EncodeToString(entry.DelayedMsg),
StartState: entry.StartState,
PreimagesB64: jsonPreimagesMap,
}
for _, binfo := range entry.BatchInfo {
encData := base64.StdEncoding.EncodeToString(binfo.Data)
res.BatchInfo = append(res.BatchInfo, BatchInfoJson{Number: binfo.Number, DataB64: encData})
}
return res
}

func ValidationInputFromJson(entry *InputJSON) (*validator.ValidationInput, error) {
preimages := make(map[arbutil.PreimageType]map[common.Hash][]byte)
for ty, jsonPreimages := range entry.PreimagesB64 {
preimages[ty] = jsonPreimages.Map
}
valInput := &validator.ValidationInput{
Id: entry.Id,
HasDelayedMsg: entry.HasDelayedMsg,
DelayedMsgNr: entry.DelayedMsgNr,
StartState: entry.StartState,
Preimages: preimages,
}
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
}
38 changes: 3 additions & 35 deletions validator/valnode/validation_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/arbutil"
"github.com/offchainlabs/nitro/util/stopwaiter"
"github.com/offchainlabs/nitro/validator"
"github.com/offchainlabs/nitro/validator/server_api"
Expand All @@ -30,7 +29,7 @@ func (a *ValidationServerAPI) Room() int {
}

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

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

func (a *ExecServerAPI) WriteToFile(ctx context.Context, jsonInput *server_api.InputJSON, expOut validator.GoGlobalState, moduleRoot common.Hash) error {
input, err := ValidationInputFromJson(jsonInput)
input, err := server_api.ValidationInputFromJson(jsonInput)
if err != nil {
return err
}
Expand Down Expand Up @@ -182,34 +181,3 @@ func (a *ExecServerAPI) CloseExec(execid uint64) {
run.run.Close()
delete(a.runs, execid)
}

func ValidationInputFromJson(entry *server_api.InputJSON) (*validator.ValidationInput, error) {
preimages := make(map[arbutil.PreimageType]map[common.Hash][]byte)
for ty, jsonPreimages := range entry.PreimagesB64 {
preimages[ty] = jsonPreimages.Map
}
valInput := &validator.ValidationInput{
Id: entry.Id,
HasDelayedMsg: entry.HasDelayedMsg,
DelayedMsgNr: entry.DelayedMsgNr,
StartState: entry.StartState,
Preimages: preimages,
}
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
}

0 comments on commit 82a68c6

Please sign in to comment.