github.com/bloxroute-labs/bor@v0.1.4/core/vm/logger_json.go (about) 1 // Copyright 2017 The go-ethereum Authors 2 // This file is part of go-ethereum. 3 // 4 // go-ethereum is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU 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 // go-ethereum 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 General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with go-ethereum. 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/maticnetwork/bor/common" 26 "github.com/maticnetwork/bor/common/math" 27 ) 28 29 type JSONLogger struct { 30 encoder *json.Encoder 31 cfg *LogConfig 32 } 33 34 // NewJSONLogger creates a new EVM 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, 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 }