Skip to content

Commit

Permalink
Merge pull request #6262 from onflow/ramtin/evm-skip-tracing-if-unsafe
Browse files Browse the repository at this point in the history
[Flow EVM] Skip tx debug tracing operation if unsafe
  • Loading branch information
ramtinms authored Jul 26, 2024
2 parents 88f843f + 6ebb887 commit 634de89
Showing 1 changed file with 148 additions and 1 deletion.
149 changes: 148 additions & 1 deletion fvm/evm/debug/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package debug
import (
"encoding/json"
"fmt"
"math/big"

gethCommon "github.com/onflow/go-ethereum/common"
"github.com/onflow/go-ethereum/core/tracing"
"github.com/onflow/go-ethereum/core/types"
"github.com/onflow/go-ethereum/eth/tracers"
"github.com/rs/zerolog"

Expand Down Expand Up @@ -49,7 +52,7 @@ func NewEVMCallTracer(uploader Uploader, logger zerolog.Logger) (*CallTracer, er
}

func (t *CallTracer) TxTracer() *tracers.Tracer {
return t.tracer
return NewSafeTxTracer(t)
}

func (t *CallTracer) WithBlockID(id flow.Identifier) {
Expand Down Expand Up @@ -113,3 +116,147 @@ func (n nopTracer) Collect(_ gethCommon.Hash) {}
func TraceID(txID gethCommon.Hash, blockID flow.Identifier) string {
return fmt.Sprintf("%s-%s", blockID.String(), txID.String())
}

func NewSafeTxTracer(ct *CallTracer) *tracers.Tracer {
wrapped := &tracers.Tracer{
Hooks: &tracing.Hooks{},
}

l := ct.logger.With().
Str("block-id", ct.blockID.String()).
Logger()

wrapped.OnTxStart = func(
vm *tracing.VMContext,
tx *types.Transaction,
from gethCommon.Address,
) {
defer func() {
if r := recover(); r != nil {
err, ok := r.(error)
if !ok {
err = fmt.Errorf("panic: %v", r)
}
l.Err(err).
Stack().
Msg("OnTxStart trace collection failed")
}
}()
ct.tracer.OnTxStart(vm, tx, from)
}

wrapped.OnTxEnd = func(receipt *types.Receipt, err error) {
defer func() {
if r := recover(); r != nil {
err, ok := r.(error)
if !ok {
err = fmt.Errorf("panic: %v", r)
}
l.Err(err).
Stack().
Msg("OnTxEnd trace collection failed")
}
}()
ct.tracer.OnTxEnd(receipt, err)
}

wrapped.OnEnter = func(
depth int,
typ byte,
from, to gethCommon.Address,
input []byte,
gas uint64,
value *big.Int,
) {
defer func() {
if r := recover(); r != nil {
err, ok := r.(error)
if !ok {
err = fmt.Errorf("panic: %v", r)
}
l.Err(err).
Stack().
Msg("OnEnter trace collection failed")
}
}()
ct.tracer.OnEnter(depth, typ, from, to, input, gas, value)
}

wrapped.OnExit = func(depth int, output []byte, gasUsed uint64, err error, reverted bool) {
defer func() {
if r := recover(); r != nil {
err, ok := r.(error)
if !ok {
err = fmt.Errorf("panic: %v", r)
}
l.Err(err).
Stack().
Msg("OnExit trace collection failed")
}
}()
ct.tracer.OnExit(depth, output, gasUsed, err, reverted)
}

wrapped.OnOpcode = func(
pc uint64,
op byte,
gas, cost uint64,
scope tracing.OpContext,
rData []byte,
depth int,
err error,
) {
defer func() {
if r := recover(); r != nil {
err, ok := r.(error)
if !ok {
err = fmt.Errorf("panic: %v", r)
}
l.Err(err).
Stack().
Msg("OnOpcode trace collection failed")
}
}()
ct.tracer.OnOpcode(pc, op, gas, cost, scope, rData, depth, err)
}

wrapped.OnFault = func(
pc uint64,
op byte,
gas, cost uint64,
scope tracing.OpContext,
depth int,
err error) {
defer func() {
if r := recover(); r != nil {
err, ok := r.(error)
if !ok {
err = fmt.Errorf("panic: %v", r)
}
l.Err(err).
Stack().
Msg("OnFault trace collection failed")
}
}()
ct.tracer.OnFault(pc, op, gas, cost, scope, depth, err)
}

wrapped.OnGasChange = func(old, new uint64, reason tracing.GasChangeReason) {
defer func() {
if r := recover(); r != nil {
err, ok := r.(error)
if !ok {
err = fmt.Errorf("panic: %v", r)
}
l.Err(err).
Stack().
Msg("OnGasChange trace collection failed")
}
}()
ct.tracer.OnGasChange(old, new, reason)
}

wrapped.GetResult = ct.tracer.GetResult
wrapped.Stop = ct.tracer.Stop
return wrapped
}

0 comments on commit 634de89

Please sign in to comment.