Fixed RPC tests. Changed some names.

This commit is contained in:
Jae Kwon
2015-04-01 13:23:20 -07:00
parent 25ab73a51a
commit bfb61fb539
9 changed files with 475 additions and 534 deletions

View File

@ -2,13 +2,14 @@ package rpc
import ( import (
"bytes" "bytes"
"encoding/json"
"fmt" "fmt"
"github.com/tendermint/tendermint2/binary" "github.com/tendermint/tendermint2/binary"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
"reflect" "reflect"
// Uncomment to use go:generate
// _ "github.com/ebuchman/go-rpc-gen"
) )
type Response struct { type Response struct {
@ -17,7 +18,7 @@ type Response struct {
Error string 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 { type ClientJSON struct {
addr string addr string
@ -98,11 +99,8 @@ func (c *ClientHTTP) RequestResponse(method string, values url.Values) (*Respons
return response, nil return response, nil
} }
func (c *ClientJSON) requestResponse(s JSONRPC) ([]byte, error) { func (c *ClientJSON) RequestResponse(s RPCRequest) (b []byte, err error) {
b, err := json.Marshal(s) b = binary.JSONBytes(s)
if err != nil {
return nil, err
}
buf := bytes.NewBuffer(b) buf := bytes.NewBuffer(b)
resp, err := http.Post(c.addr, "text/json", buf) resp, err := http.Post(c.addr, "text/json", buf)
if err != nil { if err != nil {
@ -175,17 +173,13 @@ fmt
// Template functions to be filled in // Template functions to be filled in
/*rpc-gen:template:*ClientJSON func (c *ClientJSON) {{name}}({{args.def}}) ({{response}}) { /*rpc-gen:template:*ClientJSON func (c *ClientJSON) {{name}}({{args.def}}) ({{response}}) {
params, err := binaryWriter({{args.ident}}) request := RPCRequest{
if err != nil{
return nil, err
}
s := JSONRPC{
JSONRPC: "2.0", JSONRPC: "2.0",
Method: {{lowername}}, Method: {{lowername}},
Params: params, Params: []interface{}{ {{args.ident}} },
Id: 0, Id: 0,
} }
body, err := c.requestResponse(s) body, err := c.RequestResponse(request)
if err != nil{ if err != nil{
return nil, err return nil, err
} }

View File

@ -1,5 +1,3 @@
// File generated by github.com/ebuchman/rpc-gen
package rpc package rpc
import ( import (
@ -13,109 +11,19 @@ import (
) )
type Client interface { type Client interface {
Status() (*core.ResponseStatus, error)
DumpStorage(addr []byte) (*core.ResponseDumpStorage, error)
ListAccounts() (*core.ResponseListAccounts, error)
BlockchainInfo(minHeight uint) (*core.ResponseBlockchainInfo, error) BlockchainInfo(minHeight uint) (*core.ResponseBlockchainInfo, error)
GetBlock(height uint) (*core.ResponseGetBlock, error)
BroadcastTx(tx types.Tx) (*core.ResponseBroadcastTx, error) BroadcastTx(tx types.Tx) (*core.ResponseBroadcastTx, error)
NetInfo() (*core.ResponseNetInfo, error)
Call(address []byte) (*core.ResponseCall, error) Call(address []byte) (*core.ResponseCall, error)
SignTx(tx types.Tx, privAccounts []*account.PrivAccount) (*core.ResponseSignTx, error) DumpStorage(addr []byte) (*core.ResponseDumpStorage, error)
ListValidators() (*core.ResponseListValidators, error)
GenPrivAccount() (*core.ResponseGenPrivAccount, error) GenPrivAccount() (*core.ResponseGenPrivAccount, error)
GetAccount(address []byte) (*core.ResponseGetAccount, error) GetAccount(address []byte) (*core.ResponseGetAccount, error)
GetBlock(height uint) (*core.ResponseGetBlock, error)
GetStorage(address []byte) (*core.ResponseGetStorage, error) GetStorage(address []byte) (*core.ResponseGetStorage, error)
} ListAccounts() (*core.ResponseListAccounts, error)
ListValidators() (*core.ResponseListValidators, error)
func (c *ClientHTTP) Status() (*core.ResponseStatus, error) { NetInfo() (*core.ResponseNetInfo, error)
values, err := argsToURLValues(nil, nil) SignTx(tx types.Tx, privAccounts []*account.PrivAccount) (*core.ResponseSignTx, error)
if err != nil { Status() (*core.ResponseStatus, error)
return nil, err
}
resp, err := http.PostForm(c.addr+"status", values)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var response struct {
Result *core.ResponseStatus `json:"result"`
Error string `json:"error"`
Id string `json:"id"`
JSONRPC string `json:"jsonrpc"`
}
binary.ReadJSON(&response, body, &err)
if err != nil {
return nil, err
}
if response.Error != "" {
return nil, fmt.Errorf(response.Error)
}
return response.Result, nil
}
func (c *ClientHTTP) DumpStorage(addr []byte) (*core.ResponseDumpStorage, error) {
values, err := argsToURLValues([]string{"addr"}, addr)
if err != nil {
return nil, err
}
resp, err := http.PostForm(c.addr+"dump_storage", values)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var response struct {
Result *core.ResponseDumpStorage `json:"result"`
Error string `json:"error"`
Id string `json:"id"`
JSONRPC string `json:"jsonrpc"`
}
binary.ReadJSON(&response, body, &err)
if err != nil {
return nil, err
}
if response.Error != "" {
return nil, fmt.Errorf(response.Error)
}
return response.Result, nil
}
func (c *ClientHTTP) ListAccounts() (*core.ResponseListAccounts, error) {
values, err := argsToURLValues(nil, nil)
if err != nil {
return nil, err
}
resp, err := http.PostForm(c.addr+"list_accounts", values)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var response struct {
Result *core.ResponseListAccounts `json:"result"`
Error string `json:"error"`
Id string `json:"id"`
JSONRPC string `json:"jsonrpc"`
}
binary.ReadJSON(&response, body, &err)
if err != nil {
return nil, err
}
if response.Error != "" {
return nil, fmt.Errorf(response.Error)
}
return response.Result, nil
} }
func (c *ClientHTTP) BlockchainInfo(minHeight uint) (*core.ResponseBlockchainInfo, error) { func (c *ClientHTTP) BlockchainInfo(minHeight uint) (*core.ResponseBlockchainInfo, error) {
@ -148,36 +56,6 @@ func (c *ClientHTTP) BlockchainInfo(minHeight uint) (*core.ResponseBlockchainInf
return response.Result, nil return response.Result, nil
} }
func (c *ClientHTTP) GetBlock(height uint) (*core.ResponseGetBlock, error) {
values, err := argsToURLValues([]string{"height"}, height)
if err != nil {
return nil, err
}
resp, err := http.PostForm(c.addr+"get_block", values)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var response struct {
Result *core.ResponseGetBlock `json:"result"`
Error string `json:"error"`
Id string `json:"id"`
JSONRPC string `json:"jsonrpc"`
}
binary.ReadJSON(&response, body, &err)
if err != nil {
return nil, err
}
if response.Error != "" {
return nil, fmt.Errorf(response.Error)
}
return response.Result, nil
}
func (c *ClientHTTP) BroadcastTx(tx types.Tx) (*core.ResponseBroadcastTx, error) { func (c *ClientHTTP) BroadcastTx(tx types.Tx) (*core.ResponseBroadcastTx, error) {
values, err := argsToURLValues([]string{"tx"}, tx) values, err := argsToURLValues([]string{"tx"}, tx)
if err != nil { if err != nil {
@ -208,36 +86,6 @@ func (c *ClientHTTP) BroadcastTx(tx types.Tx) (*core.ResponseBroadcastTx, error)
return response.Result, nil return response.Result, nil
} }
func (c *ClientHTTP) NetInfo() (*core.ResponseNetInfo, error) {
values, err := argsToURLValues(nil, nil)
if err != nil {
return nil, err
}
resp, err := http.PostForm(c.addr+"net_info", values)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var response struct {
Result *core.ResponseNetInfo `json:"result"`
Error string `json:"error"`
Id string `json:"id"`
JSONRPC string `json:"jsonrpc"`
}
binary.ReadJSON(&response, body, &err)
if err != nil {
return nil, err
}
if response.Error != "" {
return nil, fmt.Errorf(response.Error)
}
return response.Result, nil
}
func (c *ClientHTTP) Call(address []byte) (*core.ResponseCall, error) { func (c *ClientHTTP) Call(address []byte) (*core.ResponseCall, error) {
values, err := argsToURLValues([]string{"address"}, address) values, err := argsToURLValues([]string{"address"}, address)
if err != nil { if err != nil {
@ -268,12 +116,12 @@ func (c *ClientHTTP) Call(address []byte) (*core.ResponseCall, error) {
return response.Result, nil return response.Result, nil
} }
func (c *ClientHTTP) SignTx(tx types.Tx, privAccounts []*account.PrivAccount) (*core.ResponseSignTx, error) { func (c *ClientHTTP) DumpStorage(addr []byte) (*core.ResponseDumpStorage, error) {
values, err := argsToURLValues([]string{"tx", "privAccounts"}, tx, privAccounts) values, err := argsToURLValues([]string{"addr"}, addr)
if err != nil { if err != nil {
return nil, err return nil, err
} }
resp, err := http.PostForm(c.addr+"sign_tx", values) resp, err := http.PostForm(c.addr+"dump_storage", values)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -283,37 +131,7 @@ func (c *ClientHTTP) SignTx(tx types.Tx, privAccounts []*account.PrivAccount) (*
return nil, err return nil, err
} }
var response struct { var response struct {
Result *core.ResponseSignTx `json:"result"` Result *core.ResponseDumpStorage `json:"result"`
Error string `json:"error"`
Id string `json:"id"`
JSONRPC string `json:"jsonrpc"`
}
binary.ReadJSON(&response, body, &err)
if err != nil {
return nil, err
}
if response.Error != "" {
return nil, fmt.Errorf(response.Error)
}
return response.Result, nil
}
func (c *ClientHTTP) ListValidators() (*core.ResponseListValidators, error) {
values, err := argsToURLValues(nil, nil)
if err != nil {
return nil, err
}
resp, err := http.PostForm(c.addr+"list_validators", values)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var response struct {
Result *core.ResponseListValidators `json:"result"`
Error string `json:"error"` Error string `json:"error"`
Id string `json:"id"` Id string `json:"id"`
JSONRPC string `json:"jsonrpc"` JSONRPC string `json:"jsonrpc"`
@ -388,6 +206,36 @@ func (c *ClientHTTP) GetAccount(address []byte) (*core.ResponseGetAccount, error
return response.Result, nil return response.Result, nil
} }
func (c *ClientHTTP) GetBlock(height uint) (*core.ResponseGetBlock, error) {
values, err := argsToURLValues([]string{"height"}, height)
if err != nil {
return nil, err
}
resp, err := http.PostForm(c.addr+"get_block", values)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var response struct {
Result *core.ResponseGetBlock `json:"result"`
Error string `json:"error"`
Id string `json:"id"`
JSONRPC string `json:"jsonrpc"`
}
binary.ReadJSON(&response, body, &err)
if err != nil {
return nil, err
}
if response.Error != "" {
return nil, fmt.Errorf(response.Error)
}
return response.Result, nil
}
func (c *ClientHTTP) GetStorage(address []byte) (*core.ResponseGetStorage, error) { func (c *ClientHTTP) GetStorage(address []byte) (*core.ResponseGetStorage, error) {
values, err := argsToURLValues([]string{"address"}, address) values, err := argsToURLValues([]string{"address"}, address)
if err != nil { if err != nil {
@ -418,80 +266,17 @@ func (c *ClientHTTP) GetStorage(address []byte) (*core.ResponseGetStorage, error
return response.Result, nil return response.Result, nil
} }
func (c *ClientJSON) Status() (*core.ResponseStatus, error) { func (c *ClientHTTP) ListAccounts() (*core.ResponseListAccounts, error) {
params, err := binaryWriter(nil) values, err := argsToURLValues(nil, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
s := JSONRPC{ resp, err := http.PostForm(c.addr+"list_accounts", values)
JSONRPC: "2.0",
Method: "status",
Params: params,
Id: 0,
}
body, err := c.requestResponse(s)
if err != nil { if err != nil {
return nil, err return nil, err
} }
var response struct { defer resp.Body.Close()
Result *core.ResponseStatus `json:"result"` body, err := ioutil.ReadAll(resp.Body)
Error string `json:"error"`
Id string `json:"id"`
JSONRPC string `json:"jsonrpc"`
}
binary.ReadJSON(&response, body, &err)
if err != nil {
return nil, err
}
if response.Error != "" {
return nil, fmt.Errorf(response.Error)
}
return response.Result, nil
}
func (c *ClientJSON) DumpStorage(addr []byte) (*core.ResponseDumpStorage, error) {
params, err := binaryWriter(addr)
if err != nil {
return nil, err
}
s := JSONRPC{
JSONRPC: "2.0",
Method: "dump_storage",
Params: params,
Id: 0,
}
body, err := c.requestResponse(s)
if err != nil {
return nil, err
}
var response struct {
Result *core.ResponseDumpStorage `json:"result"`
Error string `json:"error"`
Id string `json:"id"`
JSONRPC string `json:"jsonrpc"`
}
binary.ReadJSON(&response, body, &err)
if err != nil {
return nil, err
}
if response.Error != "" {
return nil, fmt.Errorf(response.Error)
}
return response.Result, nil
}
func (c *ClientJSON) ListAccounts() (*core.ResponseListAccounts, error) {
params, err := binaryWriter(nil)
if err != nil {
return nil, err
}
s := JSONRPC{
JSONRPC: "2.0",
Method: "list_accounts",
Params: params,
Id: 0,
}
body, err := c.requestResponse(s)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -511,204 +296,17 @@ func (c *ClientJSON) ListAccounts() (*core.ResponseListAccounts, error) {
return response.Result, nil return response.Result, nil
} }
func (c *ClientJSON) BlockchainInfo(minHeight uint) (*core.ResponseBlockchainInfo, error) { func (c *ClientHTTP) ListValidators() (*core.ResponseListValidators, error) {
params, err := binaryWriter(minHeight) values, err := argsToURLValues(nil, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
s := JSONRPC{ resp, err := http.PostForm(c.addr+"list_validators", values)
JSONRPC: "2.0",
Method: "blockchain_info",
Params: params,
Id: 0,
}
body, err := c.requestResponse(s)
if err != nil { if err != nil {
return nil, err return nil, err
} }
var response struct { defer resp.Body.Close()
Result *core.ResponseBlockchainInfo `json:"result"` body, err := ioutil.ReadAll(resp.Body)
Error string `json:"error"`
Id string `json:"id"`
JSONRPC string `json:"jsonrpc"`
}
binary.ReadJSON(&response, body, &err)
if err != nil {
return nil, err
}
if response.Error != "" {
return nil, fmt.Errorf(response.Error)
}
return response.Result, nil
}
func (c *ClientJSON) GetBlock(height uint) (*core.ResponseGetBlock, error) {
params, err := binaryWriter(height)
if err != nil {
return nil, err
}
s := JSONRPC{
JSONRPC: "2.0",
Method: "get_block",
Params: params,
Id: 0,
}
body, err := c.requestResponse(s)
if err != nil {
return nil, err
}
var response struct {
Result *core.ResponseGetBlock `json:"result"`
Error string `json:"error"`
Id string `json:"id"`
JSONRPC string `json:"jsonrpc"`
}
binary.ReadJSON(&response, body, &err)
if err != nil {
return nil, err
}
if response.Error != "" {
return nil, fmt.Errorf(response.Error)
}
return response.Result, nil
}
func (c *ClientJSON) BroadcastTx(tx types.Tx) (*core.ResponseBroadcastTx, error) {
params, err := binaryWriter(tx)
if err != nil {
return nil, err
}
s := JSONRPC{
JSONRPC: "2.0",
Method: "broadcast_tx",
Params: params,
Id: 0,
}
body, err := c.requestResponse(s)
if err != nil {
return nil, err
}
var response struct {
Result *core.ResponseBroadcastTx `json:"result"`
Error string `json:"error"`
Id string `json:"id"`
JSONRPC string `json:"jsonrpc"`
}
binary.ReadJSON(&response, body, &err)
if err != nil {
return nil, err
}
if response.Error != "" {
return nil, fmt.Errorf(response.Error)
}
return response.Result, nil
}
func (c *ClientJSON) NetInfo() (*core.ResponseNetInfo, error) {
params, err := binaryWriter(nil)
if err != nil {
return nil, err
}
s := JSONRPC{
JSONRPC: "2.0",
Method: "net_info",
Params: params,
Id: 0,
}
body, err := c.requestResponse(s)
if err != nil {
return nil, err
}
var response struct {
Result *core.ResponseNetInfo `json:"result"`
Error string `json:"error"`
Id string `json:"id"`
JSONRPC string `json:"jsonrpc"`
}
binary.ReadJSON(&response, body, &err)
if err != nil {
return nil, err
}
if response.Error != "" {
return nil, fmt.Errorf(response.Error)
}
return response.Result, nil
}
func (c *ClientJSON) Call(address []byte) (*core.ResponseCall, error) {
params, err := binaryWriter(address)
if err != nil {
return nil, err
}
s := JSONRPC{
JSONRPC: "2.0",
Method: "call",
Params: params,
Id: 0,
}
body, err := c.requestResponse(s)
if err != nil {
return nil, err
}
var response struct {
Result *core.ResponseCall `json:"result"`
Error string `json:"error"`
Id string `json:"id"`
JSONRPC string `json:"jsonrpc"`
}
binary.ReadJSON(&response, body, &err)
if err != nil {
return nil, err
}
if response.Error != "" {
return nil, fmt.Errorf(response.Error)
}
return response.Result, nil
}
func (c *ClientJSON) SignTx(tx types.Tx, privAccounts []*account.PrivAccount) (*core.ResponseSignTx, error) {
params, err := binaryWriter(tx, privAccounts)
if err != nil {
return nil, err
}
s := JSONRPC{
JSONRPC: "2.0",
Method: "sign_tx",
Params: params,
Id: 0,
}
body, err := c.requestResponse(s)
if err != nil {
return nil, err
}
var response struct {
Result *core.ResponseSignTx `json:"result"`
Error string `json:"error"`
Id string `json:"id"`
JSONRPC string `json:"jsonrpc"`
}
binary.ReadJSON(&response, body, &err)
if err != nil {
return nil, err
}
if response.Error != "" {
return nil, fmt.Errorf(response.Error)
}
return response.Result, nil
}
func (c *ClientJSON) ListValidators() (*core.ResponseListValidators, error) {
params, err := binaryWriter(nil)
if err != nil {
return nil, err
}
s := JSONRPC{
JSONRPC: "2.0",
Method: "list_validators",
Params: params,
Id: 0,
}
body, err := c.requestResponse(s)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -728,18 +326,212 @@ func (c *ClientJSON) ListValidators() (*core.ResponseListValidators, error) {
return response.Result, nil return response.Result, nil
} }
func (c *ClientJSON) GenPrivAccount() (*core.ResponseGenPrivAccount, error) { func (c *ClientHTTP) NetInfo() (*core.ResponseNetInfo, error) {
params, err := binaryWriter(nil) values, err := argsToURLValues(nil, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
s := JSONRPC{ resp, err := http.PostForm(c.addr+"net_info", values)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var response struct {
Result *core.ResponseNetInfo `json:"result"`
Error string `json:"error"`
Id string `json:"id"`
JSONRPC string `json:"jsonrpc"`
}
binary.ReadJSON(&response, body, &err)
if err != nil {
return nil, err
}
if response.Error != "" {
return nil, fmt.Errorf(response.Error)
}
return response.Result, nil
}
func (c *ClientHTTP) SignTx(tx types.Tx, privAccounts []*account.PrivAccount) (*core.ResponseSignTx, error) {
values, err := argsToURLValues([]string{"tx", "privAccounts"}, tx, privAccounts)
if err != nil {
return nil, err
}
resp, err := http.PostForm(c.addr+"sign_tx", values)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var response struct {
Result *core.ResponseSignTx `json:"result"`
Error string `json:"error"`
Id string `json:"id"`
JSONRPC string `json:"jsonrpc"`
}
binary.ReadJSON(&response, body, &err)
if err != nil {
return nil, err
}
if response.Error != "" {
return nil, fmt.Errorf(response.Error)
}
return response.Result, nil
}
func (c *ClientHTTP) Status() (*core.ResponseStatus, error) {
values, err := argsToURLValues(nil, nil)
if err != nil {
return nil, err
}
resp, err := http.PostForm(c.addr+"status", values)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var response struct {
Result *core.ResponseStatus `json:"result"`
Error string `json:"error"`
Id string `json:"id"`
JSONRPC string `json:"jsonrpc"`
}
binary.ReadJSON(&response, body, &err)
if err != nil {
return nil, err
}
if response.Error != "" {
return nil, fmt.Errorf(response.Error)
}
return response.Result, nil
}
func (c *ClientJSON) BlockchainInfo(minHeight uint) (*core.ResponseBlockchainInfo, error) {
request := RPCRequest{
JSONRPC: "2.0", JSONRPC: "2.0",
Method: "gen_priv_account", Method: "blockchain_info",
Params: params, Params: []interface{}{minHeight},
Id: 0, Id: 0,
} }
body, err := c.requestResponse(s) body, err := c.RequestResponse(request)
if err != nil {
return nil, err
}
var response struct {
Result *core.ResponseBlockchainInfo `json:"result"`
Error string `json:"error"`
Id string `json:"id"`
JSONRPC string `json:"jsonrpc"`
}
binary.ReadJSON(&response, body, &err)
if err != nil {
return nil, err
}
if response.Error != "" {
return nil, fmt.Errorf(response.Error)
}
return response.Result, nil
}
func (c *ClientJSON) BroadcastTx(tx types.Tx) (*core.ResponseBroadcastTx, error) {
request := RPCRequest{
JSONRPC: "2.0",
Method: "broadcast_tx",
Params: []interface{}{tx},
Id: 0,
}
body, err := c.RequestResponse(request)
if err != nil {
return nil, err
}
var response struct {
Result *core.ResponseBroadcastTx `json:"result"`
Error string `json:"error"`
Id string `json:"id"`
JSONRPC string `json:"jsonrpc"`
}
binary.ReadJSON(&response, body, &err)
if err != nil {
return nil, err
}
if response.Error != "" {
return nil, fmt.Errorf(response.Error)
}
return response.Result, nil
}
func (c *ClientJSON) Call(address []byte) (*core.ResponseCall, error) {
request := RPCRequest{
JSONRPC: "2.0",
Method: "call",
Params: []interface{}{address},
Id: 0,
}
body, err := c.RequestResponse(request)
if err != nil {
return nil, err
}
var response struct {
Result *core.ResponseCall `json:"result"`
Error string `json:"error"`
Id string `json:"id"`
JSONRPC string `json:"jsonrpc"`
}
binary.ReadJSON(&response, body, &err)
if err != nil {
return nil, err
}
if response.Error != "" {
return nil, fmt.Errorf(response.Error)
}
return response.Result, nil
}
func (c *ClientJSON) DumpStorage(addr []byte) (*core.ResponseDumpStorage, error) {
request := RPCRequest{
JSONRPC: "2.0",
Method: "dump_storage",
Params: []interface{}{addr},
Id: 0,
}
body, err := c.RequestResponse(request)
if err != nil {
return nil, err
}
var response struct {
Result *core.ResponseDumpStorage `json:"result"`
Error string `json:"error"`
Id string `json:"id"`
JSONRPC string `json:"jsonrpc"`
}
binary.ReadJSON(&response, body, &err)
if err != nil {
return nil, err
}
if response.Error != "" {
return nil, fmt.Errorf(response.Error)
}
return response.Result, nil
}
func (c *ClientJSON) GenPrivAccount() (*core.ResponseGenPrivAccount, error) {
request := RPCRequest{
JSONRPC: "2.0",
Method: "gen_priv_account",
Params: []interface{}{nil},
Id: 0,
}
body, err := c.RequestResponse(request)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -760,17 +552,13 @@ func (c *ClientJSON) GenPrivAccount() (*core.ResponseGenPrivAccount, error) {
} }
func (c *ClientJSON) GetAccount(address []byte) (*core.ResponseGetAccount, error) { func (c *ClientJSON) GetAccount(address []byte) (*core.ResponseGetAccount, error) {
params, err := binaryWriter(address) request := RPCRequest{
if err != nil {
return nil, err
}
s := JSONRPC{
JSONRPC: "2.0", JSONRPC: "2.0",
Method: "get_account", Method: "get_account",
Params: params, Params: []interface{}{address},
Id: 0, Id: 0,
} }
body, err := c.requestResponse(s) body, err := c.RequestResponse(request)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -790,18 +578,41 @@ func (c *ClientJSON) GetAccount(address []byte) (*core.ResponseGetAccount, error
return response.Result, nil return response.Result, nil
} }
func (c *ClientJSON) GetStorage(address []byte) (*core.ResponseGetStorage, error) { func (c *ClientJSON) GetBlock(height uint) (*core.ResponseGetBlock, error) {
params, err := binaryWriter(address) request := RPCRequest{
JSONRPC: "2.0",
Method: "get_block",
Params: []interface{}{height},
Id: 0,
}
body, err := c.RequestResponse(request)
if err != nil { if err != nil {
return nil, err return nil, err
} }
s := JSONRPC{ var response struct {
Result *core.ResponseGetBlock `json:"result"`
Error string `json:"error"`
Id string `json:"id"`
JSONRPC string `json:"jsonrpc"`
}
binary.ReadJSON(&response, body, &err)
if err != nil {
return nil, err
}
if response.Error != "" {
return nil, fmt.Errorf(response.Error)
}
return response.Result, nil
}
func (c *ClientJSON) GetStorage(address []byte) (*core.ResponseGetStorage, error) {
request := RPCRequest{
JSONRPC: "2.0", JSONRPC: "2.0",
Method: "get_storage", Method: "get_storage",
Params: params, Params: []interface{}{address},
Id: 0, Id: 0,
} }
body, err := c.requestResponse(s) body, err := c.RequestResponse(request)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -820,3 +631,138 @@ func (c *ClientJSON) GetStorage(address []byte) (*core.ResponseGetStorage, error
} }
return response.Result, nil return response.Result, nil
} }
func (c *ClientJSON) ListAccounts() (*core.ResponseListAccounts, error) {
request := RPCRequest{
JSONRPC: "2.0",
Method: "list_accounts",
Params: []interface{}{nil},
Id: 0,
}
body, err := c.RequestResponse(request)
if err != nil {
return nil, err
}
var response struct {
Result *core.ResponseListAccounts `json:"result"`
Error string `json:"error"`
Id string `json:"id"`
JSONRPC string `json:"jsonrpc"`
}
binary.ReadJSON(&response, body, &err)
if err != nil {
return nil, err
}
if response.Error != "" {
return nil, fmt.Errorf(response.Error)
}
return response.Result, nil
}
func (c *ClientJSON) ListValidators() (*core.ResponseListValidators, error) {
request := RPCRequest{
JSONRPC: "2.0",
Method: "list_validators",
Params: []interface{}{nil},
Id: 0,
}
body, err := c.RequestResponse(request)
if err != nil {
return nil, err
}
var response struct {
Result *core.ResponseListValidators `json:"result"`
Error string `json:"error"`
Id string `json:"id"`
JSONRPC string `json:"jsonrpc"`
}
binary.ReadJSON(&response, body, &err)
if err != nil {
return nil, err
}
if response.Error != "" {
return nil, fmt.Errorf(response.Error)
}
return response.Result, nil
}
func (c *ClientJSON) NetInfo() (*core.ResponseNetInfo, error) {
request := RPCRequest{
JSONRPC: "2.0",
Method: "net_info",
Params: []interface{}{nil},
Id: 0,
}
body, err := c.RequestResponse(request)
if err != nil {
return nil, err
}
var response struct {
Result *core.ResponseNetInfo `json:"result"`
Error string `json:"error"`
Id string `json:"id"`
JSONRPC string `json:"jsonrpc"`
}
binary.ReadJSON(&response, body, &err)
if err != nil {
return nil, err
}
if response.Error != "" {
return nil, fmt.Errorf(response.Error)
}
return response.Result, nil
}
func (c *ClientJSON) SignTx(tx types.Tx, privAccounts []*account.PrivAccount) (*core.ResponseSignTx, error) {
request := RPCRequest{
JSONRPC: "2.0",
Method: "sign_tx",
Params: []interface{}{tx, privAccounts},
Id: 0,
}
body, err := c.RequestResponse(request)
if err != nil {
return nil, err
}
var response struct {
Result *core.ResponseSignTx `json:"result"`
Error string `json:"error"`
Id string `json:"id"`
JSONRPC string `json:"jsonrpc"`
}
binary.ReadJSON(&response, body, &err)
if err != nil {
return nil, err
}
if response.Error != "" {
return nil, fmt.Errorf(response.Error)
}
return response.Result, nil
}
func (c *ClientJSON) Status() (*core.ResponseStatus, error) {
request := RPCRequest{
JSONRPC: "2.0",
Method: "status",
Params: []interface{}{nil},
Id: 0,
}
body, err := c.RequestResponse(request)
if err != nil {
return nil, err
}
var response struct {
Result *core.ResponseStatus `json:"result"`
Error string `json:"error"`
Id string `json:"id"`
JSONRPC string `json:"jsonrpc"`
}
binary.ReadJSON(&response, body, &err)
if err != nil {
return nil, err
}
if response.Error != "" {
return nil, fmt.Errorf(response.Error)
}
return response.Result, nil
}

View File

@ -84,31 +84,24 @@ func funcReturnTypes(f interface{}) []reflect.Type {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// rpc.json // 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 // jsonrpc calls grab the given method's function info and runs reflect.Call
func JSONRPCHandler(w http.ResponseWriter, r *http.Request) { func JSONRPCHandler(w http.ResponseWriter, r *http.Request) {
b, _ := ioutil.ReadAll(r.Body) b, _ := ioutil.ReadAll(r.Body)
var jrpc JSONRPC var request RPCRequest
err := json.Unmarshal(b, &jrpc) err := json.Unmarshal(b, &request)
if err != nil { if err != nil {
WriteRPCResponse(w, NewRPCResponse(nil, err.Error())) WriteRPCResponse(w, NewRPCResponse(nil, err.Error()))
return return
} }
funcInfo := funcMap[jrpc.Method] funcInfo := funcMap[request.Method]
args, err := jsonParamsToArgs(funcInfo, jrpc.Params) args, err := jsonParamsToArgs(funcInfo, request.Params)
if err != nil { if err != nil {
WriteRPCResponse(w, NewRPCResponse(nil, err.Error())) WriteRPCResponse(w, NewRPCResponse(nil, err.Error()))
return return
} }
returns := funcInfo.f.Call(args) returns := funcInfo.f.Call(args)
response, err := returnsToResponse(returns) response, err := unreflectResponse(returns)
if err != nil { if err != nil {
WriteRPCResponse(w, NewRPCResponse(nil, err.Error())) WriteRPCResponse(w, NewRPCResponse(nil, err.Error()))
return return
@ -154,7 +147,7 @@ func toHttpHandler(funcInfo *FuncWrapper) func(http.ResponseWriter, *http.Reques
return return
} }
returns := funcInfo.f.Call(args) returns := funcInfo.f.Call(args)
response, err := returnsToResponse(returns) response, err := unreflectResponse(returns)
if err != nil { if err != nil {
WriteRPCResponse(w, NewRPCResponse(nil, err.Error())) WriteRPCResponse(w, NewRPCResponse(nil, err.Error()))
return 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 // 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] errV := returns[1]
if errV.Interface() != nil { if errV.Interface() != nil {
return nil, fmt.Errorf("%v", errV.Interface()) return nil, fmt.Errorf("%v", errV.Interface())

View File

@ -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) { func WriteRPCResponse(w http.ResponseWriter, res RPCResponse) {
buf, n, err := new(bytes.Buffer), new(int64), new(error) buf, n, err := new(bytes.Buffer), new(int64), new(error)
binary.WriteJSON(res, buf, n, err) binary.WriteJSON(res, buf, n, err)

View File

@ -1 +1 @@
{"Address":"D7DFF9806078899C8DA3FE3633CC0BF3C6C2B1BB","PubKey":[1,"2239C21C81EA7173A6C489145490C015E05D4B97448933B708A7EC5B7B4921E3"],"PrivKey":[1,"FDE3BD94CB327D19464027BA668194C5EFA46AE83E8419D7542CFF41F00C81972239C21C81EA7173A6C489145490C015E05D4B97448933B708A7EC5B7B4921E3"]} {"Address":"D7DFF9806078899C8DA3FE3633CC0BF3C6C2B1BB","PubKey":[1,"2239C21C81EA7173A6C489145490C015E05D4B97448933B708A7EC5B7B4921E3"],"PrivKey":[1,"FDE3BD94CB327D19464027BA668194C5EFA46AE83E8419D7542CFF41F00C81972239C21C81EA7173A6C489145490C015E05D4B97448933B708A7EC5B7B4921E3"],"LastHeight":2,"LastRound":0,"LastStep":2}

View File

@ -30,7 +30,7 @@ func TestHTTPStatus(t *testing.T) {
Result core.ResponseStatus `json:"result"` Result core.ResponseStatus `json:"result"`
Error string `json:"error"` Error string `json:"error"`
Id string `json:"id"` Id string `json:"id"`
JSONRPC int `json:"jsonrpc"` JSONRPC string `json:"jsonrpc"`
} }
binary.ReadJSON(&response, body, &err) binary.ReadJSON(&response, body, &err)
if err != nil { if err != nil {
@ -58,7 +58,7 @@ func TestHTTPGenPriv(t *testing.T) {
Result core.ResponseGenPrivAccount `json:"result"` Result core.ResponseGenPrivAccount `json:"result"`
Error string `json:"error"` Error string `json:"error"`
Id string `json:"id"` Id string `json:"id"`
JSONRPC int `json:"jsonrpc"` JSONRPC string `json:"jsonrpc"`
} }
binary.ReadJSON(&response, body, &err) binary.ReadJSON(&response, body, &err)
if err != nil { if err != nil {

View File

@ -18,7 +18,7 @@ import (
) )
func TestJSONStatus(t *testing.T) { func TestJSONStatus(t *testing.T) {
s := rpc.JSONRPC{ s := rpc.RPCRequest{
JSONRPC: "2.0", JSONRPC: "2.0",
Method: "status", Method: "status",
Params: []interface{}{}, Params: []interface{}{},
@ -43,7 +43,7 @@ func TestJSONStatus(t *testing.T) {
Result core.ResponseStatus `json:"result"` Result core.ResponseStatus `json:"result"`
Error string `json:"error"` Error string `json:"error"`
Id string `json:"id"` Id string `json:"id"`
JSONRPC int `json:"jsonrpc"` JSONRPC string `json:"jsonrpc"`
} }
binary.ReadJSON(&response, body, &err) binary.ReadJSON(&response, body, &err)
if err != nil { if err != nil {
@ -56,7 +56,7 @@ func TestJSONStatus(t *testing.T) {
} }
func TestJSONGenPriv(t *testing.T) { func TestJSONGenPriv(t *testing.T) {
s := rpc.JSONRPC{ s := rpc.RPCRequest{
JSONRPC: "2.0", JSONRPC: "2.0",
Method: "unsafe/gen_priv_account", Method: "unsafe/gen_priv_account",
Params: []interface{}{}, Params: []interface{}{},
@ -83,7 +83,7 @@ func TestJSONGenPriv(t *testing.T) {
Result core.ResponseGenPrivAccount `json:"result"` Result core.ResponseGenPrivAccount `json:"result"`
Error string `json:"error"` Error string `json:"error"`
Id string `json:"id"` Id string `json:"id"`
JSONRPC int `json:"jsonrpc"` JSONRPC string `json:"jsonrpc"`
} }
binary.ReadJSON(&response, body, &err) binary.ReadJSON(&response, body, &err)
if err != nil { if err != nil {
@ -146,7 +146,7 @@ func TestJSONBroadcastTx(t *testing.T) {
Result core.ResponseBroadcastTx `json:"result"` Result core.ResponseBroadcastTx `json:"result"`
Error string `json:"error"` Error string `json:"error"`
Id string `json:"id"` Id string `json:"id"`
JSONRPC int `json:"jsonrpc"` JSONRPC string `json:"jsonrpc"`
} }
requestResponse(t, "broadcast_tx", url.Values{"tx": {string(b)}}, &response) requestResponse(t, "broadcast_tx", url.Values{"tx": {string(b)}}, &response)
if response.Error != "" { if response.Error != "" {

View File

@ -123,7 +123,7 @@ func getAccount(t *testing.T, typ string, addr []byte) *account.Account {
Result core.ResponseGetAccount `json:"result"` Result core.ResponseGetAccount `json:"result"`
Error string `json:"error"` Error string `json:"error"`
Id string `json:"id"` Id string `json:"id"`
JSONRPC int `json:"jsonrpc"` JSONRPC string `json:"jsonrpc"`
} }
binary.ReadJSON(&response, body, &err) binary.ReadJSON(&response, body, &err)
if err != nil { 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"` Result core.ResponseSignTx `json:"result"`
Error string `json:"error"` Error string `json:"error"`
Id string `json:"id"` 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) requestResponse(t, "unsafe/sign_tx", url.Values{"tx": {string(b)}, "privAccounts": {string(w.Bytes())}}, &response)
if response.Error != "" { 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"` Result core.ResponseBroadcastTx `json:"result"`
Error string `json:"error"` Error string `json:"error"`
Id string `json:"id"` Id string `json:"id"`
JSONRPC int `json:"jsonrpc"` JSONRPC string `json:"jsonrpc"`
} }
requestResponse(t, "broadcast_tx", url.Values{"tx": {string(b)}}, &response) requestResponse(t, "broadcast_tx", url.Values{"tx": {string(b)}}, &response)
if response.Error != "" { if response.Error != "" {
@ -275,7 +275,7 @@ func dumpStorage(t *testing.T, addr []byte) core.ResponseDumpStorage {
Result core.ResponseDumpStorage `json:"result"` Result core.ResponseDumpStorage `json:"result"`
Error string `json:"error"` Error string `json:"error"`
Id string `json:"id"` Id string `json:"id"`
JSONRPC int `json:"jsonrpc"` JSONRPC string `json:"jsonrpc"`
} }
requestResponse(t, "dump_storage", url.Values{"address": {addrString}}, &response) requestResponse(t, "dump_storage", url.Values{"address": {addrString}}, &response)
if response.Error != "" { if response.Error != "" {
@ -291,7 +291,7 @@ func getStorage(t *testing.T, addr, slot []byte) []byte {
Result core.ResponseGetStorage `json:"result"` Result core.ResponseGetStorage `json:"result"`
Error string `json:"error"` Error string `json:"error"`
Id string `json:"id"` Id string `json:"id"`
JSONRPC int `json:"jsonrpc"` JSONRPC string `json:"jsonrpc"`
} }
requestResponse(t, "get_storage", url.Values{"address": {addrString}, "storage": {slotString}}, &response) requestResponse(t, "get_storage", url.Values{"address": {addrString}, "storage": {slotString}}, &response)
if response.Error != "" { if response.Error != "" {

27
rpc/types.go Normal file
View File

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