core, core/vm: review changed from #3348

This commit is contained in:
Jeffrey Wilcke 2016-12-13 11:31:42 +01:00
parent 556ab6f508
commit 514f733d3d
7 changed files with 26 additions and 31 deletions

View file

@ -159,7 +159,7 @@ func (self *StateTransition) to() vm.Account {
func (self *StateTransition) useGas(amount *big.Int) error {
if self.gas.Cmp(amount) < 0 {
return vm.OutOfGasError
return vm.ErrOutOfGas
}
self.gas.Sub(self.gas, amount)
@ -233,7 +233,7 @@ func (self *StateTransition) TransitionDb() (ret []byte, requiredGas, usedGas *b
)
if contractCreation {
ret, _, vmerr = vmenv.Create(sender, self.data, self.gas, self.value)
if homestead && err == vm.CodeStoreOutOfGasError {
if homestead && err == vm.ErrCodeStoreOutOfGas {
self.gas = Big0
}
} else {

View file

@ -50,7 +50,7 @@ func RunPrecompiledContract(p PrecompiledContract, input []byte, contract *Contr
return ret, nil
} else {
return nil, OutOfGasError
return nil, ErrOutOfGas
}
}

View file

@ -100,7 +100,7 @@ func (env *Environment) Call(caller ContractRef, addr common.Address, input []by
if env.Depth > int(params.CallCreateDepth.Int64()) {
caller.ReturnGas(gas)
return nil, DepthError
return nil, ErrDepth
}
if !env.Context.CanTransfer(env.StateDB, caller.Address(), value) {
caller.ReturnGas(gas)
@ -109,8 +109,8 @@ func (env *Environment) Call(caller ContractRef, addr common.Address, input []by
}
var (
to Account
snapshotPreTransfer = env.StateDB.Snapshot()
to Account
snapshot = env.StateDB.Snapshot()
)
if !env.StateDB.Exist(addr) {
if PrecompiledContracts[addr] == nil && env.ChainConfig().IsEIP158(env.BlockNumber) && value.BitLen() == 0 {
@ -138,7 +138,7 @@ func (env *Environment) Call(caller ContractRef, addr common.Address, input []by
if err != nil {
contract.UseGas(contract.Gas)
env.StateDB.RevertToSnapshot(snapshotPreTransfer)
env.StateDB.RevertToSnapshot(snapshot)
}
return ret, err
}
@ -160,7 +160,7 @@ func (env *Environment) CallCode(caller ContractRef, addr common.Address, input
if env.Depth > int(params.CallCreateDepth.Int64()) {
caller.ReturnGas(gas)
return nil, DepthError
return nil, ErrDepth
}
if !env.CanTransfer(env.StateDB, caller.Address(), value) {
caller.ReturnGas(gas)
@ -169,8 +169,8 @@ func (env *Environment) CallCode(caller ContractRef, addr common.Address, input
}
var (
snapshotPreTransfer = env.StateDB.Snapshot()
to = env.StateDB.GetAccount(caller.Address())
snapshot = env.StateDB.Snapshot()
to = env.StateDB.GetAccount(caller.Address())
)
// initialise a new contract and set the code that is to be used by the
// E The contract is a scoped environment for this execution context
@ -183,7 +183,7 @@ func (env *Environment) CallCode(caller ContractRef, addr common.Address, input
if err != nil {
contract.UseGas(contract.Gas)
env.StateDB.RevertToSnapshot(snapshotPreTransfer)
env.StateDB.RevertToSnapshot(snapshot)
}
return ret, err
@ -205,7 +205,7 @@ func (env *Environment) DelegateCall(caller ContractRef, addr common.Address, in
// limit.
if env.Depth > int(params.CallCreateDepth.Int64()) {
caller.ReturnGas(gas)
return nil, DepthError
return nil, ErrDepth
}
var (
@ -241,7 +241,7 @@ func (env *Environment) Create(caller ContractRef, code []byte, gas, value *big.
if env.Depth > int(params.CallCreateDepth.Int64()) {
caller.ReturnGas(gas)
return nil, common.Address{}, DepthError
return nil, common.Address{}, ErrDepth
}
if !env.CanTransfer(env.StateDB, caller.Address(), value) {
caller.ReturnGas(gas)
@ -253,7 +253,7 @@ func (env *Environment) Create(caller ContractRef, code []byte, gas, value *big.
nonce := env.StateDB.GetNonce(caller.Address())
env.StateDB.SetNonce(caller.Address(), nonce+1)
snapshotPreTransfer := env.StateDB.Snapshot()
snapshot := env.StateDB.Snapshot()
var (
addr = crypto.CreateAddress(caller.Address(), nonce)
to = env.StateDB.CreateAccount(addr)
@ -283,7 +283,7 @@ func (env *Environment) Create(caller ContractRef, code []byte, gas, value *big.
if contract.UseGas(dataGas) {
env.StateDB.SetCode(addr, ret)
} else {
err = CodeStoreOutOfGasError
err = ErrCodeStoreOutOfGas
}
}
@ -291,9 +291,9 @@ func (env *Environment) Create(caller ContractRef, code []byte, gas, value *big.
// 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.
if maxCodeSizeExceeded ||
(err != nil && (env.ChainConfig().IsHomestead(env.BlockNumber) || err != CodeStoreOutOfGasError)) {
(err != nil && (env.ChainConfig().IsHomestead(env.BlockNumber) || err != ErrCodeStoreOutOfGas)) {
contract.UseGas(contract.Gas)
env.StateDB.RevertToSnapshot(snapshotPreTransfer)
env.StateDB.RevertToSnapshot(snapshot)
// Nothing should be returned when an error is thrown.
return nil, addr, err

View file

@ -16,17 +16,12 @@
package vm
import (
"errors"
"fmt"
"github.com/ethereum/go-ethereum/params"
)
import "errors"
var (
OutOfGasError = errors.New("Out of gas")
CodeStoreOutOfGasError = errors.New("Contract creation code storage out of gas")
DepthError = fmt.Errorf("Max call depth exceeded (%d)", params.CallCreateDepth)
TraceLimitReachedError = errors.New("The number of logs reached the specified limit")
ErrOutOfGas = errors.New("out of gas")
ErrCodeStoreOutOfGas = errors.New("contract creation code storage out of gas")
ErrDepth = errors.New("max call depth exceeded")
ErrTraceLimitReached = errors.New("the number of logs reached the specified limit")
ErrInsufficientBalance = errors.New("insufficient balance for transfer")
)

View file

@ -478,9 +478,9 @@ func opCreate(pc *uint64, env *Environment, contract *Contract, memory *Memory,
// homestead we must check for CodeStoreOutOfGasError (homestead only
// rule) and treat as an error, if the ruleset is frontier we must
// ignore this error and pretend the operation was successful.
if env.ChainConfig().IsHomestead(env.BlockNumber) && suberr == CodeStoreOutOfGasError {
if env.ChainConfig().IsHomestead(env.BlockNumber) && suberr == ErrCodeStoreOutOfGas {
stack.push(new(big.Int))
} else if suberr != nil && suberr != CodeStoreOutOfGasError {
} else if suberr != nil && suberr != ErrCodeStoreOutOfGas {
stack.push(new(big.Int))
} else {
stack.push(addr.Big())

View file

@ -97,7 +97,7 @@ func NewStructLogger(cfg *LogConfig) *StructLogger {
func (l *StructLogger) CaptureState(env *Environment, pc uint64, op OpCode, gas, cost *big.Int, memory *Memory, stack *Stack, contract *Contract, depth int, err error) error {
// check if already accumulated the specified number of logs
if l.cfg.Limit != 0 && l.cfg.Limit <= len(l.logs) {
return TraceLimitReachedError
return ErrTraceLimitReached
}
// initialise new changed values storage container for this contract

View file

@ -168,7 +168,7 @@ func (evm *EVM) Run(contract *Contract, input []byte) (ret []byte, err error) {
// cost is explicitly set so that the capture state defer method cas get the proper cost
cost = operation.gasCost(evm.gasTable, evm.env, contract, stack, mem, memorySize)
if !contract.UseGas(cost) {
return nil, OutOfGasError
return nil, ErrOutOfGas
}
}
if memorySize != nil {