github.com/klaytn/klaytn@v1.12.1/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  
    28  	"github.com/klaytn/klaytn/common"
    29  	"github.com/klaytn/klaytn/common/math"
    30  )
    31  
    32  type JSONLogger struct {
    33  	encoder *json.Encoder
    34  	cfg     *LogConfig
    35  }
    36  
    37  // NewJSONLogger creates a new EVM tracer that prints execution steps as JSON objects
    38  // into the provided stream.
    39  func NewJSONLogger(cfg *LogConfig, writer io.Writer) *JSONLogger {
    40  	return &JSONLogger{json.NewEncoder(writer), cfg}
    41  }
    42  
    43  func (l *JSONLogger) CaptureStart(env *EVM, from common.Address, to common.Address, create bool, input []byte, gas uint64, value *big.Int) {
    44  }
    45  
    46  // CaptureState outputs state information on the logger.
    47  // TODO: Add rData (return data) later
    48  func (l *JSONLogger) CaptureState(env *EVM, pc uint64, op OpCode, gas, cost uint64, scope *ScopeContext, depth int, err error) {
    49  	memory := scope.Memory
    50  	stack := scope.Stack
    51  	log := StructLog{
    52  		Pc:            pc,
    53  		Op:            op,
    54  		Gas:           gas,
    55  		GasCost:       cost,
    56  		MemorySize:    memory.Len(),
    57  		Storage:       nil,
    58  		Depth:         depth,
    59  		RefundCounter: env.StateDB.GetRefund(),
    60  		Err:           err,
    61  	}
    62  	if !l.cfg.DisableMemory {
    63  		log.Memory = memory.Data()
    64  	}
    65  	if !l.cfg.DisableStack {
    66  		// TODO-Klaytn: residual porting PR -> https://github.com/ethereum/go-ethereum/pull/23016/files
    67  		logstack := make([]*big.Int, len(stack.Data()))
    68  		for i, item := range stack.Data() {
    69  			logstack[i] = item.ToBig()
    70  		}
    71  		log.Stack = logstack
    72  	}
    73  	l.encoder.Encode(log)
    74  }
    75  
    76  // CaptureFault outputs state information on the logger.
    77  func (l *JSONLogger) CaptureFault(env *EVM, pc uint64, op OpCode, gas, cost uint64, scope *ScopeContext, depth int, err error) {
    78  	l.CaptureState(env, pc, op, gas, cost, scope, depth, err)
    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 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) {}