From 872b2497114209119becf2e8a4d4a5818e2084ee Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 28 Jan 2015 18:35:49 +0100 Subject: further cleaned up xeth interface --- xeth/js_types.go | 235 --------------------------------------------------- xeth/object.go | 26 ------ xeth/types.go | 251 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ xeth/world.go | 4 +- xeth/xeth.go | 68 +++++++-------- 5 files changed, 287 insertions(+), 297 deletions(-) delete mode 100644 xeth/js_types.go delete mode 100644 xeth/object.go create mode 100644 xeth/types.go (limited to 'xeth') diff --git a/xeth/js_types.go b/xeth/js_types.go deleted file mode 100644 index a0be996ff..000000000 --- a/xeth/js_types.go +++ /dev/null @@ -1,235 +0,0 @@ -package xeth - -import ( - "fmt" - "strings" - - "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/p2p" - "github.com/ethereum/go-ethereum/state" -) - -func toHex(b []byte) string { - return "0x" + ethutil.Bytes2Hex(b) -} -func fromHex(s string) []byte { - if len(s) > 1 { - if s[0:2] == "0x" { - s = s[2:] - } - return ethutil.Hex2Bytes(s) - } - return nil -} - -// Block interface exposed to QML -type JSBlock struct { - //Transactions string `json:"transactions"` - ref *types.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"` - Bloom string `json:"bloom"` - Raw string `json:"raw"` -} - -// Creates a new QML Block from a chain block -func NewJSBlock(block *types.Block) *JSBlock { - if block == nil { - return &JSBlock{} - } - - ptxs := make([]*JSTransaction, len(block.Transactions())) - for i, tx := range block.Transactions() { - ptxs[i] = NewJSTx(tx) - } - txlist := ethutil.NewList(ptxs) - - puncles := make([]*JSBlock, len(block.Uncles())) - for i, uncle := range block.Uncles() { - puncles[i] = NewJSBlock(types.NewBlockWithHeader(uncle)) - } - ulist := ethutil.NewList(puncles) - - return &JSBlock{ - ref: block, Size: block.Size().String(), - Number: int(block.NumberU64()), GasUsed: block.GasUsed().String(), - GasLimit: block.GasLimit().String(), Hash: toHex(block.Hash()), - Transactions: txlist, Uncles: ulist, - Time: block.Time(), - Coinbase: toHex(block.Coinbase()), - PrevHash: toHex(block.ParentHash()), - Bloom: toHex(block.Bloom()), - Raw: block.String(), - } -} - -func (self *JSBlock) ToString() string { - if self.ref != nil { - return self.ref.String() - } - - return "" -} - -func (self *JSBlock) GetTransaction(hash string) *JSTransaction { - tx := self.ref.Transaction(fromHex(hash)) - if tx == nil { - return nil - } - - return NewJSTx(tx) -} - -type JSTransaction struct { - ref *types.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 *types.Transaction) *JSTransaction { - hash := toHex(tx.Hash()) - receiver := toHex(tx.To()) - if receiver == "0000000000000000000000000000000000000000" { - receiver = toHex(core.AddressFromMessage(tx)) - } - sender := toHex(tx.From()) - createsContract := core.MessageCreatesContract(tx) - - var data string - if createsContract { - data = strings.Join(core.Disassemble(tx.Data()), "\n") - } else { - data = toHex(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: toHex(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{toHex(key.Address()), toHex(key.PrivateKey), toHex(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, - toHex(creationAddress), - toHex(hash), - toHex(address), - } -} - -// Peer interface exposed to QML - -type JSPeer struct { - ref *p2p.Peer - Ip string `json:"ip"` - Version string `json:"version"` - Caps string `json:"caps"` -} - -func NewJSPeer(peer *p2p.Peer) *JSPeer { - var caps []string - for _, cap := range peer.Caps() { - caps = append(caps, fmt.Sprintf("%s/%d", cap.Name, cap.Version)) - } - - return &JSPeer{ - ref: peer, - Ip: fmt.Sprintf("%v", peer.RemoteAddr()), - Version: fmt.Sprintf("%v", peer.Identity()), - 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, - toHex(creationAddress), - toHex(hash), - toHex(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 *state.Message) JSMessage { - return JSMessage{ - To: toHex(message.To), - From: toHex(message.From), - Input: toHex(message.Input), - Output: toHex(message.Output), - Path: int32(message.Path), - Origin: toHex(message.Origin), - Timestamp: int32(message.Timestamp), - Coinbase: toHex(message.Origin), - Block: toHex(message.Block), - Number: int32(message.Number.Int64()), - Value: message.Value.String(), - } -} diff --git a/xeth/object.go b/xeth/object.go deleted file mode 100644 index a4ac41e89..000000000 --- a/xeth/object.go +++ /dev/null @@ -1,26 +0,0 @@ -package xeth - -import ( - "github.com/ethereum/go-ethereum/ethutil" - "github.com/ethereum/go-ethereum/state" -) - -type Object struct { - *state.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/types.go b/xeth/types.go new file mode 100644 index 000000000..4d8543eb1 --- /dev/null +++ b/xeth/types.go @@ -0,0 +1,251 @@ +package xeth + +import ( + "fmt" + "strings" + + "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/p2p" + "github.com/ethereum/go-ethereum/state" +) + +func toHex(b []byte) string { + return "0x" + ethutil.Bytes2Hex(b) +} +func fromHex(s string) []byte { + if len(s) > 1 { + if s[0:2] == "0x" { + s = s[2:] + } + return ethutil.Hex2Bytes(s) + } + return nil +} + +type Object struct { + *state.StateObject +} + +func NewObject(state *state.StateObject) *Object { + return &Object{state} +} + +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)) +} + +// Block interface exposed to QML +type Block struct { + //Transactions string `json:"transactions"` + ref *types.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"` + Bloom string `json:"bloom"` + Raw string `json:"raw"` +} + +// Creates a new QML Block from a chain block +func NewBlock(block *types.Block) *Block { + if block == nil { + return &Block{} + } + + ptxs := make([]*Transaction, len(block.Transactions())) + for i, tx := range block.Transactions() { + ptxs[i] = NewTx(tx) + } + txlist := ethutil.NewList(ptxs) + + puncles := make([]*Block, len(block.Uncles())) + for i, uncle := range block.Uncles() { + puncles[i] = NewBlock(types.NewBlockWithHeader(uncle)) + } + ulist := ethutil.NewList(puncles) + + return &Block{ + ref: block, Size: block.Size().String(), + Number: int(block.NumberU64()), GasUsed: block.GasUsed().String(), + GasLimit: block.GasLimit().String(), Hash: toHex(block.Hash()), + Transactions: txlist, Uncles: ulist, + Time: block.Time(), + Coinbase: toHex(block.Coinbase()), + PrevHash: toHex(block.ParentHash()), + Bloom: toHex(block.Bloom()), + Raw: block.String(), + } +} + +func (self *Block) ToString() string { + if self.ref != nil { + return self.ref.String() + } + + return "" +} + +func (self *Block) GetTransaction(hash string) *Transaction { + tx := self.ref.Transaction(fromHex(hash)) + if tx == nil { + return nil + } + + return NewTx(tx) +} + +type Transaction struct { + ref *types.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 NewTx(tx *types.Transaction) *Transaction { + hash := toHex(tx.Hash()) + receiver := toHex(tx.To()) + if receiver == "0000000000000000000000000000000000000000" { + receiver = toHex(core.AddressFromMessage(tx)) + } + sender := toHex(tx.From()) + createsContract := core.MessageCreatesContract(tx) + + var data string + if createsContract { + data = strings.Join(core.Disassemble(tx.Data()), "\n") + } else { + data = toHex(tx.Data()) + } + + return &Transaction{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: toHex(tx.Data())} +} + +func (self *Transaction) ToString() string { + return self.ref.String() +} + +type Key struct { + Address string `json:"address"` + PrivateKey string `json:"privateKey"` + PublicKey string `json:"publicKey"` +} + +func NewKey(key *crypto.KeyPair) *Key { + return &Key{toHex(key.Address()), toHex(key.PrivateKey), toHex(key.PublicKey)} +} + +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, + toHex(creationAddress), + toHex(hash), + toHex(address), + } +} + +// Peer interface exposed to QML + +type Peer struct { + ref *p2p.Peer + Ip string `json:"ip"` + Version string `json:"version"` + Caps string `json:"caps"` +} + +func NewPeer(peer *p2p.Peer) *Peer { + var caps []string + for _, cap := range peer.Caps() { + caps = append(caps, fmt.Sprintf("%s/%d", cap.Name, cap.Version)) + } + + return &Peer{ + ref: peer, + Ip: fmt.Sprintf("%v", peer.RemoteAddr()), + Version: fmt.Sprintf("%v", peer.Identity()), + Caps: fmt.Sprintf("%v", caps), + } +} + +type Receipt struct { + CreatedContract bool `json:"createdContract"` + Address string `json:"address"` + Hash string `json:"hash"` + Sender string `json:"sender"` +} + +func NewReciept(contractCreation bool, creationAddress, hash, address []byte) *Receipt { + return &Receipt{ + contractCreation, + toHex(creationAddress), + toHex(hash), + toHex(address), + } +} + +type Message 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 NewMessage(message *state.Message) Message { + return Message{ + To: toHex(message.To), + From: toHex(message.From), + Input: toHex(message.Input), + Output: toHex(message.Output), + Path: int32(message.Path), + Origin: toHex(message.Origin), + Timestamp: int32(message.Timestamp), + Coinbase: toHex(message.Origin), + Block: toHex(message.Block), + Number: int32(message.Number.Int64()), + Value: message.Value.String(), + } +} diff --git a/xeth/world.go b/xeth/world.go index 60d4bbb57..cdceec50d 100644 --- a/xeth/world.go +++ b/xeth/world.go @@ -3,10 +3,10 @@ package xeth import "github.com/ethereum/go-ethereum/state" type State struct { - xeth *JSXEth + xeth *XEth } -func NewState(xeth *JSXEth) *State { +func NewState(xeth *XEth) *State { return &State{xeth} } diff --git a/xeth/xeth.go b/xeth/xeth.go index 6b491b0fe..96e545b35 100644 --- a/xeth/xeth.go +++ b/xeth/xeth.go @@ -31,15 +31,15 @@ type Backend interface { TxPool() *core.TxPool } -type JSXEth struct { +type XEth struct { eth Backend blockProcessor *core.BlockProcessor chainManager *core.ChainManager world *State } -func NewJSXEth(eth Backend) *JSXEth { - xeth := &JSXEth{ +func New(eth Backend) *XEth { + xeth := &XEth{ eth: eth, blockProcessor: eth.BlockProcessor(), chainManager: eth.ChainManager(), @@ -49,24 +49,24 @@ func NewJSXEth(eth Backend) *JSXEth { return xeth } -func (self *JSXEth) State() *State { return self.world } +func (self *XEth) State() *State { return self.world } -func (self *JSXEth) BlockByHash(strHash string) *JSBlock { +func (self *XEth) BlockByHash(strHash string) *Block { hash := fromHex(strHash) block := self.chainManager.GetBlock(hash) - return NewJSBlock(block) + return NewBlock(block) } -func (self *JSXEth) BlockByNumber(num int32) *JSBlock { +func (self *XEth) BlockByNumber(num int32) *Block { if num == -1 { - return NewJSBlock(self.chainManager.CurrentBlock()) + return NewBlock(self.chainManager.CurrentBlock()) } - return NewJSBlock(self.chainManager.GetBlockByNumber(uint64(num))) + return NewBlock(self.chainManager.GetBlockByNumber(uint64(num))) } -func (self *JSXEth) Block(v interface{}) *JSBlock { +func (self *XEth) Block(v interface{}) *Block { if n, ok := v.(int32); ok { return self.BlockByNumber(n) } else if str, ok := v.(string); ok { @@ -78,63 +78,63 @@ func (self *JSXEth) Block(v interface{}) *JSBlock { return nil } -func (self *JSXEth) Accounts() []string { +func (self *XEth) Accounts() []string { return []string{toHex(self.eth.KeyManager().Address())} } /* -func (self *JSXEth) StateObject(addr string) *JSObject { +func (self *XEth) StateObject(addr string) *Object { object := &Object{self.State().safeGet(fromHex(addr))} - return NewJSObject(object) + return NewObject(object) } */ -func (self *JSXEth) PeerCount() int { +func (self *XEth) PeerCount() int { return self.eth.PeerCount() } -func (self *JSXEth) IsMining() bool { +func (self *XEth) IsMining() bool { return self.eth.IsMining() } -func (self *JSXEth) IsListening() bool { +func (self *XEth) IsListening() bool { return self.eth.IsListening() } -func (self *JSXEth) Coinbase() string { +func (self *XEth) Coinbase() string { return toHex(self.eth.KeyManager().Address()) } -func (self *JSXEth) NumberToHuman(balance string) string { +func (self *XEth) NumberToHuman(balance string) string { b := ethutil.Big(balance) return ethutil.CurrencyToString(b) } -func (self *JSXEth) StorageAt(addr, storageAddr string) string { +func (self *XEth) StorageAt(addr, storageAddr string) string { storage := self.State().SafeGet(addr).StorageString(storageAddr) return toHex(storage.Bytes()) } -func (self *JSXEth) BalanceAt(addr string) string { +func (self *XEth) BalanceAt(addr string) string { return self.State().SafeGet(addr).Balance().String() } -func (self *JSXEth) TxCountAt(address string) int { +func (self *XEth) TxCountAt(address string) int { return int(self.State().SafeGet(address).Nonce) } -func (self *JSXEth) CodeAt(address string) string { +func (self *XEth) CodeAt(address string) string { return toHex(self.State().SafeGet(address).Code) } -func (self *JSXEth) IsContract(address string) bool { +func (self *XEth) IsContract(address string) bool { return len(self.State().SafeGet(address).Code) > 0 } -func (self *JSXEth) SecretToAddress(key string) string { +func (self *XEth) SecretToAddress(key string) string { pair, err := crypto.NewKeyPairFromSec(fromHex(key)) if err != nil { return "" @@ -143,7 +143,7 @@ func (self *JSXEth) SecretToAddress(key string) string { return toHex(pair.Address()) } -func (self *JSXEth) Execute(addr, value, gas, price, data string) (string, error) { +func (self *XEth) Execute(addr, value, gas, price, data string) (string, error) { return "", nil } @@ -152,7 +152,7 @@ type KeyVal struct { Value string `json:"value"` } -func (self *JSXEth) EachStorage(addr string) string { +func (self *XEth) EachStorage(addr string) string { var values []KeyVal object := self.State().SafeGet(addr) it := object.Trie().Iterator() @@ -168,13 +168,13 @@ func (self *JSXEth) EachStorage(addr string) string { return string(valuesJson) } -func (self *JSXEth) ToAscii(str string) string { +func (self *XEth) ToAscii(str string) string { padded := ethutil.RightPadBytes([]byte(str), 32) return "0x" + toHex(padded) } -func (self *JSXEth) FromAscii(str string) string { +func (self *XEth) FromAscii(str string) string { if ethutil.IsHex(str) { str = str[2:] } @@ -182,7 +182,7 @@ func (self *JSXEth) FromAscii(str string) string { return string(bytes.Trim(fromHex(str), "\x00")) } -func (self *JSXEth) FromNumber(str string) string { +func (self *XEth) FromNumber(str string) string { if ethutil.IsHex(str) { str = str[2:] } @@ -190,20 +190,20 @@ func (self *JSXEth) FromNumber(str string) string { return ethutil.BigD(fromHex(str)).String() } -func (self *JSXEth) Transact(key, toStr, valueStr, gasStr, gasPriceStr, codeStr string) (string, error) { +func (self *XEth) Transact(key, toStr, valueStr, gasStr, gasPriceStr, codeStr string) (string, error) { return "", nil } -func ToJSMessages(messages state.Messages) *ethutil.List { - var msgs []JSMessage +func ToMessages(messages state.Messages) *ethutil.List { + var msgs []Message for _, m := range messages { - msgs = append(msgs, NewJSMessage(m)) + msgs = append(msgs, NewMessage(m)) } return ethutil.NewList(msgs) } -func (self *JSXEth) PushTx(encodedTx string) (string, error) { +func (self *XEth) PushTx(encodedTx string) (string, error) { tx := types.NewTransactionFromBytes(fromHex(encodedTx)) err := self.eth.TxPool().Add(tx) if err != nil { -- cgit v1.2.3