From 0ed1a8b50a9b9726cd57a2731d0405f6949c6188 Mon Sep 17 00:00:00 2001 From: obscuren Date: Fri, 31 Oct 2014 14:30:08 +0100 Subject: ethpipe => xeth (eXtended ETHereum) --- xeth/config.go | 33 +++++++ xeth/hexface.go | 264 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ xeth/js_types.go | 229 +++++++++++++++++++++++++++++++++++++++++++++++ xeth/object.go | 26 ++++++ xeth/pipe.go | 175 ++++++++++++++++++++++++++++++++++++ xeth/vm_env.go | 40 +++++++++ xeth/world.go | 64 ++++++++++++++ 7 files changed, 831 insertions(+) create mode 100644 xeth/config.go create mode 100644 xeth/hexface.go create mode 100644 xeth/js_types.go create mode 100644 xeth/object.go create mode 100644 xeth/pipe.go create mode 100644 xeth/vm_env.go create mode 100644 xeth/world.go (limited to 'xeth') diff --git a/xeth/config.go b/xeth/config.go new file mode 100644 index 000000000..34aa9e32d --- /dev/null +++ b/xeth/config.go @@ -0,0 +1,33 @@ +package xeth + +import "github.com/ethereum/go-ethereum/ethutil" + +var cnfCtr = ethutil.Hex2Bytes("661005d2720d855f1d9976f88bb10c1a3398c77f") + +type Config struct { + pipe *XEth +} + +func (self *Config) Get(name string) *Object { + configCtrl := self.pipe.World().safeGet(cnfCtr) + var addr []byte + + switch name { + case "NameReg": + addr = []byte{0} + case "DnsReg": + objectAddr := configCtrl.GetStorage(ethutil.BigD([]byte{0})) + domainAddr := (&Object{self.pipe.World().safeGet(objectAddr.Bytes())}).StorageString("DnsReg").Bytes() + return &Object{self.pipe.World().safeGet(domainAddr)} + default: + addr = ethutil.RightPadBytes([]byte(name), 32) + } + + objectAddr := configCtrl.GetStorage(ethutil.BigD(addr)) + + return &Object{self.pipe.World().safeGet(objectAddr.Bytes())} +} + +func (self *Config) Exist() bool { + return self.pipe.World().Get(cnfCtr) != nil +} diff --git a/xeth/hexface.go b/xeth/hexface.go new file mode 100644 index 000000000..829f530f4 --- /dev/null +++ b/xeth/hexface.go @@ -0,0 +1,264 @@ +package xeth + +import ( + "bytes" + "encoding/json" + "sync/atomic" + + "github.com/ethereum/go-ethereum/chain" + "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/ethstate" + "github.com/ethereum/go-ethereum/ethutil" +) + +type JSXEth struct { + *XEth +} + +func NewJSXEth(eth chain.EthManager) *JSXEth { + return &JSXEth{New(eth)} +} + +func (self *JSXEth) BlockByHash(strHash string) *JSBlock { + hash := ethutil.Hex2Bytes(strHash) + block := self.obj.ChainManager().GetBlock(hash) + + return NewJSBlock(block) +} + +func (self *JSXEth) BlockByNumber(num int32) *JSBlock { + if num == -1 { + return NewJSBlock(self.obj.ChainManager().CurrentBlock) + } + + return NewJSBlock(self.obj.ChainManager().GetBlockByNumber(uint64(num))) +} + +func (self *JSXEth) Block(v interface{}) *JSBlock { + if n, ok := v.(int32); ok { + return self.BlockByNumber(n) + } else if str, ok := v.(string); ok { + return self.BlockByHash(str) + } else if f, ok := v.(float64); ok { // Don't ask ... + return self.BlockByNumber(int32(f)) + } + + return nil +} + +func (self *JSXEth) Key() *JSKey { + return NewJSKey(self.obj.KeyManager().KeyPair()) +} + +func (self *JSXEth) StateObject(addr string) *JSObject { + object := &Object{self.World().safeGet(ethutil.Hex2Bytes(addr))} + + return NewJSObject(object) +} + +func (self *JSXEth) PeerCount() int { + return self.obj.PeerCount() +} + +func (self *JSXEth) Peers() []JSPeer { + var peers []JSPeer + for peer := self.obj.Peers().Front(); peer != nil; peer = peer.Next() { + p := peer.Value.(chain.Peer) + // we only want connected peers + if atomic.LoadInt32(p.Connected()) != 0 { + peers = append(peers, *NewJSPeer(p)) + } + } + + return peers +} + +func (self *JSXEth) IsMining() bool { + return self.obj.IsMining() +} + +func (self *JSXEth) IsListening() bool { + return self.obj.IsListening() +} + +func (self *JSXEth) CoinBase() string { + return ethutil.Bytes2Hex(self.obj.KeyManager().Address()) +} + +func (self *JSXEth) NumberToHuman(balance string) string { + b := ethutil.Big(balance) + + return ethutil.CurrencyToString(b) +} + +func (self *JSXEth) StorageAt(addr, storageAddr string) string { + storage := self.World().SafeGet(ethutil.Hex2Bytes(addr)).Storage(ethutil.Hex2Bytes(storageAddr)) + + return ethutil.Bytes2Hex(storage.Bytes()) +} + +func (self *JSXEth) BalanceAt(addr string) string { + return self.World().SafeGet(ethutil.Hex2Bytes(addr)).Balance().String() +} + +func (self *JSXEth) TxCountAt(address string) int { + return int(self.World().SafeGet(ethutil.Hex2Bytes(address)).Nonce) +} + +func (self *JSXEth) CodeAt(address string) string { + return ethutil.Bytes2Hex(self.World().SafeGet(ethutil.Hex2Bytes(address)).Code) +} + +func (self *JSXEth) IsContract(address string) bool { + return len(self.World().SafeGet(ethutil.Hex2Bytes(address)).Code) > 0 +} + +func (self *JSXEth) SecretToAddress(key string) string { + pair, err := crypto.NewKeyPairFromSec(ethutil.Hex2Bytes(key)) + if err != nil { + return "" + } + + return ethutil.Bytes2Hex(pair.Address()) +} + +func (self *JSXEth) 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"` +} + +func (self *JSXEth) EachStorage(addr string) string { + var values []KeyVal + object := self.World().SafeGet(ethutil.Hex2Bytes(addr)) + object.EachStorage(func(name string, value *ethutil.Value) { + value.Decode() + values = append(values, KeyVal{ethutil.Bytes2Hex([]byte(name)), ethutil.Bytes2Hex(value.Bytes())}) + }) + + valuesJson, err := json.Marshal(values) + if err != nil { + return "" + } + + return string(valuesJson) +} + +func (self *JSXEth) ToAscii(str string) string { + padded := ethutil.RightPadBytes([]byte(str), 32) + + return "0x" + ethutil.Bytes2Hex(padded) +} + +func (self *JSXEth) FromAscii(str string) string { + if ethutil.IsHex(str) { + str = str[2:] + } + + return string(bytes.Trim(ethutil.Hex2Bytes(str), "\x00")) +} + +func (self *JSXEth) FromNumber(str string) string { + if ethutil.IsHex(str) { + str = str[2:] + } + + return ethutil.BigD(ethutil.Hex2Bytes(str)).String() +} + +func (self *JSXEth) Transact(key, toStr, valueStr, gasStr, gasPriceStr, codeStr string) (*JSReceipt, error) { + var hash []byte + var contractCreation bool + if len(toStr) == 0 { + contractCreation = true + } else { + // Check if an address is stored by this address + addr := self.World().Config().Get("NameReg").StorageString(toStr).Bytes() + if len(addr) > 0 { + hash = addr + } else { + hash = ethutil.Hex2Bytes(toStr) + } + } + + var keyPair *crypto.KeyPair + var err error + if ethutil.IsHex(key) { + keyPair, err = crypto.NewKeyPairFromSec([]byte(ethutil.Hex2Bytes(key[2:]))) + } else { + keyPair, err = crypto.NewKeyPairFromSec([]byte(ethutil.Hex2Bytes(key))) + } + + if err != nil { + return nil, err + } + + var ( + value = ethutil.Big(valueStr) + gas = ethutil.Big(gasStr) + gasPrice = ethutil.Big(gasPriceStr) + data []byte + tx *chain.Transaction + ) + + if ethutil.IsHex(codeStr) { + data = ethutil.Hex2Bytes(codeStr[2:]) + } else { + data = ethutil.Hex2Bytes(codeStr) + } + + if contractCreation { + tx = chain.NewContractCreationTx(value, gas, gasPrice, data) + } else { + tx = chain.NewTransactionMessage(hash, value, gas, gasPrice, data) + } + + acc := self.obj.StateManager().TransState().GetOrNewStateObject(keyPair.Address()) + tx.Nonce = acc.Nonce + acc.Nonce += 1 + self.obj.StateManager().TransState().UpdateStateObject(acc) + + tx.Sign(keyPair.PrivateKey) + self.obj.TxPool().QueueTransaction(tx) + + if contractCreation { + pipelogger.Infof("Contract addr %x", tx.CreationAddress(self.World().State())) + } + + return NewJSReciept(contractCreation, tx.CreationAddress(self.World().State()), tx.Hash(), keyPair.Address()), nil +} + +func (self *JSXEth) PushTx(txStr string) (*JSReceipt, error) { + tx := chain.NewTransactionFromBytes(ethutil.Hex2Bytes(txStr)) + self.obj.TxPool().QueueTransaction(tx) + return NewJSReciept(tx.CreatesContract(), tx.CreationAddress(self.World().State()), tx.Hash(), tx.Sender()), nil +} + +func (self *JSXEth) CompileMutan(code string) string { + data, err := self.XEth.CompileMutan(code) + if err != nil { + return err.Error() + } + + return ethutil.Bytes2Hex(data) +} + +func ToJSMessages(messages ethstate.Messages) *ethutil.List { + var msgs []JSMessage + for _, m := range messages { + msgs = append(msgs, NewJSMessage(m)) + } + + return ethutil.NewList(msgs) +} diff --git a/xeth/js_types.go b/xeth/js_types.go new file mode 100644 index 000000000..058bfe0dd --- /dev/null +++ b/xeth/js_types.go @@ -0,0 +1,229 @@ +package xeth + +import ( + "fmt" + "strconv" + "strings" + + "github.com/ethereum/go-ethereum/chain" + "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/ethstate" + "github.com/ethereum/go-ethereum/ethutil" +) + +// Block interface exposed to QML +type JSBlock struct { + //Transactions string `json:"transactions"` + ref *chain.Block + Size string `json:"size"` + Number int `json:"number"` + Hash string `json:"hash"` + Transactions *ethutil.List `json:"transactions"` + Uncles *ethutil.List `json:"uncles"` + Time int64 `json:"time"` + Coinbase string `json:"coinbase"` + Name string `json:"name"` + GasLimit string `json:"gasLimit"` + GasUsed string `json:"gasUsed"` + PrevHash string `json:"prevHash"` +} + +// Creates a new QML Block from a chain block +func NewJSBlock(block *chain.Block) *JSBlock { + if block == nil { + return &JSBlock{} + } + + ptxs := make([]*JSTransaction, len(block.Transactions())) + for i, tx := range block.Transactions() { + ptxs[i] = NewJSTx(tx, block.State()) + } + txlist := ethutil.NewList(ptxs) + + puncles := make([]*JSBlock, len(block.Uncles)) + for i, uncle := range block.Uncles { + puncles[i] = NewJSBlock(uncle) + } + ulist := ethutil.NewList(puncles) + + return &JSBlock{ + ref: block, Size: block.Size().String(), + Number: int(block.Number.Uint64()), GasUsed: block.GasUsed.String(), + GasLimit: block.GasLimit.String(), Hash: ethutil.Bytes2Hex(block.Hash()), + Transactions: txlist, Uncles: ulist, + Time: block.Time, + Coinbase: ethutil.Bytes2Hex(block.Coinbase), + PrevHash: ethutil.Bytes2Hex(block.PrevHash), + } +} + +func (self *JSBlock) ToString() string { + if self.ref != nil { + return self.ref.String() + } + + return "" +} + +func (self *JSBlock) GetTransaction(hash string) *JSTransaction { + tx := self.ref.GetTransaction(ethutil.Hex2Bytes(hash)) + if tx == nil { + return nil + } + + return NewJSTx(tx, self.ref.State()) +} + +type JSTransaction struct { + ref *chain.Transaction + + Value string `json:"value"` + Gas string `json:"gas"` + GasPrice string `json:"gasPrice"` + Hash string `json:"hash"` + Address string `json:"address"` + Sender string `json:"sender"` + RawData string `json:"rawData"` + Data string `json:"data"` + Contract bool `json:"isContract"` + CreatesContract bool `json:"createsContract"` + Confirmations int `json:"confirmations"` +} + +func NewJSTx(tx *chain.Transaction, state *ethstate.State) *JSTransaction { + hash := ethutil.Bytes2Hex(tx.Hash()) + receiver := ethutil.Bytes2Hex(tx.Recipient) + if receiver == "0000000000000000000000000000000000000000" { + receiver = ethutil.Bytes2Hex(tx.CreationAddress(state)) + } + sender := ethutil.Bytes2Hex(tx.Sender()) + createsContract := tx.CreatesContract() + + var data string + if tx.CreatesContract() { + data = strings.Join(chain.Disassemble(tx.Data), "\n") + } else { + data = ethutil.Bytes2Hex(tx.Data) + } + + return &JSTransaction{ref: tx, Hash: hash, Value: ethutil.CurrencyToString(tx.Value), Address: receiver, Contract: tx.CreatesContract(), Gas: tx.Gas.String(), GasPrice: tx.GasPrice.String(), Data: data, Sender: sender, CreatesContract: createsContract, RawData: ethutil.Bytes2Hex(tx.Data)} +} + +func (self *JSTransaction) ToString() string { + return self.ref.String() +} + +type JSKey struct { + Address string `json:"address"` + PrivateKey string `json:"privateKey"` + PublicKey string `json:"publicKey"` +} + +func NewJSKey(key *crypto.KeyPair) *JSKey { + return &JSKey{ethutil.Bytes2Hex(key.Address()), ethutil.Bytes2Hex(key.PrivateKey), ethutil.Bytes2Hex(key.PublicKey)} +} + +type JSObject struct { + *Object +} + +func NewJSObject(object *Object) *JSObject { + return &JSObject{object} +} + +type PReceipt struct { + CreatedContract bool `json:"createdContract"` + Address string `json:"address"` + Hash string `json:"hash"` + Sender string `json:"sender"` +} + +func NewPReciept(contractCreation bool, creationAddress, hash, address []byte) *PReceipt { + return &PReceipt{ + contractCreation, + ethutil.Bytes2Hex(creationAddress), + ethutil.Bytes2Hex(hash), + ethutil.Bytes2Hex(address), + } +} + +// Peer interface exposed to QML + +type JSPeer struct { + ref *chain.Peer + Inbound bool `json:"isInbound"` + LastSend int64 `json:"lastSend"` + LastPong int64 `json:"lastPong"` + Ip string `json:"ip"` + Port int `json:"port"` + Version string `json:"version"` + LastResponse string `json:"lastResponse"` + Latency string `json:"latency"` + Caps string `json:"caps"` +} + +func NewJSPeer(peer chain.Peer) *JSPeer { + if peer == nil { + return nil + } + + var ip []string + for _, i := range peer.Host() { + ip = append(ip, strconv.Itoa(int(i))) + } + ipAddress := strings.Join(ip, ".") + + var caps []string + capsIt := peer.Caps().NewIterator() + for capsIt.Next() { + caps = append(caps, capsIt.Value().Str()) + } + + return &JSPeer{ref: &peer, Inbound: peer.Inbound(), LastSend: peer.LastSend().Unix(), LastPong: peer.LastPong(), Version: peer.Version(), Ip: ipAddress, Port: int(peer.Port()), Latency: peer.PingTime(), Caps: fmt.Sprintf("%v", caps)} +} + +type JSReceipt struct { + CreatedContract bool `json:"createdContract"` + Address string `json:"address"` + Hash string `json:"hash"` + Sender string `json:"sender"` +} + +func NewJSReciept(contractCreation bool, creationAddress, hash, address []byte) *JSReceipt { + return &JSReceipt{ + contractCreation, + ethutil.Bytes2Hex(creationAddress), + ethutil.Bytes2Hex(hash), + ethutil.Bytes2Hex(address), + } +} + +type JSMessage struct { + To string `json:"to"` + From string `json:"from"` + Input string `json:"input"` + Output string `json:"output"` + Path int32 `json:"path"` + Origin string `json:"origin"` + Timestamp int32 `json:"timestamp"` + Coinbase string `json:"coinbase"` + Block string `json:"block"` + Number int32 `json:"number"` + Value string `json:"value"` +} + +func NewJSMessage(message *ethstate.Message) JSMessage { + return JSMessage{ + To: ethutil.Bytes2Hex(message.To), + From: ethutil.Bytes2Hex(message.From), + Input: ethutil.Bytes2Hex(message.Input), + Output: ethutil.Bytes2Hex(message.Output), + Path: int32(message.Path), + Origin: ethutil.Bytes2Hex(message.Origin), + Timestamp: int32(message.Timestamp), + Coinbase: ethutil.Bytes2Hex(message.Origin), + Block: ethutil.Bytes2Hex(message.Block), + Number: int32(message.Number.Int64()), + Value: message.Value.String(), + } +} diff --git a/xeth/object.go b/xeth/object.go new file mode 100644 index 000000000..fe4e84a4a --- /dev/null +++ b/xeth/object.go @@ -0,0 +1,26 @@ +package xeth + +import ( + "github.com/ethereum/go-ethereum/ethstate" + "github.com/ethereum/go-ethereum/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/xeth/pipe.go b/xeth/pipe.go new file mode 100644 index 000000000..f2759d660 --- /dev/null +++ b/xeth/pipe.go @@ -0,0 +1,175 @@ +package xeth + +/* + * eXtended ETHereum + */ + +import ( + "fmt" + "strings" + + "github.com/ethereum/go-ethereum/chain" + "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/ethstate" + "github.com/ethereum/go-ethereum/ethutil" + "github.com/ethereum/go-ethereum/logger" + "github.com/ethereum/go-ethereum/vm" +) + +var pipelogger = logger.NewLogger("XETH") + +type VmVars struct { + State *ethstate.State +} + +type XEth struct { + obj chain.EthManager + stateManager *chain.StateManager + blockChain *chain.ChainManager + world *World + + Vm VmVars +} + +func New(obj chain.EthManager) *XEth { + pipe := &XEth{ + obj: obj, + stateManager: obj.StateManager(), + blockChain: obj.ChainManager(), + } + pipe.world = NewWorld(pipe) + + return pipe +} + +func (self *XEth) Balance(addr []byte) *ethutil.Value { + return ethutil.NewValue(self.World().safeGet(addr).Balance) +} + +func (self *XEth) Nonce(addr []byte) uint64 { + return self.World().safeGet(addr).Nonce +} + +func (self *XEth) Execute(addr []byte, data []byte, value, gas, price *ethutil.Value) ([]byte, error) { + return self.ExecuteObject(&Object{self.World().safeGet(addr)}, data, value, gas, price) +} + +func (self *XEth) ExecuteObject(object *Object, data []byte, value, gas, price *ethutil.Value) ([]byte, error) { + var ( + initiator = ethstate.NewStateObject(self.obj.KeyManager().KeyPair().Address()) + block = self.blockChain.CurrentBlock + ) + + self.Vm.State = self.World().State().Copy() + + evm := vm.New(NewEnv(self.Vm.State, block, value.BigInt(), initiator.Address()), vm.Type(ethutil.Config.VmType)) + + msg := vm.NewExecution(evm, 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 +} + +func (self *XEth) Block(hash []byte) *chain.Block { + return self.blockChain.GetBlock(hash) +} + +func (self *XEth) Storage(addr, storageAddr []byte) *ethutil.Value { + return self.World().safeGet(addr).GetStorage(ethutil.BigD(storageAddr)) +} + +func (self *XEth) ToAddress(priv []byte) []byte { + pair, err := crypto.NewKeyPairFromSec(priv) + if err != nil { + return nil + } + + return pair.Address() +} + +func (self *XEth) Exists(addr []byte) bool { + return self.World().Get(addr) != nil +} + +func (self *XEth) TransactString(key *crypto.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() + 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 *XEth) Transact(key *crypto.KeyPair, rec []byte, value, gas, price *ethutil.Value, data []byte) ([]byte, error) { + var hash []byte + var contractCreation bool + if rec == nil { + contractCreation = true + } + + var tx *chain.Transaction + // Compile and assemble the given data + if contractCreation { + script, err := ethutil.Compile(string(data), false) + if err != nil { + return nil, err + } + + tx = chain.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 = chain.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 { + addr := tx.CreationAddress(self.World().State()) + pipelogger.Infof("Contract addr %x\n", addr) + + return addr, nil + } + + return tx.Hash(), nil +} + +func (self *XEth) PushTx(tx *chain.Transaction) ([]byte, error) { + self.obj.TxPool().QueueTransaction(tx) + if tx.Recipient == nil { + addr := tx.CreationAddress(self.World().State()) + pipelogger.Infof("Contract addr %x\n", addr) + return addr, nil + } + return tx.Hash(), nil +} + +func (self *XEth) CompileMutan(code string) ([]byte, error) { + data, err := ethutil.Compile(code, false) + if err != nil { + return nil, err + } + + return data, nil +} diff --git a/xeth/vm_env.go b/xeth/vm_env.go new file mode 100644 index 000000000..952101f68 --- /dev/null +++ b/xeth/vm_env.go @@ -0,0 +1,40 @@ +package xeth + +import ( + "math/big" + + "github.com/ethereum/go-ethereum/chain" + "github.com/ethereum/go-ethereum/ethstate" + "github.com/ethereum/go-ethereum/vm" +) + +type VMEnv struct { + state *ethstate.State + block *chain.Block + value *big.Int + sender []byte +} + +func NewEnv(state *ethstate.State, block *chain.Block, value *big.Int, sender []byte) *VMEnv { + return &VMEnv{ + state: state, + block: block, + value: value, + sender: sender, + } +} + +func (self *VMEnv) Origin() []byte { return self.sender } +func (self *VMEnv) BlockNumber() *big.Int { return self.block.Number } +func (self *VMEnv) PrevHash() []byte { return self.block.PrevHash } +func (self *VMEnv) Coinbase() []byte { return self.block.Coinbase } +func (self *VMEnv) Time() int64 { return self.block.Time } +func (self *VMEnv) Difficulty() *big.Int { return self.block.Difficulty } +func (self *VMEnv) BlockHash() []byte { return self.block.Hash() } +func (self *VMEnv) Value() *big.Int { return self.value } +func (self *VMEnv) State() *ethstate.State { return self.state } +func (self *VMEnv) GasLimit() *big.Int { return self.block.GasLimit } +func (self *VMEnv) AddLog(ethstate.Log) {} +func (self *VMEnv) Transfer(from, to vm.Account, amount *big.Int) error { + return vm.Transfer(from, to, amount) +} diff --git a/xeth/world.go b/xeth/world.go new file mode 100644 index 000000000..daeb59e1c --- /dev/null +++ b/xeth/world.go @@ -0,0 +1,64 @@ +package xeth + +import ( + "container/list" + + "github.com/ethereum/go-ethereum/ethstate" +) + +type World struct { + pipe *XEth + cfg *Config +} + +func NewWorld(pipe *XEth) *World { + world := &World{pipe, nil} + world.cfg = &Config{pipe} + + return world +} + +func (self *XEth) World() *World { + return self.world +} + +func (self *World) State() *ethstate.State { + return self.pipe.stateManager.CurrentState() +} + +func (self *World) Get(addr []byte) *Object { + return &Object{self.State().GetStateObject(addr)} +} + +func (self *World) SafeGet(addr []byte) *Object { + return &Object{self.safeGet(addr)} +} + +func (self *World) safeGet(addr []byte) *ethstate.StateObject { + object := self.State().GetStateObject(addr) + if object == nil { + object = ethstate.NewStateObject(addr) + } + + return object +} + +func (self *World) Coinbase() *ethstate.StateObject { + return nil +} + +func (self *World) IsMining() bool { + return self.pipe.obj.IsMining() +} + +func (self *World) IsListening() bool { + return self.pipe.obj.IsListening() +} + +func (self *World) Peers() *list.List { + return self.pipe.obj.Peers() +} + +func (self *World) Config() *Config { + return self.cfg +} -- cgit v1.2.3 From af8f5f0b69f1c359991d12c7708804fe8dd1f944 Mon Sep 17 00:00:00 2001 From: obscuren Date: Fri, 31 Oct 2014 14:43:14 +0100 Subject: ethstate => state --- xeth/hexface.go | 4 ++-- xeth/js_types.go | 6 +++--- xeth/object.go | 4 ++-- xeth/pipe.go | 6 +++--- xeth/vm_env.go | 28 ++++++++++++++-------------- xeth/world.go | 10 +++++----- 6 files changed, 29 insertions(+), 29 deletions(-) (limited to 'xeth') diff --git a/xeth/hexface.go b/xeth/hexface.go index 829f530f4..5c8e7a3c7 100644 --- a/xeth/hexface.go +++ b/xeth/hexface.go @@ -7,8 +7,8 @@ import ( "github.com/ethereum/go-ethereum/chain" "github.com/ethereum/go-ethereum/crypto" - "github.com/ethereum/go-ethereum/ethstate" "github.com/ethereum/go-ethereum/ethutil" + "github.com/ethereum/go-ethereum/state" ) type JSXEth struct { @@ -254,7 +254,7 @@ func (self *JSXEth) CompileMutan(code string) string { return ethutil.Bytes2Hex(data) } -func ToJSMessages(messages ethstate.Messages) *ethutil.List { +func ToJSMessages(messages state.Messages) *ethutil.List { var msgs []JSMessage for _, m := range messages { msgs = append(msgs, NewJSMessage(m)) diff --git a/xeth/js_types.go b/xeth/js_types.go index 058bfe0dd..9f8f12e7f 100644 --- a/xeth/js_types.go +++ b/xeth/js_types.go @@ -7,8 +7,8 @@ import ( "github.com/ethereum/go-ethereum/chain" "github.com/ethereum/go-ethereum/crypto" - "github.com/ethereum/go-ethereum/ethstate" "github.com/ethereum/go-ethereum/ethutil" + "github.com/ethereum/go-ethereum/state" ) // Block interface exposed to QML @@ -90,7 +90,7 @@ type JSTransaction struct { Confirmations int `json:"confirmations"` } -func NewJSTx(tx *chain.Transaction, state *ethstate.State) *JSTransaction { +func NewJSTx(tx *chain.Transaction, state *state.State) *JSTransaction { hash := ethutil.Bytes2Hex(tx.Hash()) receiver := ethutil.Bytes2Hex(tx.Recipient) if receiver == "0000000000000000000000000000000000000000" { @@ -212,7 +212,7 @@ type JSMessage struct { Value string `json:"value"` } -func NewJSMessage(message *ethstate.Message) JSMessage { +func NewJSMessage(message *state.Message) JSMessage { return JSMessage{ To: ethutil.Bytes2Hex(message.To), From: ethutil.Bytes2Hex(message.From), diff --git a/xeth/object.go b/xeth/object.go index fe4e84a4a..a4ac41e89 100644 --- a/xeth/object.go +++ b/xeth/object.go @@ -1,12 +1,12 @@ package xeth import ( - "github.com/ethereum/go-ethereum/ethstate" "github.com/ethereum/go-ethereum/ethutil" + "github.com/ethereum/go-ethereum/state" ) type Object struct { - *ethstate.StateObject + *state.StateObject } func (self *Object) StorageString(str string) *ethutil.Value { diff --git a/xeth/pipe.go b/xeth/pipe.go index f2759d660..25a69137d 100644 --- a/xeth/pipe.go +++ b/xeth/pipe.go @@ -10,16 +10,16 @@ import ( "github.com/ethereum/go-ethereum/chain" "github.com/ethereum/go-ethereum/crypto" - "github.com/ethereum/go-ethereum/ethstate" "github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/logger" + "github.com/ethereum/go-ethereum/state" "github.com/ethereum/go-ethereum/vm" ) var pipelogger = logger.NewLogger("XETH") type VmVars struct { - State *ethstate.State + State *state.State } type XEth struct { @@ -56,7 +56,7 @@ func (self *XEth) Execute(addr []byte, data []byte, value, gas, price *ethutil.V func (self *XEth) ExecuteObject(object *Object, data []byte, value, gas, price *ethutil.Value) ([]byte, error) { var ( - initiator = ethstate.NewStateObject(self.obj.KeyManager().KeyPair().Address()) + initiator = state.NewStateObject(self.obj.KeyManager().KeyPair().Address()) block = self.blockChain.CurrentBlock ) diff --git a/xeth/vm_env.go b/xeth/vm_env.go index 952101f68..2c36444e9 100644 --- a/xeth/vm_env.go +++ b/xeth/vm_env.go @@ -4,18 +4,18 @@ import ( "math/big" "github.com/ethereum/go-ethereum/chain" - "github.com/ethereum/go-ethereum/ethstate" + "github.com/ethereum/go-ethereum/state" "github.com/ethereum/go-ethereum/vm" ) type VMEnv struct { - state *ethstate.State + state *state.State block *chain.Block value *big.Int sender []byte } -func NewEnv(state *ethstate.State, block *chain.Block, value *big.Int, sender []byte) *VMEnv { +func NewEnv(state *state.State, block *chain.Block, value *big.Int, sender []byte) *VMEnv { return &VMEnv{ state: state, block: block, @@ -24,17 +24,17 @@ func NewEnv(state *ethstate.State, block *chain.Block, value *big.Int, sender [] } } -func (self *VMEnv) Origin() []byte { return self.sender } -func (self *VMEnv) BlockNumber() *big.Int { return self.block.Number } -func (self *VMEnv) PrevHash() []byte { return self.block.PrevHash } -func (self *VMEnv) Coinbase() []byte { return self.block.Coinbase } -func (self *VMEnv) Time() int64 { return self.block.Time } -func (self *VMEnv) Difficulty() *big.Int { return self.block.Difficulty } -func (self *VMEnv) BlockHash() []byte { return self.block.Hash() } -func (self *VMEnv) Value() *big.Int { return self.value } -func (self *VMEnv) State() *ethstate.State { return self.state } -func (self *VMEnv) GasLimit() *big.Int { return self.block.GasLimit } -func (self *VMEnv) AddLog(ethstate.Log) {} +func (self *VMEnv) Origin() []byte { return self.sender } +func (self *VMEnv) BlockNumber() *big.Int { return self.block.Number } +func (self *VMEnv) PrevHash() []byte { return self.block.PrevHash } +func (self *VMEnv) Coinbase() []byte { return self.block.Coinbase } +func (self *VMEnv) Time() int64 { return self.block.Time } +func (self *VMEnv) Difficulty() *big.Int { return self.block.Difficulty } +func (self *VMEnv) BlockHash() []byte { return self.block.Hash() } +func (self *VMEnv) Value() *big.Int { return self.value } +func (self *VMEnv) State() *state.State { return self.state } +func (self *VMEnv) GasLimit() *big.Int { return self.block.GasLimit } +func (self *VMEnv) AddLog(state.Log) {} func (self *VMEnv) Transfer(from, to vm.Account, amount *big.Int) error { return vm.Transfer(from, to, amount) } diff --git a/xeth/world.go b/xeth/world.go index daeb59e1c..dda2df274 100644 --- a/xeth/world.go +++ b/xeth/world.go @@ -3,7 +3,7 @@ package xeth import ( "container/list" - "github.com/ethereum/go-ethereum/ethstate" + "github.com/ethereum/go-ethereum/state" ) type World struct { @@ -22,7 +22,7 @@ func (self *XEth) World() *World { return self.world } -func (self *World) State() *ethstate.State { +func (self *World) State() *state.State { return self.pipe.stateManager.CurrentState() } @@ -34,16 +34,16 @@ func (self *World) SafeGet(addr []byte) *Object { return &Object{self.safeGet(addr)} } -func (self *World) safeGet(addr []byte) *ethstate.StateObject { +func (self *World) safeGet(addr []byte) *state.StateObject { object := self.State().GetStateObject(addr) if object == nil { - object = ethstate.NewStateObject(addr) + object = state.NewStateObject(addr) } return object } -func (self *World) Coinbase() *ethstate.StateObject { +func (self *World) Coinbase() *state.StateObject { return nil } -- cgit v1.2.3 From 0c4adeceaeaff7a954fa7103a2200653ef217572 Mon Sep 17 00:00:00 2001 From: obscuren Date: Tue, 4 Nov 2014 01:47:02 +0100 Subject: Properly list caps --- xeth/js_types.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'xeth') diff --git a/xeth/js_types.go b/xeth/js_types.go index 9f8f12e7f..1a1938648 100644 --- a/xeth/js_types.go +++ b/xeth/js_types.go @@ -176,10 +176,12 @@ func NewJSPeer(peer chain.Peer) *JSPeer { var caps []string capsIt := peer.Caps().NewIterator() for capsIt.Next() { - caps = append(caps, capsIt.Value().Str()) + cap := capsIt.Value().Get(0).Str() + ver := capsIt.Value().Get(1).Uint() + caps = append(caps, fmt.Sprintf("%s/%d", cap, ver)) } - return &JSPeer{ref: &peer, Inbound: peer.Inbound(), LastSend: peer.LastSend().Unix(), LastPong: peer.LastPong(), Version: peer.Version(), Ip: ipAddress, Port: int(peer.Port()), Latency: peer.PingTime(), Caps: fmt.Sprintf("%v", caps)} + return &JSPeer{ref: &peer, Inbound: peer.Inbound(), LastSend: peer.LastSend().Unix(), LastPong: peer.LastPong(), Version: peer.Version(), Ip: ipAddress, Port: int(peer.Port()), Latency: peer.PingTime(), Caps: "[" + strings.Join(caps, ", ") + "]"} } type JSReceipt struct { -- cgit v1.2.3 From f59a3b67f69b26f969084e0de165435e80bd8e12 Mon Sep 17 00:00:00 2001 From: obscuren Date: Tue, 4 Nov 2014 10:57:02 +0100 Subject: StateManager => BlockManager --- xeth/hexface.go | 4 ++-- xeth/pipe.go | 8 ++++---- xeth/world.go | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) (limited to 'xeth') diff --git a/xeth/hexface.go b/xeth/hexface.go index 5c8e7a3c7..21e82e37d 100644 --- a/xeth/hexface.go +++ b/xeth/hexface.go @@ -224,10 +224,10 @@ func (self *JSXEth) Transact(key, toStr, valueStr, gasStr, gasPriceStr, codeStr tx = chain.NewTransactionMessage(hash, value, gas, gasPrice, data) } - acc := self.obj.StateManager().TransState().GetOrNewStateObject(keyPair.Address()) + acc := self.obj.BlockManager().TransState().GetOrNewStateObject(keyPair.Address()) tx.Nonce = acc.Nonce acc.Nonce += 1 - self.obj.StateManager().TransState().UpdateStateObject(acc) + self.obj.BlockManager().TransState().UpdateStateObject(acc) tx.Sign(keyPair.PrivateKey) self.obj.TxPool().QueueTransaction(tx) diff --git a/xeth/pipe.go b/xeth/pipe.go index 25a69137d..abed8ef9a 100644 --- a/xeth/pipe.go +++ b/xeth/pipe.go @@ -24,7 +24,7 @@ type VmVars struct { type XEth struct { obj chain.EthManager - stateManager *chain.StateManager + blockManager *chain.BlockManager blockChain *chain.ChainManager world *World @@ -34,7 +34,7 @@ type XEth struct { func New(obj chain.EthManager) *XEth { pipe := &XEth{ obj: obj, - stateManager: obj.StateManager(), + blockManager: obj.BlockManager(), blockChain: obj.ChainManager(), } pipe.world = NewWorld(pipe) @@ -137,10 +137,10 @@ func (self *XEth) Transact(key *crypto.KeyPair, rec []byte, value, gas, price *e tx = chain.NewTransactionMessage(hash, value.BigInt(), gas.BigInt(), price.BigInt(), data) } - acc := self.stateManager.TransState().GetOrNewStateObject(key.Address()) + acc := self.blockManager.TransState().GetOrNewStateObject(key.Address()) tx.Nonce = acc.Nonce acc.Nonce += 1 - self.stateManager.TransState().UpdateStateObject(acc) + self.blockManager.TransState().UpdateStateObject(acc) tx.Sign(key.PrivateKey) self.obj.TxPool().QueueTransaction(tx) diff --git a/xeth/world.go b/xeth/world.go index dda2df274..6fb757d67 100644 --- a/xeth/world.go +++ b/xeth/world.go @@ -23,7 +23,7 @@ func (self *XEth) World() *World { } func (self *World) State() *state.State { - return self.pipe.stateManager.CurrentState() + return self.pipe.blockManager.CurrentState() } func (self *World) Get(addr []byte) *Object { -- cgit v1.2.3 From 429dd2a100f3b9e2b612b59bcb48f79a805cd6f9 Mon Sep 17 00:00:00 2001 From: obscuren Date: Fri, 7 Nov 2014 12:18:48 +0100 Subject: Implemented new miner w/ ui interface for merged mining. Closes #177 * Miner has been rewritten * Added new miner pane * Added option for local txs * Added option to read from MergeMining contract and list them for merged mining --- xeth/config.go | 2 ++ xeth/hexface.go | 4 ++++ 2 files changed, 6 insertions(+) (limited to 'xeth') diff --git a/xeth/config.go b/xeth/config.go index 34aa9e32d..ad0660d75 100644 --- a/xeth/config.go +++ b/xeth/config.go @@ -19,6 +19,8 @@ func (self *Config) Get(name string) *Object { objectAddr := configCtrl.GetStorage(ethutil.BigD([]byte{0})) domainAddr := (&Object{self.pipe.World().safeGet(objectAddr.Bytes())}).StorageString("DnsReg").Bytes() return &Object{self.pipe.World().safeGet(domainAddr)} + case "MergeMining": + addr = []byte{4} default: addr = ethutil.RightPadBytes([]byte(name), 32) } diff --git a/xeth/hexface.go b/xeth/hexface.go index 21e82e37d..5ef3eaf1a 100644 --- a/xeth/hexface.go +++ b/xeth/hexface.go @@ -254,6 +254,10 @@ func (self *JSXEth) CompileMutan(code string) string { return ethutil.Bytes2Hex(data) } +func (self *JSXEth) FindInConfig(str string) string { + return ethutil.Bytes2Hex(self.World().Config().Get(str).Address()) +} + func ToJSMessages(messages state.Messages) *ethutil.List { var msgs []JSMessage for _, m := range messages { -- cgit v1.2.3 From cbeebcd47da846e1b8990313f1ff1ffe7d0bf00f Mon Sep 17 00:00:00 2001 From: obscuren Date: Mon, 10 Nov 2014 01:17:31 +0100 Subject: Fixed bloom, updated mining & block processing * Reverted back to process blocks in batches method * Bloom generation and lookup fix * Minor UI changed (mainly debug) --- xeth/js_types.go | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'xeth') diff --git a/xeth/js_types.go b/xeth/js_types.go index 1a1938648..ff240e21c 100644 --- a/xeth/js_types.go +++ b/xeth/js_types.go @@ -26,6 +26,8 @@ type JSBlock struct { GasLimit string `json:"gasLimit"` GasUsed string `json:"gasUsed"` PrevHash string `json:"prevHash"` + Bloom string `json:"bloom"` + Raw string `json:"raw"` } // Creates a new QML Block from a chain block @@ -54,6 +56,8 @@ func NewJSBlock(block *chain.Block) *JSBlock { Time: block.Time, Coinbase: ethutil.Bytes2Hex(block.Coinbase), PrevHash: ethutil.Bytes2Hex(block.PrevHash), + Bloom: ethutil.Bytes2Hex(block.LogsBloom), + Raw: block.String(), } } -- cgit v1.2.3 From 6c9e503eb8d41d331d6a74e69539a06590072190 Mon Sep 17 00:00:00 2001 From: obscuren Date: Tue, 11 Nov 2014 22:51:26 +0100 Subject: Removed all implicit logging. Fixed gas issues and jump errors --- xeth/vm_env.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'xeth') diff --git a/xeth/vm_env.go b/xeth/vm_env.go index 2c36444e9..68b13e5a8 100644 --- a/xeth/vm_env.go +++ b/xeth/vm_env.go @@ -34,7 +34,7 @@ func (self *VMEnv) BlockHash() []byte { return self.block.Hash() } func (self *VMEnv) Value() *big.Int { return self.value } func (self *VMEnv) State() *state.State { return self.state } func (self *VMEnv) GasLimit() *big.Int { return self.block.GasLimit } -func (self *VMEnv) AddLog(state.Log) {} +func (self *VMEnv) AddLog(*state.Log) {} func (self *VMEnv) Transfer(from, to vm.Account, amount *big.Int) error { return vm.Transfer(from, to, amount) } -- cgit v1.2.3 From a1b6a9ac29d0aa8d29a2c0535bafdb5fe4d4830b Mon Sep 17 00:00:00 2001 From: obscuren Date: Tue, 18 Nov 2014 16:58:22 +0100 Subject: Begin of moving objects to types package * Block(s) * Transaction(s) --- xeth/hexface.go | 9 +++++---- xeth/js_types.go | 9 +++++---- xeth/pipe.go | 11 ++++++----- xeth/vm_env.go | 7 +++---- 4 files changed, 19 insertions(+), 17 deletions(-) (limited to 'xeth') diff --git a/xeth/hexface.go b/xeth/hexface.go index 5ef3eaf1a..5bf9845d4 100644 --- a/xeth/hexface.go +++ b/xeth/hexface.go @@ -6,6 +6,7 @@ import ( "sync/atomic" "github.com/ethereum/go-ethereum/chain" + "github.com/ethereum/go-ethereum/chain/types" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/state" @@ -209,7 +210,7 @@ func (self *JSXEth) Transact(key, toStr, valueStr, gasStr, gasPriceStr, codeStr gas = ethutil.Big(gasStr) gasPrice = ethutil.Big(gasPriceStr) data []byte - tx *chain.Transaction + tx *types.Transaction ) if ethutil.IsHex(codeStr) { @@ -219,9 +220,9 @@ func (self *JSXEth) Transact(key, toStr, valueStr, gasStr, gasPriceStr, codeStr } if contractCreation { - tx = chain.NewContractCreationTx(value, gas, gasPrice, data) + tx = types.NewContractCreationTx(value, gas, gasPrice, data) } else { - tx = chain.NewTransactionMessage(hash, value, gas, gasPrice, data) + tx = types.NewTransactionMessage(hash, value, gas, gasPrice, data) } acc := self.obj.BlockManager().TransState().GetOrNewStateObject(keyPair.Address()) @@ -240,7 +241,7 @@ func (self *JSXEth) Transact(key, toStr, valueStr, gasStr, gasPriceStr, codeStr } func (self *JSXEth) PushTx(txStr string) (*JSReceipt, error) { - tx := chain.NewTransactionFromBytes(ethutil.Hex2Bytes(txStr)) + tx := types.NewTransactionFromBytes(ethutil.Hex2Bytes(txStr)) self.obj.TxPool().QueueTransaction(tx) return NewJSReciept(tx.CreatesContract(), tx.CreationAddress(self.World().State()), tx.Hash(), tx.Sender()), nil } diff --git a/xeth/js_types.go b/xeth/js_types.go index ff240e21c..cba674416 100644 --- a/xeth/js_types.go +++ b/xeth/js_types.go @@ -6,6 +6,7 @@ import ( "strings" "github.com/ethereum/go-ethereum/chain" + "github.com/ethereum/go-ethereum/chain/types" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/state" @@ -14,7 +15,7 @@ import ( // Block interface exposed to QML type JSBlock struct { //Transactions string `json:"transactions"` - ref *chain.Block + ref *types.Block Size string `json:"size"` Number int `json:"number"` Hash string `json:"hash"` @@ -31,7 +32,7 @@ type JSBlock struct { } // Creates a new QML Block from a chain block -func NewJSBlock(block *chain.Block) *JSBlock { +func NewJSBlock(block *types.Block) *JSBlock { if block == nil { return &JSBlock{} } @@ -79,7 +80,7 @@ func (self *JSBlock) GetTransaction(hash string) *JSTransaction { } type JSTransaction struct { - ref *chain.Transaction + ref *types.Transaction Value string `json:"value"` Gas string `json:"gas"` @@ -94,7 +95,7 @@ type JSTransaction struct { Confirmations int `json:"confirmations"` } -func NewJSTx(tx *chain.Transaction, state *state.State) *JSTransaction { +func NewJSTx(tx *types.Transaction, state *state.State) *JSTransaction { hash := ethutil.Bytes2Hex(tx.Hash()) receiver := ethutil.Bytes2Hex(tx.Recipient) if receiver == "0000000000000000000000000000000000000000" { diff --git a/xeth/pipe.go b/xeth/pipe.go index abed8ef9a..8130ab72e 100644 --- a/xeth/pipe.go +++ b/xeth/pipe.go @@ -9,6 +9,7 @@ import ( "strings" "github.com/ethereum/go-ethereum/chain" + "github.com/ethereum/go-ethereum/chain/types" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/logger" @@ -72,7 +73,7 @@ func (self *XEth) ExecuteObject(object *Object, data []byte, value, gas, price * return ret, err } -func (self *XEth) Block(hash []byte) *chain.Block { +func (self *XEth) Block(hash []byte) *types.Block { return self.blockChain.GetBlock(hash) } @@ -115,7 +116,7 @@ func (self *XEth) Transact(key *crypto.KeyPair, rec []byte, value, gas, price *e contractCreation = true } - var tx *chain.Transaction + var tx *types.Transaction // Compile and assemble the given data if contractCreation { script, err := ethutil.Compile(string(data), false) @@ -123,7 +124,7 @@ func (self *XEth) Transact(key *crypto.KeyPair, rec []byte, value, gas, price *e return nil, err } - tx = chain.NewContractCreationTx(value.BigInt(), gas.BigInt(), price.BigInt(), script) + tx = types.NewContractCreationTx(value.BigInt(), gas.BigInt(), price.BigInt(), script) } else { data := ethutil.StringToByteFunc(string(data), func(s string) (ret []byte) { slice := strings.Split(s, "\n") @@ -134,7 +135,7 @@ func (self *XEth) Transact(key *crypto.KeyPair, rec []byte, value, gas, price *e return }) - tx = chain.NewTransactionMessage(hash, value.BigInt(), gas.BigInt(), price.BigInt(), data) + tx = types.NewTransactionMessage(hash, value.BigInt(), gas.BigInt(), price.BigInt(), data) } acc := self.blockManager.TransState().GetOrNewStateObject(key.Address()) @@ -155,7 +156,7 @@ func (self *XEth) Transact(key *crypto.KeyPair, rec []byte, value, gas, price *e return tx.Hash(), nil } -func (self *XEth) PushTx(tx *chain.Transaction) ([]byte, error) { +func (self *XEth) PushTx(tx *types.Transaction) ([]byte, error) { self.obj.TxPool().QueueTransaction(tx) if tx.Recipient == nil { addr := tx.CreationAddress(self.World().State()) diff --git a/xeth/vm_env.go b/xeth/vm_env.go index 68b13e5a8..10575ad79 100644 --- a/xeth/vm_env.go +++ b/xeth/vm_env.go @@ -2,20 +2,19 @@ package xeth import ( "math/big" - - "github.com/ethereum/go-ethereum/chain" + "github.com/ethereum/go-ethereum/chain/types" "github.com/ethereum/go-ethereum/state" "github.com/ethereum/go-ethereum/vm" ) type VMEnv struct { state *state.State - block *chain.Block + block *types.Block value *big.Int sender []byte } -func NewEnv(state *state.State, block *chain.Block, value *big.Int, sender []byte) *VMEnv { +func NewEnv(state *state.State, block *types.Block, value *big.Int, sender []byte) *VMEnv { return &VMEnv{ state: state, block: block, -- cgit v1.2.3 From 6dc46d3341dc5fa25bd005f9606de258874139be Mon Sep 17 00:00:00 2001 From: obscuren Date: Mon, 1 Dec 2014 20:18:09 +0100 Subject: Changed the way transactions are being added to the transaction pool --- xeth/hexface.go | 118 ++++++++++++++++++++++++++++++++++++-------------------- xeth/pipe.go | 64 ++++++++++++++++-------------- 2 files changed, 112 insertions(+), 70 deletions(-) (limited to 'xeth') diff --git a/xeth/hexface.go b/xeth/hexface.go index 5ef3eaf1a..31685403d 100644 --- a/xeth/hexface.go +++ b/xeth/hexface.go @@ -177,19 +177,25 @@ func (self *JSXEth) FromNumber(str string) string { return ethutil.BigD(ethutil.Hex2Bytes(str)).String() } -func (self *JSXEth) Transact(key, toStr, valueStr, gasStr, gasPriceStr, codeStr string) (*JSReceipt, error) { - var hash []byte - var contractCreation bool - if len(toStr) == 0 { - contractCreation = true +func (self *JSXEth) Transact(key, toStr, valueStr, gasStr, gasPriceStr, codeStr string) (string, error) { + var ( + to []byte + value = ethutil.NewValue(valueStr) + gas = ethutil.NewValue(gasStr) + gasPrice = ethutil.NewValue(gasPriceStr) + data []byte + ) + + if ethutil.IsHex(codeStr) { + data = ethutil.Hex2Bytes(codeStr[2:]) } else { - // Check if an address is stored by this address - addr := self.World().Config().Get("NameReg").StorageString(toStr).Bytes() - if len(addr) > 0 { - hash = addr - } else { - hash = ethutil.Hex2Bytes(toStr) - } + data = ethutil.Hex2Bytes(codeStr) + } + + if ethutil.IsHex(toStr) { + to = ethutil.Hex2Bytes(toStr[2:]) + } else { + to = ethutil.Hex2Bytes(toStr) } var keyPair *crypto.KeyPair @@ -201,47 +207,77 @@ func (self *JSXEth) Transact(key, toStr, valueStr, gasStr, gasPriceStr, codeStr } if err != nil { - return nil, err + return "", err } - var ( - value = ethutil.Big(valueStr) - gas = ethutil.Big(gasStr) - gasPrice = ethutil.Big(gasPriceStr) - data []byte - tx *chain.Transaction - ) - - if ethutil.IsHex(codeStr) { - data = ethutil.Hex2Bytes(codeStr[2:]) - } else { - data = ethutil.Hex2Bytes(codeStr) + tx, err := self.XEth.Transact(keyPair, to, value, gas, gasPrice, data) + if err != nil { + return "", err } - - if contractCreation { - tx = chain.NewContractCreationTx(value, gas, gasPrice, data) - } else { - tx = chain.NewTransactionMessage(hash, value, gas, gasPrice, data) + if chain.IsContractAddr(to) { + return ethutil.Bytes2Hex(tx.CreationAddress(nil)), nil } - acc := self.obj.BlockManager().TransState().GetOrNewStateObject(keyPair.Address()) - tx.Nonce = acc.Nonce - acc.Nonce += 1 - self.obj.BlockManager().TransState().UpdateStateObject(acc) + return ethutil.Bytes2Hex(tx.Hash()), nil + + /* + var hash []byte + var contractCreation bool + if len(toStr) == 0 { + contractCreation = true + } else { + // Check if an address is stored by this address + addr := self.World().Config().Get("NameReg").StorageString(toStr).Bytes() + if len(addr) > 0 { + hash = addr + } else { + hash = ethutil.Hex2Bytes(toStr) + } + } + - tx.Sign(keyPair.PrivateKey) - self.obj.TxPool().QueueTransaction(tx) + var ( + value = ethutil.Big(valueStr) + gas = ethutil.Big(gasStr) + gasPrice = ethutil.Big(gasPriceStr) + data []byte + tx *chain.Transaction + ) - if contractCreation { - pipelogger.Infof("Contract addr %x", tx.CreationAddress(self.World().State())) - } + if ethutil.IsHex(codeStr) { + data = ethutil.Hex2Bytes(codeStr[2:]) + } else { + data = ethutil.Hex2Bytes(codeStr) + } - return NewJSReciept(contractCreation, tx.CreationAddress(self.World().State()), tx.Hash(), keyPair.Address()), nil + if contractCreation { + tx = chain.NewContractCreationTx(value, gas, gasPrice, data) + } else { + tx = chain.NewTransactionMessage(hash, value, gas, gasPrice, data) + } + + acc := self.obj.BlockManager().TransState().GetOrNewStateObject(keyPair.Address()) + tx.Nonce = acc.Nonce + acc.Nonce += 1 + self.obj.BlockManager().TransState().UpdateStateObject(acc) + + tx.Sign(keyPair.PrivateKey) + self.obj.TxPool().QueueTransaction(tx) + + if contractCreation { + pipelogger.Infof("Contract addr %x", tx.CreationAddress(self.World().State())) + } + + return NewJSReciept(contractCreation, tx.CreationAddress(self.World().State()), tx.Hash(), keyPair.Address()), nil + */ } func (self *JSXEth) PushTx(txStr string) (*JSReceipt, error) { tx := chain.NewTransactionFromBytes(ethutil.Hex2Bytes(txStr)) - self.obj.TxPool().QueueTransaction(tx) + err := self.obj.TxPool().Add(tx) + if err != nil { + return nil, err + } return NewJSReciept(tx.CreatesContract(), tx.CreationAddress(self.World().State()), tx.Hash(), tx.Sender()), nil } diff --git a/xeth/pipe.go b/xeth/pipe.go index abed8ef9a..6e2f325c5 100644 --- a/xeth/pipe.go +++ b/xeth/pipe.go @@ -6,7 +6,6 @@ package xeth import ( "fmt" - "strings" "github.com/ethereum/go-ethereum/chain" "github.com/ethereum/go-ethereum/crypto" @@ -93,7 +92,7 @@ func (self *XEth) Exists(addr []byte) bool { return self.World().Get(addr) != nil } -func (self *XEth) TransactString(key *crypto.KeyPair, rec string, value, gas, price *ethutil.Value, data []byte) ([]byte, error) { +func (self *XEth) TransactString(key *crypto.KeyPair, rec string, value, gas, price *ethutil.Value, data []byte) (*chain.Transaction, error) { // Check if an address is stored by this address var hash []byte addr := self.World().Config().Get("NameReg").StorageString(rec).Bytes() @@ -108,55 +107,62 @@ func (self *XEth) TransactString(key *crypto.KeyPair, rec string, value, gas, pr return self.Transact(key, hash, value, gas, price, data) } -func (self *XEth) Transact(key *crypto.KeyPair, rec []byte, value, gas, price *ethutil.Value, data []byte) ([]byte, error) { +func (self *XEth) Transact(key *crypto.KeyPair, to []byte, value, gas, price *ethutil.Value, data []byte) (*chain.Transaction, error) { var hash []byte var contractCreation bool - if rec == nil { + if chain.IsContractAddr(to) { contractCreation = true + } else { + // Check if an address is stored by this address + addr := self.World().Config().Get("NameReg").Storage(to).Bytes() + if len(addr) > 0 { + hash = addr + } else { + hash = to + } } var tx *chain.Transaction - // Compile and assemble the given data if contractCreation { - script, err := ethutil.Compile(string(data), false) - if err != nil { - return nil, err - } - - tx = chain.NewContractCreationTx(value.BigInt(), gas.BigInt(), price.BigInt(), script) + tx = chain.NewContractCreationTx(value.BigInt(), gas.BigInt(), price.BigInt(), data) } 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 = chain.NewTransactionMessage(hash, value.BigInt(), gas.BigInt(), price.BigInt(), data) } - acc := self.blockManager.TransState().GetOrNewStateObject(key.Address()) - tx.Nonce = acc.Nonce - acc.Nonce += 1 - self.blockManager.TransState().UpdateStateObject(acc) + state := self.blockManager.TransState() + nonce := state.GetNonce(key.Address()) + tx.Nonce = nonce tx.Sign(key.PrivateKey) - self.obj.TxPool().QueueTransaction(tx) + err := self.obj.TxPool().Add(tx) + if err != nil { + return nil, err + } + + state.SetNonce(key.Address(), nonce+1) if contractCreation { addr := tx.CreationAddress(self.World().State()) pipelogger.Infof("Contract addr %x\n", addr) - - return addr, nil } - return tx.Hash(), nil + return tx, nil + + //acc := self.blockManager.TransState().GetOrNewStateObject(key.Address()) + //self.obj.TxPool().QueueTransaction(tx) + + //acc.Nonce += 1 + //self.blockManager.TransState().UpdateStateObject(acc) + } func (self *XEth) PushTx(tx *chain.Transaction) ([]byte, error) { - self.obj.TxPool().QueueTransaction(tx) + err := self.obj.TxPool().Add(tx) + if err != nil { + return nil, err + } + + //self.obj.TxPool().QueueTransaction(tx) if tx.Recipient == nil { addr := tx.CreationAddress(self.World().State()) pipelogger.Infof("Contract addr %x\n", addr) -- cgit v1.2.3 From 82405501872385b240012070bad2f0eda643d423 Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 3 Dec 2014 14:05:19 +0100 Subject: updated to types --- xeth/hexface.go | 2 +- xeth/pipe.go | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) (limited to 'xeth') diff --git a/xeth/hexface.go b/xeth/hexface.go index 9b7aa6b9c..6360c7675 100644 --- a/xeth/hexface.go +++ b/xeth/hexface.go @@ -215,7 +215,7 @@ func (self *JSXEth) Transact(key, toStr, valueStr, gasStr, gasPriceStr, codeStr if err != nil { return "", err } - if chain.IsContractAddr(to) { + if types.IsContractAddr(to) { return ethutil.Bytes2Hex(tx.CreationAddress(nil)), nil } diff --git a/xeth/pipe.go b/xeth/pipe.go index 9cc163a81..c96c6efc0 100644 --- a/xeth/pipe.go +++ b/xeth/pipe.go @@ -93,7 +93,7 @@ func (self *XEth) Exists(addr []byte) bool { return self.World().Get(addr) != nil } -func (self *XEth) TransactString(key *crypto.KeyPair, rec string, value, gas, price *ethutil.Value, data []byte) (*chain.Transaction, error) { +func (self *XEth) TransactString(key *crypto.KeyPair, rec string, value, gas, price *ethutil.Value, data []byte) (*types.Transaction, error) { // Check if an address is stored by this address var hash []byte addr := self.World().Config().Get("NameReg").StorageString(rec).Bytes() @@ -108,10 +108,10 @@ func (self *XEth) TransactString(key *crypto.KeyPair, rec string, value, gas, pr return self.Transact(key, hash, value, gas, price, data) } -func (self *XEth) Transact(key *crypto.KeyPair, to []byte, value, gas, price *ethutil.Value, data []byte) (*chain.Transaction, error) { +func (self *XEth) Transact(key *crypto.KeyPair, to []byte, value, gas, price *ethutil.Value, data []byte) (*types.Transaction, error) { var hash []byte var contractCreation bool - if chain.IsContractAddr(to) { + if types.IsContractAddr(to) { contractCreation = true } else { // Check if an address is stored by this address @@ -125,9 +125,9 @@ func (self *XEth) Transact(key *crypto.KeyPair, to []byte, value, gas, price *et var tx *types.Transaction if contractCreation { - tx = chain.NewContractCreationTx(value.BigInt(), gas.BigInt(), price.BigInt(), data) + tx = types.NewContractCreationTx(value.BigInt(), gas.BigInt(), price.BigInt(), data) } else { - tx = chain.NewTransactionMessage(hash, value.BigInt(), gas.BigInt(), price.BigInt(), data) + tx = types.NewTransactionMessage(hash, value.BigInt(), gas.BigInt(), price.BigInt(), data) } state := self.blockManager.TransState() -- cgit v1.2.3 From 6d99c03d915789c445c2d40579419a16fde2b7c8 Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 3 Dec 2014 17:22:26 +0100 Subject: Updated environments according to the new interface set --- xeth/pipe.go | 17 +++++++++-------- xeth/vm_env.go | 30 +++++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 9 deletions(-) (limited to 'xeth') diff --git a/xeth/pipe.go b/xeth/pipe.go index c96c6efc0..2dfb91b7f 100644 --- a/xeth/pipe.go +++ b/xeth/pipe.go @@ -5,15 +5,12 @@ package xeth */ import ( - "fmt" - "github.com/ethereum/go-ethereum/chain" "github.com/ethereum/go-ethereum/chain/types" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/state" - "github.com/ethereum/go-ethereum/vm" ) var pipelogger = logger.NewLogger("XETH") @@ -62,14 +59,18 @@ func (self *XEth) ExecuteObject(object *Object, data []byte, value, gas, price * self.Vm.State = self.World().State().Copy() - evm := vm.New(NewEnv(self.Vm.State, block, value.BigInt(), initiator.Address()), vm.Type(ethutil.Config.VmType)) + vmenv := NewEnv(self.Vm.State, block, value.BigInt(), initiator.Address()) + return vmenv.Call(initiator, object.Address(), data, gas.BigInt(), price.BigInt(), value.BigInt()) + /* + evm := vm.New(, vm.Type(ethutil.Config.VmType)) - msg := vm.NewExecution(evm, object.Address(), data, gas.BigInt(), price.BigInt(), value.BigInt()) - ret, err := msg.Exec(object.Address(), initiator) + msg := vm.NewExecution(evm, object.Address(), data, gas.BigInt(), price.BigInt(), value.BigInt()) + ret, err := msg.Exec(object.Address(), initiator) - fmt.Println("returned from call", ret, err) + fmt.Println("returned from call", ret, err) - return ret, err + return ret, err + */ } func (self *XEth) Block(hash []byte) *types.Block { diff --git a/xeth/vm_env.go b/xeth/vm_env.go index 10575ad79..d11459626 100644 --- a/xeth/vm_env.go +++ b/xeth/vm_env.go @@ -2,6 +2,8 @@ package xeth import ( "math/big" + + "github.com/ethereum/go-ethereum/chain" "github.com/ethereum/go-ethereum/chain/types" "github.com/ethereum/go-ethereum/state" "github.com/ethereum/go-ethereum/vm" @@ -12,6 +14,8 @@ type VMEnv struct { block *types.Block value *big.Int sender []byte + + depth int } func NewEnv(state *state.State, block *types.Block, value *big.Int, sender []byte) *VMEnv { @@ -33,7 +37,31 @@ func (self *VMEnv) BlockHash() []byte { return self.block.Hash() } func (self *VMEnv) Value() *big.Int { return self.value } func (self *VMEnv) State() *state.State { return self.state } func (self *VMEnv) GasLimit() *big.Int { return self.block.GasLimit } -func (self *VMEnv) AddLog(*state.Log) {} +func (self *VMEnv) Depth() int { return self.depth } +func (self *VMEnv) SetDepth(i int) { self.depth = i } +func (self *VMEnv) AddLog(log *state.Log) { + self.state.AddLog(log) +} func (self *VMEnv) Transfer(from, to vm.Account, amount *big.Int) error { return vm.Transfer(from, to, amount) } + +func (self *VMEnv) vm(addr, data []byte, gas, price, value *big.Int) *chain.Execution { + evm := vm.New(self, vm.DebugVmTy) + + return chain.NewExecution(evm, addr, data, gas, price, value) +} + +func (self *VMEnv) Call(me vm.ClosureRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error) { + exe := self.vm(addr, data, gas, price, value) + return exe.Call(addr, me) +} +func (self *VMEnv) CallCode(me vm.ClosureRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error) { + exe := self.vm(me.Address(), data, gas, price, value) + return exe.Call(addr, me) +} + +func (self *VMEnv) Create(me vm.ClosureRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error, vm.ClosureRef) { + exe := self.vm(addr, data, gas, price, value) + return exe.Create(me) +} -- cgit v1.2.3 From 9008b155d3c8d2a32c4c8945f1174243d48d4e90 Mon Sep 17 00:00:00 2001 From: obscuren Date: Thu, 4 Dec 2014 10:28:02 +0100 Subject: Renamed `chain` => `core` --- xeth/hexface.go | 59 ++++---------------------------------------------------- xeth/js_types.go | 10 +++++----- xeth/pipe.go | 12 ++++++------ xeth/vm_env.go | 8 ++++---- 4 files changed, 19 insertions(+), 70 deletions(-) (limited to 'xeth') diff --git a/xeth/hexface.go b/xeth/hexface.go index 6360c7675..c1f49453d 100644 --- a/xeth/hexface.go +++ b/xeth/hexface.go @@ -5,8 +5,8 @@ import ( "encoding/json" "sync/atomic" - "github.com/ethereum/go-ethereum/chain" - "github.com/ethereum/go-ethereum/chain/types" + "github.com/ethereum/go-ethereum/core" + "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/state" @@ -16,7 +16,7 @@ type JSXEth struct { *XEth } -func NewJSXEth(eth chain.EthManager) *JSXEth { +func NewJSXEth(eth core.EthManager) *JSXEth { return &JSXEth{New(eth)} } @@ -64,7 +64,7 @@ func (self *JSXEth) PeerCount() int { func (self *JSXEth) Peers() []JSPeer { var peers []JSPeer for peer := self.obj.Peers().Front(); peer != nil; peer = peer.Next() { - p := peer.Value.(chain.Peer) + p := peer.Value.(core.Peer) // we only want connected peers if atomic.LoadInt32(p.Connected()) != 0 { peers = append(peers, *NewJSPeer(p)) @@ -220,57 +220,6 @@ func (self *JSXEth) Transact(key, toStr, valueStr, gasStr, gasPriceStr, codeStr } return ethutil.Bytes2Hex(tx.Hash()), nil - - /* - var hash []byte - var contractCreation bool - if len(toStr) == 0 { - contractCreation = true - } else { - // Check if an address is stored by this address - addr := self.World().Config().Get("NameReg").StorageString(toStr).Bytes() - if len(addr) > 0 { - hash = addr - } else { - hash = ethutil.Hex2Bytes(toStr) - } - } - - - var ( - value = ethutil.Big(valueStr) - gas = ethutil.Big(gasStr) - gasPrice = ethutil.Big(gasPriceStr) - data []byte - tx *chain.Transaction - ) - - if ethutil.IsHex(codeStr) { - data = ethutil.Hex2Bytes(codeStr[2:]) - } else { - data = ethutil.Hex2Bytes(codeStr) - } - - if contractCreation { - tx = chain.NewContractCreationTx(value, gas, gasPrice, data) - } else { - tx = chain.NewTransactionMessage(hash, value, gas, gasPrice, data) - } - - acc := self.obj.BlockManager().TransState().GetOrNewStateObject(keyPair.Address()) - tx.Nonce = acc.Nonce - acc.Nonce += 1 - self.obj.BlockManager().TransState().UpdateStateObject(acc) - - tx.Sign(keyPair.PrivateKey) - self.obj.TxPool().QueueTransaction(tx) - - if contractCreation { - pipelogger.Infof("Contract addr %x", tx.CreationAddress(self.World().State())) - } - - return NewJSReciept(contractCreation, tx.CreationAddress(self.World().State()), tx.Hash(), keyPair.Address()), nil - */ } func (self *JSXEth) PushTx(txStr string) (*JSReceipt, error) { diff --git a/xeth/js_types.go b/xeth/js_types.go index cba674416..6aba3d993 100644 --- a/xeth/js_types.go +++ b/xeth/js_types.go @@ -5,8 +5,8 @@ import ( "strconv" "strings" - "github.com/ethereum/go-ethereum/chain" - "github.com/ethereum/go-ethereum/chain/types" + "github.com/ethereum/go-ethereum/core" + "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/state" @@ -106,7 +106,7 @@ func NewJSTx(tx *types.Transaction, state *state.State) *JSTransaction { var data string if tx.CreatesContract() { - data = strings.Join(chain.Disassemble(tx.Data), "\n") + data = strings.Join(core.Disassemble(tx.Data), "\n") } else { data = ethutil.Bytes2Hex(tx.Data) } @@ -155,7 +155,7 @@ func NewPReciept(contractCreation bool, creationAddress, hash, address []byte) * // Peer interface exposed to QML type JSPeer struct { - ref *chain.Peer + ref *core.Peer Inbound bool `json:"isInbound"` LastSend int64 `json:"lastSend"` LastPong int64 `json:"lastPong"` @@ -167,7 +167,7 @@ type JSPeer struct { Caps string `json:"caps"` } -func NewJSPeer(peer chain.Peer) *JSPeer { +func NewJSPeer(peer core.Peer) *JSPeer { if peer == nil { return nil } diff --git a/xeth/pipe.go b/xeth/pipe.go index 2dfb91b7f..cabcc1cbe 100644 --- a/xeth/pipe.go +++ b/xeth/pipe.go @@ -5,8 +5,8 @@ package xeth */ import ( - "github.com/ethereum/go-ethereum/chain" - "github.com/ethereum/go-ethereum/chain/types" + "github.com/ethereum/go-ethereum/core" + "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/logger" @@ -20,15 +20,15 @@ type VmVars struct { } type XEth struct { - obj chain.EthManager - blockManager *chain.BlockManager - blockChain *chain.ChainManager + obj core.EthManager + blockManager *core.BlockManager + blockChain *core.ChainManager world *World Vm VmVars } -func New(obj chain.EthManager) *XEth { +func New(obj core.EthManager) *XEth { pipe := &XEth{ obj: obj, blockManager: obj.BlockManager(), diff --git a/xeth/vm_env.go b/xeth/vm_env.go index d11459626..831a310cc 100644 --- a/xeth/vm_env.go +++ b/xeth/vm_env.go @@ -3,8 +3,8 @@ package xeth import ( "math/big" - "github.com/ethereum/go-ethereum/chain" - "github.com/ethereum/go-ethereum/chain/types" + "github.com/ethereum/go-ethereum/core" + "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/state" "github.com/ethereum/go-ethereum/vm" ) @@ -46,10 +46,10 @@ func (self *VMEnv) Transfer(from, to vm.Account, amount *big.Int) error { return vm.Transfer(from, to, amount) } -func (self *VMEnv) vm(addr, data []byte, gas, price, value *big.Int) *chain.Execution { +func (self *VMEnv) vm(addr, data []byte, gas, price, value *big.Int) *core.Execution { evm := vm.New(self, vm.DebugVmTy) - return chain.NewExecution(evm, addr, data, gas, price, value) + return core.NewExecution(evm, addr, data, gas, price, value) } func (self *VMEnv) Call(me vm.ClosureRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error) { -- cgit v1.2.3 From 83663ed4b01480c628ce2c849e4e881ac04b5120 Mon Sep 17 00:00:00 2001 From: obscuren Date: Thu, 4 Dec 2014 10:53:49 +0100 Subject: Renames for chain, updated VM, moved methods * Renamed a couple more chain => core * Updated VM `pc` to be uint64 rather than big int * XEth interface cleanup --- xeth/pipe.go | 63 +++++++++++++++++++++++++++--------------------------------- 1 file changed, 28 insertions(+), 35 deletions(-) (limited to 'xeth') diff --git a/xeth/pipe.go b/xeth/pipe.go index cabcc1cbe..f1ecd19d5 100644 --- a/xeth/pipe.go +++ b/xeth/pipe.go @@ -39,6 +39,9 @@ func New(obj core.EthManager) *XEth { return pipe } +/* + * State / Account accessors + */ func (self *XEth) Balance(addr []byte) *ethutil.Value { return ethutil.NewValue(self.World().safeGet(addr).Balance) } @@ -47,32 +50,6 @@ func (self *XEth) Nonce(addr []byte) uint64 { return self.World().safeGet(addr).Nonce } -func (self *XEth) Execute(addr []byte, data []byte, value, gas, price *ethutil.Value) ([]byte, error) { - return self.ExecuteObject(&Object{self.World().safeGet(addr)}, data, value, gas, price) -} - -func (self *XEth) ExecuteObject(object *Object, data []byte, value, gas, price *ethutil.Value) ([]byte, error) { - var ( - initiator = state.NewStateObject(self.obj.KeyManager().KeyPair().Address()) - block = self.blockChain.CurrentBlock - ) - - self.Vm.State = self.World().State().Copy() - - vmenv := NewEnv(self.Vm.State, block, value.BigInt(), initiator.Address()) - return vmenv.Call(initiator, object.Address(), data, gas.BigInt(), price.BigInt(), value.BigInt()) - /* - evm := vm.New(, vm.Type(ethutil.Config.VmType)) - - msg := vm.NewExecution(evm, 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 - */ -} - func (self *XEth) Block(hash []byte) *types.Block { return self.blockChain.GetBlock(hash) } @@ -81,6 +58,11 @@ func (self *XEth) Storage(addr, storageAddr []byte) *ethutil.Value { return self.World().safeGet(addr).GetStorage(ethutil.BigD(storageAddr)) } +func (self *XEth) Exists(addr []byte) bool { + return self.World().Get(addr) != nil +} + +// Converts the given private key to an address func (self *XEth) ToAddress(priv []byte) []byte { pair, err := crypto.NewKeyPairFromSec(priv) if err != nil { @@ -90,10 +72,28 @@ func (self *XEth) ToAddress(priv []byte) []byte { return pair.Address() } -func (self *XEth) Exists(addr []byte) bool { - return self.World().Get(addr) != nil +/* + * Execution helpers + */ +func (self *XEth) Execute(addr []byte, data []byte, value, gas, price *ethutil.Value) ([]byte, error) { + return self.ExecuteObject(&Object{self.World().safeGet(addr)}, data, value, gas, price) +} + +func (self *XEth) ExecuteObject(object *Object, data []byte, value, gas, price *ethutil.Value) ([]byte, error) { + var ( + initiator = state.NewStateObject(self.obj.KeyManager().KeyPair().Address()) + block = self.blockChain.CurrentBlock + ) + + self.Vm.State = self.World().State().Copy() + + vmenv := NewEnv(self.Vm.State, block, value.BigInt(), initiator.Address()) + return vmenv.Call(initiator, object.Address(), data, gas.BigInt(), price.BigInt(), value.BigInt()) } +/* + * Transactional methods + */ func (self *XEth) TransactString(key *crypto.KeyPair, rec string, value, gas, price *ethutil.Value, data []byte) (*types.Transaction, error) { // Check if an address is stored by this address var hash []byte @@ -149,13 +149,6 @@ func (self *XEth) Transact(key *crypto.KeyPair, to []byte, value, gas, price *et } return tx, nil - - //acc := self.blockManager.TransState().GetOrNewStateObject(key.Address()) - //self.obj.TxPool().QueueTransaction(tx) - - //acc.Nonce += 1 - //self.blockManager.TransState().UpdateStateObject(acc) - } func (self *XEth) PushTx(tx *types.Transaction) ([]byte, error) { -- cgit v1.2.3 From f298ffdbb8ec2b14f254e880a65f22f4d7c66305 Mon Sep 17 00:00:00 2001 From: obscuren Date: Thu, 4 Dec 2014 11:40:20 +0100 Subject: Renamed State => StateDB --- xeth/js_types.go | 2 +- xeth/pipe.go | 2 +- xeth/vm_env.go | 6 +++--- xeth/world.go | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) (limited to 'xeth') diff --git a/xeth/js_types.go b/xeth/js_types.go index 6aba3d993..da26439cf 100644 --- a/xeth/js_types.go +++ b/xeth/js_types.go @@ -95,7 +95,7 @@ type JSTransaction struct { Confirmations int `json:"confirmations"` } -func NewJSTx(tx *types.Transaction, state *state.State) *JSTransaction { +func NewJSTx(tx *types.Transaction, state *state.StateDB) *JSTransaction { hash := ethutil.Bytes2Hex(tx.Hash()) receiver := ethutil.Bytes2Hex(tx.Recipient) if receiver == "0000000000000000000000000000000000000000" { diff --git a/xeth/pipe.go b/xeth/pipe.go index f1ecd19d5..6da92cd23 100644 --- a/xeth/pipe.go +++ b/xeth/pipe.go @@ -16,7 +16,7 @@ import ( var pipelogger = logger.NewLogger("XETH") type VmVars struct { - State *state.State + State *state.StateDB } type XEth struct { diff --git a/xeth/vm_env.go b/xeth/vm_env.go index 831a310cc..4a2827ff4 100644 --- a/xeth/vm_env.go +++ b/xeth/vm_env.go @@ -10,7 +10,7 @@ import ( ) type VMEnv struct { - state *state.State + state *state.StateDB block *types.Block value *big.Int sender []byte @@ -18,7 +18,7 @@ type VMEnv struct { depth int } -func NewEnv(state *state.State, block *types.Block, value *big.Int, sender []byte) *VMEnv { +func NewEnv(state *state.StateDB, block *types.Block, value *big.Int, sender []byte) *VMEnv { return &VMEnv{ state: state, block: block, @@ -35,7 +35,7 @@ func (self *VMEnv) Time() int64 { return self.block.Time } func (self *VMEnv) Difficulty() *big.Int { return self.block.Difficulty } func (self *VMEnv) BlockHash() []byte { return self.block.Hash() } func (self *VMEnv) Value() *big.Int { return self.value } -func (self *VMEnv) State() *state.State { return self.state } +func (self *VMEnv) State() *state.StateDB { return self.state } func (self *VMEnv) GasLimit() *big.Int { return self.block.GasLimit } func (self *VMEnv) Depth() int { return self.depth } func (self *VMEnv) SetDepth(i int) { self.depth = i } diff --git a/xeth/world.go b/xeth/world.go index 6fb757d67..c5c20c224 100644 --- a/xeth/world.go +++ b/xeth/world.go @@ -22,7 +22,7 @@ func (self *XEth) World() *World { return self.world } -func (self *World) State() *state.State { +func (self *World) State() *state.StateDB { return self.pipe.blockManager.CurrentState() } -- cgit v1.2.3 From 3043b233ea4df9b630638d75f3589b94653ccfa9 Mon Sep 17 00:00:00 2001 From: obscuren Date: Thu, 4 Dec 2014 12:35:23 +0100 Subject: Log is now interface --- xeth/vm_env.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'xeth') diff --git a/xeth/vm_env.go b/xeth/vm_env.go index 4a2827ff4..ce53e9a30 100644 --- a/xeth/vm_env.go +++ b/xeth/vm_env.go @@ -39,7 +39,7 @@ func (self *VMEnv) State() *state.StateDB { return self.state } func (self *VMEnv) GasLimit() *big.Int { return self.block.GasLimit } func (self *VMEnv) Depth() int { return self.depth } func (self *VMEnv) SetDepth(i int) { self.depth = i } -func (self *VMEnv) AddLog(log *state.Log) { +func (self *VMEnv) AddLog(log state.Log) { self.state.AddLog(log) } func (self *VMEnv) Transfer(from, to vm.Account, amount *big.Int) error { -- cgit v1.2.3 From 8c7e4b290fbdfe2c0da451aef03b94bec3c95e4c Mon Sep 17 00:00:00 2001 From: obscuren Date: Thu, 4 Dec 2014 16:44:14 +0100 Subject: Added pre processing of transaction on the transient state --- xeth/pipe.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'xeth') diff --git a/xeth/pipe.go b/xeth/pipe.go index 6da92cd23..2ca8134ce 100644 --- a/xeth/pipe.go +++ b/xeth/pipe.go @@ -136,11 +136,17 @@ func (self *XEth) Transact(key *crypto.KeyPair, to []byte, value, gas, price *et tx.Nonce = nonce tx.Sign(key.PrivateKey) + + // Do some pre processing for our "pre" events and hooks + block := self.blockChain.NewBlock(key.Address()) + coinbase := state.GetStateObject(key.Address()) + coinbase.SetGasPool(block.GasLimit) + self.blockManager.ApplyTransactions(coinbase, state, block, types.Transactions{tx}, true) + err := self.obj.TxPool().Add(tx) if err != nil { return nil, err } - state.SetNonce(key.Address(), nonce+1) if contractCreation { -- cgit v1.2.3 From 5553e5aaed5c3f4e303b7d6671d2c92a45aa487e Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 10 Dec 2014 19:59:12 +0100 Subject: states moved to chain --- xeth/pipe.go | 12 ++++++------ xeth/world.go | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) (limited to 'xeth') diff --git a/xeth/pipe.go b/xeth/pipe.go index 2ca8134ce..a8d8ed999 100644 --- a/xeth/pipe.go +++ b/xeth/pipe.go @@ -22,7 +22,7 @@ type VmVars struct { type XEth struct { obj core.EthManager blockManager *core.BlockManager - blockChain *core.ChainManager + chainManager *core.ChainManager world *World Vm VmVars @@ -32,7 +32,7 @@ func New(obj core.EthManager) *XEth { pipe := &XEth{ obj: obj, blockManager: obj.BlockManager(), - blockChain: obj.ChainManager(), + chainManager: obj.ChainManager(), } pipe.world = NewWorld(pipe) @@ -51,7 +51,7 @@ func (self *XEth) Nonce(addr []byte) uint64 { } func (self *XEth) Block(hash []byte) *types.Block { - return self.blockChain.GetBlock(hash) + return self.chainManager.GetBlock(hash) } func (self *XEth) Storage(addr, storageAddr []byte) *ethutil.Value { @@ -82,7 +82,7 @@ func (self *XEth) Execute(addr []byte, data []byte, value, gas, price *ethutil.V func (self *XEth) ExecuteObject(object *Object, data []byte, value, gas, price *ethutil.Value) ([]byte, error) { var ( initiator = state.NewStateObject(self.obj.KeyManager().KeyPair().Address()) - block = self.blockChain.CurrentBlock + block = self.chainManager.CurrentBlock ) self.Vm.State = self.World().State().Copy() @@ -131,14 +131,14 @@ func (self *XEth) Transact(key *crypto.KeyPair, to []byte, value, gas, price *et tx = types.NewTransactionMessage(hash, value.BigInt(), gas.BigInt(), price.BigInt(), data) } - state := self.blockManager.TransState() + state := self.chainManager.TransState() nonce := state.GetNonce(key.Address()) tx.Nonce = nonce tx.Sign(key.PrivateKey) // Do some pre processing for our "pre" events and hooks - block := self.blockChain.NewBlock(key.Address()) + block := self.chainManager.NewBlock(key.Address()) coinbase := state.GetStateObject(key.Address()) coinbase.SetGasPool(block.GasLimit) self.blockManager.ApplyTransactions(coinbase, state, block, types.Transactions{tx}, true) diff --git a/xeth/world.go b/xeth/world.go index c5c20c224..956ef1e15 100644 --- a/xeth/world.go +++ b/xeth/world.go @@ -23,7 +23,7 @@ func (self *XEth) World() *World { } func (self *World) State() *state.StateDB { - return self.pipe.blockManager.CurrentState() + return self.pipe.chainManager.State() } func (self *World) Get(addr []byte) *Object { -- cgit v1.2.3 From 2d09e67713757e2a80eb614562c97f962af36cf7 Mon Sep 17 00:00:00 2001 From: obscuren Date: Thu, 18 Dec 2014 13:17:24 +0100 Subject: Updated to new methods --- xeth/hexface.go | 2 +- xeth/pipe.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'xeth') diff --git a/xeth/hexface.go b/xeth/hexface.go index c1f49453d..75ec5f43d 100644 --- a/xeth/hexface.go +++ b/xeth/hexface.go @@ -29,7 +29,7 @@ func (self *JSXEth) BlockByHash(strHash string) *JSBlock { func (self *JSXEth) BlockByNumber(num int32) *JSBlock { if num == -1 { - return NewJSBlock(self.obj.ChainManager().CurrentBlock) + return NewJSBlock(self.obj.ChainManager().CurrentBlock()) } return NewJSBlock(self.obj.ChainManager().GetBlockByNumber(uint64(num))) diff --git a/xeth/pipe.go b/xeth/pipe.go index a8d8ed999..1e4d0ec60 100644 --- a/xeth/pipe.go +++ b/xeth/pipe.go @@ -82,7 +82,7 @@ func (self *XEth) Execute(addr []byte, data []byte, value, gas, price *ethutil.V func (self *XEth) ExecuteObject(object *Object, data []byte, value, gas, price *ethutil.Value) ([]byte, error) { var ( initiator = state.NewStateObject(self.obj.KeyManager().KeyPair().Address()) - block = self.chainManager.CurrentBlock + block = self.chainManager.CurrentBlock() ) self.Vm.State = self.World().State().Copy() -- cgit v1.2.3 From db494170dc819b1eb0d267b6e1ab36c6cfb63569 Mon Sep 17 00:00:00 2001 From: obscuren Date: Thu, 18 Dec 2014 15:18:13 +0100 Subject: Created generic message (easy for testing) --- xeth/hexface.go | 4 ++-- xeth/js_types.go | 10 +++++----- xeth/pipe.go | 8 ++++---- 3 files changed, 11 insertions(+), 11 deletions(-) (limited to 'xeth') diff --git a/xeth/hexface.go b/xeth/hexface.go index 75ec5f43d..8fb42b4db 100644 --- a/xeth/hexface.go +++ b/xeth/hexface.go @@ -216,7 +216,7 @@ func (self *JSXEth) Transact(key, toStr, valueStr, gasStr, gasPriceStr, codeStr return "", err } if types.IsContractAddr(to) { - return ethutil.Bytes2Hex(tx.CreationAddress(nil)), nil + return ethutil.Bytes2Hex(core.AddressFromMessage(tx)), nil } return ethutil.Bytes2Hex(tx.Hash()), nil @@ -229,7 +229,7 @@ func (self *JSXEth) PushTx(txStr string) (*JSReceipt, error) { return nil, err } - return NewJSReciept(tx.CreatesContract(), tx.CreationAddress(self.World().State()), tx.Hash(), tx.Sender()), nil + return NewJSReciept(tx.CreatesContract(), core.AddressFromMessage(tx), tx.Hash(), tx.From()), nil } func (self *JSXEth) CompileMutan(code string) string { diff --git a/xeth/js_types.go b/xeth/js_types.go index da26439cf..2d6ee91bc 100644 --- a/xeth/js_types.go +++ b/xeth/js_types.go @@ -97,21 +97,21 @@ type JSTransaction struct { func NewJSTx(tx *types.Transaction, state *state.StateDB) *JSTransaction { hash := ethutil.Bytes2Hex(tx.Hash()) - receiver := ethutil.Bytes2Hex(tx.Recipient) + receiver := ethutil.Bytes2Hex(tx.To()) if receiver == "0000000000000000000000000000000000000000" { - receiver = ethutil.Bytes2Hex(tx.CreationAddress(state)) + receiver = ethutil.Bytes2Hex(core.AddressFromMessage(tx)) } sender := ethutil.Bytes2Hex(tx.Sender()) createsContract := tx.CreatesContract() var data string if tx.CreatesContract() { - data = strings.Join(core.Disassemble(tx.Data), "\n") + data = strings.Join(core.Disassemble(tx.Data()), "\n") } else { - data = ethutil.Bytes2Hex(tx.Data) + data = ethutil.Bytes2Hex(tx.Data()) } - return &JSTransaction{ref: tx, Hash: hash, Value: ethutil.CurrencyToString(tx.Value), Address: receiver, Contract: tx.CreatesContract(), Gas: tx.Gas.String(), GasPrice: tx.GasPrice.String(), Data: data, Sender: sender, CreatesContract: createsContract, RawData: ethutil.Bytes2Hex(tx.Data)} + return &JSTransaction{ref: tx, Hash: hash, Value: ethutil.CurrencyToString(tx.Value()), Address: receiver, Contract: tx.CreatesContract(), Gas: tx.Gas().String(), GasPrice: tx.GasPrice().String(), Data: data, Sender: sender, CreatesContract: createsContract, RawData: ethutil.Bytes2Hex(tx.Data())} } func (self *JSTransaction) ToString() string { diff --git a/xeth/pipe.go b/xeth/pipe.go index 1e4d0ec60..06820cc86 100644 --- a/xeth/pipe.go +++ b/xeth/pipe.go @@ -134,7 +134,7 @@ func (self *XEth) Transact(key *crypto.KeyPair, to []byte, value, gas, price *et state := self.chainManager.TransState() nonce := state.GetNonce(key.Address()) - tx.Nonce = nonce + tx.SetNonce(nonce) tx.Sign(key.PrivateKey) // Do some pre processing for our "pre" events and hooks @@ -150,7 +150,7 @@ func (self *XEth) Transact(key *crypto.KeyPair, to []byte, value, gas, price *et state.SetNonce(key.Address(), nonce+1) if contractCreation { - addr := tx.CreationAddress(self.World().State()) + addr := core.AddressFromMessage(tx) pipelogger.Infof("Contract addr %x\n", addr) } @@ -163,8 +163,8 @@ func (self *XEth) PushTx(tx *types.Transaction) ([]byte, error) { return nil, err } - if tx.Recipient == nil { - addr := tx.CreationAddress(self.World().State()) + if tx.To() == nil { + addr := core.AddressFromMessage(tx) pipelogger.Infof("Contract addr %x\n", addr) return addr, nil } -- cgit v1.2.3 From 5ad473d7581b92811c3a3e035274a82fc5568f57 Mon Sep 17 00:00:00 2001 From: obscuren Date: Thu, 18 Dec 2014 15:33:22 +0100 Subject: Moved methods to messages --- xeth/hexface.go | 2 +- xeth/js_types.go | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'xeth') diff --git a/xeth/hexface.go b/xeth/hexface.go index 8fb42b4db..bfd2dddd9 100644 --- a/xeth/hexface.go +++ b/xeth/hexface.go @@ -229,7 +229,7 @@ func (self *JSXEth) PushTx(txStr string) (*JSReceipt, error) { return nil, err } - return NewJSReciept(tx.CreatesContract(), core.AddressFromMessage(tx), tx.Hash(), tx.From()), nil + return NewJSReciept(core.MessageCreatesContract(tx), core.AddressFromMessage(tx), tx.Hash(), tx.From()), nil } func (self *JSXEth) CompileMutan(code string) string { diff --git a/xeth/js_types.go b/xeth/js_types.go index 2d6ee91bc..62867d6a9 100644 --- a/xeth/js_types.go +++ b/xeth/js_types.go @@ -102,16 +102,16 @@ func NewJSTx(tx *types.Transaction, state *state.StateDB) *JSTransaction { receiver = ethutil.Bytes2Hex(core.AddressFromMessage(tx)) } sender := ethutil.Bytes2Hex(tx.Sender()) - createsContract := tx.CreatesContract() + createsContract := core.MessageCreatesContract(tx) var data string - if tx.CreatesContract() { + if createsContract { data = strings.Join(core.Disassemble(tx.Data()), "\n") } else { data = ethutil.Bytes2Hex(tx.Data()) } - return &JSTransaction{ref: tx, Hash: hash, Value: ethutil.CurrencyToString(tx.Value()), Address: receiver, Contract: tx.CreatesContract(), Gas: tx.Gas().String(), GasPrice: tx.GasPrice().String(), Data: data, Sender: sender, CreatesContract: createsContract, RawData: ethutil.Bytes2Hex(tx.Data())} + return &JSTransaction{ref: tx, Hash: hash, Value: ethutil.CurrencyToString(tx.Value()), Address: receiver, Contract: createsContract, Gas: tx.Gas().String(), GasPrice: tx.GasPrice().String(), Data: data, Sender: sender, CreatesContract: createsContract, RawData: ethutil.Bytes2Hex(tx.Data())} } func (self *JSTransaction) ToString() string { -- cgit v1.2.3 From 59ef6e36931c980ba15babfb3680514635faebf6 Mon Sep 17 00:00:00 2001 From: obscuren Date: Fri, 19 Dec 2014 00:18:52 +0100 Subject: Cleaned up objects --- xeth/vm_env.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'xeth') diff --git a/xeth/vm_env.go b/xeth/vm_env.go index ce53e9a30..7fb674a94 100644 --- a/xeth/vm_env.go +++ b/xeth/vm_env.go @@ -47,9 +47,7 @@ func (self *VMEnv) Transfer(from, to vm.Account, amount *big.Int) error { } func (self *VMEnv) vm(addr, data []byte, gas, price, value *big.Int) *core.Execution { - evm := vm.New(self, vm.DebugVmTy) - - return core.NewExecution(evm, addr, data, gas, price, value) + return core.NewExecution(self, addr, data, gas, price, value) } func (self *VMEnv) Call(me vm.ClosureRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error) { -- cgit v1.2.3