Skip to content

Commit 5eee1e0

Browse files
holimans1na
authored andcommitted
eth/tracers: package restructuring (ethereum#23857)
* eth/tracers: restructure tracer package * core/vm/runtime: load js tracers * eth/tracers: mv bigint js code to own file * eth/tracers: add method docs for native tracers * eth/tracers: minor doc fix * core,eth: cancel evm on nativecalltracer stop * core/vm: fix failing test Co-authored-by: Sina Mahmoodi <[email protected]>
1 parent f5c9d9e commit 5eee1e0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+1392
-1388
lines changed

cmd/geth/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ import (
4040
"github.com/ethereum/go-ethereum/metrics"
4141
"github.com/ethereum/go-ethereum/node"
4242

43-
// Force-load the native, to trigger registration
43+
// Force-load the tracer engines to trigger registration
44+
_ "github.com/ethereum/go-ethereum/eth/tracers/js"
4445
_ "github.com/ethereum/go-ethereum/eth/tracers/native"
4546

4647
"gopkg.in/urfave/cli.v1"

core/vm/access_list_tracer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ func (a *AccessListTracer) CaptureStart(env *EVM, from common.Address, to common
141141
}
142142

143143
// CaptureState captures all opcodes that touch storage or addresses and adds them to the accesslist.
144-
func (a *AccessListTracer) CaptureState(env *EVM, pc uint64, op OpCode, gas, cost uint64, scope *ScopeContext, rData []byte, depth int, err error) {
144+
func (a *AccessListTracer) CaptureState(pc uint64, op OpCode, gas, cost uint64, scope *ScopeContext, rData []byte, depth int, err error) {
145145
stack := scope.Stack
146146
if (op == SLOAD || op == SSTORE) && stack.len() >= 1 {
147147
slot := common.Hash(stack.data[stack.len()-1].Bytes32())
@@ -161,7 +161,7 @@ func (a *AccessListTracer) CaptureState(env *EVM, pc uint64, op OpCode, gas, cos
161161
}
162162
}
163163

164-
func (*AccessListTracer) CaptureFault(env *EVM, pc uint64, op OpCode, gas, cost uint64, scope *ScopeContext, depth int, err error) {
164+
func (*AccessListTracer) CaptureFault(pc uint64, op OpCode, gas, cost uint64, scope *ScopeContext, depth int, err error) {
165165
}
166166

167167
func (*AccessListTracer) CaptureEnd(output []byte, gasUsed uint64, t time.Duration, err error) {}

core/vm/interpreter.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,9 @@ func (in *EVMInterpreter) Run(contract *Contract, input []byte, readOnly bool) (
169169
defer func() {
170170
if err != nil {
171171
if !logged {
172-
in.cfg.Tracer.CaptureState(in.evm, pcCopy, op, gasCopy, cost, callContext, in.returnData, in.evm.depth, err)
172+
in.cfg.Tracer.CaptureState(pcCopy, op, gasCopy, cost, callContext, in.returnData, in.evm.depth, err)
173173
} else {
174-
in.cfg.Tracer.CaptureFault(in.evm, pcCopy, op, gasCopy, cost, callContext, in.evm.depth, err)
174+
in.cfg.Tracer.CaptureFault(pcCopy, op, gasCopy, cost, callContext, in.evm.depth, err)
175175
}
176176
}
177177
}()
@@ -253,7 +253,7 @@ func (in *EVMInterpreter) Run(contract *Contract, input []byte, readOnly bool) (
253253
}
254254

255255
if in.cfg.Debug {
256-
in.cfg.Tracer.CaptureState(in.evm, pc, op, gasCopy, cost, callContext, in.returnData, in.evm.depth, err)
256+
in.cfg.Tracer.CaptureState(pc, op, gasCopy, cost, callContext, in.returnData, in.evm.depth, err)
257257
logged = true
258258
}
259259

core/vm/logger.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,10 @@ func (s *StructLog) ErrorString() string {
105105
// if you need to retain them beyond the current call.
106106
type EVMLogger interface {
107107
CaptureStart(env *EVM, from common.Address, to common.Address, create bool, input []byte, gas uint64, value *big.Int)
108-
CaptureState(env *EVM, pc uint64, op OpCode, gas, cost uint64, scope *ScopeContext, rData []byte, depth int, err error)
108+
CaptureState(pc uint64, op OpCode, gas, cost uint64, scope *ScopeContext, rData []byte, depth int, err error)
109109
CaptureEnter(typ OpCode, from common.Address, to common.Address, input []byte, gas uint64, value *big.Int)
110110
CaptureExit(output []byte, gasUsed uint64, err error)
111-
CaptureFault(env *EVM, pc uint64, op OpCode, gas, cost uint64, scope *ScopeContext, depth int, err error)
111+
CaptureFault(pc uint64, op OpCode, gas, cost uint64, scope *ScopeContext, depth int, err error)
112112
CaptureEnd(output []byte, gasUsed uint64, t time.Duration, err error)
113113
}
114114

@@ -119,6 +119,7 @@ type EVMLogger interface {
119119
// contract their storage.
120120
type StructLogger struct {
121121
cfg LogConfig
122+
env *EVM
122123

123124
storage map[common.Address]Storage
124125
logs []StructLog
@@ -147,12 +148,13 @@ func (l *StructLogger) Reset() {
147148

148149
// CaptureStart implements the EVMLogger interface to initialize the tracing operation.
149150
func (l *StructLogger) CaptureStart(env *EVM, from common.Address, to common.Address, create bool, input []byte, gas uint64, value *big.Int) {
151+
l.env = env
150152
}
151153

152154
// CaptureState logs a new structured log message and pushes it out to the environment
153155
//
154156
// CaptureState also tracks SLOAD/SSTORE ops to track storage change.
155-
func (l *StructLogger) CaptureState(env *EVM, pc uint64, op OpCode, gas, cost uint64, scope *ScopeContext, rData []byte, depth int, err error) {
157+
func (l *StructLogger) CaptureState(pc uint64, op OpCode, gas, cost uint64, scope *ScopeContext, rData []byte, depth int, err error) {
156158
memory := scope.Memory
157159
stack := scope.Stack
158160
contract := scope.Contract
@@ -186,7 +188,7 @@ func (l *StructLogger) CaptureState(env *EVM, pc uint64, op OpCode, gas, cost ui
186188
if op == SLOAD && stack.len() >= 1 {
187189
var (
188190
address = common.Hash(stack.data[stack.len()-1].Bytes32())
189-
value = env.StateDB.GetState(contract.Address(), address)
191+
value = l.env.StateDB.GetState(contract.Address(), address)
190192
)
191193
l.storage[contract.Address()][address] = value
192194
storage = l.storage[contract.Address()].Copy()
@@ -206,13 +208,13 @@ func (l *StructLogger) CaptureState(env *EVM, pc uint64, op OpCode, gas, cost ui
206208
copy(rdata, rData)
207209
}
208210
// create a new snapshot of the EVM.
209-
log := StructLog{pc, op, gas, cost, mem, memory.Len(), stck, rdata, storage, depth, env.StateDB.GetRefund(), err}
211+
log := StructLog{pc, op, gas, cost, mem, memory.Len(), stck, rdata, storage, depth, l.env.StateDB.GetRefund(), err}
210212
l.logs = append(l.logs, log)
211213
}
212214

213215
// CaptureFault implements the EVMLogger interface to trace an execution fault
214216
// while running an opcode.
215-
func (l *StructLogger) CaptureFault(env *EVM, pc uint64, op OpCode, gas, cost uint64, scope *ScopeContext, depth int, err error) {
217+
func (l *StructLogger) CaptureFault(pc uint64, op OpCode, gas, cost uint64, scope *ScopeContext, depth int, err error) {
216218
}
217219

218220
// CaptureEnd is called after the call finishes to finalize the tracing.
@@ -291,19 +293,21 @@ func WriteLogs(writer io.Writer, logs []*types.Log) {
291293
type mdLogger struct {
292294
out io.Writer
293295
cfg *LogConfig
296+
env *EVM
294297
}
295298

296299
// NewMarkdownLogger creates a logger which outputs information in a format adapted
297300
// for human readability, and is also a valid markdown table
298301
func NewMarkdownLogger(cfg *LogConfig, writer io.Writer) *mdLogger {
299-
l := &mdLogger{writer, cfg}
302+
l := &mdLogger{out: writer, cfg: cfg}
300303
if l.cfg == nil {
301304
l.cfg = &LogConfig{}
302305
}
303306
return l
304307
}
305308

306309
func (t *mdLogger) CaptureStart(env *EVM, from common.Address, to common.Address, create bool, input []byte, gas uint64, value *big.Int) {
310+
t.env = env
307311
if !create {
308312
fmt.Fprintf(t.out, "From: `%v`\nTo: `%v`\nData: `0x%x`\nGas: `%d`\nValue `%v` wei\n",
309313
from.String(), to.String(),
@@ -321,7 +325,7 @@ func (t *mdLogger) CaptureStart(env *EVM, from common.Address, to common.Address
321325
}
322326

323327
// CaptureState also tracks SLOAD/SSTORE ops to track storage change.
324-
func (t *mdLogger) CaptureState(env *EVM, pc uint64, op OpCode, gas, cost uint64, scope *ScopeContext, rData []byte, depth int, err error) {
328+
func (t *mdLogger) CaptureState(pc uint64, op OpCode, gas, cost uint64, scope *ScopeContext, rData []byte, depth int, err error) {
325329
stack := scope.Stack
326330
fmt.Fprintf(t.out, "| %4d | %10v | %3d |", pc, op, cost)
327331

@@ -334,14 +338,14 @@ func (t *mdLogger) CaptureState(env *EVM, pc uint64, op OpCode, gas, cost uint64
334338
b := fmt.Sprintf("[%v]", strings.Join(a, ","))
335339
fmt.Fprintf(t.out, "%10v |", b)
336340
}
337-
fmt.Fprintf(t.out, "%10v |", env.StateDB.GetRefund())
341+
fmt.Fprintf(t.out, "%10v |", t.env.StateDB.GetRefund())
338342
fmt.Fprintln(t.out, "")
339343
if err != nil {
340344
fmt.Fprintf(t.out, "Error: %v\n", err)
341345
}
342346
}
343347

344-
func (t *mdLogger) CaptureFault(env *EVM, pc uint64, op OpCode, gas, cost uint64, scope *ScopeContext, depth int, err error) {
348+
func (t *mdLogger) CaptureFault(pc uint64, op OpCode, gas, cost uint64, scope *ScopeContext, depth int, err error) {
345349
fmt.Fprintf(t.out, "\nError: at pc=%d, op=%v: %v\n", pc, op, err)
346350
}
347351

core/vm/logger_json.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,27 @@ import (
2929
type JSONLogger struct {
3030
encoder *json.Encoder
3131
cfg *LogConfig
32+
env *EVM
3233
}
3334

3435
// NewJSONLogger creates a new EVM tracer that prints execution steps as JSON objects
3536
// into the provided stream.
3637
func NewJSONLogger(cfg *LogConfig, writer io.Writer) *JSONLogger {
37-
l := &JSONLogger{json.NewEncoder(writer), cfg}
38+
l := &JSONLogger{encoder: json.NewEncoder(writer), cfg: cfg}
3839
if l.cfg == nil {
3940
l.cfg = &LogConfig{}
4041
}
4142
return l
4243
}
4344

4445
func (l *JSONLogger) CaptureStart(env *EVM, from, to common.Address, create bool, input []byte, gas uint64, value *big.Int) {
46+
l.env = env
4547
}
4648

47-
func (l *JSONLogger) CaptureFault(*EVM, uint64, OpCode, uint64, uint64, *ScopeContext, int, error) {}
49+
func (l *JSONLogger) CaptureFault(uint64, OpCode, uint64, uint64, *ScopeContext, int, error) {}
4850

4951
// CaptureState outputs state information on the logger.
50-
func (l *JSONLogger) CaptureState(env *EVM, pc uint64, op OpCode, gas, cost uint64, scope *ScopeContext, rData []byte, depth int, err error) {
52+
func (l *JSONLogger) CaptureState(pc uint64, op OpCode, gas, cost uint64, scope *ScopeContext, rData []byte, depth int, err error) {
5153
memory := scope.Memory
5254
stack := scope.Stack
5355

@@ -58,7 +60,7 @@ func (l *JSONLogger) CaptureState(env *EVM, pc uint64, op OpCode, gas, cost uint
5860
GasCost: cost,
5961
MemorySize: memory.Len(),
6062
Depth: depth,
61-
RefundCounter: env.StateDB.GetRefund(),
63+
RefundCounter: l.env.StateDB.GetRefund(),
6264
Err: err,
6365
}
6466
if l.cfg.EnableMemory {

core/vm/logger_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ func TestStoreCapture(t *testing.T) {
6262
scope.Stack.push(uint256.NewInt(1))
6363
scope.Stack.push(new(uint256.Int))
6464
var index common.Hash
65-
logger.CaptureState(env, 0, SSTORE, 0, 0, scope, nil, 0, nil)
65+
logger.CaptureStart(env, common.Address{}, contract.Address(), false, nil, 0, nil)
66+
logger.CaptureState(0, SSTORE, 0, 0, scope, nil, 0, nil)
6667
if len(logger.storage[contract.Address()]) == 0 {
6768
t.Fatalf("expected exactly 1 changed value on address %x, got %d", contract.Address(),
6869
len(logger.storage[contract.Address()]))

core/vm/runtime/runtime_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ import (
3535
"github.com/ethereum/go-ethereum/core/vm"
3636
"github.com/ethereum/go-ethereum/eth/tracers"
3737
"github.com/ethereum/go-ethereum/params"
38+
39+
// force-load js tracers to trigger registration
40+
_ "github.com/ethereum/go-ethereum/eth/tracers/js"
3841
)
3942

4043
func TestDefaults(t *testing.T) {
@@ -330,12 +333,12 @@ type stepCounter struct {
330333
func (s *stepCounter) CaptureStart(env *vm.EVM, from common.Address, to common.Address, create bool, input []byte, gas uint64, value *big.Int) {
331334
}
332335

333-
func (s *stepCounter) CaptureFault(env *vm.EVM, pc uint64, op vm.OpCode, gas, cost uint64, scope *vm.ScopeContext, depth int, err error) {
336+
func (s *stepCounter) CaptureFault(pc uint64, op vm.OpCode, gas, cost uint64, scope *vm.ScopeContext, depth int, err error) {
334337
}
335338

336339
func (s *stepCounter) CaptureEnd(output []byte, gasUsed uint64, t time.Duration, err error) {}
337340

338-
func (s *stepCounter) CaptureState(env *vm.EVM, pc uint64, op vm.OpCode, gas, cost uint64, scope *vm.ScopeContext, rData []byte, depth int, err error) {
341+
func (s *stepCounter) CaptureState(pc uint64, op vm.OpCode, gas, cost uint64, scope *vm.ScopeContext, rData []byte, depth int, err error) {
339342
s.steps++
340343
// Enable this for more output
341344
//s.inner.CaptureState(env, pc, op, gas, cost, memory, stack, rStack, contract, depth, err)
@@ -511,7 +514,7 @@ func BenchmarkSimpleLoop(b *testing.B) {
511514
// TestEip2929Cases contains various testcases that are used for
512515
// EIP-2929 about gas repricings
513516
func TestEip2929Cases(t *testing.T) {
514-
517+
t.Skip("Test only useful for generating documentation")
515518
id := 1
516519
prettyPrint := func(comment string, code []byte) {
517520

0 commit comments

Comments
 (0)