github.com/klaytn/klaytn@v1.10.2/blockchain/vm/logger_json.go (about) 1 // Modifications Copyright 2018 The klaytn Authors 2 // Copyright 2017 The go-ethereum Authors 3 // This file is part of go-ethereum. 4 // 5 // go-ethereum is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // go-ethereum is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU General Public License for more details. 14 // 15 // You should have received a copy of the GNU General Public License 16 // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. 17 // 18 // This file is derived from cmd/evm/json_logger.go (2018/06/04). 19 // Modified and improved for the klaytn development. 20 21 package vm 22 23 import ( 24 "encoding/json" 25 "io" 26 "math/big" 27 "time" 28 29 "github.com/klaytn/klaytn/common" 30 "github.com/klaytn/klaytn/common/math" 31 ) 32 33 type JSONLogger struct { 34 encoder *json.Encoder 35 cfg *LogConfig 36 } 37 38 // NewJSONLogger creates a new EVM tracer that prints execution steps as JSON objects 39 // into the provided stream. 40 func NewJSONLogger(cfg *LogConfig, writer io.Writer) *JSONLogger { 41 return &JSONLogger{json.NewEncoder(writer), cfg} 42 } 43 44 func (l *JSONLogger) CaptureStart(from common.Address, to common.Address, create bool, input []byte, gas uint64, value *big.Int) error { 45 return nil 46 } 47 48 // CaptureState outputs state information on the logger. 49 func (l *JSONLogger) CaptureState(env *EVM, pc uint64, op OpCode, gas, cost uint64, memory *Memory, stack *Stack, contract *Contract, depth int, err error) error { 50 log := StructLog{ 51 Pc: pc, 52 Op: op, 53 Gas: gas, 54 GasCost: cost, 55 MemorySize: memory.Len(), 56 Storage: nil, 57 Depth: depth, 58 RefundCounter: env.StateDB.GetRefund(), 59 Err: err, 60 } 61 if !l.cfg.DisableMemory { 62 log.Memory = memory.Data() 63 } 64 if !l.cfg.DisableStack { 65 log.Stack = stack.Data() 66 } 67 return l.encoder.Encode(log) 68 } 69 70 // CaptureFault outputs state information on the logger. 71 func (l *JSONLogger) CaptureFault(env *EVM, pc uint64, op OpCode, gas, cost uint64, memory *Memory, stack *Stack, contract *Contract, depth int, err error) error { 72 return nil 73 } 74 75 // CaptureEnd is triggered at end of execution. 76 func (l *JSONLogger) CaptureEnd(output []byte, gasUsed uint64, t time.Duration, err error) error { 77 type endLog struct { 78 Output string `json:"output"` 79 GasUsed math.HexOrDecimal64 `json:"gasUsed"` 80 Time time.Duration `json:"time"` 81 Err string `json:"error,omitempty"` 82 } 83 if err != nil { 84 return l.encoder.Encode(endLog{common.Bytes2Hex(output), math.HexOrDecimal64(gasUsed), t, err.Error()}) 85 } 86 return l.encoder.Encode(endLog{common.Bytes2Hex(output), math.HexOrDecimal64(gasUsed), t, ""}) 87 }