mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-13 07:23:46 +00:00
core/vm: use json codec to generate structlog json
This commit is contained in:
parent
581dd37fa9
commit
373f587d8a
4 changed files with 187 additions and 83 deletions
69
cmd/evm/json_logger.go
Normal file
69
cmd/evm/json_logger.go
Normal file
|
|
@ -0,0 +1,69 @@
|
||||||
|
// 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"
|
||||||
|
)
|
||||||
|
|
||||||
|
// JSONLogger merely contains a writer, and immediately outputs to that channel,
|
||||||
|
// instead of collecting logs
|
||||||
|
type JSONLogger struct {
|
||||||
|
encoder *json.Encoder
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewJSONLogger returns a new JSON logger
|
||||||
|
func NewJSONLogger(writer io.Writer) *JSONLogger {
|
||||||
|
logger := &JSONLogger{
|
||||||
|
encoder: json.NewEncoder(writer),
|
||||||
|
}
|
||||||
|
return 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 {
|
||||||
|
log := vm.StructLog{
|
||||||
|
Pc: pc,
|
||||||
|
Op: op,
|
||||||
|
Gas: gas + cost,
|
||||||
|
GasCost: cost,
|
||||||
|
Memory: memory.Data(),
|
||||||
|
Stack: stack.Data(),
|
||||||
|
Storage: nil,
|
||||||
|
Depth: depth,
|
||||||
|
Err: err}
|
||||||
|
return l.encoder.Encode(log)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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"`
|
||||||
|
}
|
||||||
|
|
||||||
|
log := endLog{common.Bytes2Hex(output), math.HexOrDecimal64(gasUsed), t}
|
||||||
|
return l.encoder.Encode(log)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -82,7 +82,7 @@ func runCmd(ctx *cli.Context) error {
|
||||||
sender = common.StringToAddress("sender")
|
sender = common.StringToAddress("sender")
|
||||||
)
|
)
|
||||||
if ctx.GlobalBool(MachineFlag.Name) {
|
if ctx.GlobalBool(MachineFlag.Name) {
|
||||||
tracer = vm.NewJSONLogger(os.Stdout)
|
tracer = NewJSONLogger(os.Stdout)
|
||||||
} else if ctx.GlobalBool(DebugFlag.Name) {
|
} else if ctx.GlobalBool(DebugFlag.Name) {
|
||||||
debugLogger = vm.NewStructLogger(nil)
|
debugLogger = vm.NewStructLogger(nil)
|
||||||
tracer = debugLogger
|
tracer = debugLogger
|
||||||
|
|
|
||||||
94
core/vm/gen_structlog.go
Normal file
94
core/vm/gen_structlog.go
Normal file
|
|
@ -0,0 +1,94 @@
|
||||||
|
// 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/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 []byte `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 string `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 []byte `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
|
||||||
|
}
|
||||||
|
|
@ -18,7 +18,6 @@ package vm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"math/big"
|
"math/big"
|
||||||
|
|
@ -51,60 +50,33 @@ type LogConfig struct {
|
||||||
|
|
||||||
// StructLog is emitted to the EVM each cycle and lists information about the current internal state
|
// StructLog is emitted to the EVM each cycle and lists information about the current internal state
|
||||||
// prior to the execution of the statement.
|
// prior to the execution of the statement.
|
||||||
|
//go:generate gencodec -type StructLog -field-override structLogMarshaling -out gen_structlog.go
|
||||||
type StructLog struct {
|
type StructLog struct {
|
||||||
Pc uint64
|
Pc uint64 `json:"pc"`
|
||||||
Op OpCode
|
Op OpCode `json:"op"`
|
||||||
Gas uint64
|
Gas uint64 `json:"gas"`
|
||||||
GasCost uint64
|
GasCost uint64 `json:"gasCost"`
|
||||||
Memory []byte
|
Memory []byte `json:"memory"`
|
||||||
Stack []*big.Int
|
Stack []*big.Int `json:"stack"`
|
||||||
Storage map[common.Hash]common.Hash
|
Storage map[common.Hash]common.Hash `json:-`
|
||||||
Depth int
|
Depth int `json:"depth"`
|
||||||
Err error
|
Err error `json:"error"`
|
||||||
}
|
|
||||||
type StructLogJsonOut struct {
|
|
||||||
Pc uint64 `json:"pc"`
|
|
||||||
Op OpCode `json:"op"`
|
|
||||||
OpName string `json:"opName"`
|
|
||||||
Gas math.HexOrDecimal64 `json:"gas"`
|
|
||||||
GasCost math.HexOrDecimal64 `json:"gasCost"`
|
|
||||||
Memory string `json:"memory"`
|
|
||||||
Stack hexArray `json:"stack"`
|
|
||||||
//Storage map[common.Hash]common.Hash `json:"storage"`
|
|
||||||
Depth int `json:"depth"`
|
|
||||||
//Err error
|
|
||||||
}
|
|
||||||
type hexArray []*big.Int
|
|
||||||
|
|
||||||
// MarshalJSON encodes the memory as 32-byte hex strings instead of bigints
|
|
||||||
func (a hexArray) MarshalJSON() ([]byte, error) {
|
|
||||||
|
|
||||||
if items := ([]*big.Int)(a); len(items) > 0 {
|
|
||||||
hexitems := make([]*math.HexOrDecimal256, len(items))
|
|
||||||
for i, item := range items {
|
|
||||||
x := math.HexOrDecimal256(*item)
|
|
||||||
hexitems[i] = &x
|
|
||||||
}
|
|
||||||
return json.Marshal(hexitems)
|
|
||||||
}
|
|
||||||
return []byte("[]"), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// MarshalJSON encodes StructLog for json output
|
func (s *StructLog) OpName() string {
|
||||||
func (s StructLog) MarshalJSON() ([]byte, error) {
|
return s.Op.String()
|
||||||
var enc StructLogJsonOut
|
}
|
||||||
enc.Pc = s.Pc
|
|
||||||
enc.Op = s.Op
|
|
||||||
enc.OpName = s.Op.String()
|
|
||||||
enc.Gas = math.HexOrDecimal64(s.Gas)
|
|
||||||
enc.GasCost = math.HexOrDecimal64(s.GasCost)
|
|
||||||
//enc.Memory = fmt.Sprintf("0x%v", len(common.Bytes2Hex(s.Memory))/2)
|
|
||||||
enc.Memory = fmt.Sprintf("%v bytes", len(s.Memory))
|
|
||||||
|
|
||||||
enc.Stack = s.Stack
|
func (s *StructLog) MemorySize() string {
|
||||||
//enc.Storage = s.Storage
|
return fmt.Sprintf("%v", len(s.Memory))
|
||||||
enc.Depth = s.Depth
|
}
|
||||||
return json.Marshal(&enc)
|
|
||||||
|
type structLogMarshaling struct {
|
||||||
|
Stack []*math.HexOrDecimal256
|
||||||
|
Gas math.HexOrDecimal64
|
||||||
|
GasCost math.HexOrDecimal64
|
||||||
|
OpName string `json:"opName"`
|
||||||
|
MemorySize string `json:"memSize"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tracer is used to collect execution traces from an EVM transaction
|
// Tracer is used to collect execution traces from an EVM transaction
|
||||||
|
|
@ -129,12 +101,6 @@ type StructLogger struct {
|
||||||
changedValues map[common.Address]Storage
|
changedValues map[common.Address]Storage
|
||||||
}
|
}
|
||||||
|
|
||||||
// JSONLogger merely contains a writer, and immediately outputs to that channel,
|
|
||||||
// instead of collecting logs
|
|
||||||
type JSONLogger struct {
|
|
||||||
encoder *json.Encoder
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewStructLogger returns a new logger
|
// NewStructLogger returns a new logger
|
||||||
func NewStructLogger(cfg *LogConfig) *StructLogger {
|
func NewStructLogger(cfg *LogConfig) *StructLogger {
|
||||||
logger := &StructLogger{
|
logger := &StructLogger{
|
||||||
|
|
@ -146,31 +112,6 @@ func NewStructLogger(cfg *LogConfig) *StructLogger {
|
||||||
return logger
|
return logger
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewJSONLogger returns a new JSON logger
|
|
||||||
func NewJSONLogger(writer io.Writer) *JSONLogger {
|
|
||||||
logger := &JSONLogger{
|
|
||||||
encoder: json.NewEncoder(writer),
|
|
||||||
}
|
|
||||||
return logger
|
|
||||||
}
|
|
||||||
|
|
||||||
// CaptureState outputs state information on the logger
|
|
||||||
func (l *JSONLogger) CaptureState(env *EVM, pc uint64, op OpCode, gas, cost uint64, memory *Memory, stack *Stack, contract *Contract, depth int, err error) error {
|
|
||||||
log := StructLog{pc, op, gas + cost, cost, memory.Data(), stack.Data(), nil, env.depth, err}
|
|
||||||
return l.encoder.Encode(log)
|
|
||||||
}
|
|
||||||
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"`
|
|
||||||
}
|
|
||||||
|
|
||||||
log := endLog{common.Bytes2Hex(output), math.HexOrDecimal64(gasUsed), t}
|
|
||||||
return l.encoder.Encode(log)
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// CaptureState logs a new structured log message and pushes it out to the environment
|
// CaptureState logs a new structured log message and pushes it out to the environment
|
||||||
//
|
//
|
||||||
// CaptureState also tracks SSTORE ops to track dirty values.
|
// CaptureState also tracks SSTORE ops to track dirty values.
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue