Skip to content

Commit

Permalink
fix trace, forced batches, decodeTxs overflow, grpc limit 0xPolygonHe…
Browse files Browse the repository at this point in the history
  • Loading branch information
ARR552 authored Jun 6, 2023
1 parent f2f61f6 commit 19ed608
Show file tree
Hide file tree
Showing 27 changed files with 1,745 additions and 295 deletions.
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ services:
zkevm-prover:
container_name: zkevm-prover
restart: unless-stopped
image: hermeznetwork/zkevm-prover:v1.1.3-fork.4
image: hermeznetwork/zkevm-prover:v1.1.4-fork.4
depends_on:
zkevm-state-db:
condition: service_healthy
Expand Down
4 changes: 4 additions & 0 deletions jsonrpc/endpoints_debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@ func (d *DebugEndpoints) buildStructLogs(stateStructLogs []instrumentation.Struc
op := structLog.Op
if op == "SHA3" {
op = "KECCAK256"
} else if op == "STOP" && structLog.Pc == 0 {
// this stop is generated for calls with single
// step(no depth increase) and must be ignored
continue
}

structLogRes := StructLogRes{
Expand Down
63 changes: 40 additions & 23 deletions state/converters.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,11 @@ func (s *State) convertToProcessTransactionResponse(txs []types.Transaction, res
result.Logs = convertToLog(response.Logs)
result.ChangesStateRoot = IsStateRootChanged(response.Error)
result.ExecutionTrace = *trace
result.CallTrace = convertToExecutorTrace(response.CallTrace)
callTrace, err := convertToExecutorTrace(response.CallTrace)
if err != nil {
return nil, err
}
result.CallTrace = *callTrace
result.Tx = txs[i]

_, err = DecodeTx(common.Bytes2Hex(response.GetRlpTx()))
Expand Down Expand Up @@ -256,66 +260,79 @@ func convertToProperMap(responses map[string]string) map[common.Hash]common.Hash
return results
}

func convertToExecutorTrace(callTrace *pb.CallTrace) instrumentation.ExecutorTrace {
func convertToExecutorTrace(callTrace *pb.CallTrace) (*instrumentation.ExecutorTrace, error) {
trace := new(instrumentation.ExecutorTrace)
if callTrace != nil {
trace.Context = convertToContext(callTrace.Context)
trace.Steps = convertToInstrumentationSteps(callTrace.Steps)
steps, err := convertToInstrumentationSteps(callTrace.Steps)
if err != nil {
return nil, err
}
trace.Steps = steps
}

return *trace
return trace, nil
}

func convertToContext(context *pb.TransactionContext) instrumentation.Context {
return instrumentation.Context{
Type: context.Type,
From: context.From,
To: context.To,
Input: string(context.Data),
Gas: fmt.Sprint(context.Gas),
Value: context.Value,
Output: string(context.Output),
Input: context.Data,
Gas: context.Gas,
Value: hex.DecodeBig(context.Value),
Output: context.Output,
GasPrice: context.GasPrice,
OldStateRoot: string(context.OldStateRoot),
OldStateRoot: common.BytesToHash(context.OldStateRoot),
Time: uint64(context.ExecutionTime),
GasUsed: fmt.Sprint(context.GasUsed),
GasUsed: context.GasUsed,
}
}

func convertToInstrumentationSteps(responses []*pb.TransactionStep) []instrumentation.Step {
func convertToInstrumentationSteps(responses []*pb.TransactionStep) ([]instrumentation.Step, error) {
results := make([]instrumentation.Step, 0, len(responses))
for _, response := range responses {
step := new(instrumentation.Step)
step.StateRoot = string(response.StateRoot)
step.StateRoot = common.BytesToHash(response.StateRoot)
step.Depth = int(response.Depth)
step.Pc = response.Pc
step.Gas = fmt.Sprint(response.Gas)
step.Gas = response.Gas
step.OpCode = fakevm.OpCode(response.Op).String()
step.Refund = fmt.Sprint(response.GasRefund)
step.Op = fmt.Sprint(response.Op)
step.Op = uint64(response.Op)
err := executor.RomErr(response.Error)
if err != nil {
step.Error = err.Error()
step.Error = err
}
step.Contract = convertToInstrumentationContract(response.Contract)
step.GasCost = fmt.Sprint(response.GasCost)
step.Stack = response.Stack
step.GasCost = response.GasCost
step.Stack = make([]*big.Int, 0, len(response.Stack))
for _, s := range response.Stack {
bi, ok := new(big.Int).SetString(s, hex.Base)
if !ok {
log.Debugf("error while parsing stack valueBigInt")
return nil, ErrParsingExecutorTrace
}
step.Stack = append(step.Stack, bi)
}

step.Memory = make([]byte, len(response.Memory))
copy(step.Memory, response.Memory)
step.ReturnData = make([]byte, len(response.ReturnData))
copy(step.ReturnData, response.ReturnData)
results = append(results, *step)
}
return results
return results, nil
}

func convertToInstrumentationContract(response *pb.Contract) instrumentation.Contract {
return instrumentation.Contract{
Address: response.Address,
Caller: response.Caller,
Value: response.Value,
Input: string(response.Data),
Gas: fmt.Sprint(response.Gas),
Address: common.HexToAddress(response.Address),
Caller: common.HexToAddress(response.Caller),
Value: hex.DecodeBig(response.Value),
Input: response.Data,
Gas: response.Gas,
}
}

Expand Down
2 changes: 1 addition & 1 deletion state/runtime/executor/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"google.golang.org/grpc/credentials/insecure"
)

const maxMsgSize = 100000000
const maxMsgSize = 2 * 1024 * 1024 * 1024

// NewExecutorClient is the executor client constructor.
func NewExecutorClient(ctx context.Context, c Config) (pb.ExecutorServiceClient, *grpc.ClientConn, context.CancelFunc) {
Expand Down
72 changes: 41 additions & 31 deletions state/runtime/instrumentation/executortrace.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package instrumentation

import (
"math/big"

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

// ExecutorTrace contents executor traces.
type ExecutorTrace struct {
Context Context `json:"context"`
Expand All @@ -8,48 +14,52 @@ type ExecutorTrace struct {

// Context is the trace context.
type Context struct {
Type string `json:"type"`
From string `json:"from"`
To string `json:"to"`
Input string `json:"input"`
Gas string `json:"gas"`
Value string `json:"value"`
Output string `json:"output"`
Nonce uint64 `json:"nonce"`
GasPrice string `json:"gasPrice"`
// ChainID uint64 `json:"chainId"`
OldStateRoot string `json:"oldStateRoot"`
Time uint64 `json:"time"`
GasUsed string `json:"gasUsed"`
Type string `json:"type"`
From string `json:"from"`
To string `json:"to"`
Input []byte `json:"input"`
Gas uint64 `json:"gas"`
Value *big.Int `json:"value"`
Output []byte `json:"output"`
Nonce uint64 `json:"nonce"`
GasPrice string `json:"gasPrice"`
OldStateRoot common.Hash `json:"oldStateRoot"`
Time uint64 `json:"time"`
GasUsed uint64 `json:"gasUsed"`
}

// Step is a trace step.
type Step struct {
StateRoot string `json:"stateRoot"`
Depth int `json:"depth"`
Pc uint64 `json:"pc"`
Gas string `json:"gas"`
OpCode string `json:"opcode"`
Refund string `json:"refund"`
Op string `json:"op"`
Error string `json:"error"`
Contract Contract `json:"contract"`
GasCost string `json:"gasCost"`
Stack []string `json:"stack"`
Memory []byte `json:"memory"`
ReturnData []byte `json:"returnData"`
StateRoot common.Hash `json:"stateRoot"`
Depth int `json:"depth"`
Pc uint64 `json:"pc"`
Gas uint64 `json:"gas"`
OpCode string `json:"opcode"`
Refund string `json:"refund"`
Op uint64 `json:"op"`
Error error `json:"error"`
Contract Contract `json:"contract"`
GasCost uint64 `json:"gasCost"`
Stack []*big.Int `json:"stack"`
Memory []byte `json:"memory"`
ReturnData []byte `json:"returnData"`
}

// Contract represents a contract in the trace.
type Contract struct {
Address string `json:"address"`
Caller string `json:"caller"`
Value string `json:"value"`
Input string `json:"input"`
Gas string `json:"gas"`
Address common.Address `json:"address"`
Caller common.Address `json:"caller"`
Value *big.Int `json:"value"`
Input []byte `json:"input"`
Gas uint64 `json:"gas"`
}

// Tracer represents the executor tracer.
type Tracer struct {
Code string `json:"tracer"`
}

type InternalTxContext struct {
OpCode string
RemainingGas uint64
}
44 changes: 44 additions & 0 deletions state/stack.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package state

import (
"errors"
"sync"
)

// ErrStackEmpty returned when Pop is called and the stack is empty
var ErrStackEmpty = errors.New("Empty Stack")

// Stack is a thread safe stack data structure implementation implementing generics
type Stack[T any] struct {
lock sync.Mutex
items []T
}

// NewStack creates a new stack
func NewStack[T any]() *Stack[T] {
return &Stack[T]{sync.Mutex{}, make([]T, 0)}
}

// Push adds an item to the stack
func (s *Stack[T]) Push(v T) {
s.lock.Lock()
defer s.lock.Unlock()

s.items = append(s.items, v)
}

// Pop removes and returns the last item added to the stack
func (s *Stack[T]) Pop() (T, error) {
s.lock.Lock()
defer s.lock.Unlock()

size := len(s.items)
if size == 0 {
var r T
return r, ErrStackEmpty
}

res := s.items[size-1]
s.items = s.items[:size-1]
return res, nil
}
Loading

0 comments on commit 19ed608

Please sign in to comment.