Skip to content

Commit

Permalink
eth/tracers/logger: skip system calls (#30923)
Browse files Browse the repository at this point in the history
This commit makes it so that the struct logger will not emit logs while
system calls are being executed. This will make it consistent with
the JSON and MD loggers. It is as it stands hard to distinguish when
system calls are being processed vs when a tx is being processed.

---------

Co-authored-by: Sina Mahmoodi <itz.s1na@gmail.com>
  • Loading branch information
fjl and s1na authored Jan 2, 2025
1 parent 85ffbde commit 06883c1
Showing 1 changed file with 52 additions and 12 deletions.
64 changes: 52 additions & 12 deletions eth/tracers/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ type StructLogger struct {

interrupt atomic.Bool // Atomic flag to signal execution interruption
reason error // Textual reason for the interruption
skip bool // skip processing hooks.
}

// NewStreamingStructLogger returns a new streaming logger.
Expand All @@ -240,10 +241,12 @@ func NewStructLogger(cfg *Config) *StructLogger {

func (l *StructLogger) Hooks() *tracing.Hooks {
return &tracing.Hooks{
OnTxStart: l.OnTxStart,
OnTxEnd: l.OnTxEnd,
OnExit: l.OnExit,
OnOpcode: l.OnOpcode,
OnTxStart: l.OnTxStart,
OnTxEnd: l.OnTxEnd,
OnSystemCallStartV2: l.OnSystemCallStart,
OnSystemCallEnd: l.OnSystemCallEnd,
OnExit: l.OnExit,
OnOpcode: l.OnOpcode,
}
}

Expand All @@ -255,6 +258,10 @@ func (l *StructLogger) OnOpcode(pc uint64, opcode byte, gas, cost uint64, scope
if l.interrupt.Load() {
return
}
// Processing a system call.
if l.skip {
return
}
// check if already accumulated the size of the response.
if l.cfg.Limit != 0 && l.resultSize > l.cfg.Limit {
return
Expand Down Expand Up @@ -320,6 +327,9 @@ func (l *StructLogger) OnExit(depth int, output []byte, gasUsed uint64, err erro
if depth != 0 {
return
}
if l.skip {
return
}
l.output = output
l.err = err
// TODO @holiman, should we output the per-scope output?
Expand Down Expand Up @@ -360,6 +370,13 @@ func (l *StructLogger) Stop(err error) {
func (l *StructLogger) OnTxStart(env *tracing.VMContext, tx *types.Transaction, from common.Address) {
l.env = env
}
func (l *StructLogger) OnSystemCallStart(env *tracing.VMContext) {
l.skip = true
}

func (l *StructLogger) OnSystemCallEnd() {
l.skip = false
}

func (l *StructLogger) OnTxEnd(receipt *types.Receipt, err error) {
if err != nil {
Expand Down Expand Up @@ -389,9 +406,10 @@ func WriteTrace(writer io.Writer, logs []StructLog) {
}

type mdLogger struct {
out io.Writer
cfg *Config
env *tracing.VMContext
out io.Writer
cfg *Config
env *tracing.VMContext
skip bool
}

// NewMarkdownLogger creates a logger which outputs information in a format adapted
Expand All @@ -406,19 +424,32 @@ func NewMarkdownLogger(cfg *Config, writer io.Writer) *mdLogger {

func (t *mdLogger) Hooks() *tracing.Hooks {
return &tracing.Hooks{
OnTxStart: t.OnTxStart,
OnEnter: t.OnEnter,
OnExit: t.OnExit,
OnOpcode: t.OnOpcode,
OnFault: t.OnFault,
OnTxStart: t.OnTxStart,
OnSystemCallStartV2: t.OnSystemCallStart,
OnSystemCallEnd: t.OnSystemCallEnd,
OnEnter: t.OnEnter,
OnExit: t.OnExit,
OnOpcode: t.OnOpcode,
OnFault: t.OnFault,
}
}

func (t *mdLogger) OnTxStart(env *tracing.VMContext, tx *types.Transaction, from common.Address) {
t.env = env
}

func (t *mdLogger) OnSystemCallStart(env *tracing.VMContext) {
t.skip = true
}

func (t *mdLogger) OnSystemCallEnd() {
t.skip = false
}

func (t *mdLogger) OnEnter(depth int, typ byte, from common.Address, to common.Address, input []byte, gas uint64, value *big.Int) {
if t.skip {
return
}
if depth != 0 {
return
}
Expand Down Expand Up @@ -446,6 +477,9 @@ func (t *mdLogger) OnEnter(depth int, typ byte, from common.Address, to common.A
}

func (t *mdLogger) OnExit(depth int, output []byte, gasUsed uint64, err error, reverted bool) {
if t.skip {
return
}
if depth == 0 {
fmt.Fprintf(t.out, "\nPost-execution info:\n"+
" - output: `%#x`\n"+
Expand All @@ -457,6 +491,9 @@ func (t *mdLogger) OnExit(depth int, output []byte, gasUsed uint64, err error, r

// OnOpcode also tracks SLOAD/SSTORE ops to track storage change.
func (t *mdLogger) OnOpcode(pc uint64, op byte, gas, cost uint64, scope tracing.OpContext, rData []byte, depth int, err error) {
if t.skip {
return
}
stack := scope.StackData()
fmt.Fprintf(t.out, "| %4d | %10v | %3d |%10v |", pc, vm.OpCode(op).String(),
cost, t.env.StateDB.GetRefund())
Expand All @@ -477,6 +514,9 @@ func (t *mdLogger) OnOpcode(pc uint64, op byte, gas, cost uint64, scope tracing.
}

func (t *mdLogger) OnFault(pc uint64, op byte, gas, cost uint64, scope tracing.OpContext, depth int, err error) {
if t.skip {
return
}
fmt.Fprintf(t.out, "\nError: at pc=%d, op=%v: %v\n", pc, op, err)
}

Expand Down

0 comments on commit 06883c1

Please sign in to comment.