move error variable to error.go

This commit is contained in:
ANOTHEL 2019-05-14 12:02:56 +09:00
parent 4756ef5373
commit 1d7d63141f
9 changed files with 112 additions and 92 deletions

View file

@ -18,7 +18,6 @@ package vm
import ( import (
"crypto/sha256" "crypto/sha256"
"errors"
"math/big" "math/big"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
@ -317,9 +316,6 @@ var (
// false32Byte is returned if the bn256 pairing check fails. // false32Byte is returned if the bn256 pairing check fails.
false32Byte = make([]byte, 32) false32Byte = make([]byte, 32)
// errBadPairingInput is returned if the bn256 pairing input is invalid.
errBadPairingInput = errors.New("bad elliptic curve pairing size")
) )
// bn256Pairing implements a pairing pre-compile for the bn256 curve // bn256Pairing implements a pairing pre-compile for the bn256 curve
@ -333,7 +329,7 @@ func (c *bn256Pairing) RequiredGas(input []byte) uint64 {
func (c *bn256Pairing) Run(input []byte) ([]byte, error) { func (c *bn256Pairing) Run(input []byte) ([]byte, error) {
// Handle some corner cases cheaply // Handle some corner cases cheaply
if len(input)%192 > 0 { if len(input)%192 > 0 {
return nil, errBadPairingInput return nil, ErrBadPairingInput
} }
// Convert the input into a set of coordinates // Convert the input into a set of coordinates
var ( var (

View file

@ -20,11 +20,45 @@ import "errors"
// List execution errors // List execution errors
var ( var (
ErrOutOfGas = errors.New("out of gas") // ErrOutOfGas is returned when
ErrCodeStoreOutOfGas = errors.New("contract creation code storage out of gas") ErrOutOfGas = errors.New("out of gas")
ErrDepth = errors.New("max call depth exceeded")
ErrTraceLimitReached = errors.New("the number of logs reached the specified limit") // ErrCodeStoreOutOfGas is returned when
ErrInsufficientBalance = errors.New("insufficient balance for transfer") ErrCodeStoreOutOfGas = errors.New("contract creation code storage out of gas")
// ErrDepth is returned when
ErrDepth = errors.New("max call depth exceeded")
// ErrTraceLimitReached is returned when
ErrTraceLimitReached = errors.New("the number of logs reached the specified limit")
// ErrInsufficientBalance is returned when
ErrInsufficientBalance = errors.New("insufficient balance for transfer")
// ErrContractAddressCollision is returned when
ErrContractAddressCollision = errors.New("contract address collision") ErrContractAddressCollision = errors.New("contract address collision")
ErrNoCompatibleInterpreter = errors.New("no compatible interpreter")
// ErrNoCompatibleInterpreter is returned when
ErrNoCompatibleInterpreter = errors.New("no compatible interpreter")
// ErrBadPairingInput is returned if the bn256 pairing input is invalid.
ErrBadPairingInput = errors.New("bad elliptic curve pairing size")
// ErrWriteProtection is returned when
ErrWriteProtection = errors.New("evm: write protection")
// ErrReturnDataOutOfBounds is returned when
ErrReturnDataOutOfBounds = errors.New("evm: return data out of bounds")
// ErrExecutionReverted is returned when
ErrExecutionReverted = errors.New("evm: execution reverted")
// ErrMaxCodeSizeExceeded is returned when
ErrMaxCodeSizeExceeded = errors.New("evm: max code size exceeded")
// ErrInvalidJump is returned when
ErrInvalidJump = errors.New("evm: invalid jump destination")
// ErrGasUintOverflow is returned when
ErrGasUintOverflow = errors.New("gas uint64 overflow")
) )

View file

@ -235,7 +235,7 @@ func (evm *EVM) Call(caller ContractRef, addr common.Address, input []byte, gas
// 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.
if err != nil { if err != nil {
evm.StateDB.RevertToSnapshot(snapshot) evm.StateDB.RevertToSnapshot(snapshot)
if err != errExecutionReverted { if err != ErrExecutionReverted {
contract.UseGas(contract.Gas) contract.UseGas(contract.Gas)
} }
} }
@ -275,7 +275,7 @@ func (evm *EVM) CallCode(caller ContractRef, addr common.Address, input []byte,
ret, err = run(evm, contract, input, false) ret, err = run(evm, contract, input, false)
if err != nil { if err != nil {
evm.StateDB.RevertToSnapshot(snapshot) evm.StateDB.RevertToSnapshot(snapshot)
if err != errExecutionReverted { if err != ErrExecutionReverted {
contract.UseGas(contract.Gas) contract.UseGas(contract.Gas)
} }
} }
@ -308,7 +308,7 @@ func (evm *EVM) DelegateCall(caller ContractRef, addr common.Address, input []by
ret, err = run(evm, contract, input, false) ret, err = run(evm, contract, input, false)
if err != nil { if err != nil {
evm.StateDB.RevertToSnapshot(snapshot) evm.StateDB.RevertToSnapshot(snapshot)
if err != errExecutionReverted { if err != ErrExecutionReverted {
contract.UseGas(contract.Gas) contract.UseGas(contract.Gas)
} }
} }
@ -349,7 +349,7 @@ func (evm *EVM) StaticCall(caller ContractRef, addr common.Address, input []byte
ret, err = run(evm, contract, input, true) ret, err = run(evm, contract, input, true)
if err != nil { if err != nil {
evm.StateDB.RevertToSnapshot(snapshot) evm.StateDB.RevertToSnapshot(snapshot)
if err != errExecutionReverted { if err != ErrExecutionReverted {
contract.UseGas(contract.Gas) contract.UseGas(contract.Gas)
} }
} }
@ -430,13 +430,13 @@ func (evm *EVM) create(caller ContractRef, codeAndHash *codeAndHash, gas uint64,
// 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.
if maxCodeSizeExceeded || (err != nil && (evm.ChainConfig().IsHomestead(evm.BlockNumber) || err != ErrCodeStoreOutOfGas)) { if maxCodeSizeExceeded || (err != nil && (evm.ChainConfig().IsHomestead(evm.BlockNumber) || err != ErrCodeStoreOutOfGas)) {
evm.StateDB.RevertToSnapshot(snapshot) evm.StateDB.RevertToSnapshot(snapshot)
if err != errExecutionReverted { if err != ErrExecutionReverted {
contract.UseGas(contract.Gas) contract.UseGas(contract.Gas)
} }
} }
// Assign err if contract code size exceeds the max while the err is still empty. // Assign err if contract code size exceeds the max while the err is still empty.
if maxCodeSizeExceeded && err == nil { if maxCodeSizeExceeded && err == nil {
err = errMaxCodeSizeExceeded err = ErrMaxCodeSizeExceeded
} }
if evm.vmConfig.Debug && evm.depth == 0 { if evm.vmConfig.Debug && evm.depth == 0 {
evm.vmConfig.Tracer.CaptureEnd(ret, gas-contract.Gas, time.Since(start), err) evm.vmConfig.Tracer.CaptureEnd(ret, gas-contract.Gas, time.Since(start), err)

View file

@ -48,7 +48,7 @@ func callGas(gasTable params.GasTable, availableGas, base uint64, callCost *big.
} }
} }
if !callCost.IsUint64() { if !callCost.IsUint64() {
return 0, errGasUintOverflow return 0, ErrGasUintOverflow
} }
return callCost.Uint64(), nil return callCost.Uint64(), nil

View file

@ -34,7 +34,7 @@ func memoryGasCost(mem *Memory, newMemSize uint64) (uint64, error) {
// overflow. The constant 0x1FFFFFFFE0 is the highest number that can be used // overflow. The constant 0x1FFFFFFFE0 is the highest number that can be used
// without overflowing the gas calculation. // without overflowing the gas calculation.
if newMemSize > 0x1FFFFFFFE0 { if newMemSize > 0x1FFFFFFFE0 {
return 0, errGasUintOverflow return 0, ErrGasUintOverflow
} }
newMemSizeWords := toWordSize(newMemSize) newMemSizeWords := toWordSize(newMemSize)
newMemSize = newMemSizeWords * 32 newMemSize = newMemSizeWords * 32
@ -61,20 +61,20 @@ func gasCallDataCopy(gt params.GasTable, evm *EVM, contract *Contract, stack *St
var overflow bool var overflow bool
if gas, overflow = math.SafeAdd(gas, GasFastestStep); overflow { if gas, overflow = math.SafeAdd(gas, GasFastestStep); overflow {
return 0, errGasUintOverflow return 0, ErrGasUintOverflow
} }
words, overflow := bigUint64(stack.Back(2)) words, overflow := bigUint64(stack.Back(2))
if overflow { if overflow {
return 0, errGasUintOverflow return 0, ErrGasUintOverflow
} }
if words, overflow = math.SafeMul(toWordSize(words), params.CopyGas); overflow { if words, overflow = math.SafeMul(toWordSize(words), params.CopyGas); overflow {
return 0, errGasUintOverflow return 0, ErrGasUintOverflow
} }
if gas, overflow = math.SafeAdd(gas, words); overflow { if gas, overflow = math.SafeAdd(gas, words); overflow {
return 0, errGasUintOverflow return 0, ErrGasUintOverflow
} }
return gas, nil return gas, nil
} }
@ -87,20 +87,20 @@ func gasReturnDataCopy(gt params.GasTable, evm *EVM, contract *Contract, stack *
var overflow bool var overflow bool
if gas, overflow = math.SafeAdd(gas, GasFastestStep); overflow { if gas, overflow = math.SafeAdd(gas, GasFastestStep); overflow {
return 0, errGasUintOverflow return 0, ErrGasUintOverflow
} }
words, overflow := bigUint64(stack.Back(2)) words, overflow := bigUint64(stack.Back(2))
if overflow { if overflow {
return 0, errGasUintOverflow return 0, ErrGasUintOverflow
} }
if words, overflow = math.SafeMul(toWordSize(words), params.CopyGas); overflow { if words, overflow = math.SafeMul(toWordSize(words), params.CopyGas); overflow {
return 0, errGasUintOverflow return 0, ErrGasUintOverflow
} }
if gas, overflow = math.SafeAdd(gas, words); overflow { if gas, overflow = math.SafeAdd(gas, words); overflow {
return 0, errGasUintOverflow return 0, ErrGasUintOverflow
} }
return gas, nil return gas, nil
} }
@ -178,7 +178,7 @@ func makeGasLog(n uint64) gasFunc {
return func(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { return func(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) {
requestedSize, overflow := bigUint64(stack.Back(1)) requestedSize, overflow := bigUint64(stack.Back(1))
if overflow { if overflow {
return 0, errGasUintOverflow return 0, ErrGasUintOverflow
} }
gas, err := memoryGasCost(mem, memorySize) gas, err := memoryGasCost(mem, memorySize)
@ -187,18 +187,18 @@ func makeGasLog(n uint64) gasFunc {
} }
if gas, overflow = math.SafeAdd(gas, params.LogGas); overflow { if gas, overflow = math.SafeAdd(gas, params.LogGas); overflow {
return 0, errGasUintOverflow return 0, ErrGasUintOverflow
} }
if gas, overflow = math.SafeAdd(gas, n*params.LogTopicGas); overflow { if gas, overflow = math.SafeAdd(gas, n*params.LogTopicGas); overflow {
return 0, errGasUintOverflow return 0, ErrGasUintOverflow
} }
var memorySizeGas uint64 var memorySizeGas uint64
if memorySizeGas, overflow = math.SafeMul(requestedSize, params.LogDataGas); overflow { if memorySizeGas, overflow = math.SafeMul(requestedSize, params.LogDataGas); overflow {
return 0, errGasUintOverflow return 0, ErrGasUintOverflow
} }
if gas, overflow = math.SafeAdd(gas, memorySizeGas); overflow { if gas, overflow = math.SafeAdd(gas, memorySizeGas); overflow {
return 0, errGasUintOverflow return 0, ErrGasUintOverflow
} }
return gas, nil return gas, nil
} }
@ -212,18 +212,18 @@ func gasSha3(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem
} }
if gas, overflow = math.SafeAdd(gas, params.Sha3Gas); overflow { if gas, overflow = math.SafeAdd(gas, params.Sha3Gas); overflow {
return 0, errGasUintOverflow return 0, ErrGasUintOverflow
} }
wordGas, overflow := bigUint64(stack.Back(1)) wordGas, overflow := bigUint64(stack.Back(1))
if overflow { if overflow {
return 0, errGasUintOverflow return 0, ErrGasUintOverflow
} }
if wordGas, overflow = math.SafeMul(toWordSize(wordGas), params.Sha3WordGas); overflow { if wordGas, overflow = math.SafeMul(toWordSize(wordGas), params.Sha3WordGas); overflow {
return 0, errGasUintOverflow return 0, ErrGasUintOverflow
} }
if gas, overflow = math.SafeAdd(gas, wordGas); overflow { if gas, overflow = math.SafeAdd(gas, wordGas); overflow {
return 0, errGasUintOverflow return 0, ErrGasUintOverflow
} }
return gas, nil return gas, nil
} }
@ -236,18 +236,18 @@ func gasCodeCopy(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack,
var overflow bool var overflow bool
if gas, overflow = math.SafeAdd(gas, GasFastestStep); overflow { if gas, overflow = math.SafeAdd(gas, GasFastestStep); overflow {
return 0, errGasUintOverflow return 0, ErrGasUintOverflow
} }
wordGas, overflow := bigUint64(stack.Back(2)) wordGas, overflow := bigUint64(stack.Back(2))
if overflow { if overflow {
return 0, errGasUintOverflow return 0, ErrGasUintOverflow
} }
if wordGas, overflow = math.SafeMul(toWordSize(wordGas), params.CopyGas); overflow { if wordGas, overflow = math.SafeMul(toWordSize(wordGas), params.CopyGas); overflow {
return 0, errGasUintOverflow return 0, ErrGasUintOverflow
} }
if gas, overflow = math.SafeAdd(gas, wordGas); overflow { if gas, overflow = math.SafeAdd(gas, wordGas); overflow {
return 0, errGasUintOverflow return 0, ErrGasUintOverflow
} }
return gas, nil return gas, nil
} }
@ -260,20 +260,20 @@ func gasExtCodeCopy(gt params.GasTable, evm *EVM, contract *Contract, stack *Sta
var overflow bool var overflow bool
if gas, overflow = math.SafeAdd(gas, gt.ExtcodeCopy); overflow { if gas, overflow = math.SafeAdd(gas, gt.ExtcodeCopy); overflow {
return 0, errGasUintOverflow return 0, ErrGasUintOverflow
} }
wordGas, overflow := bigUint64(stack.Back(3)) wordGas, overflow := bigUint64(stack.Back(3))
if overflow { if overflow {
return 0, errGasUintOverflow return 0, ErrGasUintOverflow
} }
if wordGas, overflow = math.SafeMul(toWordSize(wordGas), params.CopyGas); overflow { if wordGas, overflow = math.SafeMul(toWordSize(wordGas), params.CopyGas); overflow {
return 0, errGasUintOverflow return 0, ErrGasUintOverflow
} }
if gas, overflow = math.SafeAdd(gas, wordGas); overflow { if gas, overflow = math.SafeAdd(gas, wordGas); overflow {
return 0, errGasUintOverflow return 0, ErrGasUintOverflow
} }
return gas, nil return gas, nil
} }
@ -286,10 +286,10 @@ func gasMLoad(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, me
var overflow bool var overflow bool
gas, err := memoryGasCost(mem, memorySize) gas, err := memoryGasCost(mem, memorySize)
if err != nil { if err != nil {
return 0, errGasUintOverflow return 0, ErrGasUintOverflow
} }
if gas, overflow = math.SafeAdd(gas, GasFastestStep); overflow { if gas, overflow = math.SafeAdd(gas, GasFastestStep); overflow {
return 0, errGasUintOverflow return 0, ErrGasUintOverflow
} }
return gas, nil return gas, nil
} }
@ -298,10 +298,10 @@ func gasMStore8(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack,
var overflow bool var overflow bool
gas, err := memoryGasCost(mem, memorySize) gas, err := memoryGasCost(mem, memorySize)
if err != nil { if err != nil {
return 0, errGasUintOverflow return 0, ErrGasUintOverflow
} }
if gas, overflow = math.SafeAdd(gas, GasFastestStep); overflow { if gas, overflow = math.SafeAdd(gas, GasFastestStep); overflow {
return 0, errGasUintOverflow return 0, ErrGasUintOverflow
} }
return gas, nil return gas, nil
} }
@ -310,10 +310,10 @@ func gasMStore(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, m
var overflow bool var overflow bool
gas, err := memoryGasCost(mem, memorySize) gas, err := memoryGasCost(mem, memorySize)
if err != nil { if err != nil {
return 0, errGasUintOverflow return 0, ErrGasUintOverflow
} }
if gas, overflow = math.SafeAdd(gas, GasFastestStep); overflow { if gas, overflow = math.SafeAdd(gas, GasFastestStep); overflow {
return 0, errGasUintOverflow return 0, ErrGasUintOverflow
} }
return gas, nil return gas, nil
} }
@ -325,7 +325,7 @@ func gasCreate(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, m
return 0, err return 0, err
} }
if gas, overflow = math.SafeAdd(gas, params.CreateGas); overflow { if gas, overflow = math.SafeAdd(gas, params.CreateGas); overflow {
return 0, errGasUintOverflow return 0, ErrGasUintOverflow
} }
return gas, nil return gas, nil
} }
@ -337,17 +337,17 @@ func gasCreate2(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack,
return 0, err return 0, err
} }
if gas, overflow = math.SafeAdd(gas, params.Create2Gas); overflow { if gas, overflow = math.SafeAdd(gas, params.Create2Gas); overflow {
return 0, errGasUintOverflow return 0, ErrGasUintOverflow
} }
wordGas, overflow := bigUint64(stack.Back(2)) wordGas, overflow := bigUint64(stack.Back(2))
if overflow { if overflow {
return 0, errGasUintOverflow return 0, ErrGasUintOverflow
} }
if wordGas, overflow = math.SafeMul(toWordSize(wordGas), params.Sha3WordGas); overflow { if wordGas, overflow = math.SafeMul(toWordSize(wordGas), params.Sha3WordGas); overflow {
return 0, errGasUintOverflow return 0, ErrGasUintOverflow
} }
if gas, overflow = math.SafeAdd(gas, wordGas); overflow { if gas, overflow = math.SafeAdd(gas, wordGas); overflow {
return 0, errGasUintOverflow return 0, ErrGasUintOverflow
} }
return gas, nil return gas, nil
@ -373,7 +373,7 @@ func gasExp(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem
overflow bool overflow bool
) )
if gas, overflow = math.SafeAdd(gas, params.ExpGas); overflow { if gas, overflow = math.SafeAdd(gas, params.ExpGas); overflow {
return 0, errGasUintOverflow return 0, ErrGasUintOverflow
} }
return gas, nil return gas, nil
} }
@ -401,7 +401,7 @@ func gasCall(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem
} }
var overflow bool var overflow bool
if gas, overflow = math.SafeAdd(gas, memoryGas); overflow { if gas, overflow = math.SafeAdd(gas, memoryGas); overflow {
return 0, errGasUintOverflow return 0, ErrGasUintOverflow
} }
evm.callGasTemp, err = callGas(gt, contract.Gas, gas, stack.Back(0)) evm.callGasTemp, err = callGas(gt, contract.Gas, gas, stack.Back(0))
@ -409,7 +409,7 @@ func gasCall(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack, mem
return 0, err return 0, err
} }
if gas, overflow = math.SafeAdd(gas, evm.callGasTemp); overflow { if gas, overflow = math.SafeAdd(gas, evm.callGasTemp); overflow {
return 0, errGasUintOverflow return 0, ErrGasUintOverflow
} }
return gas, nil return gas, nil
} }
@ -425,7 +425,7 @@ func gasCallCode(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack,
} }
var overflow bool var overflow bool
if gas, overflow = math.SafeAdd(gas, memoryGas); overflow { if gas, overflow = math.SafeAdd(gas, memoryGas); overflow {
return 0, errGasUintOverflow return 0, ErrGasUintOverflow
} }
evm.callGasTemp, err = callGas(gt, contract.Gas, gas, stack.Back(0)) evm.callGasTemp, err = callGas(gt, contract.Gas, gas, stack.Back(0))
@ -433,7 +433,7 @@ func gasCallCode(gt params.GasTable, evm *EVM, contract *Contract, stack *Stack,
return 0, err return 0, err
} }
if gas, overflow = math.SafeAdd(gas, evm.callGasTemp); overflow { if gas, overflow = math.SafeAdd(gas, evm.callGasTemp); overflow {
return 0, errGasUintOverflow return 0, ErrGasUintOverflow
} }
return gas, nil return gas, nil
} }
@ -479,7 +479,7 @@ func gasDelegateCall(gt params.GasTable, evm *EVM, contract *Contract, stack *St
} }
var overflow bool var overflow bool
if gas, overflow = math.SafeAdd(gas, gt.Calls); overflow { if gas, overflow = math.SafeAdd(gas, gt.Calls); overflow {
return 0, errGasUintOverflow return 0, ErrGasUintOverflow
} }
evm.callGasTemp, err = callGas(gt, contract.Gas, gas, stack.Back(0)) evm.callGasTemp, err = callGas(gt, contract.Gas, gas, stack.Back(0))
@ -487,7 +487,7 @@ func gasDelegateCall(gt params.GasTable, evm *EVM, contract *Contract, stack *St
return 0, err return 0, err
} }
if gas, overflow = math.SafeAdd(gas, evm.callGasTemp); overflow { if gas, overflow = math.SafeAdd(gas, evm.callGasTemp); overflow {
return 0, errGasUintOverflow return 0, ErrGasUintOverflow
} }
return gas, nil return gas, nil
} }
@ -499,7 +499,7 @@ func gasStaticCall(gt params.GasTable, evm *EVM, contract *Contract, stack *Stac
} }
var overflow bool var overflow bool
if gas, overflow = math.SafeAdd(gas, gt.Calls); overflow { if gas, overflow = math.SafeAdd(gas, gt.Calls); overflow {
return 0, errGasUintOverflow return 0, ErrGasUintOverflow
} }
evm.callGasTemp, err = callGas(gt, contract.Gas, gas, stack.Back(0)) evm.callGasTemp, err = callGas(gt, contract.Gas, gas, stack.Back(0))
@ -507,7 +507,7 @@ func gasStaticCall(gt params.GasTable, evm *EVM, contract *Contract, stack *Stac
return 0, err return 0, err
} }
if gas, overflow = math.SafeAdd(gas, evm.callGasTemp); overflow { if gas, overflow = math.SafeAdd(gas, evm.callGasTemp); overflow {
return 0, errGasUintOverflow return 0, ErrGasUintOverflow
} }
return gas, nil return gas, nil
} }

View file

@ -29,8 +29,8 @@ func TestMemoryGasCost(t *testing.T) {
} }
for i, tt := range tests { for i, tt := range tests {
v, err := memoryGasCost(&Memory{}, tt.size) v, err := memoryGasCost(&Memory{}, tt.size)
if (err == errGasUintOverflow) != tt.overflow { if (err == ErrGasUintOverflow) != tt.overflow {
t.Errorf("test %d: overflow mismatch: have %v, want %v", i, err == errGasUintOverflow, tt.overflow) t.Errorf("test %d: overflow mismatch: have %v, want %v", i, err == ErrGasUintOverflow, tt.overflow)
} }
if v != tt.cost { if v != tt.cost {
t.Errorf("test %d: gas cost mismatch: have %v, want %v", i, v, tt.cost) t.Errorf("test %d: gas cost mismatch: have %v, want %v", i, v, tt.cost)

View file

@ -17,7 +17,6 @@
package vm package vm
import ( import (
"errors"
"math/big" "math/big"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
@ -28,13 +27,8 @@ import (
) )
var ( var (
bigZero = new(big.Int) bigZero = new(big.Int)
tt255 = math.BigPow(2, 255) tt255 = math.BigPow(2, 255)
errWriteProtection = errors.New("evm: write protection")
errReturnDataOutOfBounds = errors.New("evm: return data out of bounds")
errExecutionReverted = errors.New("evm: execution reverted")
errMaxCodeSizeExceeded = errors.New("evm: max code size exceeded")
errInvalidJump = errors.New("evm: invalid jump destination")
) )
func opAdd(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { func opAdd(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
@ -468,7 +462,7 @@ func opReturnDataCopy(pc *uint64, interpreter *EVMInterpreter, contract *Contrac
defer interpreter.intPool.put(memOffset, dataOffset, length, end) defer interpreter.intPool.put(memOffset, dataOffset, length, end)
if !end.IsUint64() || uint64(len(interpreter.returnData)) < end.Uint64() { if !end.IsUint64() || uint64(len(interpreter.returnData)) < end.Uint64() {
return nil, errReturnDataOutOfBounds return nil, ErrReturnDataOutOfBounds
} }
memory.Set(memOffset.Uint64(), length.Uint64(), interpreter.returnData[dataOffset.Uint64():end.Uint64()]) memory.Set(memOffset.Uint64(), length.Uint64(), interpreter.returnData[dataOffset.Uint64():end.Uint64()])
@ -645,7 +639,7 @@ func opSstore(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memor
func opJump(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) { func opJump(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
pos := stack.pop() pos := stack.pop()
if !contract.validJumpdest(pos) { if !contract.validJumpdest(pos) {
return nil, errInvalidJump return nil, ErrInvalidJump
} }
*pc = pos.Uint64() *pc = pos.Uint64()
@ -657,7 +651,7 @@ func opJumpi(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory
pos, cond := stack.pop(), stack.pop() pos, cond := stack.pop(), stack.pop()
if cond.Sign() != 0 { if cond.Sign() != 0 {
if !contract.validJumpdest(pos) { if !contract.validJumpdest(pos) {
return nil, errInvalidJump return nil, ErrInvalidJump
} }
*pc = pos.Uint64() *pc = pos.Uint64()
} else { } else {
@ -714,7 +708,7 @@ func opCreate(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memor
contract.Gas += returnGas contract.Gas += returnGas
interpreter.intPool.put(value, offset, size) interpreter.intPool.put(value, offset, size)
if suberr == errExecutionReverted { if suberr == ErrExecutionReverted {
return res, nil return res, nil
} }
return nil, nil return nil, nil
@ -742,7 +736,7 @@ func opCreate2(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memo
contract.Gas += returnGas contract.Gas += returnGas
interpreter.intPool.put(endowment, offset, size, salt) interpreter.intPool.put(endowment, offset, size, salt)
if suberr == errExecutionReverted { if suberr == ErrExecutionReverted {
return res, nil return res, nil
} }
return nil, nil return nil, nil
@ -768,7 +762,7 @@ func opCall(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory
} else { } else {
stack.push(interpreter.intPool.get().SetUint64(1)) stack.push(interpreter.intPool.get().SetUint64(1))
} }
if err == nil || err == errExecutionReverted { if err == nil || err == ErrExecutionReverted {
memory.Set(retOffset.Uint64(), retSize.Uint64(), ret) memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
} }
contract.Gas += returnGas contract.Gas += returnGas
@ -797,7 +791,7 @@ func opCallCode(pc *uint64, interpreter *EVMInterpreter, contract *Contract, mem
} else { } else {
stack.push(interpreter.intPool.get().SetUint64(1)) stack.push(interpreter.intPool.get().SetUint64(1))
} }
if err == nil || err == errExecutionReverted { if err == nil || err == ErrExecutionReverted {
memory.Set(retOffset.Uint64(), retSize.Uint64(), ret) memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
} }
contract.Gas += returnGas contract.Gas += returnGas
@ -822,7 +816,7 @@ func opDelegateCall(pc *uint64, interpreter *EVMInterpreter, contract *Contract,
} else { } else {
stack.push(interpreter.intPool.get().SetUint64(1)) stack.push(interpreter.intPool.get().SetUint64(1))
} }
if err == nil || err == errExecutionReverted { if err == nil || err == ErrExecutionReverted {
memory.Set(retOffset.Uint64(), retSize.Uint64(), ret) memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
} }
contract.Gas += returnGas contract.Gas += returnGas
@ -847,7 +841,7 @@ func opStaticCall(pc *uint64, interpreter *EVMInterpreter, contract *Contract, m
} else { } else {
stack.push(interpreter.intPool.get().SetUint64(1)) stack.push(interpreter.intPool.get().SetUint64(1))
} }
if err == nil || err == errExecutionReverted { if err == nil || err == ErrExecutionReverted {
memory.Set(retOffset.Uint64(), retSize.Uint64(), ret) memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
} }
contract.Gas += returnGas contract.Gas += returnGas

View file

@ -114,7 +114,7 @@ func NewEVMInterpreter(evm *EVM, cfg Config) *EVMInterpreter {
// //
// It's important to note that any errors returned by the interpreter should be // It's important to note that any errors returned by the interpreter should be
// considered a revert-and-consume-all-gas operation except for // considered a revert-and-consume-all-gas operation except for
// errExecutionReverted which means revert-and-keep-gas-left. // ErrExecutionReverted which means revert-and-keep-gas-left.
func (in *EVMInterpreter) Run(contract *Contract, input []byte, readOnly bool) (ret []byte, err error) { func (in *EVMInterpreter) Run(contract *Contract, input []byte, readOnly bool) (ret []byte, err error) {
if in.intPool == nil { if in.intPool == nil {
in.intPool = poolOfIntPools.get() in.intPool = poolOfIntPools.get()
@ -206,7 +206,7 @@ func (in *EVMInterpreter) Run(contract *Contract, input []byte, readOnly bool) (
// account to the others means the state is modified and should also // account to the others means the state is modified and should also
// return with an error. // return with an error.
if operation.writes || (op == CALL && stack.Back(2).Sign() != 0) { if operation.writes || (op == CALL && stack.Back(2).Sign() != 0) {
return nil, errWriteProtection return nil, ErrWriteProtection
} }
} }
// Static portion of gas // Static portion of gas
@ -222,12 +222,12 @@ func (in *EVMInterpreter) Run(contract *Contract, input []byte, readOnly bool) (
if operation.memorySize != nil { if operation.memorySize != nil {
memSize, overflow := operation.memorySize(stack) memSize, overflow := operation.memorySize(stack)
if overflow { if overflow {
return nil, errGasUintOverflow return nil, ErrGasUintOverflow
} }
// memory is expanded in words of 32 bytes. Gas // memory is expanded in words of 32 bytes. Gas
// is also calculated in words. // is also calculated in words.
if memorySize, overflow = math.SafeMul(toWordSize(memSize), 32); overflow { if memorySize, overflow = math.SafeMul(toWordSize(memSize), 32); overflow {
return nil, errGasUintOverflow return nil, ErrGasUintOverflow
} }
} }
// Dynamic portion of gas // Dynamic portion of gas
@ -265,7 +265,7 @@ func (in *EVMInterpreter) Run(contract *Contract, input []byte, readOnly bool) (
case err != nil: case err != nil:
return nil, err return nil, err
case operation.reverts: case operation.reverts:
return res, errExecutionReverted return res, ErrExecutionReverted
case operation.halts: case operation.halts:
return res, nil return res, nil
case !operation.jumps: case !operation.jumps:

View file

@ -17,8 +17,6 @@
package vm package vm
import ( import (
"errors"
"github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/params"
) )
@ -29,8 +27,6 @@ type (
memorySizeFunc func(*Stack) (size uint64, overflow bool) memorySizeFunc func(*Stack) (size uint64, overflow bool)
) )
var errGasUintOverflow = errors.New("gas uint64 overflow")
type operation struct { type operation struct {
// execute is the operation function // execute is the operation function
execute executionFunc execute executionFunc