diff options
author | obscuren <geffobscura@gmail.com> | 2015-01-29 01:36:41 +0800 |
---|---|---|
committer | obscuren <geffobscura@gmail.com> | 2015-01-29 01:36:41 +0800 |
commit | 1c51e12c181fb781cafaf34fc8dd2cb07fd85df7 (patch) | |
tree | 38465edc586ae8080018f37efa8657c7dfeab6af /xeth | |
parent | 45e15f62f594d6f5191e60a54aead33602838e98 (diff) | |
parent | 872b2497114209119becf2e8a4d4a5818e2084ee (diff) | |
download | go-tangerine-1c51e12c181fb781cafaf34fc8dd2cb07fd85df7.tar.gz go-tangerine-1c51e12c181fb781cafaf34fc8dd2cb07fd85df7.tar.zst go-tangerine-1c51e12c181fb781cafaf34fc8dd2cb07fd85df7.zip |
Merge branch 'jsonrpc' into qt5.4
Diffstat (limited to 'xeth')
-rw-r--r-- | xeth/config.go | 37 | ||||
-rw-r--r-- | xeth/hexface.go | 211 | ||||
-rw-r--r-- | xeth/object.go | 26 | ||||
-rw-r--r-- | xeth/types.go (renamed from xeth/js_types.go) | 86 | ||||
-rw-r--r-- | xeth/world.go | 4 | ||||
-rw-r--r-- | xeth/xeth.go | 211 |
6 files changed, 263 insertions, 312 deletions
diff --git a/xeth/config.go b/xeth/config.go deleted file mode 100644 index db6cce3bd..000000000 --- a/xeth/config.go +++ /dev/null @@ -1,37 +0,0 @@ -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)} - case "MergeMining": - addr = []byte{4} - 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 deleted file mode 100644 index 3eb535724..000000000 --- a/xeth/hexface.go +++ /dev/null @@ -1,211 +0,0 @@ -package xeth - -import ( - "bytes" - "encoding/json" - - "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" -) - -// to resolve the import cycle -type Backend interface { - BlockProcessor() *core.BlockProcessor - ChainManager() *core.ChainManager - Coinbase() []byte - KeyManager() *crypto.KeyManager - IsMining() bool - IsListening() bool - PeerCount() int - Db() ethutil.Database - TxPool() *core.TxPool -} - -type JSXEth struct { - eth Backend - blockProcessor *core.BlockProcessor - chainManager *core.ChainManager - world *State -} - -func NewJSXEth(eth Backend) *JSXEth { - xeth := &JSXEth{ - eth: eth, - blockProcessor: eth.BlockProcessor(), - chainManager: eth.ChainManager(), - } - xeth.world = NewState(xeth) - - return xeth -} - -func (self *JSXEth) State() *State { return self.world } - -func (self *JSXEth) BlockByHash(strHash string) *JSBlock { - hash := fromHex(strHash) - block := self.chainManager.GetBlock(hash) - - return NewJSBlock(block) -} - -func (self *JSXEth) BlockByNumber(num int32) *JSBlock { - if num == -1 { - return NewJSBlock(self.chainManager.CurrentBlock()) - } - - return NewJSBlock(self.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) Accounts() []string { - return []string{toHex(self.eth.KeyManager().Address())} -} - -/* -func (self *JSXEth) StateObject(addr string) *JSObject { - object := &Object{self.State().safeGet(fromHex(addr))} - - return NewJSObject(object) -} -*/ - -func (self *JSXEth) PeerCount() int { - return self.eth.PeerCount() -} - -func (self *JSXEth) IsMining() bool { - return self.eth.IsMining() -} - -func (self *JSXEth) IsListening() bool { - return self.eth.IsListening() -} - -func (self *JSXEth) Coinbase() string { - return toHex(self.eth.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.State().SafeGet(addr).StorageString(storageAddr) - - return toHex(storage.Bytes()) -} - -func (self *JSXEth) BalanceAt(addr string) string { - return self.State().SafeGet(addr).Balance().String() -} - -func (self *JSXEth) TxCountAt(address string) int { - return int(self.State().SafeGet(address).Nonce) -} - -func (self *JSXEth) CodeAt(address string) string { - return toHex(self.State().SafeGet(address).Code) -} - -func (self *JSXEth) IsContract(address string) bool { - return len(self.State().SafeGet(address).Code) > 0 -} - -func (self *JSXEth) SecretToAddress(key string) string { - pair, err := crypto.NewKeyPairFromSec(fromHex(key)) - if err != nil { - return "" - } - - return toHex(pair.Address()) -} - -func (self *JSXEth) Execute(addr, value, gas, price, data string) (string, error) { - return "", nil -} - -type KeyVal struct { - Key string `json:"key"` - Value string `json:"value"` -} - -func (self *JSXEth) EachStorage(addr string) string { - var values []KeyVal - object := self.State().SafeGet(addr) - it := object.Trie().Iterator() - for it.Next() { - values = append(values, KeyVal{toHex(it.Key), toHex(it.Value)}) - } - - 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" + toHex(padded) -} - -func (self *JSXEth) FromAscii(str string) string { - if ethutil.IsHex(str) { - str = str[2:] - } - - return string(bytes.Trim(fromHex(str), "\x00")) -} - -func (self *JSXEth) FromNumber(str string) string { - if ethutil.IsHex(str) { - str = str[2:] - } - - return ethutil.BigD(fromHex(str)).String() -} - -func (self *JSXEth) Transact(key, toStr, valueStr, gasStr, gasPriceStr, codeStr string) (string, error) { - return "", nil -} - -func ToJSMessages(messages state.Messages) *ethutil.List { - var msgs []JSMessage - for _, m := range messages { - msgs = append(msgs, NewJSMessage(m)) - } - - return ethutil.NewList(msgs) -} - -func (self *JSXEth) PushTx(encodedTx string) (string, error) { - tx := types.NewTransactionFromBytes(fromHex(encodedTx)) - err := self.eth.TxPool().Add(tx) - if err != nil { - return "", err - } - - if tx.To() == nil { - addr := core.AddressFromMessage(tx) - return toHex(addr), nil - } - return toHex(tx.Hash()), nil -} 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/js_types.go b/xeth/types.go index a0be996ff..4d8543eb1 100644 --- a/xeth/js_types.go +++ b/xeth/types.go @@ -25,8 +25,32 @@ func fromHex(s string) []byte { 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 JSBlock struct { +type Block struct { //Transactions string `json:"transactions"` ref *types.Block Size string `json:"size"` @@ -45,24 +69,24 @@ type JSBlock struct { } // Creates a new QML Block from a chain block -func NewJSBlock(block *types.Block) *JSBlock { +func NewBlock(block *types.Block) *Block { if block == nil { - return &JSBlock{} + return &Block{} } - ptxs := make([]*JSTransaction, len(block.Transactions())) + ptxs := make([]*Transaction, len(block.Transactions())) for i, tx := range block.Transactions() { - ptxs[i] = NewJSTx(tx) + ptxs[i] = NewTx(tx) } txlist := ethutil.NewList(ptxs) - puncles := make([]*JSBlock, len(block.Uncles())) + puncles := make([]*Block, len(block.Uncles())) for i, uncle := range block.Uncles() { - puncles[i] = NewJSBlock(types.NewBlockWithHeader(uncle)) + puncles[i] = NewBlock(types.NewBlockWithHeader(uncle)) } ulist := ethutil.NewList(puncles) - return &JSBlock{ + return &Block{ ref: block, Size: block.Size().String(), Number: int(block.NumberU64()), GasUsed: block.GasUsed().String(), GasLimit: block.GasLimit().String(), Hash: toHex(block.Hash()), @@ -75,7 +99,7 @@ func NewJSBlock(block *types.Block) *JSBlock { } } -func (self *JSBlock) ToString() string { +func (self *Block) ToString() string { if self.ref != nil { return self.ref.String() } @@ -83,16 +107,16 @@ func (self *JSBlock) ToString() string { return "" } -func (self *JSBlock) GetTransaction(hash string) *JSTransaction { +func (self *Block) GetTransaction(hash string) *Transaction { tx := self.ref.Transaction(fromHex(hash)) if tx == nil { return nil } - return NewJSTx(tx) + return NewTx(tx) } -type JSTransaction struct { +type Transaction struct { ref *types.Transaction Value string `json:"value"` @@ -108,7 +132,7 @@ type JSTransaction struct { Confirmations int `json:"confirmations"` } -func NewJSTx(tx *types.Transaction) *JSTransaction { +func NewTx(tx *types.Transaction) *Transaction { hash := toHex(tx.Hash()) receiver := toHex(tx.To()) if receiver == "0000000000000000000000000000000000000000" { @@ -124,29 +148,21 @@ func NewJSTx(tx *types.Transaction) *JSTransaction { 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())} + 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 *JSTransaction) ToString() string { +func (self *Transaction) ToString() string { return self.ref.String() } -type JSKey struct { +type Key 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} +func NewKey(key *crypto.KeyPair) *Key { + return &Key{toHex(key.Address()), toHex(key.PrivateKey), toHex(key.PublicKey)} } type PReceipt struct { @@ -167,20 +183,20 @@ func NewPReciept(contractCreation bool, creationAddress, hash, address []byte) * // Peer interface exposed to QML -type JSPeer struct { +type Peer struct { ref *p2p.Peer Ip string `json:"ip"` Version string `json:"version"` Caps string `json:"caps"` } -func NewJSPeer(peer *p2p.Peer) *JSPeer { +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 &JSPeer{ + return &Peer{ ref: peer, Ip: fmt.Sprintf("%v", peer.RemoteAddr()), Version: fmt.Sprintf("%v", peer.Identity()), @@ -188,15 +204,15 @@ func NewJSPeer(peer *p2p.Peer) *JSPeer { } } -type JSReceipt struct { +type Receipt 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{ +func NewReciept(contractCreation bool, creationAddress, hash, address []byte) *Receipt { + return &Receipt{ contractCreation, toHex(creationAddress), toHex(hash), @@ -204,7 +220,7 @@ func NewJSReciept(contractCreation bool, creationAddress, hash, address []byte) } } -type JSMessage struct { +type Message struct { To string `json:"to"` From string `json:"from"` Input string `json:"input"` @@ -218,8 +234,8 @@ type JSMessage struct { Value string `json:"value"` } -func NewJSMessage(message *state.Message) JSMessage { - return JSMessage{ +func NewMessage(message *state.Message) Message { + return Message{ To: toHex(message.To), From: toHex(message.From), Input: toHex(message.Input), 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 92b9bc995..96e545b35 100644 --- a/xeth/xeth.go +++ b/xeth/xeth.go @@ -4,6 +4,215 @@ package xeth * eXtended ETHereum */ -import "github.com/ethereum/go-ethereum/logger" +import ( + "bytes" + "encoding/json" + + "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" + "github.com/ethereum/go-ethereum/state" +) var pipelogger = logger.NewLogger("XETH") + +// to resolve the import cycle +type Backend interface { + BlockProcessor() *core.BlockProcessor + ChainManager() *core.ChainManager + Coinbase() []byte + KeyManager() *crypto.KeyManager + IsMining() bool + IsListening() bool + PeerCount() int + Db() ethutil.Database + TxPool() *core.TxPool +} + +type XEth struct { + eth Backend + blockProcessor *core.BlockProcessor + chainManager *core.ChainManager + world *State +} + +func New(eth Backend) *XEth { + xeth := &XEth{ + eth: eth, + blockProcessor: eth.BlockProcessor(), + chainManager: eth.ChainManager(), + } + xeth.world = NewState(xeth) + + return xeth +} + +func (self *XEth) State() *State { return self.world } + +func (self *XEth) BlockByHash(strHash string) *Block { + hash := fromHex(strHash) + block := self.chainManager.GetBlock(hash) + + return NewBlock(block) +} + +func (self *XEth) BlockByNumber(num int32) *Block { + if num == -1 { + return NewBlock(self.chainManager.CurrentBlock()) + } + + return NewBlock(self.chainManager.GetBlockByNumber(uint64(num))) +} + +func (self *XEth) Block(v interface{}) *Block { + 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 *XEth) Accounts() []string { + return []string{toHex(self.eth.KeyManager().Address())} +} + +/* +func (self *XEth) StateObject(addr string) *Object { + object := &Object{self.State().safeGet(fromHex(addr))} + + return NewObject(object) +} +*/ + +func (self *XEth) PeerCount() int { + return self.eth.PeerCount() +} + +func (self *XEth) IsMining() bool { + return self.eth.IsMining() +} + +func (self *XEth) IsListening() bool { + return self.eth.IsListening() +} + +func (self *XEth) Coinbase() string { + return toHex(self.eth.KeyManager().Address()) +} + +func (self *XEth) NumberToHuman(balance string) string { + b := ethutil.Big(balance) + + return ethutil.CurrencyToString(b) +} + +func (self *XEth) StorageAt(addr, storageAddr string) string { + storage := self.State().SafeGet(addr).StorageString(storageAddr) + + return toHex(storage.Bytes()) +} + +func (self *XEth) BalanceAt(addr string) string { + return self.State().SafeGet(addr).Balance().String() +} + +func (self *XEth) TxCountAt(address string) int { + return int(self.State().SafeGet(address).Nonce) +} + +func (self *XEth) CodeAt(address string) string { + return toHex(self.State().SafeGet(address).Code) +} + +func (self *XEth) IsContract(address string) bool { + return len(self.State().SafeGet(address).Code) > 0 +} + +func (self *XEth) SecretToAddress(key string) string { + pair, err := crypto.NewKeyPairFromSec(fromHex(key)) + if err != nil { + return "" + } + + return toHex(pair.Address()) +} + +func (self *XEth) Execute(addr, value, gas, price, data string) (string, error) { + return "", nil +} + +type KeyVal struct { + Key string `json:"key"` + Value string `json:"value"` +} + +func (self *XEth) EachStorage(addr string) string { + var values []KeyVal + object := self.State().SafeGet(addr) + it := object.Trie().Iterator() + for it.Next() { + values = append(values, KeyVal{toHex(it.Key), toHex(it.Value)}) + } + + valuesJson, err := json.Marshal(values) + if err != nil { + return "" + } + + return string(valuesJson) +} + +func (self *XEth) ToAscii(str string) string { + padded := ethutil.RightPadBytes([]byte(str), 32) + + return "0x" + toHex(padded) +} + +func (self *XEth) FromAscii(str string) string { + if ethutil.IsHex(str) { + str = str[2:] + } + + return string(bytes.Trim(fromHex(str), "\x00")) +} + +func (self *XEth) FromNumber(str string) string { + if ethutil.IsHex(str) { + str = str[2:] + } + + return ethutil.BigD(fromHex(str)).String() +} + +func (self *XEth) Transact(key, toStr, valueStr, gasStr, gasPriceStr, codeStr string) (string, error) { + return "", nil +} + +func ToMessages(messages state.Messages) *ethutil.List { + var msgs []Message + for _, m := range messages { + msgs = append(msgs, NewMessage(m)) + } + + return ethutil.NewList(msgs) +} + +func (self *XEth) PushTx(encodedTx string) (string, error) { + tx := types.NewTransactionFromBytes(fromHex(encodedTx)) + err := self.eth.TxPool().Add(tx) + if err != nil { + return "", err + } + + if tx.To() == nil { + addr := core.AddressFromMessage(tx) + return toHex(addr), nil + } + return toHex(tx.Hash()), nil +} |