mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-26 06:36:43 +00:00
core, core/vm, tests: improved array tables & env context handling
This commit is contained in:
parent
7c2109b71e
commit
54ab362cc1
10 changed files with 421 additions and 431 deletions
|
|
@ -17,6 +17,7 @@
|
|||
package core
|
||||
|
||||
import (
|
||||
"context"
|
||||
"math/big"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
|
|
@ -33,6 +34,8 @@ 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,8 +17,10 @@
|
|||
package vm
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/crypto"
|
||||
|
|
@ -35,6 +37,7 @@ 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
|
||||
|
|
@ -66,29 +69,55 @@ type Environment struct {
|
|||
// Depth is the current call stack
|
||||
Depth int
|
||||
|
||||
// evm is the ethereum virtual machine
|
||||
evm Vm
|
||||
// chainConfig contains information about the current chain
|
||||
chainConfig *params.ChainConfig
|
||||
vmConfig Config
|
||||
// virtual machine configuration options used to initialise the
|
||||
// evm.
|
||||
vmConfig Config
|
||||
// global (to this context) ethereum virtual machine
|
||||
// used throughout the execution of the tx.
|
||||
evm Vm
|
||||
// abort is used to abort the EVM calling operations
|
||||
// NOTE: must be set atomically
|
||||
abort int32
|
||||
|
||||
quit chan struct{}
|
||||
}
|
||||
|
||||
// NewEnvironment retutrns a new EVM environment.
|
||||
func NewEnvironment(context Context, statedb StateDB, chainConfig *params.ChainConfig, vmConfig Config) *Environment {
|
||||
func NewEnvironment(ctx Context, statedb StateDB, chainConfig *params.ChainConfig, vmConfig Config) *Environment {
|
||||
env := &Environment{
|
||||
Context: context,
|
||||
Context: ctx,
|
||||
StateDB: statedb,
|
||||
vmConfig: vmConfig,
|
||||
chainConfig: chainConfig,
|
||||
}
|
||||
|
||||
env.evm = New(env, vmConfig)
|
||||
return env
|
||||
}
|
||||
|
||||
func (env *Environment) init() {
|
||||
env.quit = make(chan struct{})
|
||||
go func() {
|
||||
select {
|
||||
case <-env.quit:
|
||||
return
|
||||
case <-env.Context.Done():
|
||||
atomic.StoreInt32(&env.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 (env *Environment) Call(caller ContractRef, addr common.Address, input []byte, gas, value *big.Int) ([]byte, error) {
|
||||
func (env *Environment) Call(caller ContractRef, addr common.Address, input []byte, gas, value *big.Int) (ret []byte, err error) {
|
||||
if env.Depth == 0 {
|
||||
env.init()
|
||||
defer close(env.quit)
|
||||
}
|
||||
|
||||
if env.vmConfig.NoRecursion && env.Depth > 0 {
|
||||
caller.ReturnGas(gas)
|
||||
|
||||
|
|
@ -131,7 +160,7 @@ func (env *Environment) Call(caller ContractRef, addr common.Address, input []by
|
|||
contract.SetCallCode(&addr, env.StateDB.GetCodeHash(addr), env.StateDB.GetCode(addr))
|
||||
defer contract.Finalise()
|
||||
|
||||
ret, err := env.EVM().Run(contract, input)
|
||||
ret, err = env.EVM().Run(contract, input)
|
||||
// When an error was returned by the EVM or when setting the creation code
|
||||
// above we revert to the snapshot and consume any gas remaining. Additionally
|
||||
// when we're in homestead this also counts for code storage gas errors.
|
||||
|
|
@ -148,7 +177,12 @@ func (env *Environment) Call(caller ContractRef, addr common.Address, input []by
|
|||
// case of an execution error or failed value transfer.
|
||||
//
|
||||
// CallCode differs from Call in the sense that it executes the given address' code with the caller as context.
|
||||
func (env *Environment) CallCode(caller ContractRef, addr common.Address, input []byte, gas, value *big.Int) ([]byte, error) {
|
||||
func (env *Environment) CallCode(caller ContractRef, addr common.Address, input []byte, gas, value *big.Int) (ret []byte, err error) {
|
||||
if env.Depth == 0 {
|
||||
env.init()
|
||||
defer close(env.quit)
|
||||
}
|
||||
|
||||
if env.vmConfig.NoRecursion && env.Depth > 0 {
|
||||
caller.ReturnGas(gas)
|
||||
|
||||
|
|
@ -179,7 +213,7 @@ func (env *Environment) CallCode(caller ContractRef, addr common.Address, input
|
|||
contract.SetCallCode(&addr, env.StateDB.GetCodeHash(addr), env.StateDB.GetCode(addr))
|
||||
defer contract.Finalise()
|
||||
|
||||
ret, err := env.EVM().Run(contract, input)
|
||||
ret, err = env.EVM().Run(contract, input)
|
||||
if err != nil {
|
||||
contract.UseGas(contract.Gas)
|
||||
|
||||
|
|
@ -194,7 +228,12 @@ func (env *Environment) CallCode(caller ContractRef, addr common.Address, input
|
|||
//
|
||||
// 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 (env *Environment) DelegateCall(caller ContractRef, addr common.Address, input []byte, gas *big.Int) ([]byte, error) {
|
||||
func (env *Environment) DelegateCall(caller ContractRef, addr common.Address, input []byte, gas *big.Int) (ret []byte, err error) {
|
||||
if env.Depth == 0 {
|
||||
env.init()
|
||||
defer close(env.quit)
|
||||
}
|
||||
|
||||
if env.vmConfig.NoRecursion && env.Depth > 0 {
|
||||
caller.ReturnGas(gas)
|
||||
|
||||
|
|
@ -218,7 +257,7 @@ func (env *Environment) DelegateCall(caller ContractRef, addr common.Address, in
|
|||
contract.SetCallCode(&addr, env.StateDB.GetCodeHash(addr), env.StateDB.GetCode(addr))
|
||||
defer contract.Finalise()
|
||||
|
||||
ret, err := env.EVM().Run(contract, input)
|
||||
ret, err = env.EVM().Run(contract, input)
|
||||
if err != nil {
|
||||
contract.UseGas(contract.Gas)
|
||||
|
||||
|
|
@ -229,7 +268,12 @@ func (env *Environment) DelegateCall(caller ContractRef, addr common.Address, in
|
|||
}
|
||||
|
||||
// Create creates a new contract using code as deployment code.
|
||||
func (env *Environment) Create(caller ContractRef, code []byte, gas, value *big.Int) ([]byte, common.Address, error) {
|
||||
func (env *Environment) Create(caller ContractRef, code []byte, gas, value *big.Int) (ret []byte, contractAddr common.Address, err error) {
|
||||
if env.Depth == 0 {
|
||||
env.init()
|
||||
defer close(env.quit)
|
||||
}
|
||||
|
||||
if env.vmConfig.NoRecursion && env.Depth > 0 {
|
||||
caller.ReturnGas(gas)
|
||||
|
||||
|
|
@ -254,12 +298,10 @@ func (env *Environment) Create(caller ContractRef, code []byte, gas, value *big.
|
|||
env.StateDB.SetNonce(caller.Address(), nonce+1)
|
||||
|
||||
snapshot := env.StateDB.Snapshot()
|
||||
var (
|
||||
addr = crypto.CreateAddress(caller.Address(), nonce)
|
||||
to = env.StateDB.CreateAccount(addr)
|
||||
)
|
||||
contractAddr = crypto.CreateAddress(caller.Address(), nonce)
|
||||
to := env.StateDB.CreateAccount(contractAddr)
|
||||
if env.ChainConfig().IsEIP158(env.BlockNumber) {
|
||||
env.StateDB.SetNonce(addr, 1)
|
||||
env.StateDB.SetNonce(contractAddr, 1)
|
||||
}
|
||||
env.Transfer(env.StateDB, caller.Address(), to.Address(), value)
|
||||
|
||||
|
|
@ -267,10 +309,11 @@ func (env *Environment) Create(caller ContractRef, code []byte, gas, value *big.
|
|||
// E The contract is a scoped environment for this execution context
|
||||
// only.
|
||||
contract := NewContract(caller, to, value, gas)
|
||||
contract.SetCallCode(&addr, crypto.Keccak256Hash(code), code)
|
||||
contract.SetCallCode(&contractAddr, crypto.Keccak256Hash(code), code)
|
||||
defer contract.Finalise()
|
||||
|
||||
ret, err := env.EVM().Run(contract, nil)
|
||||
ret, err = env.EVM().Run(contract, nil)
|
||||
|
||||
// check whether the max code size has been exceeded
|
||||
maxCodeSizeExceeded := len(ret) > params.MaxCodeSize
|
||||
// if the contract creation ran successfully and no errors were returned
|
||||
|
|
@ -281,7 +324,7 @@ func (env *Environment) Create(caller ContractRef, code []byte, gas, value *big.
|
|||
dataGas := big.NewInt(int64(len(ret)))
|
||||
dataGas.Mul(dataGas, params.CreateDataGas)
|
||||
if contract.UseGas(dataGas) {
|
||||
env.StateDB.SetCode(addr, ret)
|
||||
env.StateDB.SetCode(contractAddr, ret)
|
||||
} else {
|
||||
err = ErrCodeStoreOutOfGas
|
||||
}
|
||||
|
|
@ -296,7 +339,7 @@ func (env *Environment) Create(caller ContractRef, code []byte, gas, value *big.
|
|||
env.StateDB.RevertToSnapshot(snapshot)
|
||||
|
||||
// Nothing should be returned when an error is thrown.
|
||||
return nil, addr, err
|
||||
return nil, contractAddr, err
|
||||
}
|
||||
// If the vm returned with an error the return value should be set to nil.
|
||||
// This isn't consensus critical but merely to for behaviour reasons such as
|
||||
|
|
@ -305,7 +348,7 @@ func (env *Environment) Create(caller ContractRef, code []byte, gas, value *big.
|
|||
ret = nil
|
||||
}
|
||||
|
||||
return ret, addr, err
|
||||
return ret, contractAddr, err
|
||||
}
|
||||
|
||||
// ChainConfig returns the environment's chain configuration
|
||||
|
|
|
|||
|
|
@ -245,70 +245,68 @@ func gasDup(gt params.GasTable, env *Environment, contract *Contract, stack *Sta
|
|||
return GasFastestStep
|
||||
}
|
||||
|
||||
var gasTable [256]*big.Int
|
||||
|
||||
func init() {
|
||||
gasTable[ADD] = GasFastestStep
|
||||
gasTable[LT] = GasFastestStep
|
||||
gasTable[GT] = GasFastestStep
|
||||
gasTable[SLT] = GasFastestStep
|
||||
gasTable[SGT] = GasFastestStep
|
||||
gasTable[EQ] = GasFastestStep
|
||||
gasTable[ISZERO] = GasFastestStep
|
||||
gasTable[SUB] = GasFastestStep
|
||||
gasTable[AND] = GasFastestStep
|
||||
gasTable[OR] = GasFastestStep
|
||||
gasTable[XOR] = GasFastestStep
|
||||
gasTable[NOT] = GasFastestStep
|
||||
gasTable[BYTE] = GasFastestStep
|
||||
gasTable[CALLDATALOAD] = GasFastestStep
|
||||
gasTable[CALLDATACOPY] = GasFastestStep
|
||||
gasTable[MLOAD] = GasFastestStep
|
||||
gasTable[MSTORE] = GasFastestStep
|
||||
gasTable[MSTORE8] = GasFastestStep
|
||||
gasTable[CODECOPY] = GasFastestStep
|
||||
gasTable[MUL] = GasFastStep
|
||||
gasTable[DIV] = GasFastStep
|
||||
gasTable[SDIV] = GasFastStep
|
||||
gasTable[MOD] = GasFastStep
|
||||
gasTable[SMOD] = GasFastStep
|
||||
gasTable[SIGNEXTEND] = GasFastStep
|
||||
gasTable[ADDMOD] = GasMidStep
|
||||
gasTable[MULMOD] = GasMidStep
|
||||
gasTable[JUMP] = GasMidStep
|
||||
gasTable[JUMPI] = GasSlowStep
|
||||
gasTable[EXP] = GasSlowStep
|
||||
gasTable[ADDRESS] = GasQuickStep
|
||||
gasTable[ORIGIN] = GasQuickStep
|
||||
gasTable[CALLER] = GasQuickStep
|
||||
gasTable[CALLVALUE] = GasQuickStep
|
||||
gasTable[CODESIZE] = GasQuickStep
|
||||
gasTable[GASPRICE] = GasQuickStep
|
||||
gasTable[COINBASE] = GasQuickStep
|
||||
gasTable[TIMESTAMP] = GasQuickStep
|
||||
gasTable[NUMBER] = GasQuickStep
|
||||
gasTable[CALLDATASIZE] = GasQuickStep
|
||||
gasTable[DIFFICULTY] = GasQuickStep
|
||||
gasTable[GASLIMIT] = GasQuickStep
|
||||
gasTable[POP] = GasQuickStep
|
||||
gasTable[PC] = GasQuickStep
|
||||
gasTable[MSIZE] = GasQuickStep
|
||||
gasTable[GAS] = GasQuickStep
|
||||
gasTable[BLOCKHASH] = GasExtStep
|
||||
gasTable[BALANCE] = Zero
|
||||
gasTable[EXTCODESIZE] = Zero
|
||||
gasTable[EXTCODECOPY] = Zero
|
||||
gasTable[SLOAD] = params.SloadGas
|
||||
gasTable[SSTORE] = Zero
|
||||
gasTable[SHA3] = params.Sha3Gas
|
||||
gasTable[CREATE] = params.CreateGas
|
||||
gasTable[CALL] = Zero
|
||||
gasTable[CALLCODE] = Zero
|
||||
gasTable[DELEGATECALL] = Zero
|
||||
gasTable[SUICIDE] = Zero
|
||||
gasTable[JUMPDEST] = params.JumpdestGas
|
||||
gasTable[RETURN] = Zero
|
||||
gasTable[PUSH1] = GasFastestStep
|
||||
gasTable[DUP1] = Zero
|
||||
gasTable[STOP] = Zero
|
||||
var gasTable = [256]*big.Int{
|
||||
ADD: GasFastestStep,
|
||||
LT: GasFastestStep,
|
||||
GT: GasFastestStep,
|
||||
SLT: GasFastestStep,
|
||||
SGT: GasFastestStep,
|
||||
EQ: GasFastestStep,
|
||||
ISZERO: GasFastestStep,
|
||||
SUB: GasFastestStep,
|
||||
AND: GasFastestStep,
|
||||
OR: GasFastestStep,
|
||||
XOR: GasFastestStep,
|
||||
NOT: GasFastestStep,
|
||||
BYTE: GasFastestStep,
|
||||
CALLDATALOAD: GasFastestStep,
|
||||
CALLDATACOPY: GasFastestStep,
|
||||
MLOAD: GasFastestStep,
|
||||
MSTORE: GasFastestStep,
|
||||
MSTORE8: GasFastestStep,
|
||||
CODECOPY: GasFastestStep,
|
||||
MUL: GasFastStep,
|
||||
DIV: GasFastStep,
|
||||
SDIV: GasFastStep,
|
||||
MOD: GasFastStep,
|
||||
SMOD: GasFastStep,
|
||||
SIGNEXTEND: GasFastStep,
|
||||
ADDMOD: GasMidStep,
|
||||
MULMOD: GasMidStep,
|
||||
JUMP: GasMidStep,
|
||||
JUMPI: GasSlowStep,
|
||||
EXP: GasSlowStep,
|
||||
ADDRESS: GasQuickStep,
|
||||
ORIGIN: GasQuickStep,
|
||||
CALLER: GasQuickStep,
|
||||
CALLVALUE: GasQuickStep,
|
||||
CODESIZE: GasQuickStep,
|
||||
GASPRICE: GasQuickStep,
|
||||
COINBASE: GasQuickStep,
|
||||
TIMESTAMP: GasQuickStep,
|
||||
NUMBER: GasQuickStep,
|
||||
CALLDATASIZE: GasQuickStep,
|
||||
DIFFICULTY: GasQuickStep,
|
||||
GASLIMIT: GasQuickStep,
|
||||
POP: GasQuickStep,
|
||||
PC: GasQuickStep,
|
||||
MSIZE: GasQuickStep,
|
||||
GAS: GasQuickStep,
|
||||
BLOCKHASH: GasExtStep,
|
||||
BALANCE: Zero,
|
||||
EXTCODESIZE: Zero,
|
||||
EXTCODECOPY: Zero,
|
||||
SLOAD: params.SloadGas,
|
||||
SSTORE: Zero,
|
||||
SHA3: params.Sha3Gas,
|
||||
CREATE: params.CreateGas,
|
||||
CALL: Zero,
|
||||
CALLCODE: Zero,
|
||||
DELEGATECALL: Zero,
|
||||
SUICIDE: Zero,
|
||||
JUMPDEST: params.JumpdestGas,
|
||||
RETURN: Zero,
|
||||
PUSH1: GasFastestStep,
|
||||
DUP1: Zero,
|
||||
STOP: Zero,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -553,6 +553,12 @@ func opCallCode(pc *uint64, env *Environment, contract *Contract, memory *Memory
|
|||
}
|
||||
|
||||
func opDelegateCall(pc *uint64, env *Environment, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
|
||||
// if not homestead return an error. DELEGATECALL is not supported
|
||||
// during pre-homestead.
|
||||
if !env.ChainConfig().IsHomestead(env.BlockNumber) {
|
||||
return nil, fmt.Errorf("invalid opcode %x", DELEGATECALL)
|
||||
}
|
||||
|
||||
gas, to, inOffset, inSize, outOffset, outSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop()
|
||||
|
||||
toAddr := common.BigToAddress(to)
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
|
|
@ -1,38 +0,0 @@
|
|||
// Copyright 2016 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package vm
|
||||
|
||||
import (
|
||||
"math/big"
|
||||
"testing"
|
||||
|
||||
"github.com/ethereum/go-ethereum/params"
|
||||
)
|
||||
|
||||
func TestInit(t *testing.T) {
|
||||
jumpTable := newJumpTable(¶ms.ChainConfig{HomesteadBlock: big.NewInt(1)}, big.NewInt(0))
|
||||
if jumpTable[DELEGATECALL].valid {
|
||||
t.Error("Expected DELEGATECALL not to be present")
|
||||
}
|
||||
|
||||
for _, n := range []int64{1, 2, 100} {
|
||||
jumpTable := newJumpTable(¶ms.ChainConfig{HomesteadBlock: big.NewInt(1)}, big.NewInt(n))
|
||||
if !jumpTable[DELEGATECALL].valid {
|
||||
t.Error("Expected DELEGATECALL to be present for block", n)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -17,6 +17,7 @@
|
|||
package vm
|
||||
|
||||
import (
|
||||
"context"
|
||||
"math/big"
|
||||
"testing"
|
||||
|
||||
|
|
@ -52,7 +53,7 @@ func (d dummyStateDB) GetAccount(common.Address) Account {
|
|||
|
||||
func TestStoreCapture(t *testing.T) {
|
||||
var (
|
||||
env = NewEnvironment(Context{}, nil, params.TestChainConfig, Config{EnableJit: false, ForceJit: false})
|
||||
env = NewEnvironment(Context{Context: context.TODO()}, nil, params.TestChainConfig, Config{EnableJit: false, ForceJit: false})
|
||||
logger = NewStructLogger(nil)
|
||||
mem = NewMemory()
|
||||
stack = newstack()
|
||||
|
|
@ -79,7 +80,7 @@ func TestStorageCapture(t *testing.T) {
|
|||
var (
|
||||
ref = &dummyContractRef{}
|
||||
contract = NewContract(ref, ref, new(big.Int), new(big.Int))
|
||||
env = NewEnvironment(Context{}, dummyStateDB{ref: ref}, params.TestChainConfig, Config{EnableJit: false, ForceJit: false})
|
||||
env = NewEnvironment(Context{Context: context.TODO()}, dummyStateDB{ref: ref}, params.TestChainConfig, Config{EnableJit: false, ForceJit: false})
|
||||
logger = NewStructLogger(nil)
|
||||
mem = NewMemory()
|
||||
stack = newstack()
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@
|
|||
package runtime
|
||||
|
||||
import (
|
||||
"context"
|
||||
"math/big"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
|
|
@ -27,6 +28,7 @@ import (
|
|||
|
||||
func NewEnv(cfg *Config, state *state.StateDB) *vm.Environment {
|
||||
context := vm.Context{
|
||||
Context: context.TODO(),
|
||||
CanTransfer: core.CanTransfer,
|
||||
Transfer: core.Transfer,
|
||||
GetHash: func(uint64) common.Hash { return common.Hash{} },
|
||||
|
|
|
|||
|
|
@ -17,7 +17,6 @@
|
|||
package vm
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"sync/atomic"
|
||||
|
|
@ -52,38 +51,20 @@ type Config struct {
|
|||
// The EVM will run the byte code VM or JIT VM based on the passed
|
||||
// configuration.
|
||||
type EVM struct {
|
||||
env *Environment
|
||||
jumpTable vmJumpTable
|
||||
cfg Config
|
||||
gasTable params.GasTable
|
||||
|
||||
// done is an atomic int and is used for
|
||||
// cancellation during RunWithContext.
|
||||
done int32
|
||||
env *Environment
|
||||
cfg Config
|
||||
gasTable params.GasTable
|
||||
}
|
||||
|
||||
// New returns a new instance of the EVM.
|
||||
func New(env *Environment, cfg Config) *EVM {
|
||||
return &EVM{
|
||||
env: env,
|
||||
jumpTable: newJumpTable(env.ChainConfig(), env.BlockNumber),
|
||||
cfg: cfg,
|
||||
gasTable: env.ChainConfig().GasTable(env.BlockNumber),
|
||||
env: env,
|
||||
cfg: cfg,
|
||||
gasTable: env.ChainConfig().GasTable(env.BlockNumber),
|
||||
}
|
||||
}
|
||||
|
||||
// RunWithContext allows the EVM to be ran with a cancellation method by passing in a context.Context. The EVM
|
||||
// behaves exactly the same as an EVM without a context.
|
||||
//
|
||||
// RunWithContext is only used for the initial call and shouldn't be called more than once.
|
||||
func (evm *EVM) RunWithContext(ctx context.Context, contract *Contract, input []byte) (ret []byte, err error) {
|
||||
go func() {
|
||||
<-ctx.Done()
|
||||
atomic.StoreInt32(&evm.done, 1)
|
||||
}()
|
||||
return evm.Run(contract, input)
|
||||
}
|
||||
|
||||
// Run loops and evaluates the contract's code with the given input data
|
||||
func (evm *EVM) Run(contract *Contract, input []byte) (ret []byte, err error) {
|
||||
evm.env.Depth++
|
||||
|
|
@ -135,16 +116,16 @@ func (evm *EVM) Run(contract *Contract, input []byte) (ret []byte, err error) {
|
|||
// explicit STOP, RETURN or SUICIDE is executed, an error accured during
|
||||
// the execution of one of the operations or until the evm.done is set by
|
||||
// the parent context.Context.
|
||||
for atomic.LoadInt32(&evm.done) == 0 {
|
||||
for atomic.LoadInt32(&evm.env.abort) == 0 {
|
||||
// Get the memory location of pc
|
||||
op = contract.GetOp(pc)
|
||||
|
||||
// get the operation from the jump table matching the opcode
|
||||
operation := evm.jumpTable[op]
|
||||
operation := jumpTable[op]
|
||||
|
||||
// if the op is invalid abort the process and return an error
|
||||
if !operation.valid {
|
||||
return nil, fmt.Errorf("Invalid opcode %x", op)
|
||||
return nil, fmt.Errorf("invalid opcode %x", op)
|
||||
}
|
||||
|
||||
// validate the stack and make sure there enough stack items available
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ package tests
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"math/big"
|
||||
|
|
@ -190,6 +191,8 @@ 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