github.com/ethw3/go-ethereuma@v0.0.0-20221013053120-c14602a4c23c/eth/tracers/logger/logger_json.go (about) 1 // Copyright 2021 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum 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-ethereum 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-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package logger 18 19 import ( 20 "encoding/json" 21 "io" 22 "math/big" 23 "time" 24 25 "github.com/ethw3/go-ethereuma/common" 26 "github.com/ethw3/go-ethereuma/common/math" 27 "github.com/ethw3/go-ethereuma/core/vm" 28 ) 29 30 type JSONLogger struct { 31 encoder *json.Encoder 32 cfg *Config 33 env *vm.EVM 34 } 35 36 // NewJSONLogger creates a new EVM tracer that prints execution steps as JSON objects 37 // into the provided stream. 38 func NewJSONLogger(cfg *Config, writer io.Writer) *JSONLogger { 39 l := &JSONLogger{encoder: json.NewEncoder(writer), cfg: cfg} 40 if l.cfg == nil { 41 l.cfg = &Config{} 42 } 43 return l 44 } 45 46 func (l *JSONLogger) CaptureStart(env *vm.EVM, from, to common.Address, create bool, input []byte, gas uint64, value *big.Int) { 47 l.env = env 48 } 49 50 func (l *JSONLogger) CaptureFault(pc uint64, op vm.OpCode, gas uint64, cost uint64, scope *vm.ScopeContext, depth int, err error) { 51 // TODO: Add rData to this interface as well 52 l.CaptureState(pc, op, gas, cost, scope, nil, depth, err) 53 } 54 55 // CaptureState outputs state information on the logger. 56 func (l *JSONLogger) CaptureState(pc uint64, op vm.OpCode, gas, cost uint64, scope *vm.ScopeContext, rData []byte, depth int, err error) { 57 memory := scope.Memory 58 stack := scope.Stack 59 60 log := StructLog{ 61 Pc: pc, 62 Op: op, 63 Gas: gas, 64 GasCost: cost, 65 MemorySize: memory.Len(), 66 Depth: depth, 67 RefundCounter: l.env.StateDB.GetRefund(), 68 Err: err, 69 } 70 if l.cfg.EnableMemory { 71 log.Memory = memory.Data() 72 } 73 if !l.cfg.DisableStack { 74 log.Stack = stack.Data() 75 } 76 if l.cfg.EnableReturnData { 77 log.ReturnData = rData 78 } 79 l.encoder.Encode(log) 80 } 81 82 // CaptureEnd is triggered at end of execution. 83 func (l *JSONLogger) CaptureEnd(output []byte, gasUsed uint64, t time.Duration, err error) { 84 type endLog struct { 85 Output string `json:"output"` 86 GasUsed math.HexOrDecimal64 `json:"gasUsed"` 87 Time time.Duration `json:"time"` 88 Err string `json:"error,omitempty"` 89 } 90 var errMsg string 91 if err != nil { 92 errMsg = err.Error() 93 } 94 l.encoder.Encode(endLog{common.Bytes2Hex(output), math.HexOrDecimal64(gasUsed), t, errMsg}) 95 } 96 97 func (l *JSONLogger) CaptureEnter(typ vm.OpCode, from common.Address, to common.Address, input []byte, gas uint64, value *big.Int) { 98 } 99 100 func (l *JSONLogger) CaptureExit(output []byte, gasUsed uint64, err error) {} 101 102 func (l *JSONLogger) CaptureTxStart(gasLimit uint64) {} 103 104 func (l *JSONLogger) CaptureTxEnd(restGas uint64) {}