From 65a802c6787184951753ba2f79fdf60dce526721 Mon Sep 17 00:00:00 2001 From: obscuren Date: Mon, 22 Sep 2014 14:51:21 +0200 Subject: [PATCH] Re-wrote Call and Execute to use the new vm messages --- ethpipe/js_pipe.go | 30 +++++++++++++++++++++++------- ethpipe/pipe.go | 30 ++++++++++++++++-------------- 2 files changed, 39 insertions(+), 21 deletions(-) diff --git a/ethpipe/js_pipe.go b/ethpipe/js_pipe.go index 32212b26a..96990b671 100644 --- a/ethpipe/js_pipe.go +++ b/ethpipe/js_pipe.go @@ -85,10 +85,6 @@ func (self *JSPipe) CoinBase() string { return ethutil.Bytes2Hex(self.obj.KeyManager().Address()) } -func (self *JSPipe) BalanceAt(addr string) string { - return self.World().SafeGet(ethutil.Hex2Bytes(addr)).Balance.String() -} - func (self *JSPipe) NumberToHuman(balance string) string { b := ethutil.Big(balance) @@ -101,10 +97,18 @@ func (self *JSPipe) StorageAt(addr, storageAddr string) string { return ethutil.Bytes2Hex(storage.Bytes()) } +func (self *JSPipe) BalanceAt(addr string) string { + return self.World().SafeGet(ethutil.Hex2Bytes(addr)).Balance.String() +} + func (self *JSPipe) TxCountAt(address string) int { return int(self.World().SafeGet(ethutil.Hex2Bytes(address)).Nonce) } +func (self *JSPipe) CodeAt(address string) string { + return ethutil.Bytes2Hex(self.World().SafeGet(ethutil.Hex2Bytes(address)).Code) +} + func (self *JSPipe) IsContract(address string) bool { return len(self.World().SafeGet(ethutil.Hex2Bytes(address)).Code) > 0 } @@ -118,6 +122,18 @@ func (self *JSPipe) SecretToAddress(key string) string { return ethutil.Bytes2Hex(pair.Address()) } +func (self *JSPipe) Execute(addr, value, gas, price, data string) (string, error) { + ret, err := self.ExecuteObject(&Object{ + self.World().safeGet(ethutil.Hex2Bytes(addr))}, + ethutil.Hex2Bytes(data), + ethutil.NewValue(value), + ethutil.NewValue(gas), + ethutil.NewValue(price), + ) + + return ethutil.Bytes2Hex(ret), err +} + type KeyVal struct { Key string `json:"key"` Value string `json:"value"` @@ -224,9 +240,9 @@ func (self *JSPipe) Transact(key, toStr, valueStr, gasStr, gasPriceStr, codeStr } func (self *JSPipe) PushTx(txStr string) (*JSReceipt, error) { - tx := ethchain.NewTransactionFromBytes(ethutil.Hex2Bytes(txStr)) - self.obj.TxPool().QueueTransaction(tx) - return NewJSReciept(tx.CreatesContract(), tx.CreationAddress(), tx.Hash(), tx.Sender()), nil + tx := ethchain.NewTransactionFromBytes(ethutil.Hex2Bytes(txStr)) + self.obj.TxPool().QueueTransaction(tx) + return NewJSReciept(tx.CreatesContract(), tx.CreationAddress(), tx.Hash(), tx.Sender()), nil } func (self *JSPipe) CompileMutan(code string) string { diff --git a/ethpipe/pipe.go b/ethpipe/pipe.go index b7d3be041..7c3f491d3 100644 --- a/ethpipe/pipe.go +++ b/ethpipe/pipe.go @@ -1,6 +1,7 @@ package ethpipe import ( + "fmt" "strings" "github.com/ethereum/eth-go/ethchain" @@ -51,18 +52,19 @@ func (self *Pipe) Execute(addr []byte, data []byte, value, gas, price *ethutil.V 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 - stateObject = object.StateObject + initiator = ethstate.NewStateObject(self.obj.KeyManager().KeyPair().Address()) + block = self.blockChain.CurrentBlock ) - if self.Vm.State == nil { - self.Vm.State = self.World().State().Copy() - } + + self.Vm.State = self.World().State().Copy() vm := ethvm.New(NewEnv(self.Vm.State, block, value.BigInt(), initiator.Address())) + vm.Verbose = true - closure := ethvm.NewClosure(ðstate.Message{}, initiator, stateObject, object.Code, gas.BigInt(), price.BigInt()) - ret, _, err := closure.Call(vm, data) + msg := ethvm.NewMessage(vm, object.Address(), data, gas.BigInt(), price.BigInt(), value.BigInt()) + ret, err := msg.Exec(object.Address(), initiator) + + fmt.Println("returned from call", ret, err) return ret, err } @@ -150,12 +152,12 @@ func (self *Pipe) Transact(key *ethcrypto.KeyPair, rec []byte, value, gas, price } func (self *Pipe) PushTx(tx *ethchain.Transaction) ([]byte, error) { - self.obj.TxPool().QueueTransaction(tx) - if tx.Recipient == nil { - logger.Infof("Contract addr %x", tx.CreationAddress()) - return tx.CreationAddress(), nil - } - return tx.Hash(), nil + self.obj.TxPool().QueueTransaction(tx) + if tx.Recipient == nil { + logger.Infof("Contract addr %x", tx.CreationAddress()) + return tx.CreationAddress(), nil + } + return tx.Hash(), nil } func (self *Pipe) CompileMutan(code string) ([]byte, error) {