Skip to content

Commit 9f54050

Browse files
committed
cmd/evm: refactor t8n
1 parent 69b9c1b commit 9f54050

File tree

3 files changed

+67
-69
lines changed

3 files changed

+67
-69
lines changed

cmd/evm/internal/t8ntool/file_tracer.go

Lines changed: 54 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ import (
3333
"path/filepath"
3434
)
3535

36-
// fileWritingTracer is a tracer which wraps either a different tracer,
37-
// or a logger. On tx start, it creates a new file to direct output to,
36+
// fileWritingTracer wraps either a tracer or a logger. On tx start,
37+
// it instantiates a tracer/logger, creates a new file to direct output to,
3838
// and on tx end it closes the file.
3939
type fileWritingTracer struct {
4040
txIndex int
@@ -43,8 +43,7 @@ type fileWritingTracer struct {
4343
baseDir string
4444

4545
// for json-tracing
46-
logConfig *logger.Config
47-
callFrames bool
46+
logConfig *logger.Config
4847

4948
// for custom tracing
5049
tracerName string
@@ -53,20 +52,63 @@ type fileWritingTracer struct {
5352
getResult func() (json.RawMessage, error)
5453
}
5554

56-
func newFileWritingTracer(baseDir string, logConfig *logger.Config, callFrames bool) *fileWritingTracer {
57-
return &fileWritingTracer{
58-
baseDir: baseDir,
59-
logConfig: logConfig,
60-
callFrames: callFrames,
55+
// jsonToFile creates hooks which uses an underlying jsonlogger, and writes the
56+
// jsonl-delimited output to a file, one per tx.
57+
func jsonToFile(baseDir string, logConfig *logger.Config, callFrames bool) *tracing.Hooks {
58+
t := &fileWritingTracer{
59+
baseDir: baseDir,
60+
logConfig: logConfig,
6161
}
62+
hooks := t.hooks()
63+
if !callFrames {
64+
hooks.OnEnter = nil
65+
}
66+
return hooks
6267
}
6368

64-
func newFileWritingCustomTracer(baseDir, tracerName string, traceConfig json.RawMessage, chainConfig *params.ChainConfig) *fileWritingTracer {
65-
return &fileWritingTracer{
69+
// tracerToFile creates hooks which uses an underlying tracer, and writes the
70+
// json-result to file, one per tx.
71+
func tracerToFile(baseDir, tracerName string, traceConfig json.RawMessage, chainConfig *params.ChainConfig) *tracing.Hooks {
72+
t := &fileWritingTracer{
6673
baseDir: baseDir,
6774
tracerName: tracerName,
6875
chainConfig: chainConfig,
6976
}
77+
return t.hooks()
78+
}
79+
80+
func (l *fileWritingTracer) hooks() *tracing.Hooks {
81+
hooks := &tracing.Hooks{
82+
OnTxStart: l.OnTxStartJSONL,
83+
OnTxEnd: l.OnTxEnd,
84+
// intentional no-op: we instantiate the l.inner on tx start, which has
85+
// not yet happened at this point
86+
//OnSystemCallStart: func() {},
87+
OnEnter: func(depth int, typ byte, from common.Address, to common.Address, input []byte, gas uint64, value *big.Int) {
88+
if l.inner != nil && l.inner.OnEnter != nil {
89+
l.inner.OnEnter(depth, typ, from, to, input, gas, value)
90+
}
91+
},
92+
OnExit: func(depth int, output []byte, gasUsed uint64, err error, reverted bool) {
93+
if l.inner != nil && l.inner.OnExit != nil {
94+
l.inner.OnExit(depth, output, gasUsed, err, reverted)
95+
}
96+
},
97+
OnOpcode: func(pc uint64, op byte, gas, cost uint64, scope tracing.OpContext, rData []byte, depth int, err error) {
98+
if l.inner.OnOpcode != nil {
99+
l.inner.OnOpcode(pc, op, gas, cost, scope, rData, depth, err)
100+
}
101+
},
102+
OnFault: func(pc uint64, op byte, gas, cost uint64, scope tracing.OpContext, depth int, err error) {
103+
if l.inner != nil && l.inner.OnFault != nil {
104+
l.inner.OnFault(pc, op, gas, cost, scope, depth, err)
105+
}
106+
},
107+
}
108+
if len(l.tracerName) > 0 { // a custom tracer
109+
hooks.OnTxStart = l.OnTxStartJSON
110+
}
111+
return hooks
70112
}
71113

72114
// OnTxStartJSONL is the OnTxStart-handler for jsonl logger.
@@ -79,11 +121,7 @@ func (l *fileWritingTracer) OnTxStartJSONL(env *tracing.VMContext, tx *types.Tra
79121
}
80122
log.Debug("Created tracing-file", "path", fname)
81123
l.destination = traceFile
82-
if !l.callFrames {
83-
l.inner = logger.NewJSONLogger(l.logConfig, traceFile)
84-
} else {
85-
l.inner = logger.NewJSONLoggerWithCallFrames(l.logConfig, traceFile)
86-
}
124+
l.inner = logger.NewJSONLoggerWithCallFrames(l.logConfig, traceFile)
87125
if l.inner.OnTxStart != nil {
88126
l.inner.OnTxStart(env, tx, from)
89127
}
@@ -129,41 +167,3 @@ func (l *fileWritingTracer) OnTxEnd(receipt *types.Receipt, err error) {
129167
}
130168
l.txIndex++
131169
}
132-
133-
func (l *fileWritingTracer) Tracer() *tracers.Tracer {
134-
hooks := &tracing.Hooks{
135-
OnTxStart: l.OnTxStartJSONL,
136-
OnTxEnd: l.OnTxEnd,
137-
// intentional no-op: we instantiate the l.inner on tx start, which has
138-
// not yet happened at this point
139-
//OnSystemCallStart: func() {},
140-
OnEnter: func(depth int, typ byte, from common.Address, to common.Address, input []byte, gas uint64, value *big.Int) {
141-
if l.inner != nil && l.inner.OnEnter != nil {
142-
l.inner.OnEnter(depth, typ, from, to, input, gas, value)
143-
}
144-
},
145-
OnExit: func(depth int, output []byte, gasUsed uint64, err error, reverted bool) {
146-
if l.inner != nil && l.inner.OnExit != nil {
147-
l.inner.OnExit(depth, output, gasUsed, err, reverted)
148-
}
149-
},
150-
OnOpcode: func(pc uint64, op byte, gas, cost uint64, scope tracing.OpContext, rData []byte, depth int, err error) {
151-
if l.inner.OnOpcode != nil {
152-
l.inner.OnOpcode(pc, op, gas, cost, scope, rData, depth, err)
153-
}
154-
},
155-
OnFault: func(pc uint64, op byte, gas, cost uint64, scope tracing.OpContext, depth int, err error) {
156-
if l.inner != nil && l.inner.OnFault != nil {
157-
l.inner.OnFault(pc, op, gas, cost, scope, depth, err)
158-
}
159-
},
160-
}
161-
if len(l.tracerName) > 0 { // a custom tracer
162-
hooks.OnTxStart = l.OnTxStartJSON
163-
}
164-
return &tracers.Tracer{
165-
Hooks: hooks,
166-
GetResult: func() (json.RawMessage, error) { return nil, nil },
167-
Stop: func(err error) {},
168-
}
169-
}

cmd/evm/internal/t8ntool/transition.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,16 +150,14 @@ func Transition(ctx *cli.Context) error {
150150
// Configure tracer
151151
if ctx.IsSet(TraceTracerFlag.Name) { // Custom tracing
152152
config := json.RawMessage(ctx.String(TraceTracerConfigFlag.Name))
153-
tracer := newFileWritingCustomTracer(baseDir, ctx.String(TraceTracerFlag.Name), config, chainConfig).Tracer()
154-
vmConfig.Tracer = tracer.Hooks
153+
vmConfig.Tracer = tracerToFile(baseDir, ctx.String(TraceTracerFlag.Name), config, chainConfig)
155154
} else if ctx.Bool(TraceFlag.Name) { // JSON opcode tracing
156155
logConfig := &logger.Config{
157156
DisableStack: ctx.Bool(TraceDisableStackFlag.Name),
158157
EnableMemory: ctx.Bool(TraceEnableMemoryFlag.Name),
159158
EnableReturnData: ctx.Bool(TraceEnableReturnDataFlag.Name),
160159
}
161-
tracer := newFileWritingTracer(baseDir, logConfig, ctx.Bool(TraceEnableCallFramesFlag.Name)).Tracer()
162-
vmConfig.Tracer = tracer.Hooks
160+
vmConfig.Tracer = jsonToFile(baseDir, logConfig, ctx.Bool(TraceEnableCallFramesFlag.Name))
163161
}
164162
// Run the test and aggregate the result
165163
s, result, body, err := prestate.Apply(vmConfig, chainConfig, txIt, ctx.Int64(RewardFlag.Name))

eth/tracers/logger/logger_json.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@ func NewJSONLogger(cfg *Config, writer io.Writer) *tracing.Hooks {
6969
l.cfg = &Config{}
7070
}
7171
l.hooks = &tracing.Hooks{
72-
OnTxStart: l.OnTxStart,
73-
OnSystemCallStart: l.onSystemCallStart,
74-
OnExit: l.OnExit,
75-
OnOpcode: l.OnOpcode,
76-
OnFault: l.OnFault,
72+
OnTxStart: l.OnTxStart,
73+
//OnSystemCallStart: l.onSystemCallStart,
74+
OnExit: l.OnExit,
75+
OnOpcode: l.OnOpcode,
76+
OnFault: l.OnFault,
7777
}
7878
return l.hooks
7979
}
@@ -86,12 +86,12 @@ func NewJSONLoggerWithCallFrames(cfg *Config, writer io.Writer) *tracing.Hooks {
8686
l.cfg = &Config{}
8787
}
8888
l.hooks = &tracing.Hooks{
89-
OnTxStart: l.OnTxStart,
90-
OnSystemCallStart: l.onSystemCallStart,
91-
OnEnter: l.OnEnter,
92-
OnExit: l.OnExit,
93-
OnOpcode: l.OnOpcode,
94-
OnFault: l.OnFault,
89+
OnTxStart: l.OnTxStart,
90+
//OnSystemCallStart: l.onSystemCallStart,
91+
OnEnter: l.OnEnter,
92+
OnExit: l.OnExit,
93+
OnOpcode: l.OnOpcode,
94+
OnFault: l.OnFault,
9595
}
9696
return l.hooks
9797
}

0 commit comments

Comments
 (0)