diff --git a/core/tracing/hooks.go b/core/tracing/hooks.go index 7ac08a6b44..bb82fc831f 100644 --- a/core/tracing/hooks.go +++ b/core/tracing/hooks.go @@ -25,13 +25,15 @@ import ( "github.com/holiman/uint256" ) -type ScopeContext interface { - GetMemoryData() []byte - GetStackData() []uint256.Int - GetCaller() common.Address - GetAddress() common.Address - GetCallValue() *uint256.Int - GetCallInput() []byte +// OpContext provides the context at which the opcode is being +// executed in, including the memory, stack and various contract-level information. +type OpContext interface { + MemoryData() []byte + StackData() []uint256.Int + Caller() common.Address + Address() common.Address + CallValue() *uint256.Int + CallInput() []byte } type StateDB interface { @@ -98,8 +100,8 @@ type Hooks struct { // be indicated by `reverted == false` and `err == ErrCodeStoreOutOfGas`. CaptureExit func(output []byte, gasUsed uint64, err error, reverted bool) // Opcode level - CaptureState func(pc uint64, op OpCode, gas, cost uint64, scope ScopeContext, rData []byte, depth int, err error) - CaptureFault func(pc uint64, op OpCode, gas, cost uint64, scope ScopeContext, depth int, err error) + CaptureState func(pc uint64, op OpCode, gas, cost uint64, scope OpContext, rData []byte, depth int, err error) + CaptureFault func(pc uint64, op OpCode, gas, cost uint64, scope OpContext, depth int, err error) CaptureKeccakPreimage func(hash common.Hash, data []byte) // Misc OnGasChange func(old, new uint64, reason GasChangeReason) diff --git a/core/vm/interpreter.go b/core/vm/interpreter.go index bfac7fd3ef..ff7d47d28b 100644 --- a/core/vm/interpreter.go +++ b/core/vm/interpreter.go @@ -41,33 +41,33 @@ type ScopeContext struct { Contract *Contract } -func (ctx *ScopeContext) GetMemoryData() []byte { +func (ctx *ScopeContext) MemoryData() []byte { if ctx.Memory == nil { return nil } return ctx.Memory.Data() } -func (ctx *ScopeContext) GetStackData() []uint256.Int { +func (ctx *ScopeContext) StackData() []uint256.Int { if ctx.Stack == nil { return nil } return ctx.Stack.Data() } -func (ctx *ScopeContext) GetCaller() common.Address { +func (ctx *ScopeContext) Caller() common.Address { return ctx.Contract.Caller() } -func (ctx *ScopeContext) GetAddress() common.Address { +func (ctx *ScopeContext) Address() common.Address { return ctx.Contract.Address() } -func (ctx *ScopeContext) GetCallValue() *uint256.Int { +func (ctx *ScopeContext) CallValue() *uint256.Int { return ctx.Contract.Value() } -func (ctx *ScopeContext) GetCallInput() []byte { +func (ctx *ScopeContext) CallInput() []byte { return ctx.Contract.Input } diff --git a/eth/tracers/directory/noop.go b/eth/tracers/directory/noop.go index da3bd86bde..8944e62251 100644 --- a/eth/tracers/directory/noop.go +++ b/eth/tracers/directory/noop.go @@ -68,11 +68,11 @@ func (t *NoopTracer) CaptureEnd(output []byte, gasUsed uint64, err error, revert } // CaptureState implements the EVMLogger interface to trace a single step of VM execution. -func (t *NoopTracer) CaptureState(pc uint64, op tracing.OpCode, gas, cost uint64, scope tracing.ScopeContext, rData []byte, depth int, err error) { +func (t *NoopTracer) CaptureState(pc uint64, op tracing.OpCode, gas, cost uint64, scope tracing.OpContext, rData []byte, depth int, err error) { } // CaptureFault implements the EVMLogger interface to trace an execution fault. -func (t *NoopTracer) CaptureFault(pc uint64, op tracing.OpCode, gas, cost uint64, _ tracing.ScopeContext, depth int, err error) { +func (t *NoopTracer) CaptureFault(pc uint64, op tracing.OpCode, gas, cost uint64, _ tracing.OpContext, depth int, err error) { } // CaptureKeccakPreimage is called during the KECCAK256 opcode. diff --git a/eth/tracers/js/goja.go b/eth/tracers/js/goja.go index 5de49608bf..01618f7331 100644 --- a/eth/tracers/js/goja.go +++ b/eth/tracers/js/goja.go @@ -310,7 +310,7 @@ func (t *jsTracer) CaptureStart(from common.Address, to common.Address, create b } // CaptureState implements the Tracer interface to trace a single step of VM execution. -func (t *jsTracer) CaptureState(pc uint64, op tracing.OpCode, gas, cost uint64, scope tracing.ScopeContext, rData []byte, depth int, err error) { +func (t *jsTracer) CaptureState(pc uint64, op tracing.OpCode, gas, cost uint64, scope tracing.OpContext, rData []byte, depth int, err error) { if !t.traceStep { return } @@ -320,8 +320,8 @@ func (t *jsTracer) CaptureState(pc uint64, op tracing.OpCode, gas, cost uint64, log := t.log log.op.op = vm.OpCode(op) - log.memory.memory = scope.GetMemoryData() - log.stack.stack = scope.GetStackData() + log.memory.memory = scope.MemoryData() + log.stack.stack = scope.StackData() log.contract.scope = scope log.pc = pc log.gas = gas @@ -335,7 +335,7 @@ func (t *jsTracer) CaptureState(pc uint64, op tracing.OpCode, gas, cost uint64, } // CaptureFault implements the Tracer interface to trace an execution fault -func (t *jsTracer) CaptureFault(pc uint64, op tracing.OpCode, gas, cost uint64, scope tracing.ScopeContext, depth int, err error) { +func (t *jsTracer) CaptureFault(pc uint64, op tracing.OpCode, gas, cost uint64, scope tracing.OpContext, depth int, err error) { if t.err != nil { return } @@ -805,14 +805,14 @@ func (do *dbObj) setupObject() *goja.Object { } type contractObj struct { - scope tracing.ScopeContext + scope tracing.OpContext vm *goja.Runtime toBig toBigFn toBuf toBufFn } func (co *contractObj) GetCaller() goja.Value { - caller := co.scope.GetCaller().Bytes() + caller := co.scope.Caller().Bytes() res, err := co.toBuf(co.vm, caller) if err != nil { co.vm.Interrupt(err) @@ -822,7 +822,7 @@ func (co *contractObj) GetCaller() goja.Value { } func (co *contractObj) GetAddress() goja.Value { - addr := co.scope.GetAddress().Bytes() + addr := co.scope.Address().Bytes() res, err := co.toBuf(co.vm, addr) if err != nil { co.vm.Interrupt(err) @@ -832,7 +832,7 @@ func (co *contractObj) GetAddress() goja.Value { } func (co *contractObj) GetValue() goja.Value { - value := co.scope.GetCallValue() + value := co.scope.CallValue() res, err := co.toBig(co.vm, value.String()) if err != nil { co.vm.Interrupt(err) @@ -842,7 +842,7 @@ func (co *contractObj) GetValue() goja.Value { } func (co *contractObj) GetInput() goja.Value { - input := common.CopyBytes(co.scope.GetCallInput()) + input := common.CopyBytes(co.scope.CallInput()) res, err := co.toBuf(co.vm, input) if err != nil { co.vm.Interrupt(err) diff --git a/eth/tracers/live/noop.go b/eth/tracers/live/noop.go index 39b01dcbb3..5fd3702e6c 100644 --- a/eth/tracers/live/noop.go +++ b/eth/tracers/live/noop.go @@ -56,11 +56,11 @@ func (t *noop) CaptureEnd(output []byte, gasUsed uint64, err error, reverted boo } // CaptureState implements the EVMLogger interface to trace a single step of VM execution. -func (t *noop) CaptureState(pc uint64, op tracing.OpCode, gas, cost uint64, scope tracing.ScopeContext, rData []byte, depth int, err error) { +func (t *noop) CaptureState(pc uint64, op tracing.OpCode, gas, cost uint64, scope tracing.OpContext, rData []byte, depth int, err error) { } // CaptureFault implements the EVMLogger interface to trace an execution fault. -func (t *noop) CaptureFault(pc uint64, op tracing.OpCode, gas, cost uint64, _ tracing.ScopeContext, depth int, err error) { +func (t *noop) CaptureFault(pc uint64, op tracing.OpCode, gas, cost uint64, _ tracing.OpContext, depth int, err error) { } // CaptureKeccakPreimage is called during the KECCAK256 opcode. diff --git a/eth/tracers/logger/access_list_tracer.go b/eth/tracers/logger/access_list_tracer.go index a69c8b3048..7e76080a94 100644 --- a/eth/tracers/logger/access_list_tracer.go +++ b/eth/tracers/logger/access_list_tracer.go @@ -140,13 +140,13 @@ func (a *AccessListTracer) Hooks() *tracing.Hooks { } // CaptureState captures all opcodes that touch storage or addresses and adds them to the accesslist. -func (a *AccessListTracer) CaptureState(pc uint64, opcode tracing.OpCode, gas, cost uint64, scope tracing.ScopeContext, rData []byte, depth int, err error) { - stackData := scope.GetStackData() +func (a *AccessListTracer) CaptureState(pc uint64, opcode tracing.OpCode, gas, cost uint64, scope tracing.OpContext, rData []byte, depth int, err error) { + stackData := scope.StackData() stackLen := len(stackData) op := vm.OpCode(opcode) if (op == vm.SLOAD || op == vm.SSTORE) && stackLen >= 1 { slot := common.Hash(stackData[stackLen-1].Bytes32()) - a.list.addSlot(scope.GetAddress(), slot) + a.list.addSlot(scope.Address(), slot) } if (op == vm.EXTCODECOPY || op == vm.EXTCODEHASH || op == vm.EXTCODESIZE || op == vm.BALANCE || op == vm.SELFDESTRUCT) && stackLen >= 1 { addr := common.Address(stackData[stackLen-1].Bytes20()) diff --git a/eth/tracers/logger/logger.go b/eth/tracers/logger/logger.go index 57002aff69..cc5132f3c2 100644 --- a/eth/tracers/logger/logger.go +++ b/eth/tracers/logger/logger.go @@ -162,7 +162,7 @@ func (l *StructLogger) Reset() { // CaptureState logs a new structured log message and pushes it out to the environment // // CaptureState also tracks SLOAD/SSTORE ops to track storage change. -func (l *StructLogger) CaptureState(pc uint64, opcode tracing.OpCode, gas, cost uint64, scope tracing.ScopeContext, rData []byte, depth int, err error) { +func (l *StructLogger) CaptureState(pc uint64, opcode tracing.OpCode, gas, cost uint64, scope tracing.OpContext, rData []byte, depth int, err error) { // If tracing was interrupted, set the error and stop if l.interrupt.Load() { return @@ -173,8 +173,8 @@ func (l *StructLogger) CaptureState(pc uint64, opcode tracing.OpCode, gas, cost } op := vm.OpCode(opcode) - memory := scope.GetMemoryData() - stack := scope.GetStackData() + memory := scope.MemoryData() + stack := scope.StackData() // Copy a snapshot of the current memory state to a new buffer var mem []byte if l.cfg.EnableMemory { @@ -189,7 +189,7 @@ func (l *StructLogger) CaptureState(pc uint64, opcode tracing.OpCode, gas, cost stck[i] = item } } - contractAddr := scope.GetAddress() + contractAddr := scope.Address() stackLen := len(stack) // Copy a snapshot of the current storage to a new container var storage Storage @@ -385,8 +385,8 @@ func (t *mdLogger) CaptureStart(from common.Address, to common.Address, create b } // CaptureState also tracks SLOAD/SSTORE ops to track storage change. -func (t *mdLogger) CaptureState(pc uint64, op tracing.OpCode, gas, cost uint64, scope tracing.ScopeContext, rData []byte, depth int, err error) { - stack := scope.GetStackData() +func (t *mdLogger) CaptureState(pc uint64, op tracing.OpCode, gas, cost uint64, scope tracing.OpContext, rData []byte, depth int, err error) { + stack := scope.StackData() fmt.Fprintf(t.out, "| %4d | %10v | %3d |", pc, op, cost) if !t.cfg.DisableStack { @@ -405,7 +405,7 @@ func (t *mdLogger) CaptureState(pc uint64, op tracing.OpCode, gas, cost uint64, } } -func (t *mdLogger) CaptureFault(pc uint64, op tracing.OpCode, gas, cost uint64, scope tracing.ScopeContext, depth int, err error) { +func (t *mdLogger) CaptureFault(pc uint64, op tracing.OpCode, gas, cost uint64, scope tracing.OpContext, depth int, err error) { fmt.Fprintf(t.out, "\nError: at pc=%d, op=%v: %v\n", pc, op, err) } diff --git a/eth/tracers/logger/logger_json.go b/eth/tracers/logger/logger_json.go index 246b91bdf3..72eda302d8 100644 --- a/eth/tracers/logger/logger_json.go +++ b/eth/tracers/logger/logger_json.go @@ -54,15 +54,15 @@ func (l *JSONLogger) Hooks() *tracing.Hooks { } } -func (l *JSONLogger) CaptureFault(pc uint64, op tracing.OpCode, gas uint64, cost uint64, scope tracing.ScopeContext, depth int, err error) { +func (l *JSONLogger) CaptureFault(pc uint64, op tracing.OpCode, gas uint64, cost uint64, scope tracing.OpContext, depth int, err error) { // TODO: Add rData to this interface as well l.CaptureState(pc, op, gas, cost, scope, nil, depth, err) } // CaptureState outputs state information on the logger. -func (l *JSONLogger) CaptureState(pc uint64, op tracing.OpCode, gas, cost uint64, scope tracing.ScopeContext, rData []byte, depth int, err error) { - memory := scope.GetMemoryData() - stack := scope.GetStackData() +func (l *JSONLogger) CaptureState(pc uint64, op tracing.OpCode, gas, cost uint64, scope tracing.OpContext, rData []byte, depth int, err error) { + memory := scope.MemoryData() + stack := scope.StackData() log := StructLog{ Pc: pc, diff --git a/eth/tracers/native/call.go b/eth/tracers/native/call.go index 33c92403a7..dac4fd0293 100644 --- a/eth/tracers/native/call.go +++ b/eth/tracers/native/call.go @@ -176,7 +176,7 @@ func (t *callTracer) CaptureEnd(output []byte, gasUsed uint64, err error, revert } // CaptureState implements the EVMLogger interface to trace a single step of VM execution. -func (t *callTracer) CaptureState(pc uint64, op tracing.OpCode, gas, cost uint64, scope tracing.ScopeContext, rData []byte, depth int, err error) { +func (t *callTracer) CaptureState(pc uint64, op tracing.OpCode, gas, cost uint64, scope tracing.OpContext, rData []byte, depth int, err error) { } // CaptureEnter is called when EVM enters a new scope (via call, create or selfdestruct). diff --git a/eth/tracers/native/call_flat.go b/eth/tracers/native/call_flat.go index ad3368a1b7..460c159da2 100644 --- a/eth/tracers/native/call_flat.go +++ b/eth/tracers/native/call_flat.go @@ -167,12 +167,12 @@ func (t *flatCallTracer) CaptureEnd(output []byte, gasUsed uint64, err error, re } // CaptureState implements the EVMLogger interface to trace a single step of VM execution. -func (t *flatCallTracer) CaptureState(pc uint64, op tracing.OpCode, gas, cost uint64, scope tracing.ScopeContext, rData []byte, depth int, err error) { +func (t *flatCallTracer) CaptureState(pc uint64, op tracing.OpCode, gas, cost uint64, scope tracing.OpContext, rData []byte, depth int, err error) { t.tracer.CaptureState(pc, op, gas, cost, scope, rData, depth, err) } // CaptureFault implements the EVMLogger interface to trace an execution fault. -func (t *flatCallTracer) CaptureFault(pc uint64, op tracing.OpCode, gas, cost uint64, scope tracing.ScopeContext, depth int, err error) { +func (t *flatCallTracer) CaptureFault(pc uint64, op tracing.OpCode, gas, cost uint64, scope tracing.OpContext, depth int, err error) { t.tracer.CaptureFault(pc, op, gas, cost, scope, depth, err) } diff --git a/eth/tracers/native/mux.go b/eth/tracers/native/mux.go index 49b071ee70..9804c57ce6 100644 --- a/eth/tracers/native/mux.go +++ b/eth/tracers/native/mux.go @@ -99,7 +99,7 @@ func (t *muxTracer) CaptureEnd(output []byte, gasUsed uint64, err error, reverte } // CaptureState implements the EVMLogger interface to trace a single step of VM execution. -func (t *muxTracer) CaptureState(pc uint64, op tracing.OpCode, gas, cost uint64, scope tracing.ScopeContext, rData []byte, depth int, err error) { +func (t *muxTracer) CaptureState(pc uint64, op tracing.OpCode, gas, cost uint64, scope tracing.OpContext, rData []byte, depth int, err error) { for _, t := range t.tracers { if t.CaptureState != nil { t.CaptureState(pc, op, gas, cost, scope, rData, depth, err) @@ -108,7 +108,7 @@ func (t *muxTracer) CaptureState(pc uint64, op tracing.OpCode, gas, cost uint64, } // CaptureFault implements the EVMLogger interface to trace an execution fault. -func (t *muxTracer) CaptureFault(pc uint64, op tracing.OpCode, gas, cost uint64, scope tracing.ScopeContext, depth int, err error) { +func (t *muxTracer) CaptureFault(pc uint64, op tracing.OpCode, gas, cost uint64, scope tracing.OpContext, depth int, err error) { for _, t := range t.tracers { if t.CaptureFault != nil { t.CaptureFault(pc, op, gas, cost, scope, depth, err) diff --git a/eth/tracers/native/prestate.go b/eth/tracers/native/prestate.go index 6f0a14fdcd..43f8261372 100644 --- a/eth/tracers/native/prestate.go +++ b/eth/tracers/native/prestate.go @@ -100,7 +100,7 @@ func newPrestateTracer(ctx *directory.Context, cfg json.RawMessage) (*directory. } // CaptureState implements the EVMLogger interface to trace a single step of VM execution. -func (t *prestateTracer) CaptureState(pc uint64, opcode tracing.OpCode, gas, cost uint64, scope tracing.ScopeContext, rData []byte, depth int, err error) { +func (t *prestateTracer) CaptureState(pc uint64, opcode tracing.OpCode, gas, cost uint64, scope tracing.OpContext, rData []byte, depth int, err error) { if err != nil { return } @@ -109,9 +109,9 @@ func (t *prestateTracer) CaptureState(pc uint64, opcode tracing.OpCode, gas, cos return } op := vm.OpCode(opcode) - stackData := scope.GetStackData() + stackData := scope.StackData() stackLen := len(stackData) - caller := scope.GetAddress() + caller := scope.Address() switch { case stackLen >= 1 && (op == vm.SLOAD || op == vm.SSTORE): slot := common.Hash(stackData[stackLen-1].Bytes32()) @@ -133,7 +133,7 @@ func (t *prestateTracer) CaptureState(pc uint64, opcode tracing.OpCode, gas, cos case stackLen >= 4 && op == vm.CREATE2: offset := stackData[stackLen-2] size := stackData[stackLen-3] - init, err := directory.GetMemoryCopyPadded(scope.GetMemoryData(), int64(offset.Uint64()), int64(size.Uint64())) + init, err := directory.GetMemoryCopyPadded(scope.MemoryData(), int64(offset.Uint64()), int64(size.Uint64())) if err != nil { log.Warn("failed to copy CREATE2 input", "err", err, "tracer", "prestateTracer", "offset", offset, "size", size) return