mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-27 23:26:44 +00:00
# Conflicts: # consensus/beacon/consensus.go # consensus/ethash/consensus.go # consensus/misc/dao.go # core/blockchain.go # core/evm.go # core/genesis.go # core/state/dump.go # core/state/state_object.go # core/state/statedb.go # core/state_transition.go # core/vm/evm.go # core/vm/instructions.go # core/vm/interface.go # internal/ethapi/api.go
105 lines
3.2 KiB
Go
105 lines
3.2 KiB
Go
// Copyright 2021 The go-ethereum Authors
|
|
// This file is part of the go-ethereum library.
|
|
//
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU Lesser General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
package logger
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
"github.com/ethereum/go-ethereum/common/math"
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
"github.com/ethereum/go-ethereum/core/vm"
|
|
"github.com/ethereum/go-ethereum/eth/tracers/directory"
|
|
"github.com/ethereum/go-ethereum/eth/tracers/directory/live"
|
|
)
|
|
|
|
type JSONLogger struct {
|
|
directory.NoopTracer
|
|
encoder *json.Encoder
|
|
cfg *Config
|
|
env *live.VMContext
|
|
}
|
|
|
|
// NewJSONLogger creates a new EVM tracer that prints execution steps as JSON objects
|
|
// into the provided stream.
|
|
func NewJSONLogger(cfg *Config, writer io.Writer) *JSONLogger {
|
|
l := &JSONLogger{encoder: json.NewEncoder(writer), cfg: cfg}
|
|
if l.cfg == nil {
|
|
l.cfg = &Config{}
|
|
}
|
|
return l
|
|
}
|
|
|
|
func (l *JSONLogger) GetLogger() *live.LiveLogger {
|
|
return &live.LiveLogger{
|
|
CaptureTxStart: l.CaptureTxStart,
|
|
CaptureEnd: l.CaptureEnd,
|
|
CaptureState: l.CaptureState,
|
|
CaptureFault: l.CaptureFault,
|
|
}
|
|
}
|
|
|
|
func (l *JSONLogger) CaptureFault(pc uint64, op live.OpCode, gas uint64, cost uint64, scope live.ScopeContext, depth int, err error) {
|
|
// TODO: Add rData to this interface as well
|
|
l.CaptureState(pc, op, gas, cost, scope, nil, depth, err)
|
|
}
|
|
|
|
// CaptureState outputs state information on the logger.
|
|
func (l *JSONLogger) CaptureState(pc uint64, op live.OpCode, gas, cost uint64, scope live.ScopeContext, rData []byte, depth int, err error) {
|
|
memory := scope.GetMemoryData()
|
|
stack := scope.GetStackData()
|
|
|
|
log := StructLog{
|
|
Pc: pc,
|
|
Op: vm.OpCode(op),
|
|
Gas: gas,
|
|
GasCost: cost,
|
|
MemorySize: len(memory),
|
|
Depth: depth,
|
|
RefundCounter: l.env.StateDB.GetRefund(),
|
|
Err: err,
|
|
}
|
|
if l.cfg.EnableMemory {
|
|
log.Memory = memory
|
|
}
|
|
if !l.cfg.DisableStack {
|
|
log.Stack = stack
|
|
}
|
|
if l.cfg.EnableReturnData {
|
|
log.ReturnData = rData
|
|
}
|
|
l.encoder.Encode(log)
|
|
}
|
|
|
|
// CaptureEnd is triggered at end of execution.
|
|
func (l *JSONLogger) CaptureEnd(output []byte, gasUsed uint64, err error, reverted bool) {
|
|
type endLog struct {
|
|
Output string `json:"output"`
|
|
GasUsed math.HexOrDecimal64 `json:"gasUsed"`
|
|
Err string `json:"error,omitempty"`
|
|
}
|
|
var errMsg string
|
|
if err != nil {
|
|
errMsg = err.Error()
|
|
}
|
|
l.encoder.Encode(endLog{common.Bytes2Hex(output), math.HexOrDecimal64(gasUsed), errMsg})
|
|
}
|
|
|
|
func (l *JSONLogger) CaptureTxStart(env *live.VMContext, tx *types.Transaction, from common.Address) {
|
|
l.env = env
|
|
}
|