tendermint/rpc/handlers.go

392 lines
10 KiB
Go
Raw Normal View History

2015-01-11 17:10:34 -08:00
package rpc
2015-03-30 15:55:05 -07:00
/*
TODO: support Call && GetStorage.
*/
2015-01-11 17:10:34 -08:00
import (
2015-04-02 14:30:30 -07:00
"bytes"
"encoding/json"
"fmt"
2015-04-01 17:30:16 -07:00
"github.com/tendermint/tendermint/binary"
2015-04-02 14:30:30 -07:00
"github.com/tendermint/tendermint/events"
2015-04-01 17:30:16 -07:00
"github.com/tendermint/tendermint/rpc/core"
2015-04-02 14:30:30 -07:00
"golang.org/x/net/websocket"
"io/ioutil"
2015-01-11 17:10:34 -08:00
"net/http"
"reflect"
"runtime"
"strings"
2015-04-02 14:30:30 -07:00
"time"
2015-01-11 17:10:34 -08:00
)
// cache all type information about each function up front
// (func, responseStruct, argNames)
var funcMap = map[string]*FuncWrapper{
"status": funcWrap(core.Status, []string{}),
"net_info": funcWrap(core.NetInfo, []string{}),
"blockchain": funcWrap(core.BlockchainInfo, []string{"min_height", "max_height"}),
"get_block": funcWrap(core.GetBlock, []string{"height"}),
"get_account": funcWrap(core.GetAccount, []string{"address"}),
"get_storage": funcWrap(core.GetStorage, []string{"address", "storage"}),
"call": funcWrap(core.Call, []string{"address", "data"}),
"list_validators": funcWrap(core.ListValidators, []string{}),
2015-03-31 14:08:21 -07:00
"dump_storage": funcWrap(core.DumpStorage, []string{"address"}),
"broadcast_tx": funcWrap(core.BroadcastTx, []string{"tx"}),
"list_accounts": funcWrap(core.ListAccounts, []string{}),
"unsafe/gen_priv_account": funcWrap(core.GenPrivAccount, []string{}),
"unsafe/sign_tx": funcWrap(core.SignTx, []string{"tx", "privAccounts"}),
}
2015-04-02 14:30:30 -07:00
// maps camel-case function names to lower case rpc version
// populated by calls to funcWrap
var reverseFuncMap = fillReverseFuncMap()
2015-04-02 14:30:30 -07:00
// fill the map from camelcase to lowercase
func fillReverseFuncMap() map[string]string {
fMap := make(map[string]string)
for name, f := range funcMap {
camelName := runtime.FuncForPC(f.f.Pointer()).Name()
spl := strings.Split(camelName, ".")
if len(spl) > 1 {
camelName = spl[len(spl)-1]
}
2015-04-02 14:30:30 -07:00
fMap[camelName] = name
}
return fMap
}
func initHandlers(ew *events.EventSwitch) {
// HTTP endpoints
for funcName, funcInfo := range funcMap {
http.HandleFunc("/"+funcName, toHTTPHandler(funcInfo))
}
2015-04-02 14:30:30 -07:00
// JSONRPC endpoints
http.HandleFunc("/", JSONRPCHandler)
w := NewWebsocketManager(ew)
// websocket endpoint
http.Handle("/events", websocket.Handler(w.eventsHandler))
2015-03-30 15:55:05 -07:00
}
//-------------------------------------
// holds all type information for each function
type FuncWrapper struct {
f reflect.Value // function from "rpc/core"
args []reflect.Type // type of each function arg
returns []reflect.Type // type of each return arg
argNames []string // name of each argument
}
func funcWrap(f interface{}, args []string) *FuncWrapper {
return &FuncWrapper{
f: reflect.ValueOf(f),
args: funcArgTypes(f),
returns: funcReturnTypes(f),
argNames: args,
}
}
2015-03-30 15:55:05 -07:00
func funcArgTypes(f interface{}) []reflect.Type {
t := reflect.TypeOf(f)
n := t.NumIn()
types := make([]reflect.Type, n)
for i := 0; i < n; i++ {
types[i] = t.In(i)
}
2015-03-30 15:55:05 -07:00
return types
}
2015-03-30 15:55:05 -07:00
func funcReturnTypes(f interface{}) []reflect.Type {
t := reflect.TypeOf(f)
n := t.NumOut()
types := make([]reflect.Type, n)
for i := 0; i < n; i++ {
types[i] = t.Out(i)
}
return types
}
//-----------------------------------------------------------------------------
// rpc.json
// jsonrpc calls grab the given method's function info and runs reflect.Call
func JSONRPCHandler(w http.ResponseWriter, r *http.Request) {
if len(r.URL.Path) > 1 {
WriteRPCResponse(w, NewRPCResponse(nil, fmt.Sprintf("Invalid JSONRPC endpoint %s", r.URL.Path)))
return
}
2015-03-30 15:55:05 -07:00
b, _ := ioutil.ReadAll(r.Body)
2015-04-01 13:23:20 -07:00
var request RPCRequest
err := json.Unmarshal(b, &request)
if err != nil {
2015-04-01 04:58:33 -07:00
WriteRPCResponse(w, NewRPCResponse(nil, err.Error()))
return
}
2015-03-30 15:55:05 -07:00
2015-04-01 13:23:20 -07:00
funcInfo := funcMap[request.Method]
if funcInfo == nil {
WriteRPCResponse(w, NewRPCResponse(nil, "RPC method unknown: "+request.Method))
return
}
2015-04-01 13:23:20 -07:00
args, err := jsonParamsToArgs(funcInfo, request.Params)
2015-03-30 15:55:05 -07:00
if err != nil {
2015-04-01 04:58:33 -07:00
WriteRPCResponse(w, NewRPCResponse(nil, err.Error()))
2015-03-30 15:55:05 -07:00
return
}
returns := funcInfo.f.Call(args)
2015-04-01 13:23:20 -07:00
response, err := unreflectResponse(returns)
2015-03-30 15:55:05 -07:00
if err != nil {
2015-04-01 04:58:33 -07:00
WriteRPCResponse(w, NewRPCResponse(nil, err.Error()))
2015-03-30 15:55:05 -07:00
return
}
2015-04-01 04:58:33 -07:00
WriteRPCResponse(w, NewRPCResponse(response, ""))
2015-03-30 15:55:05 -07:00
}
// covert a list of interfaces to properly typed values
func jsonParamsToArgs(funcInfo *FuncWrapper, params []interface{}) ([]reflect.Value, error) {
values := make([]reflect.Value, len(params))
for i, p := range params {
ty := funcInfo.args[i]
v, err := _jsonObjectToArg(ty, p)
if err != nil {
return nil, err
}
values[i] = v
}
return values, nil
}
2015-03-30 15:55:05 -07:00
func _jsonObjectToArg(ty reflect.Type, object interface{}) (reflect.Value, error) {
var err error
v := reflect.New(ty)
binary.ReadJSONFromObject(v.Interface(), object, &err)
if err != nil {
return v, err
}
v = v.Elem()
return v, nil
}
2015-03-30 15:55:05 -07:00
// rpc.json
//-----------------------------------------------------------------------------
// rpc.http
// convert from a function name to the http handler
func toHTTPHandler(funcInfo *FuncWrapper) func(http.ResponseWriter, *http.Request) {
2015-03-30 15:55:05 -07:00
return func(w http.ResponseWriter, r *http.Request) {
args, err := httpParamsToArgs(funcInfo, r)
if err != nil {
2015-04-01 04:58:33 -07:00
WriteRPCResponse(w, NewRPCResponse(nil, err.Error()))
2015-03-30 15:55:05 -07:00
return
}
returns := funcInfo.f.Call(args)
2015-04-01 13:23:20 -07:00
response, err := unreflectResponse(returns)
2015-03-30 15:55:05 -07:00
if err != nil {
2015-04-01 04:58:33 -07:00
WriteRPCResponse(w, NewRPCResponse(nil, err.Error()))
2015-03-30 15:55:05 -07:00
return
}
2015-04-01 04:58:33 -07:00
WriteRPCResponse(w, NewRPCResponse(response, ""))
2015-03-30 15:55:05 -07:00
}
}
// Covert an http query to a list of properly typed values.
// To be properly decoded the arg must be a concrete type from tendermint (if its an interface).
func httpParamsToArgs(funcInfo *FuncWrapper, r *http.Request) ([]reflect.Value, error) {
argTypes := funcInfo.args
argNames := funcInfo.argNames
var err error
values := make([]reflect.Value, len(argNames))
for i, name := range argNames {
ty := argTypes[i]
arg := GetParam(r, name)
2015-03-30 15:55:05 -07:00
values[i], err = _jsonStringToArg(ty, arg)
if err != nil {
return nil, err
}
}
return values, nil
}
2015-03-30 15:55:05 -07:00
func _jsonStringToArg(ty reflect.Type, arg string) (reflect.Value, error) {
var err error
v := reflect.New(ty)
binary.ReadJSON(v.Interface(), []byte(arg), &err)
if err != nil {
return v, err
}
2015-03-30 15:55:05 -07:00
v = v.Elem()
return v, nil
}
2015-03-30 15:55:05 -07:00
// rpc.http
//-----------------------------------------------------------------------------
2015-04-02 14:30:30 -07:00
// rpc.websocket
// for requests coming in
type WsRequest struct {
Type string // subscribe or unsubscribe
Event string
}
// for responses going out
type WsResponse struct {
Event string
Data interface{}
Error string
}
// a single websocket connection
// contains the listeners id
type Connection struct {
id string
wsCon *websocket.Conn
writeChan chan WsResponse
quitChan chan struct{}
failedSends uint
}
// new websocket connection wrapper
func NewConnection(con *websocket.Conn) *Connection {
return &Connection{
id: con.RemoteAddr().String(),
wsCon: con,
writeChan: make(chan WsResponse, WriteChanBuffer), // buffered. we keep track when its full
}
}
// close the connection
func (c *Connection) Close() {
c.wsCon.Close()
close(c.writeChan)
close(c.quitChan)
}
2015-04-02 14:30:30 -07:00
// main manager for all websocket connections
// holds the event switch
type WebsocketManager struct {
ew *events.EventSwitch
cons map[string]*Connection
}
func NewWebsocketManager(ew *events.EventSwitch) *WebsocketManager {
return &WebsocketManager{
ew: ew,
cons: make(map[string]*Connection),
}
}
func (w *WebsocketManager) eventsHandler(con *websocket.Conn) {
// register connection
c := NewConnection(con)
w.cons[con.RemoteAddr().String()] = c
// read subscriptions/unsubscriptions to events
go w.read(c)
// write responses
go w.write(c)
}
const (
WsConnectionReaperSeconds = 5
MaxFailedSendsSeconds = 10
WriteChanBuffer = 10
)
// read from the socket and subscribe to or unsubscribe from events
func (w *WebsocketManager) read(con *Connection) {
reaper := time.Tick(time.Second * WsConnectionReaperSeconds)
for {
select {
case <-reaper:
if con.failedSends > MaxFailedSendsSeconds {
// sending has failed too many times.
// kill the connection
con.quitChan <- struct{}{}
}
default:
var in []byte
if err := websocket.Message.Receive(con.wsCon, &in); err != nil {
// an error reading the connection,
// so kill the connection
con.quitChan <- struct{}{}
}
var req WsRequest
err := json.Unmarshal(in, &req)
if err != nil {
errStr := fmt.Sprintf("Error unmarshaling data: %s", err.Error())
con.writeChan <- WsResponse{Error: errStr}
}
switch req.Type {
case "subscribe":
w.ew.AddListenerForEvent(con.id, req.Event, func(msg interface{}) {
resp := WsResponse{
Event: req.Event,
Data: msg,
}
select {
case con.writeChan <- resp:
// yay
con.failedSends = 0
default:
// channel is full
// if this happens too many times,
// close connection
con.failedSends += 1
}
})
case "unsubscribe":
if req.Event != "" {
w.ew.RemoveListenerForEvent(req.Event, con.id)
} else {
w.ew.RemoveListener(con.id)
}
default:
con.writeChan <- WsResponse{Error: "Unknown request type: " + req.Type}
}
}
}
}
// receives on a write channel and writes out to the socket
func (w *WebsocketManager) write(con *Connection) {
n, err := new(int64), new(error)
for {
select {
case msg := <-con.writeChan:
buf := new(bytes.Buffer)
binary.WriteJSON(msg, buf, n, err)
if *err != nil {
log.Error("Failed to write JSON WsResponse", "error", err)
} else {
websocket.Message.Send(con.wsCon, buf.Bytes())
}
case <-con.quitChan:
w.closeConn(con)
2015-04-02 14:30:30 -07:00
return
}
}
}
// close a connection and delete from manager
func (w *WebsocketManager) closeConn(con *Connection) {
con.Close()
delete(w.cons, con.id)
2015-04-02 14:30:30 -07:00
}
// rpc.websocket
//-----------------------------------------------------------------------------
2015-03-30 15:55:05 -07:00
// returns is Response struct and error. If error is not nil, return it
2015-04-01 13:23:20 -07:00
func unreflectResponse(returns []reflect.Value) (interface{}, error) {
errV := returns[1]
if errV.Interface() != nil {
return nil, fmt.Errorf("%v", errV.Interface())
}
return returns[0].Interface(), nil
}