gitee.com/liu-zhao234568/cntest@v1.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  	"gitee.com/liu-zhao234568/cntest/common"
    26  	"gitee.com/liu-zhao234568/cntest/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(env *EVM, from, to common.Address, create bool, input []byte, gas uint64, value *big.Int) {
    45  }
    46  
    47  func (l *JSONLogger) CaptureFault(*EVM, uint64, OpCode, uint64, uint64, *ScopeContext, int, error) {}
    48  
    49  // CaptureState outputs state information on the logger.
    50  func (l *JSONLogger) CaptureState(env *EVM, pc uint64, op OpCode, gas, cost uint64, scope *ScopeContext, rData []byte, depth int, err error) {
    51  	memory := scope.Memory
    52  	stack := scope.Stack
    53  
    54  	log := StructLog{
    55  		Pc:            pc,
    56  		Op:            op,
    57  		Gas:           gas,
    58  		GasCost:       cost,
    59  		MemorySize:    memory.Len(),
    60  		Depth:         depth,
    61  		RefundCounter: env.StateDB.GetRefund(),
    62  		Err:           err,
    63  	}
    64  	if !l.cfg.DisableMemory {
    65  		log.Memory = memory.Data()
    66  	}
    67  	if !l.cfg.DisableStack {
    68  		log.Stack = stack.data
    69  	}
    70  	if !l.cfg.DisableReturnData {
    71  		log.ReturnData = rData
    72  	}
    73  	l.encoder.Encode(log)
    74  }
    75  
    76  // CaptureEnd is triggered at end of execution.
    77  func (l *JSONLogger) CaptureEnd(output []byte, gasUsed uint64, t time.Duration, err error) {
    78  	type endLog struct {
    79  		Output  string              `json:"output"`
    80  		GasUsed math.HexOrDecimal64 `json:"gasUsed"`
    81  		Time    time.Duration       `json:"time"`
    82  		Err     string              `json:"error,omitempty"`
    83  	}
    84  	var errMsg string
    85  	if err != nil {
    86  		errMsg = err.Error()
    87  	}
    88  	l.encoder.Encode(endLog{common.Bytes2Hex(output), math.HexOrDecimal64(gasUsed), t, errMsg})
    89  }