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

View file

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

View file

@ -16,17 +16,12 @@
package vm package vm
import ( import "errors"
"errors"
"fmt"
"github.com/ethereum/go-ethereum/params"
)
var ( var (
OutOfGasError = errors.New("Out of gas") ErrOutOfGas = errors.New("out of gas")
CodeStoreOutOfGasError = errors.New("Contract creation code storage out of gas") ErrCodeStoreOutOfGas = errors.New("contract creation code storage out of gas")
DepthError = fmt.Errorf("Max call depth exceeded (%d)", params.CallCreateDepth) ErrDepth = errors.New("max call depth exceeded")
TraceLimitReachedError = errors.New("The number of logs reached the specified limit") ErrTraceLimitReached = errors.New("the number of logs reached the specified limit")
ErrInsufficientBalance = errors.New("insufficient balance for transfer") 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 // homestead we must check for CodeStoreOutOfGasError (homestead only
// rule) and treat as an error, if the ruleset is frontier we must // rule) and treat as an error, if the ruleset is frontier we must
// ignore this error and pretend the operation was successful. // 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)) stack.push(new(big.Int))
} else if suberr != nil && suberr != CodeStoreOutOfGasError { } else if suberr != nil && suberr != ErrCodeStoreOutOfGas {
stack.push(new(big.Int)) stack.push(new(big.Int))
} else { } else {
stack.push(addr.Big()) 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 { 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 // check if already accumulated the specified number of logs
if l.cfg.Limit != 0 && l.cfg.Limit <= len(l.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 // 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 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) cost = operation.gasCost(evm.gasTable, evm.env, contract, stack, mem, memorySize)
if !contract.UseGas(cost) { if !contract.UseGas(cost) {
return nil, OutOfGasError return nil, ErrOutOfGas
} }
} }
if memorySize != nil { if memorySize != nil {