github.com/reapchain/go-reapchain@v0.2.15-0.20210609012950-9735c110c705/cmd/evm/json_logger.go (about)

     1  // Copyright 2017 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser 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  // The go-ethereum library 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 Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package main
    18  
    19  import (
    20  	"encoding/json"
    21  	"io"
    22  	"time"
    23  
    24  	"github.com/ethereum/go-ethereum/common"
    25  	"github.com/ethereum/go-ethereum/common/math"
    26  	"github.com/ethereum/go-ethereum/core/vm"
    27  )
    28  
    29  type JSONLogger struct {
    30  	encoder *json.Encoder
    31  }
    32  
    33  func NewJSONLogger(writer io.Writer) *JSONLogger {
    34  	return &JSONLogger{json.NewEncoder(writer)}
    35  }
    36  
    37  // CaptureState outputs state information on the logger.
    38  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 {
    39  	return l.encoder.Encode(vm.StructLog{
    40  		Pc:      pc,
    41  		Op:      op,
    42  		Gas:     gas + cost,
    43  		GasCost: cost,
    44  		Memory:  memory.Data(),
    45  		Stack:   stack.Data(),
    46  		Storage: nil,
    47  		Depth:   depth,
    48  		Err:     err,
    49  	})
    50  }
    51  
    52  // CaptureEnd is triggered at end of execution.
    53  func (l *JSONLogger) CaptureEnd(output []byte, gasUsed uint64, t time.Duration) error {
    54  	type endLog struct {
    55  		Output  string              `json:"output"`
    56  		GasUsed math.HexOrDecimal64 `json:"gasUsed"`
    57  		Time    time.Duration       `json:"time"`
    58  	}
    59  	return l.encoder.Encode(endLog{common.Bytes2Hex(output), math.HexOrDecimal64(gasUsed), t})
    60  }