Skip to content

Commit

Permalink
Merge pull request #2263 from OffchainLabs/small-fixes-redis-validation
Browse files Browse the repository at this point in the history
validate rpc sub-configs, move out ValidationInputToJson and ValidationInputFromJson into server api package [NIT-2450]
  • Loading branch information
anodar authored May 14, 2024
2 parents 8e2bf8b + 29e82e3 commit 0146d77
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 91 deletions.
42 changes: 4 additions & 38 deletions validator/client/validation_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,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 @@ -44,7 +42,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 @@ -104,10 +102,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 @@ -131,7 +126,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 @@ -162,7 +157,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 @@ -231,32 +226,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,
UserWasms: make(map[common.Hash]server_api.UserWasmJson),
DebugChain: entry.DebugChain,
}
for _, binfo := range entry.BatchInfo {
encData := base64.StdEncoding.EncodeToString(binfo.Data)
res.BatchInfo = append(res.BatchInfo, server_api.BatchInfoJson{Number: binfo.Number, DataB64: encData})
}
for moduleHash, info := range entry.UserWasms {
encWasm := server_api.UserWasmJson{
Asm: base64.StdEncoding.EncodeToString(info.Asm),
Module: base64.StdEncoding.EncodeToString(info.Module),
}
res.UserWasms[moduleHash] = encWasm
}
return res
}
79 changes: 79 additions & 0 deletions validator/server_api/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
package server_api

import (
"encoding/base64"
"fmt"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/state"
"github.com/offchainlabs/nitro/arbutil"

"github.com/offchainlabs/nitro/util/jsonapi"
Expand Down Expand Up @@ -71,3 +73,80 @@ 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,
UserWasms: make(map[common.Hash]UserWasmJson),
DebugChain: entry.DebugChain,
}
for _, binfo := range entry.BatchInfo {
encData := base64.StdEncoding.EncodeToString(binfo.Data)
res.BatchInfo = append(res.BatchInfo, BatchInfoJson{Number: binfo.Number, DataB64: encData})
}
for moduleHash, info := range entry.UserWasms {
encWasm := UserWasmJson{
Asm: base64.StdEncoding.EncodeToString(info.Asm),
Module: base64.StdEncoding.EncodeToString(info.Module),
}
res.UserWasms[moduleHash] = encWasm
}
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,
UserWasms: make(state.UserWasms),
DebugChain: entry.DebugChain,
}
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)
}
for moduleHash, info := range entry.UserWasms {
asm, err := base64.StdEncoding.DecodeString(info.Asm)
if err != nil {
return nil, err
}
module, err := base64.StdEncoding.DecodeString(info.Module)
if err != nil {
return nil, err
}
decInfo := state.ActivatedWasm{
Asm: asm,
Module: module,
}
valInput.UserWasms[moduleHash] = decInfo
}
return valInput, nil
}
56 changes: 3 additions & 53 deletions validator/valnode/validation_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ import (
"time"

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

"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 @@ -34,7 +32,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 @@ -78,7 +76,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 @@ -116,7 +114,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 @@ -190,51 +188,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,
UserWasms: make(state.UserWasms),
DebugChain: entry.DebugChain,
}
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)
}
for moduleHash, info := range entry.UserWasms {
asm, err := base64.StdEncoding.DecodeString(info.Asm)
if err != nil {
return nil, err
}
module, err := base64.StdEncoding.DecodeString(info.Module)
if err != nil {
return nil, err
}
decInfo := state.ActivatedWasm{
Asm: asm,
Module: module,
}
valInput.UserWasms[moduleHash] = decInfo
}
return valInput, nil
}

0 comments on commit 0146d77

Please sign in to comment.