From 4f0bda403ea332eeb477f8e56457423628772b19 Mon Sep 17 00:00:00 2001 From: obscuren Date: Tue, 5 Aug 2014 11:10:24 +0200 Subject: [PATCH] Added vm options for object execution --- ethpipe/config.go | 26 ++++---------------------- ethpipe/object.go | 26 ++++++++++++++++++++++++++ ethpipe/pipe.go | 33 ++++++++++++++++++++++----------- ethpipe/world.go | 12 ++++++------ 4 files changed, 58 insertions(+), 39 deletions(-) create mode 100644 ethpipe/object.go diff --git a/ethpipe/config.go b/ethpipe/config.go index 764f5596f..81d36d514 100644 --- a/ethpipe/config.go +++ b/ethpipe/config.go @@ -1,33 +1,14 @@ package ethpipe -import ( - "github.com/ethereum/eth-go/ethstate" - "github.com/ethereum/eth-go/ethutil" -) +import "github.com/ethereum/eth-go/ethutil" var cnfCtr = ethutil.Hex2Bytes("661005d2720d855f1d9976f88bb10c1a3398c77f") -type object struct { - *ethstate.StateObject -} - -func (self object) StorageString(str string) *ethutil.Value { - if ethutil.IsHex(str) { - return self.Storage(ethutil.Hex2Bytes(str[2:])) - } else { - return self.Storage(ethutil.RightPadBytes([]byte(str), 32)) - } -} - -func (self object) Storage(addr []byte) *ethutil.Value { - return self.StateObject.GetStorage(ethutil.BigD(addr)) -} - type config struct { pipe *Pipe } -func (self *config) Get(name string) object { +func (self *config) Get(name string) *object { configCtrl := self.pipe.World().safeGet(cnfCtr) var addr []byte @@ -39,7 +20,8 @@ func (self *config) Get(name string) object { } objectAddr := configCtrl.GetStorage(ethutil.BigD(addr)) - return object{self.pipe.World().safeGet(objectAddr.Bytes())} + + return &object{self.pipe.World().safeGet(objectAddr.Bytes())} } func (self *config) Exist() bool { diff --git a/ethpipe/object.go b/ethpipe/object.go new file mode 100644 index 000000000..ebfb5b904 --- /dev/null +++ b/ethpipe/object.go @@ -0,0 +1,26 @@ +package ethpipe + +import ( + "github.com/ethereum/eth-go/ethstate" + "github.com/ethereum/eth-go/ethutil" +) + +type object struct { + *ethstate.StateObject +} + +func (self *object) StorageString(str string) *ethutil.Value { + if ethutil.IsHex(str) { + return self.Storage(ethutil.Hex2Bytes(str[2:])) + } else { + return self.Storage(ethutil.RightPadBytes([]byte(str), 32)) + } +} + +func (self *object) StorageValue(addr *ethutil.Value) *ethutil.Value { + return self.Storage(addr.Bytes()) +} + +func (self *object) Storage(addr []byte) *ethutil.Value { + return self.StateObject.GetStorage(ethutil.BigD(addr)) +} diff --git a/ethpipe/pipe.go b/ethpipe/pipe.go index ca0a3416c..876a953aa 100644 --- a/ethpipe/pipe.go +++ b/ethpipe/pipe.go @@ -13,11 +13,17 @@ import ( var logger = ethlog.NewLogger("PIPE") +type VmVars struct { + State *ethstate.State +} + type Pipe struct { obj ethchain.EthManager stateManager *ethchain.StateManager blockChain *ethchain.BlockChain world *world + + Vm VmVars } func New(obj ethchain.EthManager) *Pipe { @@ -40,19 +46,22 @@ func (self *Pipe) Nonce(addr []byte) uint64 { } func (self *Pipe) Execute(addr []byte, data []byte, value, gas, price *ethutil.Value) ([]byte, error) { - return self.ExecuteObject(self.World().safeGet(addr), data, value, gas, price) + return self.ExecuteObject(&object{self.World().safeGet(addr)}, data, value, gas, price) } -func (self *Pipe) ExecuteObject(object *ethstate.StateObject, data []byte, value, gas, price *ethutil.Value) ([]byte, error) { +func (self *Pipe) ExecuteObject(object *object, data []byte, value, gas, price *ethutil.Value) ([]byte, error) { var ( - initiator = ethstate.NewStateObject([]byte{0}) - state = self.World().State().Copy() - block = self.blockChain.CurrentBlock + initiator = ethstate.NewStateObject([]byte{0}) + block = self.blockChain.CurrentBlock + stateObject = object.StateObject ) + if self.Vm.State == nil { + self.Vm.State = self.World().State().Copy() + } - vm := ethvm.New(NewEnv(state, block, value.BigInt(), initiator.Address())) + vm := ethvm.New(NewEnv(self.Vm.State, block, value.BigInt(), initiator.Address())) - closure := ethvm.NewClosure(initiator, object, object.Code, gas.BigInt(), price.BigInt()) + closure := ethvm.NewClosure(initiator, stateObject, object.Code, gas.BigInt(), price.BigInt()) ret, _, err := closure.Call(vm, data) return ret, err @@ -79,7 +88,7 @@ func (self *Pipe) Exists(addr []byte) bool { return self.World().Get(addr) != nil } -func (self *Pipe) TransactString(key *ethcrypto.KeyPair, rec string, value, gas, price *ethutil.Value, data []byte) error { +func (self *Pipe) TransactString(key *ethcrypto.KeyPair, rec string, value, gas, price *ethutil.Value, data []byte) ([]byte, error) { // Check if an address is stored by this address var hash []byte addr := self.World().Config().Get("NameReg").StorageString(rec).Bytes() @@ -94,7 +103,7 @@ func (self *Pipe) TransactString(key *ethcrypto.KeyPair, rec string, value, gas, return self.Transact(key, hash, value, gas, price, data) } -func (self *Pipe) Transact(key *ethcrypto.KeyPair, rec []byte, value, gas, price *ethutil.Value, data []byte) error { +func (self *Pipe) Transact(key *ethcrypto.KeyPair, rec []byte, value, gas, price *ethutil.Value, data []byte) ([]byte, error) { var hash []byte var contractCreation bool if rec == nil { @@ -106,7 +115,7 @@ func (self *Pipe) Transact(key *ethcrypto.KeyPair, rec []byte, value, gas, price if contractCreation { script, err := ethutil.Compile(string(data), false) if err != nil { - return err + return nil, err } tx = ethchain.NewContractCreationTx(value.BigInt(), gas.BigInt(), price.BigInt(), script) @@ -133,7 +142,9 @@ func (self *Pipe) Transact(key *ethcrypto.KeyPair, rec []byte, value, gas, price if contractCreation { logger.Infof("Contract addr %x", tx.CreationAddress()) + + return tx.CreationAddress(), nil } - return nil + return tx.Hash(), nil } diff --git a/ethpipe/world.go b/ethpipe/world.go index 42e19fe00..25e2a1952 100644 --- a/ethpipe/world.go +++ b/ethpipe/world.go @@ -26,17 +26,17 @@ func (self *world) State() *ethstate.State { return self.pipe.stateManager.CurrentState() } -func (self *world) Get(addr []byte) *ethstate.StateObject { - return self.State().GetStateObject(addr) +func (self *world) Get(addr []byte) *object { + return &object{self.State().GetStateObject(addr)} } func (self *world) safeGet(addr []byte) *ethstate.StateObject { - object := self.Get(addr) - if object != nil { - return object + object := self.State().GetStateObject(addr) + if object == nil { + object = ethstate.NewStateObject(addr) } - return ethstate.NewStateObject(addr) + return object } func (self *world) Coinbase() *ethstate.StateObject {