core, tests: reduced state copy by N calls

Reduced the amount of state copied that are required by N calls by doing
a balance check prior to any state modifications.
This commit is contained in:
Jeffrey Wilcke 2015-08-02 02:20:41 +02:00
parent 846f34f78b
commit 184e9ae9a8
9 changed files with 39 additions and 18 deletions

View File

@ -206,6 +206,9 @@ func (self *VMEnv) StructLogs() []vm.StructLog {
func (self *VMEnv) AddLog(log *state.Log) { func (self *VMEnv) AddLog(log *state.Log) {
self.state.AddLog(log) self.state.AddLog(log)
} }
func (self *VMEnv) CanTransfer(from vm.Account, balance *big.Int) bool {
return from.Balance().Cmp(balance) >= 0
}
func (self *VMEnv) Transfer(from, to vm.Account, amount *big.Int) error { func (self *VMEnv) Transfer(from, to vm.Account, amount *big.Int) error {
return vm.Transfer(from, to, amount) return vm.Transfer(from, to, amount)
} }

View File

@ -26,6 +26,7 @@ import (
"github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/params"
) )
// Execution is the execution environment for the given call or create action.
type Execution struct { type Execution struct {
env vm.Environment env vm.Environment
address *common.Address address *common.Address
@ -35,12 +36,15 @@ type Execution struct {
Gas, price, value *big.Int Gas, price, value *big.Int
} }
// NewExecution returns a new execution environment that handles all calling
// and creation logic defined by the YP.
func NewExecution(env vm.Environment, address *common.Address, input []byte, gas, gasPrice, value *big.Int) *Execution { func NewExecution(env vm.Environment, address *common.Address, input []byte, gas, gasPrice, value *big.Int) *Execution {
exe := &Execution{env: env, address: address, input: input, Gas: gas, price: gasPrice, value: value} exe := &Execution{env: env, address: address, input: input, Gas: gas, price: gasPrice, value: value}
exe.evm = vm.NewVm(env) exe.evm = vm.NewVm(env)
return exe return exe
} }
// Call executes within the given context
func (self *Execution) Call(codeAddr common.Address, caller vm.ContextRef) ([]byte, error) { func (self *Execution) Call(codeAddr common.Address, caller vm.ContextRef) ([]byte, error) {
// Retrieve the executing code // Retrieve the executing code
code := self.env.State().GetCode(codeAddr) code := self.env.State().GetCode(codeAddr)
@ -48,6 +52,9 @@ func (self *Execution) Call(codeAddr common.Address, caller vm.ContextRef) ([]by
return self.exec(&codeAddr, code, caller) return self.exec(&codeAddr, code, caller)
} }
// Create creates a new contract and runs the initialisation procedure of the
// contract. This returns the returned code for the contract and is stored
// elsewhere.
func (self *Execution) Create(caller vm.ContextRef) (ret []byte, err error, account *state.StateObject) { func (self *Execution) Create(caller vm.ContextRef) (ret []byte, err error, account *state.StateObject) {
// Input must be nil for create // Input must be nil for create
code := self.input code := self.input
@ -63,16 +70,24 @@ func (self *Execution) Create(caller vm.ContextRef) (ret []byte, err error, acco
return return
} }
// exec executes the given code and executes within the contextAddr context.
func (self *Execution) exec(contextAddr *common.Address, code []byte, caller vm.ContextRef) (ret []byte, err error) { func (self *Execution) exec(contextAddr *common.Address, code []byte, caller vm.ContextRef) (ret []byte, err error) {
env := self.env env := self.env
evm := self.evm evm := self.evm
// Depth check execution. Fail if we're trying to execute above the
// limit.
if env.Depth() > int(params.CallCreateDepth.Int64()) { if env.Depth() > int(params.CallCreateDepth.Int64()) {
caller.ReturnGas(self.Gas, self.price) caller.ReturnGas(self.Gas, self.price)
return nil, vm.DepthError return nil, vm.DepthError
} }
vsnapshot := env.State().Copy() if !env.CanTransfer(env.State().GetStateObject(caller.Address()), self.value) {
caller.ReturnGas(self.Gas, self.price)
return nil, ValueTransferErr("insufficient funds to transfer value. Req %v, has %v", self.value, env.State().GetBalance(caller.Address()))
}
var createAccount bool var createAccount bool
if self.address == nil { if self.address == nil {
// Generate a new address // Generate a new address
@ -95,15 +110,7 @@ func (self *Execution) exec(contextAddr *common.Address, code []byte, caller vm.
} else { } else {
to = env.State().GetOrNewStateObject(*self.address) to = env.State().GetOrNewStateObject(*self.address)
} }
vm.Transfer(from, to, self.value)
err = env.Transfer(from, to, self.value)
if err != nil {
env.State().Set(vsnapshot)
caller.ReturnGas(self.Gas, self.price)
return nil, ValueTransferErr("insufficient funds to transfer value. Req %v, has %v", self.value, from.Balance())
}
context := vm.NewContext(caller, to, self.value, self.Gas, self.price) context := vm.NewContext(caller, to, self.value, self.Gas, self.price)
context.SetCallCode(contextAddr, code) context.SetCallCode(contextAddr, code)

View File

@ -36,6 +36,7 @@ type Environment interface {
Time() uint64 Time() uint64
Difficulty() *big.Int Difficulty() *big.Int
GasLimit() *big.Int GasLimit() *big.Int
CanTransfer(from Account, balance *big.Int) bool
Transfer(from, to Account, amount *big.Int) error Transfer(from, to Account, amount *big.Int) error
AddLog(*state.Log) AddLog(*state.Log)
AddStructLog(StructLog) AddStructLog(StructLog)

View File

@ -13,6 +13,7 @@
// //
// You should have received a copy of the GNU Lesser General Public License // 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/>. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package vm package vm
import ( import (

View File

@ -13,6 +13,7 @@
// //
// You should have received a copy of the GNU Lesser General Public License // 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/>. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package vm package vm
import ( import (
@ -48,7 +49,7 @@ func SetJITCacheSize(size int) {
programs, _ = lru.New(size) programs, _ = lru.New(size)
} }
// GetProgram returns the program by id or nil when non-existant // GetProgram returns the program by id or nil when non-existent
func GetProgram(id common.Hash) *Program { func GetProgram(id common.Hash) *Program {
if p, ok := programs.Get(id); ok { if p, ok := programs.Get(id); ok {
return p.(*Program) return p.(*Program)

View File

@ -105,6 +105,9 @@ func (self *Env) AddLog(log *state.Log) {
} }
func (self *Env) Depth() int { return self.depth } func (self *Env) Depth() int { return self.depth }
func (self *Env) SetDepth(i int) { self.depth = i } func (self *Env) SetDepth(i int) { self.depth = i }
func (self *Env) CanTransfer(from Account, balance *big.Int) bool {
return from.Balance().Cmp(balance) >= 0
}
func (self *Env) Transfer(from, to Account, amount *big.Int) error { func (self *Env) Transfer(from, to Account, amount *big.Int) error {
return nil return nil
} }

View File

@ -13,6 +13,7 @@
// //
// You should have received a copy of the GNU Lesser General Public License // 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/>. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package vm package vm
var ( var (

View File

@ -69,6 +69,10 @@ func (self *VMEnv) GetHash(n uint64) common.Hash {
func (self *VMEnv) AddLog(log *state.Log) { func (self *VMEnv) AddLog(log *state.Log) {
self.state.AddLog(log) self.state.AddLog(log)
} }
func (self *VMEnv) CanTransfer(from vm.Account, balance *big.Int) bool {
return from.Balance().Cmp(balance) >= 0
}
func (self *VMEnv) Transfer(from, to vm.Account, amount *big.Int) error { func (self *VMEnv) Transfer(from, to vm.Account, amount *big.Int) error {
return vm.Transfer(from, to, amount) return vm.Transfer(from, to, amount)
} }

View File

@ -18,7 +18,6 @@ package tests
import ( import (
"bytes" "bytes"
"errors"
"fmt" "fmt"
"math/big" "math/big"
@ -192,18 +191,19 @@ func (self *Env) AddLog(log *state.Log) {
} }
func (self *Env) Depth() int { return self.depth } func (self *Env) Depth() int { return self.depth }
func (self *Env) SetDepth(i int) { self.depth = i } func (self *Env) SetDepth(i int) { self.depth = i }
func (self *Env) Transfer(from, to vm.Account, amount *big.Int) error { func (self *Env) CanTransfer(from vm.Account, balance *big.Int) bool {
if self.skipTransfer { if self.skipTransfer {
// ugly hack
if self.initial { if self.initial {
self.initial = false self.initial = false
return nil return true
} }
}
if from.Balance().Cmp(amount) < 0 { return from.Balance().Cmp(balance) >= 0
return errors.New("Insufficient balance in account") }
}
func (self *Env) Transfer(from, to vm.Account, amount *big.Int) error {
if self.skipTransfer {
return nil return nil
} }
return vm.Transfer(from, to, amount) return vm.Transfer(from, to, amount)