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