diff --git a/cmd/mist/assets/qml/browser.qml b/cmd/mist/assets/qml/browser.qml index f6d2d2d64..fcec774a5 100644 --- a/cmd/mist/assets/qml/browser.qml +++ b/cmd/mist/assets/qml/browser.qml @@ -66,7 +66,7 @@ Rectangle { onMessages: { // Bit of a cheat to get proper JSON var m = JSON.parse(JSON.parse(JSON.stringify(messages))) - webview.postEvent("messages", id, m); + webview.postEvent("eth_changed", id, m); } function onShhMessage(message, id) { diff --git a/cmd/mist/ui_lib.go b/cmd/mist/ui_lib.go index 60c7181c1..f3559183e 100644 --- a/cmd/mist/ui_lib.go +++ b/cmd/mist/ui_lib.go @@ -70,6 +70,7 @@ func NewUiLib(engine *qml.Engine, eth *eth.Ethereum, assetPath string) *UiLib { lib := &UiLib{JSXEth: xeth.NewJSXEth(eth), engine: engine, eth: eth, assetPath: assetPath, jsEngine: javascript.NewJSRE(eth), filterCallbacks: make(map[int][]int)} //, filters: make(map[int]*xeth.JSFilter)} lib.miner = miner.New(eth.KeyManager().Address(), eth) lib.filterManager = filter.NewFilterManager(eth.EventMux()) + go lib.filterManager.Start() return lib } diff --git a/cmd/utils/websockets.go b/cmd/utils/websockets.go index 003e1bc22..5d97599c3 100644 --- a/cmd/utils/websockets.go +++ b/cmd/utils/websockets.go @@ -21,9 +21,14 @@ package utils import ( + "github.com/ethereum/go-ethereum/core" + "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/eth" "github.com/ethereum/go-ethereum/ethutil" + "github.com/ethereum/go-ethereum/event/filter" "github.com/ethereum/go-ethereum/logger" + "github.com/ethereum/go-ethereum/state" + "github.com/ethereum/go-ethereum/ui/qt" "github.com/ethereum/go-ethereum/websocket" "github.com/ethereum/go-ethereum/xeth" ) @@ -35,16 +40,19 @@ func args(v ...interface{}) []interface{} { } type WebSocketServer struct { - ethereum *eth.Ethereum - filterCallbacks map[int][]int + eth *eth.Ethereum + filterManager *filter.FilterManager } func NewWebSocketServer(eth *eth.Ethereum) *WebSocketServer { - return &WebSocketServer{eth, make(map[int][]int)} + filterManager := filter.NewFilterManager(eth.EventMux()) + go filterManager.Start() + + return &WebSocketServer{eth, filterManager} } func (self *WebSocketServer) Serv() { - pipe := xeth.NewJSXEth(self.ethereum) + pipe := xeth.NewJSXEth(self.eth) wsServ := websocket.NewServer("/eth", ":40404") wsServ.MessageFunc(func(c *websocket.Client, msg *websocket.Message) { @@ -112,15 +120,32 @@ func (self *WebSocketServer) Serv() { c.Write(pipe.BalanceAt(args.Get(0).Str()), msg.Id) - case "eth_secretToAddress": - args := msg.Arguments() - - c.Write(pipe.SecretToAddress(args.Get(0).Str()), msg.Id) + case "eth_accounts": + c.Write(pipe.Accounts(), msg.Id) case "eth_newFilter": + if mp, ok := msg.Args[0].(map[string]interface{}); ok { + var id int + filter := qt.NewFilterFromMap(mp, self.eth) + filter.MessageCallback = func(messages state.Messages) { + c.Event(toMessages(messages), "eth_changed", id) + } + id = self.filterManager.InstallFilter(filter) + c.Write(id, msg.Id) + } case "eth_newFilterString": - case "eth_messages": - // TODO + var id int + filter := core.NewFilter(self.eth) + filter.BlockCallback = func(block *types.Block) { + c.Event(nil, "eth_changed", id) + } + id = self.filterManager.InstallFilter(filter) + c.Write(id, msg.Id) + case "eth_filterLogs": + filter := self.filterManager.GetFilter(int(msg.Arguments().Get(0).Uint())) + if filter != nil { + c.Write(toMessages(filter.Find()), msg.Id) + } } }) @@ -128,6 +153,15 @@ func (self *WebSocketServer) Serv() { wsServ.Listen() } +func toMessages(messages state.Messages) (msgs []xeth.JSMessage) { + msgs = make([]xeth.JSMessage, len(messages)) + for i, msg := range messages { + msgs[i] = xeth.NewJSMessage(msg) + } + + return +} + func StartWebSockets(eth *eth.Ethereum) { wslogger.Infoln("Starting WebSockets") diff --git a/event/filter/old_filter.go b/event/filter/old_filter.go index 1a9a88173..6c7f053d4 100644 --- a/event/filter/old_filter.go +++ b/event/filter/old_filter.go @@ -2,6 +2,7 @@ package filter import ( + "fmt" "sync" "github.com/ethereum/go-ethereum/core" @@ -78,6 +79,7 @@ out: self.filterMu.RUnlock() case state.Messages: + fmt.Println("got messages") self.filterMu.RLock() for _, filter := range self.filters { if filter.MessageCallback != nil { diff --git a/ui/filter.go b/ui/filter.go index 88faad5ca..e0797dad2 100644 --- a/ui/filter.go +++ b/ui/filter.go @@ -5,6 +5,16 @@ import ( "github.com/ethereum/go-ethereum/ethutil" ) +func fromHex(s string) []byte { + if len(s) > 1 { + if s[0:2] == "0x" { + s = s[2:] + } + return ethutil.Hex2Bytes(s) + } + return nil +} + func NewFilterFromMap(object map[string]interface{}, eth core.EthManager) *core.Filter { filter := core.NewFilter(eth) @@ -20,12 +30,12 @@ func NewFilterFromMap(object map[string]interface{}, eth core.EthManager) *core. if object["to"] != nil { val := ethutil.NewValue(object["to"]) - filter.AddTo(ethutil.Hex2Bytes(val.Str())) + filter.AddTo(fromHex(val.Str())) } if object["from"] != nil { val := ethutil.NewValue(object["from"]) - filter.AddFrom(ethutil.Hex2Bytes(val.Str())) + filter.AddFrom(fromHex(val.Str())) } if object["max"] != nil { @@ -48,11 +58,11 @@ func NewFilterFromMap(object map[string]interface{}, eth core.EthManager) *core. // Conversion methodn func mapToAccountChange(m map[string]interface{}) (d core.AccountChange) { if str, ok := m["id"].(string); ok { - d.Address = ethutil.Hex2Bytes(str) + d.Address = fromHex(str) } if str, ok := m["at"].(string); ok { - d.StateAddress = ethutil.Hex2Bytes(str) + d.StateAddress = fromHex(str) } return @@ -62,7 +72,7 @@ func mapToAccountChange(m map[string]interface{}) (d core.AccountChange) { // ["aabbccdd", {id: "ccddee", at: "11223344"}], "aabbcc", {id: "ccddee", at: "1122"} func makeAltered(v interface{}) (d []core.AccountChange) { if str, ok := v.(string); ok { - d = append(d, core.AccountChange{ethutil.Hex2Bytes(str), nil}) + d = append(d, core.AccountChange{fromHex(str), nil}) } else if obj, ok := v.(map[string]interface{}); ok { d = append(d, mapToAccountChange(obj)) } else if slice, ok := v.([]interface{}); ok { diff --git a/ui/qt/filter.go b/ui/qt/filter.go index c68936401..423d5bd43 100644 --- a/ui/qt/filter.go +++ b/ui/qt/filter.go @@ -1,8 +1,6 @@ package qt import ( - "fmt" - "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/ui" "gopkg.in/qml.v1" @@ -23,13 +21,10 @@ func makeAltered(v interface{}) (d []core.AccountChange) { var s []interface{} qList.Convert(&s) - fmt.Println(s) - d = makeAltered(s) } else if qMap, ok := v.(*qml.Map); ok { var m map[string]interface{} qMap.Convert(&m) - fmt.Println(m) d = makeAltered(m) } diff --git a/websocket/client.go b/websocket/client.go index db2c8e5c9..340a67aea 100644 --- a/websocket/client.go +++ b/websocket/client.go @@ -51,7 +51,13 @@ func (c *Client) Conn() *ws.Conn { } func (c *Client) Write(data interface{}, id int) { - msg := &Message{Id: id, Data: data} + c.write(&Message{Id: id, Data: data}) +} +func (c *Client) Event(data interface{}, ev string, id int) { + c.write(&Message{Id: id, Data: data, Event: ev}) +} + +func (c *Client) write(msg *Message) { select { case c.ch <- msg: default: diff --git a/websocket/message.go b/websocket/message.go index 73b47456f..cd2af5099 100644 --- a/websocket/message.go +++ b/websocket/message.go @@ -3,10 +3,11 @@ package websocket import "github.com/ethereum/go-ethereum/ethutil" type Message struct { - Call string `json:"call"` - Args []interface{} `json:"args"` - Id int `json:"_id"` - Data interface{} `json:"data"` + Call string `json:"call"` + Args []interface{} `json:"args"` + Id int `json:"_id"` + Data interface{} `json:"data"` + Event string `json:"_event"` } func (self *Message) Arguments() *ethutil.Value { diff --git a/xeth/hexface.go b/xeth/hexface.go index c3d8cef86..808ae7d39 100644 --- a/xeth/hexface.go +++ b/xeth/hexface.go @@ -20,7 +20,7 @@ func NewJSXEth(eth core.EthManager) *JSXEth { } func (self *JSXEth) BlockByHash(strHash string) *JSBlock { - hash := ethutil.Hex2Bytes(strHash) + hash := fromHex(strHash) block := self.obj.ChainManager().GetBlock(hash) return NewJSBlock(block) @@ -50,8 +50,12 @@ func (self *JSXEth) Key() *JSKey { return NewJSKey(self.obj.KeyManager().KeyPair()) } +func (self *JSXEth) Accounts() []string { + return []string{toHex(self.obj.KeyManager().Address())} +} + func (self *JSXEth) StateObject(addr string) *JSObject { - object := &Object{self.World().safeGet(ethutil.Hex2Bytes(addr))} + object := &Object{self.World().safeGet(fromHex(addr))} return NewJSObject(object) } @@ -78,7 +82,7 @@ func (self *JSXEth) IsListening() bool { } func (self *JSXEth) CoinBase() string { - return ethutil.Bytes2Hex(self.obj.KeyManager().Address()) + return toHex(self.obj.KeyManager().Address()) } func (self *JSXEth) NumberToHuman(balance string) string { @@ -88,46 +92,46 @@ func (self *JSXEth) NumberToHuman(balance string) string { } func (self *JSXEth) StorageAt(addr, storageAddr string) string { - storage := self.World().SafeGet(ethutil.Hex2Bytes(addr)).Storage(ethutil.Hex2Bytes(storageAddr)) + storage := self.World().SafeGet(fromHex(addr)).Storage(fromHex(storageAddr)) - return ethutil.Bytes2Hex(storage.Bytes()) + return toHex(storage.Bytes()) } func (self *JSXEth) BalanceAt(addr string) string { - return self.World().SafeGet(ethutil.Hex2Bytes(addr)).Balance().String() + return self.World().SafeGet(fromHex(addr)).Balance().String() } func (self *JSXEth) TxCountAt(address string) int { - return int(self.World().SafeGet(ethutil.Hex2Bytes(address)).Nonce) + return int(self.World().SafeGet(fromHex(address)).Nonce) } func (self *JSXEth) CodeAt(address string) string { - return ethutil.Bytes2Hex(self.World().SafeGet(ethutil.Hex2Bytes(address)).Code) + return toHex(self.World().SafeGet(fromHex(address)).Code) } func (self *JSXEth) IsContract(address string) bool { - return len(self.World().SafeGet(ethutil.Hex2Bytes(address)).Code) > 0 + return len(self.World().SafeGet(fromHex(address)).Code) > 0 } func (self *JSXEth) SecretToAddress(key string) string { - pair, err := crypto.NewKeyPairFromSec(ethutil.Hex2Bytes(key)) + pair, err := crypto.NewKeyPairFromSec(fromHex(key)) if err != nil { return "" } - return ethutil.Bytes2Hex(pair.Address()) + return toHex(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), + self.World().safeGet(fromHex(addr))}, + fromHex(data), ethutil.NewValue(value), ethutil.NewValue(gas), ethutil.NewValue(price), ) - return ethutil.Bytes2Hex(ret), err + return toHex(ret), err } type KeyVal struct { @@ -137,10 +141,10 @@ type KeyVal struct { func (self *JSXEth) EachStorage(addr string) string { var values []KeyVal - object := self.World().SafeGet(ethutil.Hex2Bytes(addr)) + object := self.World().SafeGet(fromHex(addr)) it := object.Trie().Iterator() for it.Next() { - values = append(values, KeyVal{ethutil.Bytes2Hex(it.Key), ethutil.Bytes2Hex(it.Value)}) + values = append(values, KeyVal{toHex(it.Key), toHex(it.Value)}) } valuesJson, err := json.Marshal(values) @@ -154,7 +158,7 @@ func (self *JSXEth) EachStorage(addr string) string { func (self *JSXEth) ToAscii(str string) string { padded := ethutil.RightPadBytes([]byte(str), 32) - return "0x" + ethutil.Bytes2Hex(padded) + return "0x" + toHex(padded) } func (self *JSXEth) FromAscii(str string) string { @@ -162,7 +166,7 @@ func (self *JSXEth) FromAscii(str string) string { str = str[2:] } - return string(bytes.Trim(ethutil.Hex2Bytes(str), "\x00")) + return string(bytes.Trim(fromHex(str), "\x00")) } func (self *JSXEth) FromNumber(str string) string { @@ -170,7 +174,7 @@ func (self *JSXEth) FromNumber(str string) string { str = str[2:] } - return ethutil.BigD(ethutil.Hex2Bytes(str)).String() + return ethutil.BigD(fromHex(str)).String() } func (self *JSXEth) Transact(key, toStr, valueStr, gasStr, gasPriceStr, codeStr string) (string, error) { @@ -182,26 +186,11 @@ func (self *JSXEth) Transact(key, toStr, valueStr, gasStr, gasPriceStr, codeStr data []byte ) - if ethutil.IsHex(codeStr) { - data = ethutil.Hex2Bytes(codeStr[2:]) - } else { - data = ethutil.Hex2Bytes(codeStr) - } + data = fromHex(codeStr) - if ethutil.IsHex(toStr) { - to = ethutil.Hex2Bytes(toStr[2:]) - } else { - to = 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))) - } + to = fromHex(toStr) + keyPair, err := crypto.NewKeyPairFromSec([]byte(fromHex(key))) if err != nil { return "", err } @@ -211,14 +200,14 @@ func (self *JSXEth) Transact(key, toStr, valueStr, gasStr, gasPriceStr, codeStr return "", err } if types.IsContractAddr(to) { - return ethutil.Bytes2Hex(core.AddressFromMessage(tx)), nil + return toHex(core.AddressFromMessage(tx)), nil } - return ethutil.Bytes2Hex(tx.Hash()), nil + return toHex(tx.Hash()), nil } func (self *JSXEth) PushTx(txStr string) (*JSReceipt, error) { - tx := types.NewTransactionFromBytes(ethutil.Hex2Bytes(txStr)) + tx := types.NewTransactionFromBytes(fromHex(txStr)) err := self.obj.TxPool().Add(tx) if err != nil { return nil, err @@ -233,11 +222,11 @@ func (self *JSXEth) CompileMutan(code string) string { return err.Error() } - return ethutil.Bytes2Hex(data) + return toHex(data) } func (self *JSXEth) FindInConfig(str string) string { - return ethutil.Bytes2Hex(self.World().Config().Get(str).Address()) + return toHex(self.World().Config().Get(str).Address()) } func ToJSMessages(messages state.Messages) *ethutil.List { diff --git a/xeth/js_types.go b/xeth/js_types.go index 5fe5946b1..a0be996ff 100644 --- a/xeth/js_types.go +++ b/xeth/js_types.go @@ -12,6 +12,19 @@ import ( "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"` @@ -52,12 +65,12 @@ func NewJSBlock(block *types.Block) *JSBlock { return &JSBlock{ ref: block, Size: block.Size().String(), Number: int(block.NumberU64()), GasUsed: block.GasUsed().String(), - GasLimit: block.GasLimit().String(), Hash: ethutil.Bytes2Hex(block.Hash()), + GasLimit: block.GasLimit().String(), Hash: toHex(block.Hash()), Transactions: txlist, Uncles: ulist, Time: block.Time(), - Coinbase: ethutil.Bytes2Hex(block.Coinbase()), - PrevHash: ethutil.Bytes2Hex(block.ParentHash()), - Bloom: ethutil.Bytes2Hex(block.Bloom()), + Coinbase: toHex(block.Coinbase()), + PrevHash: toHex(block.ParentHash()), + Bloom: toHex(block.Bloom()), Raw: block.String(), } } @@ -71,7 +84,7 @@ func (self *JSBlock) ToString() string { } func (self *JSBlock) GetTransaction(hash string) *JSTransaction { - tx := self.ref.Transaction(ethutil.Hex2Bytes(hash)) + tx := self.ref.Transaction(fromHex(hash)) if tx == nil { return nil } @@ -96,22 +109,22 @@ type JSTransaction struct { } func NewJSTx(tx *types.Transaction) *JSTransaction { - hash := ethutil.Bytes2Hex(tx.Hash()) - receiver := ethutil.Bytes2Hex(tx.To()) + hash := toHex(tx.Hash()) + receiver := toHex(tx.To()) if receiver == "0000000000000000000000000000000000000000" { - receiver = ethutil.Bytes2Hex(core.AddressFromMessage(tx)) + receiver = toHex(core.AddressFromMessage(tx)) } - sender := ethutil.Bytes2Hex(tx.From()) + sender := toHex(tx.From()) createsContract := core.MessageCreatesContract(tx) var data string if createsContract { data = strings.Join(core.Disassemble(tx.Data()), "\n") } else { - data = ethutil.Bytes2Hex(tx.Data()) + 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: 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: toHex(tx.Data())} } func (self *JSTransaction) ToString() string { @@ -125,7 +138,7 @@ type JSKey struct { } func NewJSKey(key *crypto.KeyPair) *JSKey { - return &JSKey{ethutil.Bytes2Hex(key.Address()), ethutil.Bytes2Hex(key.PrivateKey), ethutil.Bytes2Hex(key.PublicKey)} + return &JSKey{toHex(key.Address()), toHex(key.PrivateKey), toHex(key.PublicKey)} } type JSObject struct { @@ -146,9 +159,9 @@ type PReceipt struct { func NewPReciept(contractCreation bool, creationAddress, hash, address []byte) *PReceipt { return &PReceipt{ contractCreation, - ethutil.Bytes2Hex(creationAddress), - ethutil.Bytes2Hex(hash), - ethutil.Bytes2Hex(address), + toHex(creationAddress), + toHex(hash), + toHex(address), } } @@ -185,9 +198,9 @@ type JSReceipt struct { func NewJSReciept(contractCreation bool, creationAddress, hash, address []byte) *JSReceipt { return &JSReceipt{ contractCreation, - ethutil.Bytes2Hex(creationAddress), - ethutil.Bytes2Hex(hash), - ethutil.Bytes2Hex(address), + toHex(creationAddress), + toHex(hash), + toHex(address), } } @@ -207,15 +220,15 @@ type JSMessage struct { func NewJSMessage(message *state.Message) JSMessage { return JSMessage{ - To: ethutil.Bytes2Hex(message.To), - From: ethutil.Bytes2Hex(message.From), - Input: ethutil.Bytes2Hex(message.Input), - Output: ethutil.Bytes2Hex(message.Output), + To: toHex(message.To), + From: toHex(message.From), + Input: toHex(message.Input), + Output: toHex(message.Output), Path: int32(message.Path), - Origin: ethutil.Bytes2Hex(message.Origin), + Origin: toHex(message.Origin), Timestamp: int32(message.Timestamp), - Coinbase: ethutil.Bytes2Hex(message.Origin), - Block: ethutil.Bytes2Hex(message.Block), + Coinbase: toHex(message.Origin), + Block: toHex(message.Block), Number: int32(message.Number.Int64()), Value: message.Value.String(), }