From 726852e3d3ba5c2167bbdb3bdd3ecbaff6b4f242 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Wed, 28 Jan 2015 21:39:49 -0600 Subject: [PATCH 1/3] Remove old websocket implementation --- cmd/utils/cmd.go | 3 - websocket/client.go | 126 -------------------------- websocket/message.go | 15 ---- websocket/server.go | 125 -------------------------- websocket/util.go | 205 ------------------------------------------- 5 files changed, 474 deletions(-) delete mode 100644 websocket/client.go delete mode 100644 websocket/message.go delete mode 100644 websocket/server.go delete mode 100644 websocket/util.go diff --git a/cmd/utils/cmd.go b/cmd/utils/cmd.go index 1d4655433..43ad88c55 100644 --- a/cmd/utils/cmd.go +++ b/cmd/utils/cmd.go @@ -41,7 +41,6 @@ import ( rpchttp "github.com/ethereum/go-ethereum/rpc/http" rpcws "github.com/ethereum/go-ethereum/rpc/ws" "github.com/ethereum/go-ethereum/state" - // "github.com/ethereum/go-ethereum/websocket" "github.com/ethereum/go-ethereum/xeth" ) @@ -205,8 +204,6 @@ func StartRpc(ethereum *eth.Ethereum, RpcPort int) { func StartWebSockets(eth *eth.Ethereum, wsPort int) { clilogger.Infoln("Starting WebSockets") - // sock := websocket.NewWebSocketServer(eth) - // go sock.Serv() var err error eth.WsServer, err = rpcws.NewWebSocketServer(eth, wsPort) if err != nil { diff --git a/websocket/client.go b/websocket/client.go deleted file mode 100644 index 340a67aea..000000000 --- a/websocket/client.go +++ /dev/null @@ -1,126 +0,0 @@ -package websocket - -import ( - "fmt" - "io" - - ws "code.google.com/p/go.net/websocket" -) - -const channelBufSize = 100 - -var maxId int = 0 - -type MsgFunc func(c *Client, msg *Message) - -// Chat client. -type Client struct { - id int - ws *ws.Conn - server *Server - ch chan *Message - doneCh chan bool - - onMessage MsgFunc -} - -// Create new chat client. -func NewClient(ws *ws.Conn, server *Server) *Client { - - if ws == nil { - panic("ws cannot be nil") - } - - if server == nil { - panic("server cannot be nil") - } - - maxId++ - ch := make(chan *Message, channelBufSize) - doneCh := make(chan bool) - - return &Client{maxId, ws, server, ch, doneCh, nil} -} - -func (c *Client) Id() int { - return c.id -} - -func (c *Client) Conn() *ws.Conn { - return c.ws -} - -func (c *Client) Write(data interface{}, id int) { - 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: - c.server.Del(c) - err := fmt.Errorf("client %d is disconnected.", c.id) - c.server.Err(err) - } -} - -func (c *Client) Done() { - c.doneCh <- true -} - -// Listen Write and Read request via chanel -func (c *Client) Listen() { - go c.listenWrite() - c.listenRead() -} - -// Listen write request via chanel -func (c *Client) listenWrite() { - for { - select { - - // send message to the client - case msg := <-c.ch: - wslogger.Debugln("Send:", msg) - ws.JSON.Send(c.ws, msg) - - // receive done request - case <-c.doneCh: - c.server.Del(c) - c.doneCh <- true // for listenRead method - return - } - } -} - -// Listen read request via chanel -func (c *Client) listenRead() { - for { - select { - - // receive done request - case <-c.doneCh: - c.server.Del(c) - c.doneCh <- true // for listenWrite method - return - - // read data from ws connection - default: - var msg Message - err := ws.JSON.Receive(c.ws, &msg) - if err == io.EOF { - c.doneCh <- true - } else if err != nil { - c.server.Err(err) - } else { - wslogger.Debugln(&msg) - if c.onMessage != nil { - c.onMessage(c, &msg) - } - } - } - } -} diff --git a/websocket/message.go b/websocket/message.go deleted file mode 100644 index cd2af5099..000000000 --- a/websocket/message.go +++ /dev/null @@ -1,15 +0,0 @@ -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"` - Event string `json:"_event"` -} - -func (self *Message) Arguments() *ethutil.Value { - return ethutil.NewValue(self.Args) -} diff --git a/websocket/server.go b/websocket/server.go deleted file mode 100644 index b0658b1b4..000000000 --- a/websocket/server.go +++ /dev/null @@ -1,125 +0,0 @@ -package websocket - -import ( - "net/http" - - "github.com/ethereum/go-ethereum/logger" - - ws "code.google.com/p/go.net/websocket" -) - -var wslogger = logger.NewLogger("WS") - -// Chat server. -type Server struct { - httpServ string - pattern string - messages []*Message - clients map[int]*Client - addCh chan *Client - delCh chan *Client - sendAllCh chan string - doneCh chan bool - errCh chan error - msgFunc MsgFunc -} - -// Create new chat server. -func NewServer(pattern, httpServ string) *Server { - clients := make(map[int]*Client) - addCh := make(chan *Client) - delCh := make(chan *Client) - sendAllCh := make(chan string) - doneCh := make(chan bool) - errCh := make(chan error) - - return &Server{ - httpServ, - pattern, - nil, - clients, - addCh, - delCh, - sendAllCh, - doneCh, - errCh, - nil, - } -} - -func (s *Server) Add(c *Client) { - s.addCh <- c -} - -func (s *Server) Del(c *Client) { - s.delCh <- c -} - -func (s *Server) SendAll(msg string) { - s.sendAllCh <- msg -} - -func (s *Server) Done() { - s.doneCh <- true -} - -func (s *Server) Err(err error) { - s.errCh <- err -} - -func (s *Server) servHTTP() { - wslogger.Debugln("Serving http", s.httpServ) - err := http.ListenAndServe(s.httpServ, nil) - - wslogger.Warnln(err) -} - -func (s *Server) MessageFunc(f MsgFunc) { - s.msgFunc = f -} - -// Listen and serve. -// It serves client connection and broadcast request. -func (s *Server) Listen() { - // ws handler - onConnected := func(ws *ws.Conn) { - defer func() { - err := ws.Close() - if err != nil { - s.errCh <- err - } - }() - - client := NewClient(ws, s) - client.onMessage = s.msgFunc - s.Add(client) - client.Listen() - } - // Disable Origin check. Request don't need to come necessarily from origin. - http.HandleFunc(s.pattern, func(w http.ResponseWriter, req *http.Request) { - s := ws.Server{Handler: ws.Handler(onConnected)} - s.ServeHTTP(w, req) - }) - wslogger.Debugln("Created handler") - - go s.servHTTP() - - for { - select { - - // Add new a client - case c := <-s.addCh: - s.clients[c.id] = c - - // del a client - case c := <-s.delCh: - delete(s.clients, c.id) - - case err := <-s.errCh: - wslogger.Debugln("Error:", err.Error()) - - case <-s.doneCh: - return - } - } -} diff --git a/websocket/util.go b/websocket/util.go deleted file mode 100644 index 3c1388de0..000000000 --- a/websocket/util.go +++ /dev/null @@ -1,205 +0,0 @@ -/* - This file is part of go-ethereum - - go-ethereum is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - go-ethereum is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with go-ethereum. If not, see . -*/ -package websocket - -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/state" - "github.com/ethereum/go-ethereum/ui" - "github.com/ethereum/go-ethereum/xeth" -) - -func args(v ...interface{}) []interface{} { - return v -} - -type WebSocketServer struct { - eth *eth.Ethereum - filterManager *filter.FilterManager -} - -func NewWebSocketServer(eth *eth.Ethereum) *WebSocketServer { - filterManager := filter.NewFilterManager(eth.EventMux()) - go filterManager.Start() - - return &WebSocketServer{eth, filterManager} -} - -func (self *WebSocketServer) Serv() { - pipe := xeth.NewJSXEth(self.eth) - - wsServ := NewServer("/eth", ":40404") - wsServ.MessageFunc(func(c *Client, msg *Message) { - switch msg.Call { - case "compile": - data := ethutil.NewValue(msg.Args) - bcode, err := ethutil.Compile(data.Get(0).Str(), false) - if err != nil { - c.Write(args(nil, err.Error()), msg.Id) - } - - code := ethutil.Bytes2Hex(bcode) - c.Write(args(code, nil), msg.Id) - case "eth_blockByNumber": - args := msg.Arguments() - - block := pipe.BlockByNumber(int32(args.Get(0).Uint())) - c.Write(block, msg.Id) - - case "eth_blockByHash": - args := msg.Arguments() - - c.Write(pipe.BlockByHash(args.Get(0).Str()), msg.Id) - - case "eth_transact": - if mp, ok := msg.Args[0].(map[string]interface{}); ok { - object := mapToTxParams(mp) - c.Write( - args(pipe.Transact(pipe.Key().PrivateKey, object["to"], object["value"], object["gas"], object["gasPrice"], object["data"])), - msg.Id, - ) - - } - case "eth_gasPrice": - c.Write("10000000000000", msg.Id) - case "eth_coinbase": - c.Write(pipe.CoinBase(), msg.Id) - - case "eth_listening": - c.Write(pipe.IsListening(), msg.Id) - - case "eth_mining": - c.Write(pipe.IsMining(), msg.Id) - - case "eth_peerCount": - c.Write(pipe.PeerCount(), msg.Id) - - case "eth_countAt": - args := msg.Arguments() - - c.Write(pipe.TxCountAt(args.Get(0).Str()), msg.Id) - - case "eth_codeAt": - args := msg.Arguments() - - c.Write(len(pipe.CodeAt(args.Get(0).Str())), msg.Id) - - case "eth_storageAt": - args := msg.Arguments() - - c.Write(pipe.StorageAt(args.Get(0).Str(), args.Get(1).Str()), msg.Id) - - case "eth_balanceAt": - args := msg.Arguments() - - c.Write(pipe.BalanceAt(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 := ui.NewFilterFromMap(mp, self.eth) - filter.LogsCallback = func(logs state.Logs) { - //c.Event(toMessages(messages), "eth_changed", id) - } - id = self.filterManager.InstallFilter(filter) - c.Write(id, msg.Id) - } - case "eth_newFilterString": - 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) - } - } - - }) - - 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 -} - -// TODO This is starting to become a generic method. Move to utils -func mapToTxParams(object map[string]interface{}) map[string]string { - // Default values - if object["from"] == nil { - object["from"] = "" - } - if object["to"] == nil { - object["to"] = "" - } - if object["value"] == nil { - object["value"] = "" - } - if object["gas"] == nil { - object["gas"] = "" - } - if object["gasPrice"] == nil { - object["gasPrice"] = "" - } - - var dataStr string - var data []string - if str, ok := object["data"].(string); ok { - data = []string{str} - } - - for _, str := range data { - if ethutil.IsHex(str) { - str = str[2:] - - if len(str) != 64 { - str = ethutil.LeftPadString(str, 64) - } - } else { - str = ethutil.Bytes2Hex(ethutil.LeftPadBytes(ethutil.Big(str).Bytes(), 32)) - } - - dataStr += str - } - object["data"] = dataStr - - conv := make(map[string]string) - for key, value := range object { - if v, ok := value.(string); ok { - conv[key] = v - } - } - - return conv -} From 89a48e365c6f401ed6e2b5fdbd5ab9ee9a91b033 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Thu, 29 Jan 2015 11:39:59 -0600 Subject: [PATCH 2/3] Cleanup --- rpc/message.go | 25 ----------------------- rpc/packages.go | 41 -------------------------------------- rpc/{json.go => util.go} | 43 ++++++++++++++++++++++++++++++++++++++++ rpc/ws/server.go | 2 +- 4 files changed, 44 insertions(+), 67 deletions(-) rename rpc/{json.go => util.go} (66%) diff --git a/rpc/message.go b/rpc/message.go index 05f66ee95..440bf4f8f 100644 --- a/rpc/message.go +++ b/rpc/message.go @@ -21,8 +21,6 @@ import ( "encoding/json" "errors" "fmt" - - "github.com/ethereum/go-ethereum/state" ) const ( @@ -216,26 +214,3 @@ func (req *RpcRequest) ToFilterChangedArgs() (int, error) { rpclogger.DebugDetailf("%T %v", id, id) return id, nil } - -type Log struct { - Address string `json:"address"` - Topics []string `json:"topics"` - Data string `json:"data"` -} - -func toLogs(logs state.Logs) (ls []Log) { - ls = make([]Log, len(logs)) - - for i, log := range logs { - var l Log - l.Topics = make([]string, len(log.Topics())) - l.Address = toHex(log.Address()) - l.Data = toHex(log.Data()) - for j, topic := range log.Topics() { - l.Topics[j] = toHex(topic) - } - ls[i] = l - } - - return -} diff --git a/rpc/packages.go b/rpc/packages.go index e8dc570fd..53159d9ba 100644 --- a/rpc/packages.go +++ b/rpc/packages.go @@ -39,24 +39,6 @@ import ( "github.com/ethereum/go-ethereum/xeth" ) -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 RpcServer interface { - Start() - Stop() -} - type EthereumApi struct { xeth *xeth.XEth filterManager *filter.FilterManager @@ -91,29 +73,6 @@ func (self *EthereumApi) NewFilter(args *FilterOptions, reply *interface{}) erro return nil } -type Log struct { - Address string `json:"address"` - Topics []string `json:"topics"` - Data string `json:"data"` -} - -func toLogs(logs state.Logs) (ls []Log) { - ls = make([]Log, len(logs)) - - for i, log := range logs { - var l Log - l.Topics = make([]string, len(log.Topics())) - l.Address = toHex(log.Address()) - l.Data = toHex(log.Data()) - for j, topic := range log.Topics() { - l.Topics[j] = toHex(topic) - } - ls[i] = l - } - - return -} - func (self *EthereumApi) FilterChanged(id int, reply *interface{}) error { self.mut.RLock() defer self.mut.RUnlock() diff --git a/rpc/json.go b/rpc/util.go similarity index 66% rename from rpc/json.go rename to rpc/util.go index 9bd204818..bb4087c51 100644 --- a/rpc/json.go +++ b/rpc/util.go @@ -21,6 +21,8 @@ import ( "github.com/ethereum/go-ethereum/logger" "io" "net/http" + "github.com/ethereum/go-ethereum/ethutil" + "github.com/ethereum/go-ethereum/state" ) var rpclogger = logger.NewLogger("RPC") @@ -56,3 +58,44 @@ func (self JsonWrapper) ParseRequestBody(req *http.Request) (RpcRequest, error) return reqParsed, nil } + +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 RpcServer interface { + Start() + Stop() +} + +type Log struct { + Address string `json:"address"` + Topics []string `json:"topics"` + Data string `json:"data"` +} + +func toLogs(logs state.Logs) (ls []Log) { + ls = make([]Log, len(logs)) + + for i, log := range logs { + var l Log + l.Topics = make([]string, len(log.Topics())) + l.Address = toHex(log.Address()) + l.Data = toHex(log.Data()) + for j, topic := range log.Topics() { + l.Topics[j] = toHex(topic) + } + ls[i] = l + } + + return +} diff --git a/rpc/ws/server.go b/rpc/ws/server.go index 37b08ebd9..2853181ec 100644 --- a/rpc/ws/server.go +++ b/rpc/ws/server.go @@ -14,7 +14,7 @@ You should have received a copy of the GNU General Public License along with go-ethereum. If not, see . */ -package ws +package rpcws import ( "fmt" From 447bad9dbd9ef033332b1166f7ac95aab5b82e43 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Thu, 29 Jan 2015 12:12:19 -0600 Subject: [PATCH 3/3] Travis fix Satisfy dependency for obscuren/qml fork --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 21c15068b..6911eec84 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,6 +11,7 @@ install: # - go get golang.org/x/tools/cmd/vet - if ! go get code.google.com/p/go.tools/cmd/cover; then go get golang.org/x/tools/cmd/cover; fi - go get github.com/mattn/goveralls + - go get gopkg.in/qml.v1 - ETH_DEPS=$(go list -f '{{.Imports}} {{.TestImports}} {{.XTestImports}}' github.com/ethereum/go-ethereum/... | sed -e 's/\[//g' | sed -e 's/\]//g' | sed -e 's/C //g'); if [ "$ETH_DEPS" ]; then go get $ETH_DEPS; fi before_script: - gofmt -l -w .