From 342cc122b43f01301d0188de1e333c32ed64ae8c Mon Sep 17 00:00:00 2001 From: obscuren Date: Mon, 4 Aug 2014 16:25:53 +0200 Subject: Added general Pipe API --- ethpipe/pipe.go | 135 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 ethpipe/pipe.go (limited to 'ethpipe/pipe.go') diff --git a/ethpipe/pipe.go b/ethpipe/pipe.go new file mode 100644 index 000000000..710fc4e7c --- /dev/null +++ b/ethpipe/pipe.go @@ -0,0 +1,135 @@ +package ethpipe + +import ( + "strings" + + "github.com/ethereum/eth-go/ethchain" + "github.com/ethereum/eth-go/ethcrypto" + "github.com/ethereum/eth-go/ethlog" + "github.com/ethereum/eth-go/ethstate" + "github.com/ethereum/eth-go/ethutil" + "github.com/ethereum/eth-go/ethvm" +) + +var logger = ethlog.NewLogger("PIPE") + +type Pipe struct { + obj ethchain.EthManager + stateManager *ethchain.StateManager + blockChain *ethchain.BlockChain + world *world +} + +func New(obj ethchain.EthManager) *Pipe { + pipe := &Pipe{ + obj: obj, + stateManager: obj.StateManager(), + blockChain: obj.BlockChain(), + } + pipe.world = NewWorld(pipe) + + return pipe +} + +func (self *Pipe) Balance(addr []byte) *ethutil.Value { + return ethutil.NewValue(self.World().safeGet(addr).Balance) +} + +func (self *Pipe) Nonce(addr []byte) uint64 { + return self.World().safeGet(addr).Nonce +} + +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) +} + +func (self *Pipe) ExecuteObject(object *ethstate.StateObject, data []byte, value, gas, price *ethutil.Value) ([]byte, error) { + var ( + initiator = ethstate.NewStateObject([]byte{0}) + state = self.World().State().Copy() + block = self.blockChain.CurrentBlock + ) + + vm := ethvm.New(NewEnv(state, block, value.BigInt(), initiator.Address())) + + closure := ethvm.NewClosure(initiator, object, object.Code, gas.BigInt(), price.BigInt()) + ret, _, err := closure.Call(vm, data) + + return ret, err +} + +func (self *Pipe) Block(hash []byte) *ethchain.Block { + return self.blockChain.GetBlock(hash) +} + +func (self *Pipe) Storage(addr, storageAddr []byte) *ethutil.Value { + return self.World().safeGet(addr).GetStorage(ethutil.BigD(storageAddr)) +} + +func (self *Pipe) ToAddress(priv []byte) []byte { + pair, err := ethcrypto.NewKeyPairFromSec(priv) + if err != nil { + return nil + } + + return pair.Address() +} + +func (self *Pipe) TransactString(key *ethcrypto.KeyPair, rec string, value, gas, price *ethutil.Value, data []byte) error { + // Check if an address is stored by this address + var hash []byte + addr := self.World().Config().Get("NameReg").StorageString(rec).Bytes() + if len(addr) > 0 { + hash = addr + } else if ethutil.IsHex(rec) { + hash = ethutil.Hex2Bytes(rec[2:]) + } else { + hash = ethutil.Hex2Bytes(rec) + } + + 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 { + var hash []byte + var contractCreation bool + if rec == nil { + contractCreation = true + } + + var tx *ethchain.Transaction + // Compile and assemble the given data + if contractCreation { + script, err := ethutil.Compile(string(data), false) + if err != nil { + return err + } + + tx = ethchain.NewContractCreationTx(value.BigInt(), gas.BigInt(), price.BigInt(), script) + } else { + data := ethutil.StringToByteFunc(string(data), func(s string) (ret []byte) { + slice := strings.Split(s, "\n") + for _, dataItem := range slice { + d := ethutil.FormatData(dataItem) + ret = append(ret, d...) + } + return + }) + + tx = ethchain.NewTransactionMessage(hash, value.BigInt(), gas.BigInt(), price.BigInt(), data) + } + + acc := self.stateManager.TransState().GetOrNewStateObject(key.Address()) + tx.Nonce = acc.Nonce + acc.Nonce += 1 + self.stateManager.TransState().UpdateStateObject(acc) + + tx.Sign(key.PrivateKey) + self.obj.TxPool().QueueTransaction(tx) + + if contractCreation { + logger.Infof("Contract addr %x", tx.CreationAddress()) + } + + return nil +} -- cgit v1.2.3 From 0f84b9c30d06a59f20a2d33ffd1281d5e6e2681a Mon Sep 17 00:00:00 2001 From: obscuren Date: Mon, 4 Aug 2014 16:34:55 +0200 Subject: Added exist method --- ethpipe/pipe.go | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'ethpipe/pipe.go') diff --git a/ethpipe/pipe.go b/ethpipe/pipe.go index 710fc4e7c..ca0a3416c 100644 --- a/ethpipe/pipe.go +++ b/ethpipe/pipe.go @@ -75,6 +75,10 @@ func (self *Pipe) ToAddress(priv []byte) []byte { return pair.Address() } +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 { // Check if an address is stored by this address var hash []byte -- cgit v1.2.3 From 4f0bda403ea332eeb477f8e56457423628772b19 Mon Sep 17 00:00:00 2001 From: obscuren Date: Tue, 5 Aug 2014 11:10:24 +0200 Subject: Added vm options for object execution --- ethpipe/pipe.go | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) (limited to 'ethpipe/pipe.go') 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 } -- cgit v1.2.3 From e71b198e3d8df1bd8b73bae9bc934b778a3115bf Mon Sep 17 00:00:00 2001 From: obscuren Date: Tue, 5 Aug 2014 11:26:12 +0200 Subject: Renamed object to Object --- ethpipe/pipe.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'ethpipe/pipe.go') diff --git a/ethpipe/pipe.go b/ethpipe/pipe.go index 876a953aa..c00731b84 100644 --- a/ethpipe/pipe.go +++ b/ethpipe/pipe.go @@ -46,10 +46,10 @@ 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(&object{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 *object, 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}) block = self.blockChain.CurrentBlock -- cgit v1.2.3 From 3c78e418fbe70cfb574302f00962cf7fac50f69e Mon Sep 17 00:00:00 2001 From: obscuren Date: Tue, 5 Aug 2014 11:30:12 +0200 Subject: world => World --- ethpipe/pipe.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ethpipe/pipe.go') diff --git a/ethpipe/pipe.go b/ethpipe/pipe.go index c00731b84..a9da66ab8 100644 --- a/ethpipe/pipe.go +++ b/ethpipe/pipe.go @@ -21,7 +21,7 @@ type Pipe struct { obj ethchain.EthManager stateManager *ethchain.StateManager blockChain *ethchain.BlockChain - world *world + world *World Vm VmVars } -- cgit v1.2.3 From 7d95e8624a3bdca4a68b2a7ff6ed133264088cc1 Mon Sep 17 00:00:00 2001 From: obscuren Date: Fri, 15 Aug 2014 16:19:10 +0200 Subject: Added message to closure && added change addresses --- ethpipe/pipe.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ethpipe/pipe.go') diff --git a/ethpipe/pipe.go b/ethpipe/pipe.go index a9da66ab8..8a81734cd 100644 --- a/ethpipe/pipe.go +++ b/ethpipe/pipe.go @@ -61,7 +61,7 @@ func (self *Pipe) ExecuteObject(object *Object, data []byte, value, gas, price * vm := ethvm.New(NewEnv(self.Vm.State, block, value.BigInt(), initiator.Address())) - closure := ethvm.NewClosure(initiator, stateObject, object.Code, gas.BigInt(), price.BigInt()) + closure := ethvm.NewClosure(ðstate.Message{}, initiator, stateObject, object.Code, gas.BigInt(), price.BigInt()) ret, _, err := closure.Call(vm, data) return ret, err -- cgit v1.2.3 From 4008ff32c903e6894f5fb4fb69c795641641d192 Mon Sep 17 00:00:00 2001 From: obscuren Date: Sun, 17 Aug 2014 12:42:02 +0200 Subject: Mutan compile --- ethpipe/pipe.go | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'ethpipe/pipe.go') diff --git a/ethpipe/pipe.go b/ethpipe/pipe.go index 8a81734cd..800a71139 100644 --- a/ethpipe/pipe.go +++ b/ethpipe/pipe.go @@ -148,3 +148,12 @@ func (self *Pipe) Transact(key *ethcrypto.KeyPair, rec []byte, value, gas, price return tx.Hash(), nil } + +func (self *Pipe) CompileMutan(code string) ([]byte, error) { + data, err := ethutil.Compile(code, false) + if err != nil { + return nil, err + } + + return data, nil +} -- cgit v1.2.3