github.com/neatio-net/neatio@v1.7.3-0.20231114194659-f4d7a2226baa/chain/core/vm/json_logger.go (about)

     1  package vm
     2  
     3  import (
     4  	"encoding/json"
     5  	"io"
     6  	"math/big"
     7  	"time"
     8  
     9  	"github.com/neatio-net/neatio/utilities/common"
    10  	"github.com/neatio-net/neatio/utilities/common/math"
    11  )
    12  
    13  type JSONLogger struct {
    14  	encoder *json.Encoder
    15  	cfg     *LogConfig
    16  }
    17  
    18  // NewJSONLogger creates a new EVM tracer that prints execution steps as JSON objects
    19  // into the provided stream.
    20  func NewJSONLogger(cfg *LogConfig, writer io.Writer) *JSONLogger {
    21  	l := &JSONLogger{json.NewEncoder(writer), cfg}
    22  	if l.cfg == nil {
    23  		l.cfg = &LogConfig{}
    24  	}
    25  	return l
    26  }
    27  
    28  func (l *JSONLogger) CaptureStart(from common.Address, to common.Address, create bool, input []byte, gas uint64, value *big.Int) error {
    29  	return nil
    30  }
    31  
    32  // CaptureState outputs state information on the logger.
    33  func (l *JSONLogger) CaptureState(env *EVM, pc uint64, op OpCode, gas, cost uint64, memory *Memory, stack *Stack, contract *Contract, depth int, err error) error {
    34  	log := StructLog{
    35  		Pc:            pc,
    36  		Op:            op,
    37  		Gas:           gas,
    38  		GasCost:       cost,
    39  		MemorySize:    memory.Len(),
    40  		Storage:       nil,
    41  		Depth:         depth,
    42  		RefundCounter: env.StateDB.GetRefund(),
    43  		Err:           err,
    44  	}
    45  	if !l.cfg.DisableMemory {
    46  		log.Memory = memory.Data()
    47  	}
    48  	if !l.cfg.DisableStack {
    49  		log.Stack = stack.Data()
    50  	}
    51  	return l.encoder.Encode(log)
    52  }
    53  
    54  // CaptureFault outputs state information on the logger.
    55  func (l *JSONLogger) CaptureFault(env *EVM, pc uint64, op OpCode, gas, cost uint64, memory *Memory, stack *Stack, contract *Contract, depth int, err error) error {
    56  	return nil
    57  }
    58  
    59  // CaptureEnd is triggered at end of execution.
    60  func (l *JSONLogger) CaptureEnd(output []byte, gasUsed uint64, t time.Duration, err error) error {
    61  	type endLog struct {
    62  		Output  string              `json:"output"`
    63  		GasUsed math.HexOrDecimal64 `json:"gasUsed"`
    64  		Time    time.Duration       `json:"time"`
    65  		Err     string              `json:"error,omitempty"`
    66  	}
    67  	if err != nil {
    68  		return l.encoder.Encode(endLog{common.Bytes2Hex(output), math.HexOrDecimal64(gasUsed), t, err.Error()})
    69  	}
    70  	return l.encoder.Encode(endLog{common.Bytes2Hex(output), math.HexOrDecimal64(gasUsed), t, ""})
    71  }