github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/core/vm/logger_json.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 19:16:36</date>
    10  //</624450082908934144>
    11  
    12  
    13  package vm
    14  
    15  import (
    16  	"encoding/json"
    17  	"io"
    18  	"math/big"
    19  	"time"
    20  
    21  	"github.com/ethereum/go-ethereum/common"
    22  	"github.com/ethereum/go-ethereum/common/math"
    23  )
    24  
    25  type JSONLogger struct {
    26  	encoder *json.Encoder
    27  	cfg     *LogConfig
    28  }
    29  
    30  //newjsonLogger创建了一个新的EVM跟踪程序,它将执行步骤作为JSON对象打印出来。
    31  //进入提供的流。
    32  func NewJSONLogger(cfg *LogConfig, writer io.Writer) *JSONLogger {
    33  	return &JSONLogger{json.NewEncoder(writer), cfg}
    34  }
    35  
    36  func (l *JSONLogger) CaptureStart(from common.Address, to common.Address, create bool, input []byte, gas uint64, value *big.Int) error {
    37  	return nil
    38  }
    39  
    40  //CaptureState在记录器上输出状态信息。
    41  func (l *JSONLogger) CaptureState(env *EVM, pc uint64, op OpCode, gas, cost uint64, memory *Memory, stack *Stack, contract *Contract, depth int, err error) error {
    42  	log := StructLog{
    43  		Pc:            pc,
    44  		Op:            op,
    45  		Gas:           gas,
    46  		GasCost:       cost,
    47  		MemorySize:    memory.Len(),
    48  		Storage:       nil,
    49  		Depth:         depth,
    50  		RefundCounter: env.StateDB.GetRefund(),
    51  		Err:           err,
    52  	}
    53  	if !l.cfg.DisableMemory {
    54  		log.Memory = memory.Data()
    55  	}
    56  	if !l.cfg.DisableStack {
    57  		log.Stack = stack.Data()
    58  	}
    59  	return l.encoder.Encode(log)
    60  }
    61  
    62  //CaptureFault在记录器上输出状态信息。
    63  func (l *JSONLogger) CaptureFault(env *EVM, pc uint64, op OpCode, gas, cost uint64, memory *Memory, stack *Stack, contract *Contract, depth int, err error) error {
    64  	return nil
    65  }
    66  
    67  //CaptureEnd在执行结束时触发。
    68  func (l *JSONLogger) CaptureEnd(output []byte, gasUsed uint64, t time.Duration, err error) error {
    69  	type endLog struct {
    70  		Output  string              `json:"output"`
    71  		GasUsed math.HexOrDecimal64 `json:"gasUsed"`
    72  		Time    time.Duration       `json:"time"`
    73  		Err     string              `json:"error,omitempty"`
    74  	}
    75  	if err != nil {
    76  		return l.encoder.Encode(endLog{common.Bytes2Hex(output), math.HexOrDecimal64(gasUsed), t, err.Error()})
    77  	}
    78  	return l.encoder.Encode(endLog{common.Bytes2Hex(output), math.HexOrDecimal64(gasUsed), t, ""})
    79  }
    80