github.com/cryptotooltop/go-ethereum@v0.0.0-20231103184714-151d1922f3e5/core/vm/logger_json.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 vm
    18  
    19  import (
    20  	"encoding/json"
    21  	"io"
    22  	"math/big"
    23  	"time"
    24  
    25  	"github.com/scroll-tech/go-ethereum/common"
    26  	"github.com/scroll-tech/go-ethereum/common/math"
    27  )
    28  
    29  type JSONLogger struct {
    30  	encoder *json.Encoder
    31  	cfg     *LogConfig
    32  	env     *EVM
    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{encoder: json.NewEncoder(writer), cfg: cfg}
    39  	if l.cfg == nil {
    40  		l.cfg = &LogConfig{}
    41  	}
    42  	return l
    43  }
    44  
    45  func (l *JSONLogger) CaptureStart(env *EVM, from, to common.Address, create bool, input []byte, gas uint64, value *big.Int) {
    46  	l.env = env
    47  }
    48  
    49  func (l *JSONLogger) CaptureFault(uint64, OpCode, uint64, uint64, *ScopeContext, int, error) {}
    50  
    51  // CaptureState outputs state information on the logger.
    52  func (l *JSONLogger) CaptureState(pc uint64, op OpCode, gas, cost uint64, scope *ScopeContext, rData []byte, depth int, err error) {
    53  	memory := scope.Memory
    54  	stack := scope.Stack
    55  
    56  	log := StructLog{
    57  		Pc:            pc,
    58  		Op:            op,
    59  		Gas:           gas,
    60  		GasCost:       cost,
    61  		MemorySize:    memory.Len(),
    62  		Depth:         depth,
    63  		RefundCounter: l.env.StateDB.GetRefund(),
    64  		Err:           err,
    65  	}
    66  	if l.cfg.EnableMemory {
    67  		log.Memory.Write(memory.Data())
    68  	}
    69  	if !l.cfg.DisableStack {
    70  		log.Stack = stack.data
    71  	}
    72  	if l.cfg.EnableReturnData {
    73  		log.ReturnData.Write(rData)
    74  	}
    75  	l.encoder.Encode(log)
    76  }
    77  
    78  // CaptureStateAfter for special needs, tracks SSTORE ops and records the storage change.
    79  func (l *JSONLogger) CaptureStateAfter(pc uint64, op OpCode, gas, cost uint64, scope *ScopeContext, rData []byte, depth int, err error) {
    80  }
    81  
    82  // CaptureEnd is triggered at end of execution.
    83  func (l *JSONLogger) CaptureEnd(output []byte, gasUsed uint64, t time.Duration, err error) {
    84  	type endLog struct {
    85  		Output  string              `json:"output"`
    86  		GasUsed math.HexOrDecimal64 `json:"gasUsed"`
    87  		Time    time.Duration       `json:"time"`
    88  		Err     string              `json:"error,omitempty"`
    89  	}
    90  	var errMsg string
    91  	if err != nil {
    92  		errMsg = err.Error()
    93  	}
    94  	l.encoder.Encode(endLog{common.Bytes2Hex(output), math.HexOrDecimal64(gasUsed), t, errMsg})
    95  }
    96  
    97  func (l *JSONLogger) CaptureEnter(typ OpCode, from common.Address, to common.Address, input []byte, gas uint64, value *big.Int) {
    98  }
    99  
   100  func (l *JSONLogger) CaptureExit(output []byte, gasUsed uint64, err error) {}