Skip to content

Commit 03abb17

Browse files
committed
eth/tracers: package restructuring ethereum#23857
1 parent db50cdd commit 03abb17

Some content is hidden

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

55 files changed

+1406
-1378
lines changed

cmd/XDC/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ import (
4242
"github.com/XinFinOrg/XDPoSChain/params"
4343
"github.com/urfave/cli/v2"
4444

45-
// Force-load the native, to trigger registration
45+
// Force-load the tracer engines to trigger registration
46+
_ "github.com/XinFinOrg/XDPoSChain/eth/tracers/js"
4647
_ "github.com/XinFinOrg/XDPoSChain/eth/tracers/native"
4748
)
4849

core/vm/access_list_tracer.go

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

138138
// CaptureState captures all opcodes that touch storage or addresses and adds them to the accesslist.
139-
func (a *AccessListTracer) CaptureState(env *EVM, pc uint64, op OpCode, gas, cost uint64, scope *ScopeContext, rData []byte, depth int, err error) {
139+
func (a *AccessListTracer) CaptureState(pc uint64, op OpCode, gas, cost uint64, scope *ScopeContext, rData []byte, depth int, err error) {
140140
stack := scope.Stack
141141
if (op == SLOAD || op == SSTORE) && stack.len() >= 1 {
142142
slot := common.Hash(stack.data[stack.len()-1].Bytes32())
@@ -156,7 +156,7 @@ func (a *AccessListTracer) CaptureState(env *EVM, pc uint64, op OpCode, gas, cos
156156
}
157157
}
158158

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

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

core/vm/interpreter.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,9 @@ func (in *EVMInterpreter) Run(contract *Contract, input []byte, readOnly bool) (
160160
defer func() {
161161
if err != nil {
162162
if !logged {
163-
in.evm.Config.Tracer.CaptureState(in.evm, pcCopy, op, gasCopy, cost, callContext, in.returnData, in.evm.depth, err)
163+
in.evm.Config.Tracer.CaptureState(pcCopy, op, gasCopy, cost, callContext, in.returnData, in.evm.depth, err)
164164
} else {
165-
in.evm.Config.Tracer.CaptureFault(in.evm, pcCopy, op, gasCopy, cost, callContext, in.evm.depth, err)
165+
in.evm.Config.Tracer.CaptureFault(pcCopy, op, gasCopy, cost, callContext, in.evm.depth, err)
166166
}
167167
}
168168
}()
@@ -220,14 +220,14 @@ func (in *EVMInterpreter) Run(contract *Contract, input []byte, readOnly bool) (
220220

221221
// Do tracing before memory expansion
222222
if debug {
223-
in.evm.Config.Tracer.CaptureState(in.evm, pc, op, gasCopy, cost, callContext, in.returnData, in.evm.depth, err)
223+
in.evm.Config.Tracer.CaptureState(pc, op, gasCopy, cost, callContext, in.returnData, in.evm.depth, err)
224224
logged = true
225225
}
226226
if memorySize > 0 {
227227
mem.Resize(memorySize)
228228
}
229229
} else if debug {
230-
in.evm.Config.Tracer.CaptureState(in.evm, pc, op, gasCopy, cost, callContext, in.returnData, in.evm.depth, err)
230+
in.evm.Config.Tracer.CaptureState(pc, op, gasCopy, cost, callContext, in.returnData, in.evm.depth, err)
231231
logged = true
232232
}
233233
// execute the operation

core/vm/logger.go

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

@@ -120,6 +120,7 @@ type EVMLogger interface {
120120
// contract their storage.
121121
type StructLogger struct {
122122
cfg LogConfig
123+
env *EVM
123124

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

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

153155
// CaptureState logs a new structured log message and pushes it out to the environment
154156
//
155157
// CaptureState also tracks SLOAD/SSTORE ops to track storage change.
156-
func (l *StructLogger) CaptureState(env *EVM, pc uint64, op OpCode, gas, cost uint64, scope *ScopeContext, rData []byte, depth int, err error) {
158+
func (l *StructLogger) CaptureState(pc uint64, op OpCode, gas, cost uint64, scope *ScopeContext, rData []byte, depth int, err error) {
157159
memory := scope.Memory
158160
stack := scope.Stack
159161
contract := scope.Contract
@@ -187,7 +189,7 @@ func (l *StructLogger) CaptureState(env *EVM, pc uint64, op OpCode, gas, cost ui
187189
if op == SLOAD && stack.len() >= 1 {
188190
var (
189191
address = common.Hash(stack.data[stack.len()-1].Bytes32())
190-
value = env.StateDB.GetState(contract.Address(), address)
192+
value = l.env.StateDB.GetState(contract.Address(), address)
191193
)
192194
l.storage[contract.Address()][address] = value
193195
storage = l.storage[contract.Address()].Copy()
@@ -207,13 +209,13 @@ func (l *StructLogger) CaptureState(env *EVM, pc uint64, op OpCode, gas, cost ui
207209
copy(rdata, rData)
208210
}
209211
// create a new snapshot of the EVM.
210-
log := StructLog{pc, op, gas, cost, mem, memory.Len(), stck, rdata, storage, depth, env.StateDB.GetRefund(), err}
212+
log := StructLog{pc, op, gas, cost, mem, memory.Len(), stck, rdata, storage, depth, l.env.StateDB.GetRefund(), err}
211213
l.logs = append(l.logs, log)
212214
}
213215

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

219221
// CaptureEnd is called after the call finishes to finalize the tracing.
@@ -292,19 +294,21 @@ func WriteLogs(writer io.Writer, logs []*types.Log) {
292294
type mdLogger struct {
293295
out io.Writer
294296
cfg *LogConfig
297+
env *EVM
295298
}
296299

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

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

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

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

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

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(), len(logger.storage[contract.Address()]))
6869
}

core/vm/runtime/runtime_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ import (
3333
"github.com/XinFinOrg/XDPoSChain/core/types"
3434
"github.com/XinFinOrg/XDPoSChain/core/vm"
3535
"github.com/XinFinOrg/XDPoSChain/params"
36+
37+
// force-load js tracers to trigger registration
38+
_ "github.com/XinFinOrg/XDPoSChain/eth/tracers/js"
3639
)
3740

3841
func TestDefaults(t *testing.T) {
@@ -486,7 +489,7 @@ func BenchmarkSimpleLoop(b *testing.B) {
486489
// TestEip2929Cases contains various testcases that are used for
487490
// EIP-2929 about gas repricings
488491
func TestEip2929Cases(t *testing.T) {
489-
492+
t.Skip("Test only useful for generating documentation")
490493
id := 1
491494
prettyPrint := func(comment string, code []byte) {
492495

0 commit comments

Comments
 (0)