From 922ce0da85d720dae3a1c25d9f334da9d1b01e6b Mon Sep 17 00:00:00 2001 From: ramtinms Date: Thu, 25 Jul 2024 20:45:12 -0700 Subject: [PATCH 1/2] skip tx tracing operation if unsafe --- fvm/evm/debug/tracer.go | 109 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 108 insertions(+), 1 deletion(-) diff --git a/fvm/evm/debug/tracer.go b/fvm/evm/debug/tracer.go index 473a2dc2ba9..8a0095eaeb8 100644 --- a/fvm/evm/debug/tracer.go +++ b/fvm/evm/debug/tracer.go @@ -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" @@ -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.tracer) } func (t *CallTracer) WithBlockID(id flow.Identifier) { @@ -113,3 +116,107 @@ 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(tc *tracers.Tracer) *tracers.Tracer { + wrapped := &tracers.Tracer{ + Hooks: &tracing.Hooks{}, + } + wrapped.OnTxStart = func( + vm *tracing.VMContext, + tx *types.Transaction, + from gethCommon.Address, + ) { + defer func() { + if r := recover(); r != nil { + _ = r + // TODO log + } + }() + tc.OnTxStart(vm, tx, from) + } + + wrapped.OnTxEnd = func(receipt *types.Receipt, err error) { + defer func() { + if r := recover(); r != nil { + _ = r + // TODO log + } + }() + tc.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 { + _ = r + // TODO log + } + }() + tc.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 { + _ = r + // TODO log + } + }() + tc.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 { + _ = r + // TODO log + } + }() + tc.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 { + _ = r + // TODO log + } + }() + tc.OnFault(pc, op, gas, cost, scope, depth, err) + } + + wrapped.OnGasChange = func(old, new uint64, reason tracing.GasChangeReason) { + defer func() { + if r := recover(); r != nil { + _ = r + // TODO log + } + }() + tc.OnGasChange(old, new, reason) + } + + wrapped.GetResult = tc.GetResult + wrapped.Stop = tc.Stop + return wrapped +} From 6ebb887257488951d01532a61640f246c8e81110 Mon Sep 17 00:00:00 2001 From: ramtinms Date: Thu, 25 Jul 2024 21:29:47 -0700 Subject: [PATCH 2/2] add log --- fvm/evm/debug/tracer.go | 90 +++++++++++++++++++++++++++++------------ 1 file changed, 65 insertions(+), 25 deletions(-) diff --git a/fvm/evm/debug/tracer.go b/fvm/evm/debug/tracer.go index 8a0095eaeb8..5af0828166c 100644 --- a/fvm/evm/debug/tracer.go +++ b/fvm/evm/debug/tracer.go @@ -52,7 +52,7 @@ func NewEVMCallTracer(uploader Uploader, logger zerolog.Logger) (*CallTracer, er } func (t *CallTracer) TxTracer() *tracers.Tracer { - return NewSafeTxTracer(t.tracer) + return NewSafeTxTracer(t) } func (t *CallTracer) WithBlockID(id flow.Identifier) { @@ -117,10 +117,15 @@ func TraceID(txID gethCommon.Hash, blockID flow.Identifier) string { return fmt.Sprintf("%s-%s", blockID.String(), txID.String()) } -func NewSafeTxTracer(tc *tracers.Tracer) *tracers.Tracer { +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, @@ -128,21 +133,31 @@ func NewSafeTxTracer(tc *tracers.Tracer) *tracers.Tracer { ) { defer func() { if r := recover(); r != nil { - _ = r - // TODO log + err, ok := r.(error) + if !ok { + err = fmt.Errorf("panic: %v", r) + } + l.Err(err). + Stack(). + Msg("OnTxStart trace collection failed") } }() - tc.OnTxStart(vm, tx, from) + ct.tracer.OnTxStart(vm, tx, from) } wrapped.OnTxEnd = func(receipt *types.Receipt, err error) { defer func() { if r := recover(); r != nil { - _ = r - // TODO log + err, ok := r.(error) + if !ok { + err = fmt.Errorf("panic: %v", r) + } + l.Err(err). + Stack(). + Msg("OnTxEnd trace collection failed") } }() - tc.OnTxEnd(receipt, err) + ct.tracer.OnTxEnd(receipt, err) } wrapped.OnEnter = func( @@ -155,21 +170,31 @@ func NewSafeTxTracer(tc *tracers.Tracer) *tracers.Tracer { ) { defer func() { if r := recover(); r != nil { - _ = r - // TODO log + err, ok := r.(error) + if !ok { + err = fmt.Errorf("panic: %v", r) + } + l.Err(err). + Stack(). + Msg("OnEnter trace collection failed") } }() - tc.OnEnter(depth, typ, from, to, input, gas, value) + 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 { - _ = r - // TODO log + err, ok := r.(error) + if !ok { + err = fmt.Errorf("panic: %v", r) + } + l.Err(err). + Stack(). + Msg("OnExit trace collection failed") } }() - tc.OnExit(depth, output, gasUsed, err, reverted) + ct.tracer.OnExit(depth, output, gasUsed, err, reverted) } wrapped.OnOpcode = func( @@ -183,11 +208,16 @@ func NewSafeTxTracer(tc *tracers.Tracer) *tracers.Tracer { ) { defer func() { if r := recover(); r != nil { - _ = r - // TODO log + err, ok := r.(error) + if !ok { + err = fmt.Errorf("panic: %v", r) + } + l.Err(err). + Stack(). + Msg("OnOpcode trace collection failed") } }() - tc.OnOpcode(pc, op, gas, cost, scope, rData, depth, err) + ct.tracer.OnOpcode(pc, op, gas, cost, scope, rData, depth, err) } wrapped.OnFault = func( @@ -199,24 +229,34 @@ func NewSafeTxTracer(tc *tracers.Tracer) *tracers.Tracer { err error) { defer func() { if r := recover(); r != nil { - _ = r - // TODO log + err, ok := r.(error) + if !ok { + err = fmt.Errorf("panic: %v", r) + } + l.Err(err). + Stack(). + Msg("OnFault trace collection failed") } }() - tc.OnFault(pc, op, gas, cost, scope, depth, err) + 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 { - _ = r - // TODO log + err, ok := r.(error) + if !ok { + err = fmt.Errorf("panic: %v", r) + } + l.Err(err). + Stack(). + Msg("OnGasChange trace collection failed") } }() - tc.OnGasChange(old, new, reason) + ct.tracer.OnGasChange(old, new, reason) } - wrapped.GetResult = tc.GetResult - wrapped.Stop = tc.Stop + wrapped.GetResult = ct.tracer.GetResult + wrapped.Stop = ct.tracer.Stop return wrapped }