cmd/evm: refactor t8n

This commit is contained in:
Martin Holst Swende 2024-12-05 20:58:00 +01:00
parent 58908636fb
commit 412147e7ac
No known key found for this signature in database
GPG key ID: 683B438C05A5DDF0
3 changed files with 67 additions and 69 deletions

View file

@ -33,8 +33,8 @@ import (
"path/filepath" "path/filepath"
) )
// fileWritingTracer is a tracer which wraps either a different tracer, // fileWritingTracer wraps either a tracer or a logger. On tx start,
// or a logger. On tx start, it creates a new file to direct output to, // it instantiates a tracer/logger, creates a new file to direct output to,
// and on tx end it closes the file. // and on tx end it closes the file.
type fileWritingTracer struct { type fileWritingTracer struct {
txIndex int txIndex int
@ -44,7 +44,6 @@ type fileWritingTracer struct {
// for json-tracing // for json-tracing
logConfig *logger.Config logConfig *logger.Config
callFrames bool
// for custom tracing // for custom tracing
tracerName string tracerName string
@ -53,20 +52,63 @@ type fileWritingTracer struct {
getResult func() (json.RawMessage, error) getResult func() (json.RawMessage, error)
} }
func newFileWritingTracer(baseDir string, logConfig *logger.Config, callFrames bool) *fileWritingTracer { // jsonToFile creates hooks which uses an underlying jsonlogger, and writes the
return &fileWritingTracer{ // jsonl-delimited output to a file, one per tx.
func jsonToFile(baseDir string, logConfig *logger.Config, callFrames bool) *tracing.Hooks {
t := &fileWritingTracer{
baseDir: baseDir, baseDir: baseDir,
logConfig: logConfig, logConfig: logConfig,
callFrames: callFrames,
} }
hooks := t.hooks()
if !callFrames {
hooks.OnEnter = nil
}
return hooks
} }
func newFileWritingCustomTracer(baseDir, tracerName string, traceConfig json.RawMessage, chainConfig *params.ChainConfig) *fileWritingTracer { // tracerToFile creates hooks which uses an underlying tracer, and writes the
return &fileWritingTracer{ // json-result to file, one per tx.
func tracerToFile(baseDir, tracerName string, traceConfig json.RawMessage, chainConfig *params.ChainConfig) *tracing.Hooks {
t := &fileWritingTracer{
baseDir: baseDir, baseDir: baseDir,
tracerName: tracerName, tracerName: tracerName,
chainConfig: chainConfig, chainConfig: chainConfig,
} }
return t.hooks()
}
func (l *fileWritingTracer) hooks() *tracing.Hooks {
hooks := &tracing.Hooks{
OnTxStart: l.OnTxStartJSONL,
OnTxEnd: l.OnTxEnd,
// intentional no-op: we instantiate the l.inner on tx start, which has
// not yet happened at this point
//OnSystemCallStart: func() {},
OnEnter: func(depth int, typ byte, from common.Address, to common.Address, input []byte, gas uint64, value *big.Int) {
if l.inner != nil && l.inner.OnEnter != nil {
l.inner.OnEnter(depth, typ, from, to, input, gas, value)
}
},
OnExit: func(depth int, output []byte, gasUsed uint64, err error, reverted bool) {
if l.inner != nil && l.inner.OnExit != nil {
l.inner.OnExit(depth, output, gasUsed, err, reverted)
}
},
OnOpcode: func(pc uint64, op byte, gas, cost uint64, scope tracing.OpContext, rData []byte, depth int, err error) {
if l.inner.OnOpcode != nil {
l.inner.OnOpcode(pc, op, gas, cost, scope, rData, depth, err)
}
},
OnFault: func(pc uint64, op byte, gas, cost uint64, scope tracing.OpContext, depth int, err error) {
if l.inner != nil && l.inner.OnFault != nil {
l.inner.OnFault(pc, op, gas, cost, scope, depth, err)
}
},
}
if len(l.tracerName) > 0 { // a custom tracer
hooks.OnTxStart = l.OnTxStartJSON
}
return hooks
} }
// OnTxStartJSONL is the OnTxStart-handler for jsonl logger. // OnTxStartJSONL is the OnTxStart-handler for jsonl logger.
@ -79,11 +121,7 @@ func (l *fileWritingTracer) OnTxStartJSONL(env *tracing.VMContext, tx *types.Tra
} }
log.Debug("Created tracing-file", "path", fname) log.Debug("Created tracing-file", "path", fname)
l.destination = traceFile l.destination = traceFile
if !l.callFrames {
l.inner = logger.NewJSONLogger(l.logConfig, traceFile)
} else {
l.inner = logger.NewJSONLoggerWithCallFrames(l.logConfig, traceFile) l.inner = logger.NewJSONLoggerWithCallFrames(l.logConfig, traceFile)
}
if l.inner.OnTxStart != nil { if l.inner.OnTxStart != nil {
l.inner.OnTxStart(env, tx, from) l.inner.OnTxStart(env, tx, from)
} }
@ -129,41 +167,3 @@ func (l *fileWritingTracer) OnTxEnd(receipt *types.Receipt, err error) {
} }
l.txIndex++ l.txIndex++
} }
func (l *fileWritingTracer) Tracer() *tracers.Tracer {
hooks := &tracing.Hooks{
OnTxStart: l.OnTxStartJSONL,
OnTxEnd: l.OnTxEnd,
// intentional no-op: we instantiate the l.inner on tx start, which has
// not yet happened at this point
//OnSystemCallStart: func() {},
OnEnter: func(depth int, typ byte, from common.Address, to common.Address, input []byte, gas uint64, value *big.Int) {
if l.inner != nil && l.inner.OnEnter != nil {
l.inner.OnEnter(depth, typ, from, to, input, gas, value)
}
},
OnExit: func(depth int, output []byte, gasUsed uint64, err error, reverted bool) {
if l.inner != nil && l.inner.OnExit != nil {
l.inner.OnExit(depth, output, gasUsed, err, reverted)
}
},
OnOpcode: func(pc uint64, op byte, gas, cost uint64, scope tracing.OpContext, rData []byte, depth int, err error) {
if l.inner.OnOpcode != nil {
l.inner.OnOpcode(pc, op, gas, cost, scope, rData, depth, err)
}
},
OnFault: func(pc uint64, op byte, gas, cost uint64, scope tracing.OpContext, depth int, err error) {
if l.inner != nil && l.inner.OnFault != nil {
l.inner.OnFault(pc, op, gas, cost, scope, depth, err)
}
},
}
if len(l.tracerName) > 0 { // a custom tracer
hooks.OnTxStart = l.OnTxStartJSON
}
return &tracers.Tracer{
Hooks: hooks,
GetResult: func() (json.RawMessage, error) { return nil, nil },
Stop: func(err error) {},
}
}

View file

@ -151,16 +151,14 @@ func Transition(ctx *cli.Context) error {
// Configure tracer // Configure tracer
if ctx.IsSet(TraceTracerFlag.Name) { // Custom tracing if ctx.IsSet(TraceTracerFlag.Name) { // Custom tracing
config := json.RawMessage(ctx.String(TraceTracerConfigFlag.Name)) config := json.RawMessage(ctx.String(TraceTracerConfigFlag.Name))
tracer := newFileWritingCustomTracer(baseDir, ctx.String(TraceTracerFlag.Name), config, chainConfig).Tracer() vmConfig.Tracer = tracerToFile(baseDir, ctx.String(TraceTracerFlag.Name), config, chainConfig)
vmConfig.Tracer = tracer.Hooks
} else if ctx.Bool(TraceFlag.Name) { // JSON opcode tracing } else if ctx.Bool(TraceFlag.Name) { // JSON opcode tracing
logConfig := &logger.Config{ logConfig := &logger.Config{
DisableStack: ctx.Bool(TraceDisableStackFlag.Name), DisableStack: ctx.Bool(TraceDisableStackFlag.Name),
EnableMemory: ctx.Bool(TraceEnableMemoryFlag.Name), EnableMemory: ctx.Bool(TraceEnableMemoryFlag.Name),
EnableReturnData: ctx.Bool(TraceEnableReturnDataFlag.Name), EnableReturnData: ctx.Bool(TraceEnableReturnDataFlag.Name),
} }
tracer := newFileWritingTracer(baseDir, logConfig, ctx.Bool(TraceEnableCallFramesFlag.Name)).Tracer() vmConfig.Tracer = jsonToFile(baseDir, logConfig, ctx.Bool(TraceEnableCallFramesFlag.Name))
vmConfig.Tracer = tracer.Hooks
} }
// Run the test and aggregate the result // Run the test and aggregate the result
s, result, body, err := prestate.Apply(vmConfig, chainConfig, txIt, ctx.Int64(RewardFlag.Name)) s, result, body, err := prestate.Apply(vmConfig, chainConfig, txIt, ctx.Int64(RewardFlag.Name))

View file

@ -70,7 +70,7 @@ func NewJSONLogger(cfg *Config, writer io.Writer) *tracing.Hooks {
} }
l.hooks = &tracing.Hooks{ l.hooks = &tracing.Hooks{
OnTxStart: l.OnTxStart, OnTxStart: l.OnTxStart,
OnSystemCallStart: l.onSystemCallStart, //OnSystemCallStart: l.onSystemCallStart,
OnExit: l.OnExit, OnExit: l.OnExit,
OnOpcode: l.OnOpcode, OnOpcode: l.OnOpcode,
OnFault: l.OnFault, OnFault: l.OnFault,
@ -87,7 +87,7 @@ func NewJSONLoggerWithCallFrames(cfg *Config, writer io.Writer) *tracing.Hooks {
} }
l.hooks = &tracing.Hooks{ l.hooks = &tracing.Hooks{
OnTxStart: l.OnTxStart, OnTxStart: l.OnTxStart,
OnSystemCallStart: l.onSystemCallStart, //OnSystemCallStart: l.onSystemCallStart,
OnEnter: l.OnEnter, OnEnter: l.OnEnter,
OnExit: l.OnExit, OnExit: l.OnExit,
OnOpcode: l.OnOpcode, OnOpcode: l.OnOpcode,