Skip to content
12 changes: 0 additions & 12 deletions core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,18 +502,6 @@ func (st *StateTransition) TransitionDb() (*ExecutionResult, error) {
return nil, err
}

if tracer := st.evm.Config.Tracer; tracer != nil {
var payer *common.Address
if st.msg.From != st.msg.Payer {
payerAddr := st.msg.Payer
payer = &payerAddr
}
tracer.CaptureTxStart(st.initialGas, payer)
defer func() {
tracer.CaptureTxEnd(st.gas)
}()
}

msg := st.msg
sender := vm.AccountRef(msg.From)
rules := st.evm.ChainConfig().Rules(st.evm.Context.BlockNumber)
Expand Down
115 changes: 0 additions & 115 deletions core/vm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"errors"
"math/big"
"sync/atomic"
"time"

"github.com/ethereum/go-ethereum/core/tracing"
"github.com/ethereum/go-ethereum/core/types"
Expand Down Expand Up @@ -235,31 +234,16 @@ func (evm *EVM) Call(caller ContractRef, addr common.Address, input []byte, gas
evm.captureEnd(evm.depth, startGas, leftOverGas, ret, err)
}(gas)
}
debug := evm.Config.Tracer != nil
captureTraceEarly := func(err error) {
if debug && evm.Config.FullCallTracing {
if evm.depth == 0 {
evm.Config.Tracer.CaptureStart(evm, caller.Address(), addr, false, input, gas, value)
evm.Config.Tracer.CaptureEnd(ret, 0, err)
} else {
evm.Config.Tracer.CaptureEnter(CALL, caller.Address(), addr, input, gas, value)
evm.Config.Tracer.CaptureExit(ret, 0, err)
}
}
}

if evm.Config.NoRecursion && evm.depth > 0 {
captureTraceEarly(nil)
return nil, gas, nil
}
// Fail if we're trying to execute above the call depth limit
if evm.depth > int(params.CallCreateDepth) {
captureTraceEarly(ErrDepth)
return nil, gas, ErrDepth
}
// Fail if we're trying to transfer more than the available balance
if value.Sign() != 0 && !evm.Context.CanTransfer(evm.StateDB, caller.Address(), value) {
captureTraceEarly(ErrInsufficientBalance)
return nil, gas, ErrInsufficientBalance
}
snapshot := evm.StateDB.Snapshot()
Expand All @@ -268,37 +252,12 @@ func (evm *EVM) Call(caller ContractRef, addr common.Address, input []byte, gas
if !evm.StateDB.Exist(addr) {
if !isPrecompile && evm.chainRules.IsEIP158 && value.Sign() == 0 {
// Calling a non existing account, don't do anything, but ping the tracer
if debug {
if evm.depth == 0 {
evm.Config.Tracer.CaptureStart(evm, caller.Address(), addr, false, input, gas, value)
evm.Config.Tracer.CaptureEnd(ret, 0, nil)
} else {
evm.Config.Tracer.CaptureEnter(CALL, caller.Address(), addr, input, gas, value)
evm.Config.Tracer.CaptureExit(ret, 0, nil)
}
}
return nil, gas, nil
}
evm.StateDB.CreateAccount(addr)
}
evm.Context.Transfer(evm.StateDB, caller.Address(), addr, value)

// Capture the tracer start/end events in debug mode
if debug {
if evm.depth == 0 {
evm.Config.Tracer.CaptureStart(evm, caller.Address(), addr, false, input, gas, value)
defer func(startGas uint64, startTime time.Time) { // Lazy evaluation of the parameters
evm.Config.Tracer.CaptureEnd(ret, startGas-gas, err)
}(gas, time.Now())
} else {
// Handle tracer events for entering and exiting a call frame
evm.Config.Tracer.CaptureEnter(CALL, caller.Address(), addr, input, gas, value)
defer func(startGas uint64) {
evm.Config.Tracer.CaptureExit(ret, startGas-gas, err)
}(gas)
}
}

if isPrecompile {
ret, gas, err = RunPrecompiledContract(p, input, gas, evm.Config.LiveTracer)
} else {
Expand Down Expand Up @@ -366,14 +325,6 @@ func (evm *EVM) CallCode(caller ContractRef, addr common.Address, input []byte,
}
var snapshot = evm.StateDB.Snapshot()

// Invoke tracer hooks that signal entering/exiting a call frame
if evm.Config.Tracer != nil {
evm.Config.Tracer.CaptureEnter(CALLCODE, caller.Address(), addr, input, gas, value)
defer func(startGas uint64) {
evm.Config.Tracer.CaptureExit(ret, startGas-gas, err)
}(gas)
}

// It is allowed to call precompiles, even via delegatecall
if p, isPrecompile := evm.precompile(caller, addr); isPrecompile {
ret, gas, err = RunPrecompiledContract(p, input, gas, evm.Config.LiveTracer)
Expand Down Expand Up @@ -415,38 +366,16 @@ func (evm *EVM) DelegateCall(caller ContractRef, addr common.Address, input []by
evm.captureEnd(evm.depth, startGas, leftOverGas, ret, err)
}(gas)
}
debug := evm.Config.Tracer != nil
captureTraceEarly := func(err error) {
if debug && evm.Config.FullCallTracing {
parent := caller.(*Contract)
evm.Config.Tracer.CaptureEnter(DELEGATECALL, caller.Address(), addr, input, gas, parent.value)
evm.Config.Tracer.CaptureExit(ret, 0, err)
}
}

if evm.Config.NoRecursion && evm.depth > 0 {
captureTraceEarly(nil)
return nil, gas, nil
}
// Fail if we're trying to execute above the call depth limit
if evm.depth > int(params.CallCreateDepth) {
captureTraceEarly(ErrDepth)
return nil, gas, ErrDepth
}
var snapshot = evm.StateDB.Snapshot()

// Invoke tracer hooks that signal entering/exiting a call frame
if evm.Config.Tracer != nil {
// NOTE: caller must, at all times be a contract. It should never happen
// that caller is something other than a Contract.
parent := caller.(*Contract)
// DELEGATECALL inherits value from parent call
evm.Config.Tracer.CaptureEnter(DELEGATECALL, caller.Address(), addr, input, gas, parent.value)
defer func(startGas uint64) {
evm.Config.Tracer.CaptureExit(ret, startGas-gas, err)
}(gas)
}

// It is allowed to call precompiles, even via delegatecall
if p, isPrecompile := evm.precompile(caller, addr); isPrecompile {
ret, gas, err = RunPrecompiledContract(p, input, gas, evm.Config.LiveTracer)
Expand Down Expand Up @@ -502,14 +431,6 @@ func (evm *EVM) StaticCall(caller ContractRef, addr common.Address, input []byte
// future scenarios
evm.StateDB.AddBalance(addr, big0, tracing.BalanceChangeTouchAccount)

// Invoke tracer hooks that signal entering/exiting a call frame
if evm.Config.Tracer != nil {
evm.Config.Tracer.CaptureEnter(STATICCALL, caller.Address(), addr, input, gas, nil)
defer func(startGas uint64) {
evm.Config.Tracer.CaptureExit(ret, startGas-gas, err)
}(gas)
}

if p, isPrecompile := evm.precompile(caller, addr); isPrecompile {
ret, gas, err = RunPrecompiledContract(p, input, gas, evm.Config.LiveTracer)
} else {
Expand Down Expand Up @@ -560,20 +481,6 @@ func (evm *EVM) create(caller ContractRef, codeAndHash *codeAndHash, gas uint64,
}(gas)
}

debug := evm.Config.Tracer != nil

captureTraceEarly := func(err error) {
if debug && evm.Config.FullCallTracing {
if evm.depth == 0 {
evm.Config.Tracer.CaptureStart(evm, caller.Address(), address, true, codeAndHash.code, gas, value)
evm.Config.Tracer.CaptureEnd(nil, 0, err)
} else {
evm.Config.Tracer.CaptureEnter(typ, caller.Address(), address, codeAndHash.code, gas, value)
evm.Config.Tracer.CaptureExit(nil, 0, err)
}
}
}

if evm.evmHook != nil {
mustReturn, ret, address, gas, err := evm.evmHook.CreateHook(evm, gas, caller.Address())
if mustReturn {
Expand All @@ -585,12 +492,10 @@ func (evm *EVM) create(caller ContractRef, codeAndHash *codeAndHash, gas uint64,
if !evm.chainRules.IsVenoki {
if evm.chainRules.IsAntenna {
if !evm.StateDB.ValidDeployerV2(caller.Address(), evm.Context.Time, evm.ChainConfig().WhiteListDeployerContractV2Address) {
captureTraceEarly(ErrExecutionReverted)
return nil, common.Address{}, gas, ErrExecutionReverted
}
} else if evm.chainRules.IsOdysseusFork {
if !evm.StateDB.ValidDeployer(caller.Address()) {
captureTraceEarly(ErrExecutionReverted)
return nil, common.Address{}, gas, ErrExecutionReverted
}
}
Expand All @@ -599,17 +504,14 @@ func (evm *EVM) create(caller ContractRef, codeAndHash *codeAndHash, gas uint64,
// Depth check execution. Fail if we're trying to execute above the
// limit.
if evm.depth > int(params.CallCreateDepth) {
captureTraceEarly(ErrDepth)
return nil, common.Address{}, gas, ErrDepth
}
if !evm.Context.CanTransfer(evm.StateDB, caller.Address(), value) {
captureTraceEarly(ErrInsufficientBalance)
return nil, common.Address{}, gas, ErrInsufficientBalance
}
nonce := evm.StateDB.GetNonce(caller.Address())

if nonce+1 < nonce {
captureTraceEarly(ErrNonceUintOverflow)
return nil, common.Address{}, gas, ErrNonceUintOverflow
}
evm.StateDB.SetNonce(caller.Address(), nonce+1)
Expand All @@ -621,7 +523,6 @@ func (evm *EVM) create(caller ContractRef, codeAndHash *codeAndHash, gas uint64,
// Ensure there's no existing contract already at the designated address
contractHash := evm.StateDB.GetCodeHash(address)
if evm.StateDB.GetNonce(address) != 0 || (contractHash != (common.Hash{}) && contractHash != emptyCodeHash) {
captureTraceEarly(ErrContractAddressCollision)
if evm.Config.LiveTracer != nil && evm.Config.LiveTracer.OnGasChange != nil {
evm.Config.LiveTracer.OnGasChange(gas, 0, tracing.GasChangeCallFailedExecution)
}
Expand All @@ -641,18 +542,9 @@ func (evm *EVM) create(caller ContractRef, codeAndHash *codeAndHash, gas uint64,
contract.SetCodeOptionalHash(&address, codeAndHash)

if evm.Config.NoRecursion && evm.depth > 0 {
captureTraceEarly(nil)
return nil, address, gas, nil
}

if debug {
if evm.depth == 0 {
evm.Config.Tracer.CaptureStart(evm, caller.Address(), address, true, codeAndHash.code, gas, value)
} else {
evm.Config.Tracer.CaptureEnter(typ, caller.Address(), address, codeAndHash.code, gas, value)
}
}

ret, err = evm.interpreter.Run(contract, nil, false)

// Check whether the max code size has been exceeded, assign err if the case.
Expand Down Expand Up @@ -696,13 +588,6 @@ func (evm *EVM) create(caller ContractRef, codeAndHash *codeAndHash, gas uint64,
}
}

if debug {
if evm.depth == 0 {
evm.Config.Tracer.CaptureEnd(ret, gas-contract.Gas, err)
} else {
evm.Config.Tracer.CaptureExit(ret, gas-contract.Gas, err)
}
}
return ret, address, contract.Gas, err
}

Expand Down
8 changes: 0 additions & 8 deletions core/vm/instructions.go
Original file line number Diff line number Diff line change
Expand Up @@ -807,10 +807,6 @@ func opSelfdestruct(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext
balance := interpreter.evm.StateDB.GetBalance(scope.Contract.Address())
interpreter.evm.StateDB.AddBalance(beneficiary.Bytes20(), balance, tracing.BalanceIncreaseSelfdestruct)
interpreter.evm.StateDB.SelfDestruct(scope.Contract.Address())
if interpreter.evm.Config.Tracer != nil {
interpreter.cfg.Tracer.CaptureEnter(SELFDESTRUCT, scope.Contract.Address(), beneficiary.Bytes20(), []byte{}, 0, balance)
interpreter.cfg.Tracer.CaptureExit([]byte{}, 0, nil)
}
if tracer := interpreter.evm.Config.LiveTracer; tracer != nil {
if tracer.OnEnter != nil {
tracer.OnEnter(interpreter.evm.depth, byte(SELFDESTRUCT), scope.Contract.Address(), beneficiary.Bytes20(), []byte{}, 0, balance)
Expand All @@ -828,10 +824,6 @@ func opSelfdestruct6780(pc *uint64, interpreter *EVMInterpreter, scope *ScopeCon
interpreter.evm.StateDB.SubBalance(scope.Contract.Address(), balance, tracing.BalanceDecreaseSelfdestruct)
interpreter.evm.StateDB.AddBalance(beneficiary.Bytes20(), balance, tracing.BalanceIncreaseSelfdestruct)
interpreter.evm.StateDB.SelfDestruct6780(scope.Contract.Address())
if tracer := interpreter.evm.Config.Tracer; tracer != nil {
tracer.CaptureEnter(SELFDESTRUCT, scope.Contract.Address(), beneficiary.Bytes20(), []byte{}, 0, balance)
tracer.CaptureExit([]byte{}, 0, nil)
}
if tracer := interpreter.evm.Config.LiveTracer; tracer != nil {
if tracer.OnEnter != nil {
tracer.OnEnter(interpreter.evm.depth, byte(SELFDESTRUCT), scope.Contract.Address(), beneficiary.Bytes20(), []byte{}, 0, balance)
Expand Down
40 changes: 10 additions & 30 deletions core/vm/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,13 +208,11 @@ func (in *EVMInterpreter) Run(contract *Contract, input []byte, readOnly bool) (
pc = uint64(0) // program counter
cost uint64
// copies used by tracer
pcCopy uint64 // needed for the deferred EVMLogger
gasCopy uint64 // for EVMLogger to log gas remaining before execution
logged bool // deferred EVMLogger should ignore already logged steps
loggedLiveTracer bool // deferred LiveTracer should ignore already logged steps
res []byte // result of the opcode execution function
debug = in.evm.Config.Tracer != nil
debugLiveTracer = in.evm.Config.LiveTracer != nil
pcCopy uint64 // needed for the deferred EVMLogger
gasCopy uint64 // for EVMLogger to log gas remaining before execution
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we remove struct EVMLogger ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#91
Nice catch, please review

logged bool // deferred LiveTracer should ignore already logged steps
res []byte // result of the opcode execution function
debug = in.evm.Config.LiveTracer != nil
)
// Don't move this deferrred function, it's placed before the capturestate-deferred method,
// so that it get's executed _after_: the capturestate needs the stacks before
Expand All @@ -224,28 +222,15 @@ func (in *EVMInterpreter) Run(contract *Contract, input []byte, readOnly bool) (
}()
contract.Input = input

// TODO: remove old tracer
if debug {
defer func() {
if err != nil {
if !logged {
in.cfg.Tracer.CaptureState(pcCopy, op, gasCopy, cost, callContext, in.returnData, in.evm.depth, err)
} else {
in.cfg.Tracer.CaptureFault(pcCopy, op, gasCopy, cost, callContext, in.evm.depth, err)
}
}
}()
}

if debugLiveTracer {
defer func() {
if err == nil {
return
}
if !loggedLiveTracer && in.evm.Config.LiveTracer.OnOpcode != nil {
if !logged && in.evm.Config.LiveTracer.OnOpcode != nil {
in.evm.Config.LiveTracer.OnOpcode(pcCopy, byte(op), gasCopy, cost, callContext, in.returnData, in.evm.depth, VMErrorFromErr(err))
}
if loggedLiveTracer && in.evm.Config.LiveTracer.OnFault != nil {
if logged && in.evm.Config.LiveTracer.OnFault != nil {
in.evm.Config.LiveTracer.OnFault(pcCopy, byte(op), gasCopy, cost, callContext, in.evm.depth, VMErrorFromErr(err))
}
}()
Expand All @@ -262,7 +247,7 @@ func (in *EVMInterpreter) Run(contract *Contract, input []byte, readOnly bool) (
}
if debug {
// Capture pre-execution values for tracing.
logged, loggedLiveTracer, pcCopy, gasCopy = false, false, pc, contract.Gas
logged, logged, pcCopy, gasCopy = false, false, pc, contract.Gas
}

// Get the operation from the jump table and validate the stack to ensure there are
Expand Down Expand Up @@ -326,24 +311,19 @@ func (in *EVMInterpreter) Run(contract *Contract, input []byte, readOnly bool) (
}

// Do tracing before memory expansion
if debugLiveTracer {
if debug {
if in.evm.Config.LiveTracer.OnGasChange != nil {
in.evm.Config.LiveTracer.OnGasChange(gasCopy, gasCopy-cost, tracing.GasChangeCallOpCode)
}
if in.evm.Config.LiveTracer.OnOpcode != nil {
in.evm.Config.LiveTracer.OnOpcode(pc, byte(op), gasCopy, cost, callContext, in.returnData, in.evm.depth, VMErrorFromErr(err))
loggedLiveTracer = true
logged = true
}
}
if memorySize > 0 {
mem.Resize(memorySize)
}

if debug {
in.cfg.Tracer.CaptureState(pc, op, gasCopy, cost, callContext, in.returnData, in.evm.depth, err)
logged = true
}

// execute the operation
res, err = operation.execute(&pc, in, callContext)
// if the operation clears the return data (e.g. it has returning data)
Expand Down
Loading