Added vm options for object execution

This commit is contained in:
obscuren 2014-08-05 11:10:24 +02:00
parent c215bbadf1
commit 4f0bda403e
4 changed files with 58 additions and 39 deletions

@ -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 {

26
ethpipe/object.go Normal file

@ -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))
}

@ -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
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
}

@ -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 {