From 514f733d3dac8dff0424e9553d9667f1b140b694 Mon Sep 17 00:00:00 2001 From: Jeffrey Wilcke Date: Tue, 13 Dec 2016 11:31:42 +0100 Subject: [PATCH] core, core/vm: review changed from #3348 --- core/state_transition.go | 4 ++-- core/vm/contracts.go | 2 +- core/vm/environment.go | 28 ++++++++++++++-------------- core/vm/errors.go | 15 +++++---------- core/vm/instructions.go | 4 ++-- core/vm/logger.go | 2 +- core/vm/vm.go | 2 +- 7 files changed, 26 insertions(+), 31 deletions(-) diff --git a/core/state_transition.go b/core/state_transition.go index 48540be142..81cd22abd2 100644 --- a/core/state_transition.go +++ b/core/state_transition.go @@ -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 { diff --git a/core/vm/contracts.go b/core/vm/contracts.go index a976688c0e..d145206493 100644 --- a/core/vm/contracts.go +++ b/core/vm/contracts.go @@ -50,7 +50,7 @@ func RunPrecompiledContract(p PrecompiledContract, input []byte, contract *Contr return ret, nil } else { - return nil, OutOfGasError + return nil, ErrOutOfGas } } diff --git a/core/vm/environment.go b/core/vm/environment.go index ca39dec589..b2ac286f4f 100644 --- a/core/vm/environment.go +++ b/core/vm/environment.go @@ -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 diff --git a/core/vm/errors.go b/core/vm/errors.go index f8d26b1f0a..69c7d6a98c 100644 --- a/core/vm/errors.go +++ b/core/vm/errors.go @@ -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") ) diff --git a/core/vm/instructions.go b/core/vm/instructions.go index 91fdbf8251..5d4c42b2fc 100644 --- a/core/vm/instructions.go +++ b/core/vm/instructions.go @@ -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()) diff --git a/core/vm/logger.go b/core/vm/logger.go index 6a605a59cd..370d234aa0 100644 --- a/core/vm/logger.go +++ b/core/vm/logger.go @@ -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 diff --git a/core/vm/vm.go b/core/vm/vm.go index 4df8457cd5..2281daecdb 100644 --- a/core/vm/vm.go +++ b/core/vm/vm.go @@ -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 {