github.com/ubiq/go-ubiq/v6@v6.0.0/core/vm/logger_json.go (about)

     1  // Copyright 2017 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 vm
    18  
    19  import (
    20  	"encoding/json"
    21  	"io"
    22  	"math/big"
    23  	"time"
    24  
    25  	"github.com/ubiq/go-ubiq/v6/common"
    26  	"github.com/ubiq/go-ubiq/v6/common/math"
    27  )
    28  
    29  type JSONLogger struct {
    30  	encoder *json.Encoder
    31  	cfg     *LogConfig
    32  	env     *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 *LogConfig, writer io.Writer) *JSONLogger {
    38  	l := &JSONLogger{encoder: json.NewEncoder(writer), cfg: cfg}
    39  	if l.cfg == nil {
    40  		l.cfg = &LogConfig{}
    41  	}
    42  	return l
    43  }
    44  
    45  func (l *JSONLogger) CaptureStart(env *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(uint64, OpCode, uint64, uint64, *ScopeContext, int, error) {}
    50  
    51  // CaptureState outputs state information on the logger.
    52  func (l *JSONLogger) CaptureState(pc uint64, op OpCode, gas, cost uint64, scope *ScopeContext, rData []byte, depth int, err error) {
    53  	memory := scope.Memory
    54  	stack := scope.Stack
    55  
    56  	log := StructLog{
    57  		Pc:            pc,
    58  		Op:            op,
    59  		Gas:           gas,
    60  		GasCost:       cost,
    61  		MemorySize:    memory.Len(),
    62  		Depth:         depth,
    63  		RefundCounter: l.env.StateDB.GetRefund(),
    64  		Err:           err,
    65  	}
    66  	if l.cfg.EnableMemory {
    67  		log.Memory = memory.Data()
    68  	}
    69  	if !l.cfg.DisableStack {
    70  		log.Stack = stack.data
    71  	}
    72  	if l.cfg.EnableReturnData {
    73  		log.ReturnData = rData
    74  	}
    75  	l.encoder.Encode(log)
    76  }
    77  
    78  // CaptureEnd is triggered at end of execution.
    79  func (l *JSONLogger) CaptureEnd(output []byte, gasUsed uint64, t time.Duration, err error) {
    80  	type endLog struct {
    81  		Output  string              `json:"output"`
    82  		GasUsed math.HexOrDecimal64 `json:"gasUsed"`
    83  		Time    time.Duration       `json:"time"`
    84  		Err     string              `json:"error,omitempty"`
    85  	}
    86  	var errMsg string
    87  	if err != nil {
    88  		errMsg = err.Error()
    89  	}
    90  	l.encoder.Encode(endLog{common.Bytes2Hex(output), math.HexOrDecimal64(gasUsed), t, errMsg})
    91  }
    92  
    93  func (l *JSONLogger) CaptureEnter(typ OpCode, from common.Address, to common.Address, input []byte, gas uint64, value *big.Int) {
    94  }
    95  
    96  func (l *JSONLogger) CaptureExit(output []byte, gasUsed uint64, err error) {}