cmd/evm: fixups for json logger

This commit is contained in:
Felix Lange 2017-06-07 15:01:35 +02:00
parent 373f587d8a
commit 0e250abf32
3 changed files with 19 additions and 25 deletions

View file

@ -26,23 +26,17 @@ import (
"github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/core/vm"
) )
// JSONLogger merely contains a writer, and immediately outputs to that channel,
// instead of collecting logs
type JSONLogger struct { type JSONLogger struct {
encoder *json.Encoder encoder *json.Encoder
} }
// NewJSONLogger returns a new JSON logger
func NewJSONLogger(writer io.Writer) *JSONLogger { func NewJSONLogger(writer io.Writer) *JSONLogger {
logger := &JSONLogger{ return &JSONLogger{json.NewEncoder(writer)}
encoder: json.NewEncoder(writer),
}
return logger
} }
// CaptureState outputs state information on the logger // 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 { 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 {
log := vm.StructLog{ return l.encoder.Encode(vm.StructLog{
Pc: pc, Pc: pc,
Op: op, Op: op,
Gas: gas + cost, Gas: gas + cost,
@ -51,19 +45,16 @@ func (l *JSONLogger) CaptureState(env *vm.EVM, pc uint64, op vm.OpCode, gas, cos
Stack: stack.Data(), Stack: stack.Data(),
Storage: nil, Storage: nil,
Depth: depth, Depth: depth,
Err: err} Err: err,
return l.encoder.Encode(log) })
} }
// CaptureEnd is triggered at end of execution // CaptureEnd is triggered at end of execution.
func (l *JSONLogger) CaptureEnd(output []byte, gasUsed uint64, t time.Duration) error { func (l *JSONLogger) CaptureEnd(output []byte, gasUsed uint64, t time.Duration) error {
type endLog struct { type endLog struct {
Output string `json:"output"` Output string `json:"output"`
GasUsed math.HexOrDecimal64 `json:"gasUsed"` GasUsed math.HexOrDecimal64 `json:"gasUsed"`
Time time.Duration `json:"time"` Time time.Duration `json:"time"`
} }
return l.encoder.Encode(endLog{common.Bytes2Hex(output), math.HexOrDecimal64(gasUsed), t})
log := endLog{common.Bytes2Hex(output), math.HexOrDecimal64(gasUsed), t}
return l.encoder.Encode(log)
} }

View file

@ -7,6 +7,7 @@ import (
"math/big" "math/big"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/common/math" "github.com/ethereum/go-ethereum/common/math"
) )
@ -16,13 +17,13 @@ func (s StructLog) MarshalJSON() ([]byte, error) {
Op OpCode `json:"op"` Op OpCode `json:"op"`
Gas math.HexOrDecimal64 `json:"gas"` Gas math.HexOrDecimal64 `json:"gas"`
GasCost math.HexOrDecimal64 `json:"gasCost"` GasCost math.HexOrDecimal64 `json:"gasCost"`
Memory []byte `json:"memory"` Memory hexutil.Bytes `json:"memory"`
Stack []*math.HexOrDecimal256 `json:"stack"` Stack []*math.HexOrDecimal256 `json:"stack"`
Storage map[common.Hash]common.Hash `json:-` Storage map[common.Hash]common.Hash `json:"-"`
Depth int `json:"depth"` Depth int `json:"depth"`
Err error `json:"error"` Err error `json:"error"`
OpName string `json:"opName"` OpName string `json:"opName"`
MemorySize string `json:"memSize"` MemorySize int `json:"memSize"`
} }
var enc StructLog var enc StructLog
enc.Pc = s.Pc enc.Pc = s.Pc
@ -50,9 +51,9 @@ func (s *StructLog) UnmarshalJSON(input []byte) error {
Op *OpCode `json:"op"` Op *OpCode `json:"op"`
Gas *math.HexOrDecimal64 `json:"gas"` Gas *math.HexOrDecimal64 `json:"gas"`
GasCost *math.HexOrDecimal64 `json:"gasCost"` GasCost *math.HexOrDecimal64 `json:"gasCost"`
Memory []byte `json:"memory"` Memory hexutil.Bytes `json:"memory"`
Stack []*math.HexOrDecimal256 `json:"stack"` Stack []*math.HexOrDecimal256 `json:"stack"`
Storage map[common.Hash]common.Hash `json:-` Storage map[common.Hash]common.Hash `json:"-"`
Depth *int `json:"depth"` Depth *int `json:"depth"`
Err *error `json:"error"` Err *error `json:"error"`
} }

View file

@ -24,6 +24,7 @@ import (
"time" "time"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/common/math" "github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
) )
@ -58,7 +59,7 @@ type StructLog struct {
GasCost uint64 `json:"gasCost"` GasCost uint64 `json:"gasCost"`
Memory []byte `json:"memory"` Memory []byte `json:"memory"`
Stack []*big.Int `json:"stack"` Stack []*big.Int `json:"stack"`
Storage map[common.Hash]common.Hash `json:-` Storage map[common.Hash]common.Hash `json:"-"`
Depth int `json:"depth"` Depth int `json:"depth"`
Err error `json:"error"` Err error `json:"error"`
} }
@ -67,16 +68,17 @@ func (s *StructLog) OpName() string {
return s.Op.String() return s.Op.String()
} }
func (s *StructLog) MemorySize() string { func (s *StructLog) MemorySize() int {
return fmt.Sprintf("%v", len(s.Memory)) return len(s.Memory)
} }
type structLogMarshaling struct { type structLogMarshaling struct {
Stack []*math.HexOrDecimal256 Stack []*math.HexOrDecimal256
Gas math.HexOrDecimal64 Gas math.HexOrDecimal64
GasCost math.HexOrDecimal64 GasCost math.HexOrDecimal64
Memory hexutil.Bytes
OpName string `json:"opName"` OpName string `json:"opName"`
MemorySize string `json:"memSize"` MemorySize int `json:"memSize"`
} }
// Tracer is used to collect execution traces from an EVM transaction // Tracer is used to collect execution traces from an EVM transaction