diff options
author | obscuren <geffobscura@gmail.com> | 2015-01-10 07:51:56 +0800 |
---|---|---|
committer | obscuren <geffobscura@gmail.com> | 2015-01-10 07:51:56 +0800 |
commit | e3da85faedf21a3ddb73a0fa29decf65364e6c39 (patch) | |
tree | 516faf70a5346bec9016bbdd308b6bcc23d5bccb /cmd/utils | |
parent | 491c23a728e4f5cdedfa040aded6a6b136f6bee0 (diff) | |
download | dexon-e3da85faedf21a3ddb73a0fa29decf65364e6c39.tar.gz dexon-e3da85faedf21a3ddb73a0fa29decf65364e6c39.tar.zst dexon-e3da85faedf21a3ddb73a0fa29decf65364e6c39.zip |
Implemented filter for ws + fixes
* proper 0xhex
* filters fixed
* start of filter manager
* accounts for ws. Closes #246
Diffstat (limited to 'cmd/utils')
-rw-r--r-- | cmd/utils/websockets.go | 54 |
1 files changed, 44 insertions, 10 deletions
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") |