mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-26 14:46:42 +00:00
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.
This commit is contained in:
parent
4e777fb7aa
commit
85921bbb85
5 changed files with 6 additions and 45 deletions
|
|
@ -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),
|
||||
|
|
|
|||
|
|
@ -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():
|
||||
// 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)
|
||||
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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{} },
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Reference in a new issue