github.com/codingfuture/orig-energi3@v0.8.4/core/vm/logger_json.go (about)

     1  // Copyright 2018 The Energi Core Authors
     2  // Copyright 2017 The go-ethereum Authors
     3  // This file is part of the Energi Core library.
     4  //
     5  // The Energi Core library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Lesser 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  // The Energi Core library 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 Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with the Energi Core library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package vm
    19  
    20  import (
    21  	"encoding/json"
    22  	"io"
    23  	"math/big"
    24  	"time"
    25  
    26  	"github.com/ethereum/go-ethereum/common"
    27  	"github.com/ethereum/go-ethereum/common/math"
    28  )
    29  
    30  type JSONLogger struct {
    31  	encoder *json.Encoder
    32  	cfg     *LogConfig
    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{json.NewEncoder(writer), cfg}
    39  	if l.cfg == nil {
    40  		l.cfg = &LogConfig{}
    41  	}
    42  	return l
    43  }
    44  
    45  func (l *JSONLogger) CaptureStart(from common.Address, to common.Address, create bool, input []byte, gas uint64, value *big.Int) error {
    46  	return nil
    47  }
    48  
    49  // CaptureState outputs state information on the logger.
    50  func (l *JSONLogger) CaptureState(env *EVM, pc uint64, op OpCode, gas, cost uint64, memory *Memory, stack *Stack, contract *Contract, depth int, err error) error {
    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  		log.Stack = stack.Data()
    67  	}
    68  	return l.encoder.Encode(log)
    69  }
    70  
    71  // CaptureFault outputs state information on the logger.
    72  func (l *JSONLogger) CaptureFault(env *EVM, pc uint64, op OpCode, gas, cost uint64, memory *Memory, stack *Stack, contract *Contract, depth int, err error) error {
    73  	return nil
    74  }
    75  
    76  // CaptureEnd is triggered at end of execution.
    77  func (l *JSONLogger) CaptureEnd(output []byte, gasUsed uint64, t time.Duration, err error) 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  	if err != nil {
    85  		return l.encoder.Encode(endLog{common.Bytes2Hex(output), math.HexOrDecimal64(gasUsed), t, err.Error()})
    86  	}
    87  	return l.encoder.Encode(endLog{common.Bytes2Hex(output), math.HexOrDecimal64(gasUsed), t, ""})
    88  }