From 85921bbb858101ceaf86b1b562f6c41ed779d98f Mon Sep 17 00:00:00 2001 From: Jeffrey Wilcke Date: Fri, 23 Dec 2016 09:29:42 +0100 Subject: [PATCH] core/vm: removed context.Context from evm ENV (review change) Removed the context.Context cancellation approach from vm.Environment and added a Cancel method instead; this however makes the environment a one shot object due to the `evm.abort` being set to 1, disallowing any further operation to be done using that environment. --- core/evm.go | 3 --- core/vm/environment.go | 38 ++++---------------------------------- core/vm/logger_test.go | 5 ++--- core/vm/runtime/env.go | 2 -- tests/util.go | 3 --- 5 files changed, 6 insertions(+), 45 deletions(-) diff --git a/core/evm.go b/core/evm.go index a2fe671d9b..6a5713075b 100644 --- a/core/evm.go +++ b/core/evm.go @@ -17,7 +17,6 @@ package core import ( - "context" "math/big" "github.com/ethereum/go-ethereum/common" @@ -34,8 +33,6 @@ type HeaderFetcher interface { // NewEVMContext creates a new context for use in the EVM. func NewEVMContext(msg Message, header *types.Header, chain HeaderFetcher) vm.Context { return vm.Context{ - Context: context.TODO(), - CanTransfer: CanTransfer, Transfer: Transfer, GetHash: GetHashFn(header, chain), diff --git a/core/vm/environment.go b/core/vm/environment.go index db3aaa2a4d..4a5d2d2da4 100644 --- a/core/vm/environment.go +++ b/core/vm/environment.go @@ -17,7 +17,6 @@ package vm import ( - "context" "fmt" "math/big" "sync/atomic" @@ -37,7 +36,6 @@ type ( // Context provides the EVM with auxilary information. Once provided it shouldn't be modified. type Context struct { - context.Context // CanTransfer returns whether the account contains // sufficient ether to transfer the value CanTransfer CanTransferFunc @@ -80,8 +78,6 @@ type EVM struct { // abort is used to abort the EVM calling operations // NOTE: must be set atomically abort int32 - - quit chan struct{} } // NewEVM retutrns a new EVM evmironment. @@ -97,27 +93,16 @@ func NewEVM(ctx Context, statedb StateDB, chainConfig *params.ChainConfig, vmCon return evm } -func (evm *EVM) init() { - evm.quit = make(chan struct{}) - go func() { - select { - case <-evm.quit: - return - case <-evm.Context.Done(): - atomic.StoreInt32(&evm.abort, 1) - } - }() +// Cancel cancels any running EVM operation. This may be called concurrently and it's safe to be +// called multiple times. +func (evm *EVM) Cancel() { + atomic.StoreInt32(&evm.abort, 1) } // Call executes the contract associated with the addr with the given input as paramaters. It also handles any // necessary value transfer required and takes the necessary steps to create accounts and reverses the state in // case of an execution error or failed value transfer. func (evm *EVM) Call(caller ContractRef, addr common.Address, input []byte, gas, value *big.Int) (ret []byte, err error) { - if evm.depth == 0 { - evm.init() - defer close(evm.quit) - } - if evm.vmConfig.NoRecursion && evm.depth > 0 { caller.ReturnGas(gas) @@ -178,11 +163,6 @@ func (evm *EVM) Call(caller ContractRef, addr common.Address, input []byte, gas, // // CallCode differs from Call in the sense that it executes the given address' code with the caller as context. func (evm *EVM) CallCode(caller ContractRef, addr common.Address, input []byte, gas, value *big.Int) (ret []byte, err error) { - if evm.depth == 0 { - evm.init() - defer close(evm.quit) - } - if evm.vmConfig.NoRecursion && evm.depth > 0 { caller.ReturnGas(gas) @@ -229,11 +209,6 @@ func (evm *EVM) CallCode(caller ContractRef, addr common.Address, input []byte, // DelegateCall differs from CallCode in the sense that it executes the given address' code with the caller as context // and the caller is set to the caller of the caller. func (evm *EVM) DelegateCall(caller ContractRef, addr common.Address, input []byte, gas *big.Int) (ret []byte, err error) { - if evm.depth == 0 { - evm.init() - defer close(evm.quit) - } - if evm.vmConfig.NoRecursion && evm.depth > 0 { caller.ReturnGas(gas) @@ -269,11 +244,6 @@ func (evm *EVM) DelegateCall(caller ContractRef, addr common.Address, input []by // Create creates a new contract using code as deployment code. func (evm *EVM) Create(caller ContractRef, code []byte, gas, value *big.Int) (ret []byte, contractAddr common.Address, err error) { - if evm.depth == 0 { - evm.init() - defer close(evm.quit) - } - if evm.vmConfig.NoRecursion && evm.depth > 0 { caller.ReturnGas(gas) diff --git a/core/vm/logger_test.go b/core/vm/logger_test.go index 05db46e323..1d0bd96fad 100644 --- a/core/vm/logger_test.go +++ b/core/vm/logger_test.go @@ -17,7 +17,6 @@ package vm import ( - "context" "math/big" "testing" @@ -53,7 +52,7 @@ func (d dummyStateDB) GetAccount(common.Address) Account { func TestStoreCapture(t *testing.T) { var ( - env = NewEVM(Context{Context: context.TODO()}, nil, params.TestChainConfig, Config{EnableJit: false, ForceJit: false}) + env = NewEVM(Context{}, nil, params.TestChainConfig, Config{EnableJit: false, ForceJit: false}) logger = NewStructLogger(nil) mem = NewMemory() stack = newstack() @@ -80,7 +79,7 @@ func TestStorageCapture(t *testing.T) { var ( ref = &dummyContractRef{} contract = NewContract(ref, ref, new(big.Int), new(big.Int)) - env = NewEVM(Context{Context: context.TODO()}, dummyStateDB{ref: ref}, params.TestChainConfig, Config{EnableJit: false, ForceJit: false}) + env = NewEVM(Context{}, dummyStateDB{ref: ref}, params.TestChainConfig, Config{EnableJit: false, ForceJit: false}) logger = NewStructLogger(nil) mem = NewMemory() stack = newstack() diff --git a/core/vm/runtime/env.go b/core/vm/runtime/env.go index 51d7ba1dbd..a25c6d71c6 100644 --- a/core/vm/runtime/env.go +++ b/core/vm/runtime/env.go @@ -17,7 +17,6 @@ package runtime import ( - "context" "math/big" "github.com/ethereum/go-ethereum/common" @@ -28,7 +27,6 @@ import ( func NewEnv(cfg *Config, state *state.StateDB) *vm.EVM { context := vm.Context{ - Context: context.TODO(), CanTransfer: core.CanTransfer, Transfer: core.Transfer, GetHash: func(uint64) common.Hash { return common.Hash{} }, diff --git a/tests/util.go b/tests/util.go index 91e826f265..a0a6ab3747 100644 --- a/tests/util.go +++ b/tests/util.go @@ -18,7 +18,6 @@ package tests import ( "bytes" - "context" "encoding/hex" "fmt" "math/big" @@ -191,8 +190,6 @@ func NewEVMEnvironment(vmTest bool, chainConfig *params.ChainConfig, statedb *st } context := vm.Context{ - Context: context.TODO(), - CanTransfer: canTransfer, Transfer: transfer, GetHash: func(n uint64) common.Hash {