github.com/oskarth/go-ethereum@v1.6.8-0.20191013093314-dac24a9d3494/cmd/evm/json_logger.go (about)

     1  // Copyright 2017 The go-ethereum Authors
     2  // This file is part of go-ethereum.
     3  //
     4  // go-ethereum is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU 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  // go-ethereum 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 General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU General Public License
    15  // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package main
    18  
    19  import (
    20  	"encoding/json"
    21  	"io"
    22  	"math/big"
    23  	"time"
    24  
    25  	"github.com/ethereum/go-ethereum/common"
    26  	"github.com/ethereum/go-ethereum/common/math"
    27  	"github.com/ethereum/go-ethereum/core/vm"
    28  )
    29  
    30  type JSONLogger struct {
    31  	encoder *json.Encoder
    32  	cfg     *vm.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 *vm.LogConfig, writer io.Writer) *JSONLogger {
    38  	return &JSONLogger{json.NewEncoder(writer), cfg}
    39  }
    40  
    41  func (l *JSONLogger) CaptureStart(from common.Address, to common.Address, create bool, input []byte, gas uint64, value *big.Int) error {
    42  	return nil
    43  }
    44  
    45  // CaptureState outputs state information on the logger.
    46  func (l *JSONLogger) CaptureState(env *vm.EVM, pc uint64, op vm.OpCode, gas, cost uint64, memory *vm.Memory, stack *vm.Stack, contract *vm.Contract, depth int, err error) error {
    47  	log := vm.StructLog{
    48  		Pc:            pc,
    49  		Op:            op,
    50  		Gas:           gas,
    51  		GasCost:       cost,
    52  		MemorySize:    memory.Len(),
    53  		Storage:       nil,
    54  		Depth:         depth,
    55  		RefundCounter: env.StateDB.GetRefund(),
    56  		Err:           err,
    57  	}
    58  	if !l.cfg.DisableMemory {
    59  		log.Memory = memory.Data()
    60  	}
    61  	if !l.cfg.DisableStack {
    62  		log.Stack = stack.Data()
    63  	}
    64  	return l.encoder.Encode(log)
    65  }
    66  
    67  // CaptureFault outputs state information on the logger.
    68  func (l *JSONLogger) CaptureFault(env *vm.EVM, pc uint64, op vm.OpCode, gas, cost uint64, memory *vm.Memory, stack *vm.Stack, contract *vm.Contract, depth int, err error) error {
    69  	return nil
    70  }
    71  
    72  // CaptureEnd is triggered at end of execution.
    73  func (l *JSONLogger) CaptureEnd(output []byte, gasUsed uint64, t time.Duration, err error) error {
    74  	type endLog struct {
    75  		Output  string              `json:"output"`
    76  		GasUsed math.HexOrDecimal64 `json:"gasUsed"`
    77  		Time    time.Duration       `json:"time"`
    78  		Err     string              `json:"error,omitempty"`
    79  	}
    80  	if err != nil {
    81  		return l.encoder.Encode(endLog{common.Bytes2Hex(output), math.HexOrDecimal64(gasUsed), t, err.Error()})
    82  	}
    83  	return l.encoder.Encode(endLog{common.Bytes2Hex(output), math.HexOrDecimal64(gasUsed), t, ""})
    84  }