github.com/core-coin/go-core/v2@v2.1.9/core/vm/logger_json.go (about) 1 // Copyright 2017 by the Authors 2 // This file is part of the go-core library. 3 // 4 // The go-core library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-core library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-core library. If not, see <http://www.gnu.org/licenses/>. 16 17 package vm 18 19 import ( 20 "encoding/json" 21 "io" 22 "math/big" 23 "time" 24 25 "github.com/core-coin/go-core/v2/common" 26 "github.com/core-coin/go-core/v2/common/math" 27 ) 28 29 type JSONLogger struct { 30 encoder *json.Encoder 31 cfg *LogConfig 32 } 33 34 // NewJSONLogger creates a new CVM tracer that prints execution steps as JSON objects 35 // into the provided stream. 36 func NewJSONLogger(cfg *LogConfig, writer io.Writer) *JSONLogger { 37 l := &JSONLogger{json.NewEncoder(writer), cfg} 38 if l.cfg == nil { 39 l.cfg = &LogConfig{} 40 } 41 return l 42 } 43 44 func (l *JSONLogger) CaptureStart(from common.Address, to common.Address, create bool, input []byte, energy uint64, value *big.Int) error { 45 return nil 46 } 47 48 // CaptureState outputs state information on the logger. 49 func (l *JSONLogger) CaptureState(env *CVM, pc uint64, op OpCode, energy, cost uint64, memory *Memory, stack *Stack, rStack *ReturnStack, rData []byte, contract *Contract, depth int, err error) error { 50 log := StructLog{ 51 Pc: pc, 52 Op: op, 53 Energy: energy, 54 EnergyCost: 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 //TODO(@raisty) improve this 66 logstack := make([]*big.Int, len(stack.Data())) 67 for i, item := range stack.Data() { 68 logstack[i] = item.ToBig() 69 } 70 log.Stack = logstack 71 log.ReturnStack = rStack.data 72 } 73 if !l.cfg.DisableReturnData { 74 log.ReturnData = rData 75 } 76 return l.encoder.Encode(log) 77 } 78 79 // CaptureFault outputs state information on the logger. 80 func (l *JSONLogger) CaptureFault(env *CVM, pc uint64, op OpCode, energy, cost uint64, memory *Memory, stack *Stack, rStack *ReturnStack, contract *Contract, depth int, err error) error { 81 return nil 82 } 83 84 // CaptureEnd is triggered at end of execution. 85 func (l *JSONLogger) CaptureEnd(output []byte, energyUsed uint64, t time.Duration, err error) error { 86 type endLog struct { 87 Output string `json:"output"` 88 EnergyUsed math.HexOrDecimal64 `json:"energyUsed"` 89 Time time.Duration `json:"time"` 90 Err string `json:"error,omitempty"` 91 } 92 if err != nil { 93 return l.encoder.Encode(endLog{common.Bytes2Hex(output), math.HexOrDecimal64(energyUsed), t, err.Error()}) 94 } 95 return l.encoder.Encode(endLog{common.Bytes2Hex(output), math.HexOrDecimal64(energyUsed), t, ""}) 96 }