github.com/niluplatform/go-nilu@v1.7.4-0.20200912082737-a0cb0776d52c/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/NiluPlatform/go-nilu/common"
    26  	"github.com/NiluPlatform/go-nilu/common/math"
    27  	"github.com/NiluPlatform/go-nilu/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  		Err:        err,
    56  	}
    57  	if !l.cfg.DisableMemory {
    58  		log.Memory = memory.Data()
    59  	}
    60  	if !l.cfg.DisableStack {
    61  		log.Stack = stack.Data()
    62  	}
    63  	return l.encoder.Encode(log)
    64  }
    65  
    66  // CaptureFault outputs state information on the logger.
    67  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 {
    68  	return nil
    69  }
    70  
    71  // CaptureEnd is triggered at end of execution.
    72  func (l *JSONLogger) CaptureEnd(output []byte, gasUsed uint64, t time.Duration, err error) error {
    73  	type endLog struct {
    74  		Output  string              `json:"output"`
    75  		GasUsed math.HexOrDecimal64 `json:"gasUsed"`
    76  		Time    time.Duration       `json:"time"`
    77  		Err     string              `json:"error,omitempty"`
    78  	}
    79  	if err != nil {
    80  		return l.encoder.Encode(endLog{common.Bytes2Hex(output), math.HexOrDecimal64(gasUsed), t, err.Error()})
    81  	}
    82  	return l.encoder.Encode(endLog{common.Bytes2Hex(output), math.HexOrDecimal64(gasUsed), t, ""})
    83  }