gitlab.com/flarenetwork/coreth@v0.1.1/core/vm/logger_json.go (about)

     1  // (c) 2019-2020, Ava Labs, Inc.
     2  //
     3  // This file is a derived work, based on the go-ethereum library whose original
     4  // notices appear below.
     5  //
     6  // It is distributed under a license compatible with the licensing terms of the
     7  // original code from which it is derived.
     8  //
     9  // Much love to the original authors for their work.
    10  // **********
    11  // Copyright 2017 The go-ethereum Authors
    12  // This file is part of the go-ethereum library.
    13  //
    14  // The go-ethereum library is free software: you can redistribute it and/or modify
    15  // it under the terms of the GNU Lesser General Public License as published by
    16  // the Free Software Foundation, either version 3 of the License, or
    17  // (at your option) any later version.
    18  //
    19  // The go-ethereum library is distributed in the hope that it will be useful,
    20  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    21  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    22  // GNU Lesser General Public License for more details.
    23  //
    24  // You should have received a copy of the GNU Lesser General Public License
    25  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    26  
    27  package vm
    28  
    29  import (
    30  	"encoding/json"
    31  	"io"
    32  	"math/big"
    33  	"time"
    34  
    35  	"github.com/ethereum/go-ethereum/common"
    36  	"github.com/ethereum/go-ethereum/common/math"
    37  )
    38  
    39  type JSONLogger struct {
    40  	encoder *json.Encoder
    41  	cfg     *LogConfig
    42  }
    43  
    44  // NewJSONLogger creates a new EVM tracer that prints execution steps as JSON objects
    45  // into the provided stream.
    46  func NewJSONLogger(cfg *LogConfig, writer io.Writer) *JSONLogger {
    47  	l := &JSONLogger{json.NewEncoder(writer), cfg}
    48  	if l.cfg == nil {
    49  		l.cfg = &LogConfig{}
    50  	}
    51  	return l
    52  }
    53  
    54  func (l *JSONLogger) CaptureStart(env *EVM, from, to common.Address, create bool, input []byte, gas uint64, value *big.Int) {
    55  }
    56  
    57  func (l *JSONLogger) CaptureFault(*EVM, uint64, OpCode, uint64, uint64, *ScopeContext, int, error) {}
    58  
    59  // CaptureState outputs state information on the logger.
    60  func (l *JSONLogger) CaptureState(env *EVM, pc uint64, op OpCode, gas, cost uint64, scope *ScopeContext, rData []byte, depth int, err error) {
    61  	memory := scope.Memory
    62  	stack := scope.Stack
    63  
    64  	log := StructLog{
    65  		Pc:            pc,
    66  		Op:            op,
    67  		Gas:           gas,
    68  		GasCost:       cost,
    69  		MemorySize:    memory.Len(),
    70  		Depth:         depth,
    71  		RefundCounter: env.StateDB.GetRefund(),
    72  		Err:           err,
    73  	}
    74  	if !l.cfg.DisableMemory {
    75  		log.Memory = memory.Data()
    76  	}
    77  	if !l.cfg.DisableStack {
    78  		log.Stack = stack.data
    79  	}
    80  	if !l.cfg.DisableReturnData {
    81  		log.ReturnData = rData
    82  	}
    83  	l.encoder.Encode(log)
    84  }
    85  
    86  // CaptureEnd is triggered at end of execution.
    87  func (l *JSONLogger) CaptureEnd(output []byte, gasUsed uint64, t time.Duration, err error) {
    88  	type endLog struct {
    89  		Output  string              `json:"output"`
    90  		GasUsed math.HexOrDecimal64 `json:"gasUsed"`
    91  		Time    time.Duration       `json:"time"`
    92  		Err     string              `json:"error,omitempty"`
    93  	}
    94  	var errMsg string
    95  	if err != nil {
    96  		errMsg = err.Error()
    97  	}
    98  	l.encoder.Encode(endLog{common.Bytes2Hex(output), math.HexOrDecimal64(gasUsed), t, errMsg})
    99  }