mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-26 14:46:42 +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
|
package core
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"math/big"
|
"math/big"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
|
|
@ -33,6 +34,8 @@ type HeaderFetcher interface {
|
||||||
// NewEVMContext creates a new context for use in the EVM.
|
// NewEVMContext creates a new context for use in the EVM.
|
||||||
func NewEVMContext(msg Message, header *types.Header, chain HeaderFetcher) vm.Context {
|
func NewEVMContext(msg Message, header *types.Header, chain HeaderFetcher) vm.Context {
|
||||||
return vm.Context{
|
return vm.Context{
|
||||||
|
Context: context.TODO(),
|
||||||
|
|
||||||
CanTransfer: CanTransfer,
|
CanTransfer: CanTransfer,
|
||||||
Transfer: Transfer,
|
Transfer: Transfer,
|
||||||
GetHash: GetHashFn(header, chain),
|
GetHash: GetHashFn(header, chain),
|
||||||
|
|
|
||||||
|
|
@ -17,8 +17,10 @@
|
||||||
package vm
|
package vm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/big"
|
"math/big"
|
||||||
|
"sync/atomic"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/ethereum/go-ethereum/crypto"
|
"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.
|
// Context provides the EVM with auxilary information. Once provided it shouldn't be modified.
|
||||||
type Context struct {
|
type Context struct {
|
||||||
|
context.Context
|
||||||
// CanTransfer returns whether the account contains
|
// CanTransfer returns whether the account contains
|
||||||
// sufficient ether to transfer the value
|
// sufficient ether to transfer the value
|
||||||
CanTransfer CanTransferFunc
|
CanTransfer CanTransferFunc
|
||||||
|
|
@ -66,29 +69,55 @@ type Environment struct {
|
||||||
// Depth is the current call stack
|
// Depth is the current call stack
|
||||||
Depth int
|
Depth int
|
||||||
|
|
||||||
// evm is the ethereum virtual machine
|
|
||||||
evm Vm
|
|
||||||
// chainConfig contains information about the current chain
|
// chainConfig contains information about the current chain
|
||||||
chainConfig *params.ChainConfig
|
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.
|
// 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{
|
env := &Environment{
|
||||||
Context: context,
|
Context: ctx,
|
||||||
StateDB: statedb,
|
StateDB: statedb,
|
||||||
vmConfig: vmConfig,
|
vmConfig: vmConfig,
|
||||||
chainConfig: chainConfig,
|
chainConfig: chainConfig,
|
||||||
}
|
}
|
||||||
|
|
||||||
env.evm = New(env, vmConfig)
|
env.evm = New(env, vmConfig)
|
||||||
return env
|
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
|
// 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
|
// 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.
|
// 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 {
|
if env.vmConfig.NoRecursion && env.Depth > 0 {
|
||||||
caller.ReturnGas(gas)
|
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))
|
contract.SetCallCode(&addr, env.StateDB.GetCodeHash(addr), env.StateDB.GetCode(addr))
|
||||||
defer contract.Finalise()
|
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
|
// 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
|
// 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.
|
// 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.
|
// 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.
|
// 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 {
|
if env.vmConfig.NoRecursion && env.Depth > 0 {
|
||||||
caller.ReturnGas(gas)
|
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))
|
contract.SetCallCode(&addr, env.StateDB.GetCodeHash(addr), env.StateDB.GetCode(addr))
|
||||||
defer contract.Finalise()
|
defer contract.Finalise()
|
||||||
|
|
||||||
ret, err := env.EVM().Run(contract, input)
|
ret, err = env.EVM().Run(contract, input)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
contract.UseGas(contract.Gas)
|
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
|
// 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.
|
// 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 {
|
if env.vmConfig.NoRecursion && env.Depth > 0 {
|
||||||
caller.ReturnGas(gas)
|
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))
|
contract.SetCallCode(&addr, env.StateDB.GetCodeHash(addr), env.StateDB.GetCode(addr))
|
||||||
defer contract.Finalise()
|
defer contract.Finalise()
|
||||||
|
|
||||||
ret, err := env.EVM().Run(contract, input)
|
ret, err = env.EVM().Run(contract, input)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
contract.UseGas(contract.Gas)
|
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.
|
// 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 {
|
if env.vmConfig.NoRecursion && env.Depth > 0 {
|
||||||
caller.ReturnGas(gas)
|
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)
|
env.StateDB.SetNonce(caller.Address(), nonce+1)
|
||||||
|
|
||||||
snapshot := env.StateDB.Snapshot()
|
snapshot := env.StateDB.Snapshot()
|
||||||
var (
|
contractAddr = crypto.CreateAddress(caller.Address(), nonce)
|
||||||
addr = crypto.CreateAddress(caller.Address(), nonce)
|
to := env.StateDB.CreateAccount(contractAddr)
|
||||||
to = env.StateDB.CreateAccount(addr)
|
|
||||||
)
|
|
||||||
if env.ChainConfig().IsEIP158(env.BlockNumber) {
|
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)
|
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
|
// E The contract is a scoped environment for this execution context
|
||||||
// only.
|
// only.
|
||||||
contract := NewContract(caller, to, value, gas)
|
contract := NewContract(caller, to, value, gas)
|
||||||
contract.SetCallCode(&addr, crypto.Keccak256Hash(code), code)
|
contract.SetCallCode(&contractAddr, crypto.Keccak256Hash(code), code)
|
||||||
defer contract.Finalise()
|
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
|
// check whether the max code size has been exceeded
|
||||||
maxCodeSizeExceeded := len(ret) > params.MaxCodeSize
|
maxCodeSizeExceeded := len(ret) > params.MaxCodeSize
|
||||||
// if the contract creation ran successfully and no errors were returned
|
// 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 := big.NewInt(int64(len(ret)))
|
||||||
dataGas.Mul(dataGas, params.CreateDataGas)
|
dataGas.Mul(dataGas, params.CreateDataGas)
|
||||||
if contract.UseGas(dataGas) {
|
if contract.UseGas(dataGas) {
|
||||||
env.StateDB.SetCode(addr, ret)
|
env.StateDB.SetCode(contractAddr, ret)
|
||||||
} else {
|
} else {
|
||||||
err = ErrCodeStoreOutOfGas
|
err = ErrCodeStoreOutOfGas
|
||||||
}
|
}
|
||||||
|
|
@ -296,7 +339,7 @@ func (env *Environment) Create(caller ContractRef, code []byte, gas, value *big.
|
||||||
env.StateDB.RevertToSnapshot(snapshot)
|
env.StateDB.RevertToSnapshot(snapshot)
|
||||||
|
|
||||||
// Nothing should be returned when an error is thrown.
|
// 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.
|
// 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
|
// 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
|
ret = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret, addr, err
|
return ret, contractAddr, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// ChainConfig returns the environment's chain configuration
|
// ChainConfig returns the environment's chain configuration
|
||||||
|
|
|
||||||
|
|
@ -245,70 +245,68 @@ func gasDup(gt params.GasTable, env *Environment, contract *Contract, stack *Sta
|
||||||
return GasFastestStep
|
return GasFastestStep
|
||||||
}
|
}
|
||||||
|
|
||||||
var gasTable [256]*big.Int
|
var gasTable = [256]*big.Int{
|
||||||
|
ADD: GasFastestStep,
|
||||||
func init() {
|
LT: GasFastestStep,
|
||||||
gasTable[ADD] = GasFastestStep
|
GT: GasFastestStep,
|
||||||
gasTable[LT] = GasFastestStep
|
SLT: GasFastestStep,
|
||||||
gasTable[GT] = GasFastestStep
|
SGT: GasFastestStep,
|
||||||
gasTable[SLT] = GasFastestStep
|
EQ: GasFastestStep,
|
||||||
gasTable[SGT] = GasFastestStep
|
ISZERO: GasFastestStep,
|
||||||
gasTable[EQ] = GasFastestStep
|
SUB: GasFastestStep,
|
||||||
gasTable[ISZERO] = GasFastestStep
|
AND: GasFastestStep,
|
||||||
gasTable[SUB] = GasFastestStep
|
OR: GasFastestStep,
|
||||||
gasTable[AND] = GasFastestStep
|
XOR: GasFastestStep,
|
||||||
gasTable[OR] = GasFastestStep
|
NOT: GasFastestStep,
|
||||||
gasTable[XOR] = GasFastestStep
|
BYTE: GasFastestStep,
|
||||||
gasTable[NOT] = GasFastestStep
|
CALLDATALOAD: GasFastestStep,
|
||||||
gasTable[BYTE] = GasFastestStep
|
CALLDATACOPY: GasFastestStep,
|
||||||
gasTable[CALLDATALOAD] = GasFastestStep
|
MLOAD: GasFastestStep,
|
||||||
gasTable[CALLDATACOPY] = GasFastestStep
|
MSTORE: GasFastestStep,
|
||||||
gasTable[MLOAD] = GasFastestStep
|
MSTORE8: GasFastestStep,
|
||||||
gasTable[MSTORE] = GasFastestStep
|
CODECOPY: GasFastestStep,
|
||||||
gasTable[MSTORE8] = GasFastestStep
|
MUL: GasFastStep,
|
||||||
gasTable[CODECOPY] = GasFastestStep
|
DIV: GasFastStep,
|
||||||
gasTable[MUL] = GasFastStep
|
SDIV: GasFastStep,
|
||||||
gasTable[DIV] = GasFastStep
|
MOD: GasFastStep,
|
||||||
gasTable[SDIV] = GasFastStep
|
SMOD: GasFastStep,
|
||||||
gasTable[MOD] = GasFastStep
|
SIGNEXTEND: GasFastStep,
|
||||||
gasTable[SMOD] = GasFastStep
|
ADDMOD: GasMidStep,
|
||||||
gasTable[SIGNEXTEND] = GasFastStep
|
MULMOD: GasMidStep,
|
||||||
gasTable[ADDMOD] = GasMidStep
|
JUMP: GasMidStep,
|
||||||
gasTable[MULMOD] = GasMidStep
|
JUMPI: GasSlowStep,
|
||||||
gasTable[JUMP] = GasMidStep
|
EXP: GasSlowStep,
|
||||||
gasTable[JUMPI] = GasSlowStep
|
ADDRESS: GasQuickStep,
|
||||||
gasTable[EXP] = GasSlowStep
|
ORIGIN: GasQuickStep,
|
||||||
gasTable[ADDRESS] = GasQuickStep
|
CALLER: GasQuickStep,
|
||||||
gasTable[ORIGIN] = GasQuickStep
|
CALLVALUE: GasQuickStep,
|
||||||
gasTable[CALLER] = GasQuickStep
|
CODESIZE: GasQuickStep,
|
||||||
gasTable[CALLVALUE] = GasQuickStep
|
GASPRICE: GasQuickStep,
|
||||||
gasTable[CODESIZE] = GasQuickStep
|
COINBASE: GasQuickStep,
|
||||||
gasTable[GASPRICE] = GasQuickStep
|
TIMESTAMP: GasQuickStep,
|
||||||
gasTable[COINBASE] = GasQuickStep
|
NUMBER: GasQuickStep,
|
||||||
gasTable[TIMESTAMP] = GasQuickStep
|
CALLDATASIZE: GasQuickStep,
|
||||||
gasTable[NUMBER] = GasQuickStep
|
DIFFICULTY: GasQuickStep,
|
||||||
gasTable[CALLDATASIZE] = GasQuickStep
|
GASLIMIT: GasQuickStep,
|
||||||
gasTable[DIFFICULTY] = GasQuickStep
|
POP: GasQuickStep,
|
||||||
gasTable[GASLIMIT] = GasQuickStep
|
PC: GasQuickStep,
|
||||||
gasTable[POP] = GasQuickStep
|
MSIZE: GasQuickStep,
|
||||||
gasTable[PC] = GasQuickStep
|
GAS: GasQuickStep,
|
||||||
gasTable[MSIZE] = GasQuickStep
|
BLOCKHASH: GasExtStep,
|
||||||
gasTable[GAS] = GasQuickStep
|
BALANCE: Zero,
|
||||||
gasTable[BLOCKHASH] = GasExtStep
|
EXTCODESIZE: Zero,
|
||||||
gasTable[BALANCE] = Zero
|
EXTCODECOPY: Zero,
|
||||||
gasTable[EXTCODESIZE] = Zero
|
SLOAD: params.SloadGas,
|
||||||
gasTable[EXTCODECOPY] = Zero
|
SSTORE: Zero,
|
||||||
gasTable[SLOAD] = params.SloadGas
|
SHA3: params.Sha3Gas,
|
||||||
gasTable[SSTORE] = Zero
|
CREATE: params.CreateGas,
|
||||||
gasTable[SHA3] = params.Sha3Gas
|
CALL: Zero,
|
||||||
gasTable[CREATE] = params.CreateGas
|
CALLCODE: Zero,
|
||||||
gasTable[CALL] = Zero
|
DELEGATECALL: Zero,
|
||||||
gasTable[CALLCODE] = Zero
|
SUICIDE: Zero,
|
||||||
gasTable[DELEGATECALL] = Zero
|
JUMPDEST: params.JumpdestGas,
|
||||||
gasTable[SUICIDE] = Zero
|
RETURN: Zero,
|
||||||
gasTable[JUMPDEST] = params.JumpdestGas
|
PUSH1: GasFastestStep,
|
||||||
gasTable[RETURN] = Zero
|
DUP1: Zero,
|
||||||
gasTable[PUSH1] = GasFastestStep
|
STOP: Zero,
|
||||||
gasTable[DUP1] = Zero
|
|
||||||
gasTable[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) {
|
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()
|
gas, to, inOffset, inSize, outOffset, outSize := stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop(), stack.pop()
|
||||||
|
|
||||||
toAddr := common.BigToAddress(to)
|
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
|
package vm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"math/big"
|
"math/big"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
|
@ -52,7 +53,7 @@ func (d dummyStateDB) GetAccount(common.Address) Account {
|
||||||
|
|
||||||
func TestStoreCapture(t *testing.T) {
|
func TestStoreCapture(t *testing.T) {
|
||||||
var (
|
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)
|
logger = NewStructLogger(nil)
|
||||||
mem = NewMemory()
|
mem = NewMemory()
|
||||||
stack = newstack()
|
stack = newstack()
|
||||||
|
|
@ -79,7 +80,7 @@ func TestStorageCapture(t *testing.T) {
|
||||||
var (
|
var (
|
||||||
ref = &dummyContractRef{}
|
ref = &dummyContractRef{}
|
||||||
contract = NewContract(ref, ref, new(big.Int), new(big.Int))
|
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)
|
logger = NewStructLogger(nil)
|
||||||
mem = NewMemory()
|
mem = NewMemory()
|
||||||
stack = newstack()
|
stack = newstack()
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,7 @@
|
||||||
package runtime
|
package runtime
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"math/big"
|
"math/big"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
|
|
@ -27,6 +28,7 @@ import (
|
||||||
|
|
||||||
func NewEnv(cfg *Config, state *state.StateDB) *vm.Environment {
|
func NewEnv(cfg *Config, state *state.StateDB) *vm.Environment {
|
||||||
context := vm.Context{
|
context := vm.Context{
|
||||||
|
Context: context.TODO(),
|
||||||
CanTransfer: core.CanTransfer,
|
CanTransfer: core.CanTransfer,
|
||||||
Transfer: core.Transfer,
|
Transfer: core.Transfer,
|
||||||
GetHash: func(uint64) common.Hash { return common.Hash{} },
|
GetHash: func(uint64) common.Hash { return common.Hash{} },
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,6 @@
|
||||||
package vm
|
package vm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/big"
|
"math/big"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
|
|
@ -52,38 +51,20 @@ type Config struct {
|
||||||
// The EVM will run the byte code VM or JIT VM based on the passed
|
// The EVM will run the byte code VM or JIT VM based on the passed
|
||||||
// configuration.
|
// configuration.
|
||||||
type EVM struct {
|
type EVM struct {
|
||||||
env *Environment
|
env *Environment
|
||||||
jumpTable vmJumpTable
|
cfg Config
|
||||||
cfg Config
|
gasTable params.GasTable
|
||||||
gasTable params.GasTable
|
|
||||||
|
|
||||||
// done is an atomic int and is used for
|
|
||||||
// cancellation during RunWithContext.
|
|
||||||
done int32
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// New returns a new instance of the EVM.
|
// New returns a new instance of the EVM.
|
||||||
func New(env *Environment, cfg Config) *EVM {
|
func New(env *Environment, cfg Config) *EVM {
|
||||||
return &EVM{
|
return &EVM{
|
||||||
env: env,
|
env: env,
|
||||||
jumpTable: newJumpTable(env.ChainConfig(), env.BlockNumber),
|
cfg: cfg,
|
||||||
cfg: cfg,
|
gasTable: env.ChainConfig().GasTable(env.BlockNumber),
|
||||||
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
|
// 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) {
|
func (evm *EVM) Run(contract *Contract, input []byte) (ret []byte, err error) {
|
||||||
evm.env.Depth++
|
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
|
// 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 execution of one of the operations or until the evm.done is set by
|
||||||
// the parent context.Context.
|
// the parent context.Context.
|
||||||
for atomic.LoadInt32(&evm.done) == 0 {
|
for atomic.LoadInt32(&evm.env.abort) == 0 {
|
||||||
// Get the memory location of pc
|
// Get the memory location of pc
|
||||||
op = contract.GetOp(pc)
|
op = contract.GetOp(pc)
|
||||||
|
|
||||||
// get the operation from the jump table matching the opcode
|
// 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 the op is invalid abort the process and return an error
|
||||||
if !operation.valid {
|
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
|
// validate the stack and make sure there enough stack items available
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,7 @@ package tests
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"context"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/big"
|
"math/big"
|
||||||
|
|
@ -190,6 +191,8 @@ func NewEVMEnvironment(vmTest bool, chainConfig *params.ChainConfig, statedb *st
|
||||||
}
|
}
|
||||||
|
|
||||||
context := vm.Context{
|
context := vm.Context{
|
||||||
|
Context: context.TODO(),
|
||||||
|
|
||||||
CanTransfer: canTransfer,
|
CanTransfer: canTransfer,
|
||||||
Transfer: transfer,
|
Transfer: transfer,
|
||||||
GetHash: func(n uint64) common.Hash {
|
GetHash: func(n uint64) common.Hash {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue