mirror of https://github.com/ethereum/go-ethereum
cmd/evm: add --prestate, --sender, --json flags for fuzzing (#14476)
parent
bc24b7a912
commit
80f7c6c299
@ -0,0 +1,60 @@ |
|||||||
|
// Copyright 2017 The go-ethereum Authors
|
||||||
|
// This file is part of the go-ethereum library.
|
||||||
|
//
|
||||||
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU Lesser General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Lesser General Public License
|
||||||
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
package main |
||||||
|
|
||||||
|
import ( |
||||||
|
"encoding/json" |
||||||
|
"io" |
||||||
|
"time" |
||||||
|
|
||||||
|
"github.com/ethereum/go-ethereum/common" |
||||||
|
"github.com/ethereum/go-ethereum/common/math" |
||||||
|
"github.com/ethereum/go-ethereum/core/vm" |
||||||
|
) |
||||||
|
|
||||||
|
type JSONLogger struct { |
||||||
|
encoder *json.Encoder |
||||||
|
} |
||||||
|
|
||||||
|
func NewJSONLogger(writer io.Writer) *JSONLogger { |
||||||
|
return &JSONLogger{json.NewEncoder(writer)} |
||||||
|
} |
||||||
|
|
||||||
|
// CaptureState outputs state information on the logger.
|
||||||
|
func (l *JSONLogger) CaptureState(env *vm.EVM, pc uint64, op vm.OpCode, gas, cost uint64, memory *vm.Memory, stack *vm.Stack, contract *vm.Contract, depth int, err error) error { |
||||||
|
return l.encoder.Encode(vm.StructLog{ |
||||||
|
Pc: pc, |
||||||
|
Op: op, |
||||||
|
Gas: gas + cost, |
||||||
|
GasCost: cost, |
||||||
|
Memory: memory.Data(), |
||||||
|
Stack: stack.Data(), |
||||||
|
Storage: nil, |
||||||
|
Depth: depth, |
||||||
|
Err: err, |
||||||
|
}) |
||||||
|
} |
||||||
|
|
||||||
|
// CaptureEnd is triggered at end of execution.
|
||||||
|
func (l *JSONLogger) CaptureEnd(output []byte, gasUsed uint64, t time.Duration) error { |
||||||
|
type endLog struct { |
||||||
|
Output string `json:"output"` |
||||||
|
GasUsed math.HexOrDecimal64 `json:"gasUsed"` |
||||||
|
Time time.Duration `json:"time"` |
||||||
|
} |
||||||
|
return l.encoder.Encode(endLog{common.Bytes2Hex(output), math.HexOrDecimal64(gasUsed), t}) |
||||||
|
} |
@ -0,0 +1,95 @@ |
|||||||
|
// Code generated by github.com/fjl/gencodec. DO NOT EDIT.
|
||||||
|
|
||||||
|
package vm |
||||||
|
|
||||||
|
import ( |
||||||
|
"encoding/json" |
||||||
|
"math/big" |
||||||
|
|
||||||
|
"github.com/ethereum/go-ethereum/common" |
||||||
|
"github.com/ethereum/go-ethereum/common/hexutil" |
||||||
|
"github.com/ethereum/go-ethereum/common/math" |
||||||
|
) |
||||||
|
|
||||||
|
func (s StructLog) MarshalJSON() ([]byte, error) { |
||||||
|
type StructLog struct { |
||||||
|
Pc uint64 `json:"pc"` |
||||||
|
Op OpCode `json:"op"` |
||||||
|
Gas math.HexOrDecimal64 `json:"gas"` |
||||||
|
GasCost math.HexOrDecimal64 `json:"gasCost"` |
||||||
|
Memory hexutil.Bytes `json:"memory"` |
||||||
|
Stack []*math.HexOrDecimal256 `json:"stack"` |
||||||
|
Storage map[common.Hash]common.Hash `json:"-"` |
||||||
|
Depth int `json:"depth"` |
||||||
|
Err error `json:"error"` |
||||||
|
OpName string `json:"opName"` |
||||||
|
MemorySize int `json:"memSize"` |
||||||
|
} |
||||||
|
var enc StructLog |
||||||
|
enc.Pc = s.Pc |
||||||
|
enc.Op = s.Op |
||||||
|
enc.Gas = math.HexOrDecimal64(s.Gas) |
||||||
|
enc.GasCost = math.HexOrDecimal64(s.GasCost) |
||||||
|
enc.Memory = s.Memory |
||||||
|
if s.Stack != nil { |
||||||
|
enc.Stack = make([]*math.HexOrDecimal256, len(s.Stack)) |
||||||
|
for k, v := range s.Stack { |
||||||
|
enc.Stack[k] = (*math.HexOrDecimal256)(v) |
||||||
|
} |
||||||
|
} |
||||||
|
enc.Storage = s.Storage |
||||||
|
enc.Depth = s.Depth |
||||||
|
enc.Err = s.Err |
||||||
|
enc.OpName = s.OpName() |
||||||
|
enc.MemorySize = s.MemorySize() |
||||||
|
return json.Marshal(&enc) |
||||||
|
} |
||||||
|
|
||||||
|
func (s *StructLog) UnmarshalJSON(input []byte) error { |
||||||
|
type StructLog struct { |
||||||
|
Pc *uint64 `json:"pc"` |
||||||
|
Op *OpCode `json:"op"` |
||||||
|
Gas *math.HexOrDecimal64 `json:"gas"` |
||||||
|
GasCost *math.HexOrDecimal64 `json:"gasCost"` |
||||||
|
Memory hexutil.Bytes `json:"memory"` |
||||||
|
Stack []*math.HexOrDecimal256 `json:"stack"` |
||||||
|
Storage map[common.Hash]common.Hash `json:"-"` |
||||||
|
Depth *int `json:"depth"` |
||||||
|
Err *error `json:"error"` |
||||||
|
} |
||||||
|
var dec StructLog |
||||||
|
if err := json.Unmarshal(input, &dec); err != nil { |
||||||
|
return err |
||||||
|
} |
||||||
|
if dec.Pc != nil { |
||||||
|
s.Pc = *dec.Pc |
||||||
|
} |
||||||
|
if dec.Op != nil { |
||||||
|
s.Op = *dec.Op |
||||||
|
} |
||||||
|
if dec.Gas != nil { |
||||||
|
s.Gas = uint64(*dec.Gas) |
||||||
|
} |
||||||
|
if dec.GasCost != nil { |
||||||
|
s.GasCost = uint64(*dec.GasCost) |
||||||
|
} |
||||||
|
if dec.Memory != nil { |
||||||
|
s.Memory = dec.Memory |
||||||
|
} |
||||||
|
if dec.Stack != nil { |
||||||
|
s.Stack = make([]*big.Int, len(dec.Stack)) |
||||||
|
for k, v := range dec.Stack { |
||||||
|
s.Stack[k] = (*big.Int)(v) |
||||||
|
} |
||||||
|
} |
||||||
|
if dec.Storage != nil { |
||||||
|
s.Storage = dec.Storage |
||||||
|
} |
||||||
|
if dec.Depth != nil { |
||||||
|
s.Depth = *dec.Depth |
||||||
|
} |
||||||
|
if dec.Err != nil { |
||||||
|
s.Err = *dec.Err |
||||||
|
} |
||||||
|
return nil |
||||||
|
} |
Loading…
Reference in new issue