From 06883c16861fc034e1471c4f911cd309612b1f7f Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Thu, 2 Jan 2025 18:37:58 +0100 Subject: [PATCH] eth/tracers/logger: skip system calls (#30923) 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 --- eth/tracers/logger/logger.go | 64 +++++++++++++++++++++++++++++------- 1 file changed, 52 insertions(+), 12 deletions(-) diff --git a/eth/tracers/logger/logger.go b/eth/tracers/logger/logger.go index dc9e6e62b7..07de871f14 100644 --- a/eth/tracers/logger/logger.go +++ b/eth/tracers/logger/logger.go @@ -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. @@ -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, } } @@ -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 @@ -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? @@ -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 { @@ -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 @@ -406,11 +424,13 @@ 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, } } @@ -418,7 +438,18 @@ func (t *mdLogger) OnTxStart(env *tracing.VMContext, tx *types.Transaction, from 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 } @@ -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"+ @@ -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()) @@ -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) }