Fixed RPC tests. Changed some names.
This commit is contained in:
parent
25ab73a51a
commit
bfb61fb539
|
@ -2,13 +2,14 @@ package rpc
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/tendermint/tendermint2/binary"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"reflect"
|
||||
// Uncomment to use go:generate
|
||||
// _ "github.com/ebuchman/go-rpc-gen"
|
||||
)
|
||||
|
||||
type Response struct {
|
||||
|
@ -17,7 +18,7 @@ type Response struct {
|
|||
Error string
|
||||
}
|
||||
|
||||
//go:generate rpc-gen -interface Client -pkg core -type *ClientHTTP,*ClientJSON -exclude pipe.go -out-pkg rpc
|
||||
//go:generate go-rpc-gen -interface Client -pkg core -type *ClientHTTP,*ClientJSON -exclude pipe.go -out-pkg rpc
|
||||
|
||||
type ClientJSON struct {
|
||||
addr string
|
||||
|
@ -98,11 +99,8 @@ func (c *ClientHTTP) RequestResponse(method string, values url.Values) (*Respons
|
|||
return response, nil
|
||||
}
|
||||
|
||||
func (c *ClientJSON) requestResponse(s JSONRPC) ([]byte, error) {
|
||||
b, err := json.Marshal(s)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
func (c *ClientJSON) RequestResponse(s RPCRequest) (b []byte, err error) {
|
||||
b = binary.JSONBytes(s)
|
||||
buf := bytes.NewBuffer(b)
|
||||
resp, err := http.Post(c.addr, "text/json", buf)
|
||||
if err != nil {
|
||||
|
@ -175,17 +173,13 @@ fmt
|
|||
// Template functions to be filled in
|
||||
|
||||
/*rpc-gen:template:*ClientJSON func (c *ClientJSON) {{name}}({{args.def}}) ({{response}}) {
|
||||
params, err := binaryWriter({{args.ident}})
|
||||
if err != nil{
|
||||
return nil, err
|
||||
}
|
||||
s := JSONRPC{
|
||||
request := RPCRequest{
|
||||
JSONRPC: "2.0",
|
||||
Method: {{lowername}},
|
||||
Params: params,
|
||||
Params: []interface{}{ {{args.ident}} },
|
||||
Id: 0,
|
||||
}
|
||||
body, err := c.requestResponse(s)
|
||||
body, err := c.RequestResponse(request)
|
||||
if err != nil{
|
||||
return nil, err
|
||||
}
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -84,31 +84,24 @@ func funcReturnTypes(f interface{}) []reflect.Type {
|
|||
//-----------------------------------------------------------------------------
|
||||
// rpc.json
|
||||
|
||||
type JSONRPC struct {
|
||||
JSONRPC string `json:"jsonrpc"`
|
||||
Method string `json:"method"`
|
||||
Params []interface{} `json:"params"`
|
||||
Id int `json:"id"`
|
||||
}
|
||||
|
||||
// jsonrpc calls grab the given method's function info and runs reflect.Call
|
||||
func JSONRPCHandler(w http.ResponseWriter, r *http.Request) {
|
||||
b, _ := ioutil.ReadAll(r.Body)
|
||||
var jrpc JSONRPC
|
||||
err := json.Unmarshal(b, &jrpc)
|
||||
var request RPCRequest
|
||||
err := json.Unmarshal(b, &request)
|
||||
if err != nil {
|
||||
WriteRPCResponse(w, NewRPCResponse(nil, err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
funcInfo := funcMap[jrpc.Method]
|
||||
args, err := jsonParamsToArgs(funcInfo, jrpc.Params)
|
||||
funcInfo := funcMap[request.Method]
|
||||
args, err := jsonParamsToArgs(funcInfo, request.Params)
|
||||
if err != nil {
|
||||
WriteRPCResponse(w, NewRPCResponse(nil, err.Error()))
|
||||
return
|
||||
}
|
||||
returns := funcInfo.f.Call(args)
|
||||
response, err := returnsToResponse(returns)
|
||||
response, err := unreflectResponse(returns)
|
||||
if err != nil {
|
||||
WriteRPCResponse(w, NewRPCResponse(nil, err.Error()))
|
||||
return
|
||||
|
@ -154,7 +147,7 @@ func toHttpHandler(funcInfo *FuncWrapper) func(http.ResponseWriter, *http.Reques
|
|||
return
|
||||
}
|
||||
returns := funcInfo.f.Call(args)
|
||||
response, err := returnsToResponse(returns)
|
||||
response, err := unreflectResponse(returns)
|
||||
if err != nil {
|
||||
WriteRPCResponse(w, NewRPCResponse(nil, err.Error()))
|
||||
return
|
||||
|
@ -197,7 +190,7 @@ func _jsonStringToArg(ty reflect.Type, arg string) (reflect.Value, error) {
|
|||
//-----------------------------------------------------------------------------
|
||||
|
||||
// returns is Response struct and error. If error is not nil, return it
|
||||
func returnsToResponse(returns []reflect.Value) (interface{}, error) {
|
||||
func unreflectResponse(returns []reflect.Value) (interface{}, error) {
|
||||
errV := returns[1]
|
||||
if errV.Interface() != nil {
|
||||
return nil, fmt.Errorf("%v", errV.Interface())
|
||||
|
|
|
@ -23,25 +23,6 @@ func StartHTTPServer() {
|
|||
}()
|
||||
}
|
||||
|
||||
type RPCResponse struct {
|
||||
Result interface{} `json:"result"`
|
||||
Error string `json:"error"`
|
||||
Id string `json:"id"`
|
||||
JSONRPC string `json:"jsonrpc"`
|
||||
}
|
||||
|
||||
func NewRPCResponse(res interface{}, err string) RPCResponse {
|
||||
if res == nil {
|
||||
res = struct{}{}
|
||||
}
|
||||
return RPCResponse{
|
||||
Result: res,
|
||||
Error: err,
|
||||
Id: "",
|
||||
JSONRPC: "2.0",
|
||||
}
|
||||
}
|
||||
|
||||
func WriteRPCResponse(w http.ResponseWriter, res RPCResponse) {
|
||||
buf, n, err := new(bytes.Buffer), new(int64), new(error)
|
||||
binary.WriteJSON(res, buf, n, err)
|
||||
|
|
|
@ -1 +1 @@
|
|||
{"Address":"D7DFF9806078899C8DA3FE3633CC0BF3C6C2B1BB","PubKey":[1,"2239C21C81EA7173A6C489145490C015E05D4B97448933B708A7EC5B7B4921E3"],"PrivKey":[1,"FDE3BD94CB327D19464027BA668194C5EFA46AE83E8419D7542CFF41F00C81972239C21C81EA7173A6C489145490C015E05D4B97448933B708A7EC5B7B4921E3"]}
|
||||
{"Address":"D7DFF9806078899C8DA3FE3633CC0BF3C6C2B1BB","PubKey":[1,"2239C21C81EA7173A6C489145490C015E05D4B97448933B708A7EC5B7B4921E3"],"PrivKey":[1,"FDE3BD94CB327D19464027BA668194C5EFA46AE83E8419D7542CFF41F00C81972239C21C81EA7173A6C489145490C015E05D4B97448933B708A7EC5B7B4921E3"],"LastHeight":2,"LastRound":0,"LastStep":2}
|
|
@ -30,7 +30,7 @@ func TestHTTPStatus(t *testing.T) {
|
|||
Result core.ResponseStatus `json:"result"`
|
||||
Error string `json:"error"`
|
||||
Id string `json:"id"`
|
||||
JSONRPC int `json:"jsonrpc"`
|
||||
JSONRPC string `json:"jsonrpc"`
|
||||
}
|
||||
binary.ReadJSON(&response, body, &err)
|
||||
if err != nil {
|
||||
|
@ -58,7 +58,7 @@ func TestHTTPGenPriv(t *testing.T) {
|
|||
Result core.ResponseGenPrivAccount `json:"result"`
|
||||
Error string `json:"error"`
|
||||
Id string `json:"id"`
|
||||
JSONRPC int `json:"jsonrpc"`
|
||||
JSONRPC string `json:"jsonrpc"`
|
||||
}
|
||||
binary.ReadJSON(&response, body, &err)
|
||||
if err != nil {
|
||||
|
|
|
@ -18,7 +18,7 @@ import (
|
|||
)
|
||||
|
||||
func TestJSONStatus(t *testing.T) {
|
||||
s := rpc.JSONRPC{
|
||||
s := rpc.RPCRequest{
|
||||
JSONRPC: "2.0",
|
||||
Method: "status",
|
||||
Params: []interface{}{},
|
||||
|
@ -43,7 +43,7 @@ func TestJSONStatus(t *testing.T) {
|
|||
Result core.ResponseStatus `json:"result"`
|
||||
Error string `json:"error"`
|
||||
Id string `json:"id"`
|
||||
JSONRPC int `json:"jsonrpc"`
|
||||
JSONRPC string `json:"jsonrpc"`
|
||||
}
|
||||
binary.ReadJSON(&response, body, &err)
|
||||
if err != nil {
|
||||
|
@ -56,7 +56,7 @@ func TestJSONStatus(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestJSONGenPriv(t *testing.T) {
|
||||
s := rpc.JSONRPC{
|
||||
s := rpc.RPCRequest{
|
||||
JSONRPC: "2.0",
|
||||
Method: "unsafe/gen_priv_account",
|
||||
Params: []interface{}{},
|
||||
|
@ -83,7 +83,7 @@ func TestJSONGenPriv(t *testing.T) {
|
|||
Result core.ResponseGenPrivAccount `json:"result"`
|
||||
Error string `json:"error"`
|
||||
Id string `json:"id"`
|
||||
JSONRPC int `json:"jsonrpc"`
|
||||
JSONRPC string `json:"jsonrpc"`
|
||||
}
|
||||
binary.ReadJSON(&response, body, &err)
|
||||
if err != nil {
|
||||
|
@ -146,7 +146,7 @@ func TestJSONBroadcastTx(t *testing.T) {
|
|||
Result core.ResponseBroadcastTx `json:"result"`
|
||||
Error string `json:"error"`
|
||||
Id string `json:"id"`
|
||||
JSONRPC int `json:"jsonrpc"`
|
||||
JSONRPC string `json:"jsonrpc"`
|
||||
}
|
||||
requestResponse(t, "broadcast_tx", url.Values{"tx": {string(b)}}, &response)
|
||||
if response.Error != "" {
|
||||
|
|
|
@ -123,7 +123,7 @@ func getAccount(t *testing.T, typ string, addr []byte) *account.Account {
|
|||
Result core.ResponseGetAccount `json:"result"`
|
||||
Error string `json:"error"`
|
||||
Id string `json:"id"`
|
||||
JSONRPC int `json:"jsonrpc"`
|
||||
JSONRPC string `json:"jsonrpc"`
|
||||
}
|
||||
binary.ReadJSON(&response, body, &err)
|
||||
if err != nil {
|
||||
|
@ -235,7 +235,7 @@ func signTx(t *testing.T, typ string, fromAddr, toAddr, data []byte, key [64]byt
|
|||
Result core.ResponseSignTx `json:"result"`
|
||||
Error string `json:"error"`
|
||||
Id string `json:"id"`
|
||||
JSONRPC int `json:"jsonrpc"`
|
||||
JSONRPC string `json:"jsonrpc"`
|
||||
}
|
||||
requestResponse(t, "unsafe/sign_tx", url.Values{"tx": {string(b)}, "privAccounts": {string(w.Bytes())}}, &response)
|
||||
if response.Error != "" {
|
||||
|
@ -260,7 +260,7 @@ func broadcastTx(t *testing.T, typ string, fromAddr, toAddr, data []byte, key [6
|
|||
Result core.ResponseBroadcastTx `json:"result"`
|
||||
Error string `json:"error"`
|
||||
Id string `json:"id"`
|
||||
JSONRPC int `json:"jsonrpc"`
|
||||
JSONRPC string `json:"jsonrpc"`
|
||||
}
|
||||
requestResponse(t, "broadcast_tx", url.Values{"tx": {string(b)}}, &response)
|
||||
if response.Error != "" {
|
||||
|
@ -275,7 +275,7 @@ func dumpStorage(t *testing.T, addr []byte) core.ResponseDumpStorage {
|
|||
Result core.ResponseDumpStorage `json:"result"`
|
||||
Error string `json:"error"`
|
||||
Id string `json:"id"`
|
||||
JSONRPC int `json:"jsonrpc"`
|
||||
JSONRPC string `json:"jsonrpc"`
|
||||
}
|
||||
requestResponse(t, "dump_storage", url.Values{"address": {addrString}}, &response)
|
||||
if response.Error != "" {
|
||||
|
@ -291,7 +291,7 @@ func getStorage(t *testing.T, addr, slot []byte) []byte {
|
|||
Result core.ResponseGetStorage `json:"result"`
|
||||
Error string `json:"error"`
|
||||
Id string `json:"id"`
|
||||
JSONRPC int `json:"jsonrpc"`
|
||||
JSONRPC string `json:"jsonrpc"`
|
||||
}
|
||||
requestResponse(t, "get_storage", url.Values{"address": {addrString}, "storage": {slotString}}, &response)
|
||||
if response.Error != "" {
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
package rpc
|
||||
|
||||
type RPCRequest struct {
|
||||
JSONRPC string `json:"jsonrpc"`
|
||||
Method string `json:"method"`
|
||||
Params []interface{} `json:"params"`
|
||||
Id int `json:"id"`
|
||||
}
|
||||
|
||||
type RPCResponse struct {
|
||||
Result interface{} `json:"result"`
|
||||
Error string `json:"error"`
|
||||
Id string `json:"id"`
|
||||
JSONRPC string `json:"jsonrpc"`
|
||||
}
|
||||
|
||||
func NewRPCResponse(res interface{}, err string) RPCResponse {
|
||||
if res == nil {
|
||||
res = struct{}{}
|
||||
}
|
||||
return RPCResponse{
|
||||
Result: res,
|
||||
Error: err,
|
||||
Id: "",
|
||||
JSONRPC: "2.0",
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue