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"
)
// fileWritingTracer is a tracer which wraps either a different tracer,
// or a logger. On tx start, it creates a new file to direct output to,
// fileWritingTracer wraps either a tracer or a logger. On tx start,
// it instantiates a tracer/logger, creates a new file to direct output to,
// and on tx end it closes the file.
type fileWritingTracer struct {
txIndex int
@ -44,7 +44,6 @@ type fileWritingTracer struct {
// for json-tracing
logConfig *logger.Config
callFrames bool
// for custom tracing
tracerName string
@ -53,20 +52,63 @@ type fileWritingTracer struct {
getResult func() (json.RawMessage, error)
}
func newFileWritingTracer(baseDir string, logConfig *logger.Config, callFrames bool) *fileWritingTracer {
return &fileWritingTracer{
// jsonToFile creates hooks which uses an underlying jsonlogger, and writes the
// jsonl-delimited output to a file, one per tx.
func jsonToFile(baseDir string, logConfig *logger.Config, callFrames bool) *tracing.Hooks {
t := &fileWritingTracer{
baseDir: baseDir,
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 {
return &fileWritingTracer{
// tracerToFile creates hooks which uses an underlying tracer, and writes the
// json-result to file, one per tx.
func tracerToFile(baseDir, tracerName string, traceConfig json.RawMessage, chainConfig *params.ChainConfig) *tracing.Hooks {
t := &fileWritingTracer{
baseDir: baseDir,
tracerName: tracerName,
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.
@ -79,11 +121,7 @@ func (l *fileWritingTracer) OnTxStartJSONL(env *tracing.VMContext, tx *types.Tra
}
log.Debug("Created tracing-file", "path", fname)
l.destination = traceFile
if !l.callFrames {
l.inner = logger.NewJSONLogger(l.logConfig, traceFile)
} else {
l.inner = logger.NewJSONLoggerWithCallFrames(l.logConfig, traceFile)
}
if l.inner.OnTxStart != nil {
l.inner.OnTxStart(env, tx, from)
}
@ -129,41 +167,3 @@ func (l *fileWritingTracer) OnTxEnd(receipt *types.Receipt, err error) {
}
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
if ctx.IsSet(TraceTracerFlag.Name) { // Custom tracing
config := json.RawMessage(ctx.String(TraceTracerConfigFlag.Name))
tracer := newFileWritingCustomTracer(baseDir, ctx.String(TraceTracerFlag.Name), config, chainConfig).Tracer()
vmConfig.Tracer = tracer.Hooks
vmConfig.Tracer = tracerToFile(baseDir, ctx.String(TraceTracerFlag.Name), config, chainConfig)
} else if ctx.Bool(TraceFlag.Name) { // JSON opcode tracing
logConfig := &logger.Config{
DisableStack: ctx.Bool(TraceDisableStackFlag.Name),
EnableMemory: ctx.Bool(TraceEnableMemoryFlag.Name),
EnableReturnData: ctx.Bool(TraceEnableReturnDataFlag.Name),
}
tracer := newFileWritingTracer(baseDir, logConfig, ctx.Bool(TraceEnableCallFramesFlag.Name)).Tracer()
vmConfig.Tracer = tracer.Hooks
vmConfig.Tracer = jsonToFile(baseDir, logConfig, ctx.Bool(TraceEnableCallFramesFlag.Name))
}
// Run the test and aggregate the result
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{
OnTxStart: l.OnTxStart,
OnSystemCallStart: l.onSystemCallStart,
//OnSystemCallStart: l.onSystemCallStart,
OnExit: l.OnExit,
OnOpcode: l.OnOpcode,
OnFault: l.OnFault,
@ -87,7 +87,7 @@ func NewJSONLoggerWithCallFrames(cfg *Config, writer io.Writer) *tracing.Hooks {
}
l.hooks = &tracing.Hooks{
OnTxStart: l.OnTxStart,
OnSystemCallStart: l.onSystemCallStart,
//OnSystemCallStart: l.onSystemCallStart,
OnEnter: l.OnEnter,
OnExit: l.OnExit,
OnOpcode: l.OnOpcode,