Merge branch 'qt5.4' of github.com-obscure:ethereum/go-ethereum into qt5.4

Conflicts:
	rpc/message.go
This commit is contained in:
obscuren 2015-01-29 20:41:10 +01:00
commit bd992e7baf
10 changed files with 45 additions and 518 deletions

View File

@ -11,6 +11,7 @@ install:
# - go get golang.org/x/tools/cmd/vet # - 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 - 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 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 - 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: before_script:
- gofmt -l -w . - gofmt -l -w .

View File

@ -41,7 +41,6 @@ import (
rpchttp "github.com/ethereum/go-ethereum/rpc/http" rpchttp "github.com/ethereum/go-ethereum/rpc/http"
rpcws "github.com/ethereum/go-ethereum/rpc/ws" rpcws "github.com/ethereum/go-ethereum/rpc/ws"
"github.com/ethereum/go-ethereum/state" "github.com/ethereum/go-ethereum/state"
// "github.com/ethereum/go-ethereum/websocket"
"github.com/ethereum/go-ethereum/xeth" "github.com/ethereum/go-ethereum/xeth"
) )
@ -205,8 +204,6 @@ func StartRpc(ethereum *eth.Ethereum, RpcPort int) {
func StartWebSockets(eth *eth.Ethereum, wsPort int) { func StartWebSockets(eth *eth.Ethereum, wsPort int) {
clilogger.Infoln("Starting WebSockets") clilogger.Infoln("Starting WebSockets")
// sock := websocket.NewWebSocketServer(eth)
// go sock.Serv()
var err error var err error
eth.WsServer, err = rpcws.NewWebSocketServer(eth, wsPort) eth.WsServer, err = rpcws.NewWebSocketServer(eth, wsPort)
if err != nil { if err != nil {

View File

@ -21,8 +21,6 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"github.com/ethereum/go-ethereum/state"
) )
const ( const (
@ -232,29 +230,6 @@ func (req *RpcRequest) ToFilterChangedArgs() (int, error) {
return id, nil 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
}
func (req *RpcRequest) ToDbPutArgs() (*DbArgs, error) { func (req *RpcRequest) ToDbPutArgs() (*DbArgs, error) {
if len(req.Params) < 3 { if len(req.Params) < 3 {
return nil, NewErrorResponse(ErrorArguments) return nil, NewErrorResponse(ErrorArguments)

View File

@ -40,24 +40,6 @@ import (
"github.com/ethereum/go-ethereum/xeth" "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 { type EthereumApi struct {
xeth *xeth.XEth xeth *xeth.XEth
filterManager *filter.FilterManager filterManager *filter.FilterManager

View File

@ -21,6 +21,8 @@ import (
"github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/logger"
"io" "io"
"net/http" "net/http"
"github.com/ethereum/go-ethereum/ethutil"
"github.com/ethereum/go-ethereum/state"
) )
var rpclogger = logger.NewLogger("RPC") var rpclogger = logger.NewLogger("RPC")
@ -56,3 +58,44 @@ func (self JsonWrapper) ParseRequestBody(req *http.Request) (RpcRequest, error)
return reqParsed, nil 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
}

View File

@ -14,7 +14,7 @@
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/ */
package ws package rpcws
import ( import (
"fmt" "fmt"

View File

@ -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)
}
}
}
}
}

View File

@ -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)
}

View File

@ -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
}
}
}

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
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
}