Merge branch 'rpc_jae' into develop

Conflicts:
	node/node.go
	rpc/core/accounts.go
	rpc/core_client/client.go
	rpc/handlers.go
	rpc/http_server.go
	rpc/test/helpers.go
	rpc/test/http_rpc_test.go
	rpc/test/json_rpc_test.go
This commit is contained in:
Jae Kwon
2015-04-08 14:27:03 -07:00
20 changed files with 892 additions and 657 deletions

View File

@ -5,14 +5,51 @@ import (
"fmt"
"github.com/tendermint/tendermint/binary"
"github.com/tendermint/tendermint/rpc"
// NOTE: do not import rpc/core.
// What kind of client imports all of core logic? :P
"io/ioutil"
"net/http"
"net/url"
"reflect"
//"reflect"
// Uncomment to use go:generate
// _ "github.com/tendermint/go-rpc-gen"
)
// maps camel-case function names to lower case rpc version
var reverseFuncMap = map[string]string{
"Status": "status",
"NetInfo": "net_info",
"BlockchainInfo": "blockchain",
"GetBlock": "get_block",
"GetAccount": "get_account",
"GetStorage": "get_storage",
"Call": "call",
"CallCode": "call_code",
"ListValidators": "list_validators",
"DumpStorage": "dump_storage",
"BroadcastTx": "broadcast_tx",
"ListAccounts": "list_accounts",
"GenPrivAccount": "unsafe/gen_priv_account",
"SignTx": "unsafe/sign_tx",
}
/*
// fill the map from camelcase to lowercase
func fillReverseFuncMap() map[string]string {
fMap := make(map[string]string)
for name, f := range core.Routes {
camelName := runtime.FuncForPC(f.f.Pointer()).Name()
spl := strings.Split(camelName, ".")
if len(spl) > 1 {
camelName = spl[len(spl)-1]
}
fMap[camelName] = name
}
return fMap
}
*/
type Response struct {
Status string
Data interface{}
@ -39,67 +76,21 @@ func NewClient(addr, typ string) Client {
return nil
}
func argsToJson(args ...interface{}) ([][]string, error) {
func argsToJson(args ...interface{}) ([]string, error) {
l := len(args)
jsons := make([][]string, l)
jsons := make([]string, l)
n, err := new(int64), new(error)
for i, a := range args {
//if its a slice, we serliaze separately and pack into a slice of strings
// otherwise its a slice of length 1
if v := reflect.ValueOf(a); v.Kind() == reflect.Slice {
ty := v.Type()
rt := ty.Elem()
if rt.Kind() == reflect.Uint8 {
buf := new(bytes.Buffer)
binary.WriteJSON(a, buf, n, err)
if *err != nil {
return nil, *err
}
jsons[i] = []string{string(buf.Bytes())}
} else {
slice := make([]string, v.Len())
for j := 0; j < v.Len(); j++ {
buf := new(bytes.Buffer)
binary.WriteJSON(v.Index(j).Interface(), buf, n, err)
if *err != nil {
return nil, *err
}
slice[j] = string(buf.Bytes())
}
jsons[i] = slice
}
} else {
buf := new(bytes.Buffer)
binary.WriteJSON(a, buf, n, err)
if *err != nil {
return nil, *err
}
jsons[i] = []string{string(buf.Bytes())}
buf := new(bytes.Buffer)
binary.WriteJSON(a, buf, n, err)
if *err != nil {
return nil, *err
}
jsons[i] = string(buf.Bytes())
}
return jsons, nil
}
func (c *ClientHTTP) RequestResponse(method string, values url.Values) (*Response, error) {
resp, err := http.PostForm(c.addr+method, values)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
response := new(Response)
fmt.Println(string(body))
binary.ReadJSON(response, body, &err)
if err != nil {
return nil, err
}
fmt.Println(response.Data)
return response, nil
}
func (c *ClientJSON) RequestResponse(s rpc.RPCRequest) (b []byte, err error) {
b = binary.JSONBytes(s)
buf := bytes.NewBuffer(b)
@ -154,10 +145,10 @@ func argsToURLValues(argNames []string, args ...interface{}) (url.Values, error)
}
for i, name := range argNames {
s := slice[i]
values.Set(name, s[0])
for i := 1; i < len(s); i++ {
values.Add(name, s[i])
}
values.Set(name, s) // s[0]
/*for j := 1; j < len(s); j++ {
values.Add(name, s[j])
}*/
}
return values, nil
}
@ -177,7 +168,7 @@ fmt
/*rpc-gen:template:*ClientJSON func (c *ClientJSON) {{name}}({{args.def}}) ({{response}}) {
request := rpc.RPCRequest{
JSONRPC: "2.0",
Method: {{lowername}},
Method: reverseFuncMap["{{name}}"],
Params: []interface{}{ {{args.ident}} },
Id: 0,
}
@ -206,7 +197,7 @@ fmt
if err != nil{
return nil, err
}
resp, err := http.PostForm(c.addr+{{lowername}}, values)
resp, err := http.PostForm(c.addr+reverseFuncMap["{{name}}"], values)
if err != nil {
return nil, err
}

View File

@ -14,14 +14,15 @@ import (
)
type Client interface {
BlockchainInfo(minHeight uint) (*ctypes.ResponseBlockchainInfo, error)
BlockchainInfo(minHeight uint, maxHeight uint) (*ctypes.ResponseBlockchainInfo, error)
BroadcastTx(tx types.Tx) (*ctypes.ResponseBroadcastTx, error)
Call(address []byte) (*ctypes.ResponseCall, error)
Call(address []byte, data []byte) (*ctypes.ResponseCall, error)
CallCode(code []byte, data []byte) (*ctypes.ResponseCall, error)
DumpStorage(addr []byte) (*ctypes.ResponseDumpStorage, error)
GenPrivAccount() (*ctypes.ResponseGenPrivAccount, error)
GetAccount(address []byte) (*ctypes.ResponseGetAccount, error)
GetBlock(height uint) (*ctypes.ResponseGetBlock, error)
GetStorage(address []byte) (*ctypes.ResponseGetStorage, error)
GetStorage(address []byte, key []byte) (*ctypes.ResponseGetStorage, error)
ListAccounts() (*ctypes.ResponseListAccounts, error)
ListValidators() (*ctypes.ResponseListValidators, error)
NetInfo() (*ctypes.ResponseNetInfo, error)
@ -29,12 +30,12 @@ type Client interface {
Status() (*ctypes.ResponseStatus, error)
}
func (c *ClientHTTP) BlockchainInfo(minHeight uint) (*ctypes.ResponseBlockchainInfo, error) {
values, err := argsToURLValues([]string{"minHeight"}, minHeight)
func (c *ClientHTTP) BlockchainInfo(minHeight uint, maxHeight uint) (*ctypes.ResponseBlockchainInfo, error) {
values, err := argsToURLValues([]string{"minHeight", "maxHeight"}, minHeight, maxHeight)
if err != nil {
return nil, err
}
resp, err := http.PostForm(c.addr+"blockchain_info", values)
resp, err := http.PostForm(c.addr+reverseFuncMap["BlockchainInfo"], values)
if err != nil {
return nil, err
}
@ -64,7 +65,7 @@ func (c *ClientHTTP) BroadcastTx(tx types.Tx) (*ctypes.ResponseBroadcastTx, erro
if err != nil {
return nil, err
}
resp, err := http.PostForm(c.addr+"broadcast_tx", values)
resp, err := http.PostForm(c.addr+reverseFuncMap["BroadcastTx"], values)
if err != nil {
return nil, err
}
@ -89,12 +90,42 @@ func (c *ClientHTTP) BroadcastTx(tx types.Tx) (*ctypes.ResponseBroadcastTx, erro
return response.Result, nil
}
func (c *ClientHTTP) Call(address []byte) (*ctypes.ResponseCall, error) {
values, err := argsToURLValues([]string{"address"}, address)
func (c *ClientHTTP) Call(address []byte, data []byte) (*ctypes.ResponseCall, error) {
values, err := argsToURLValues([]string{"address", "data"}, address, data)
if err != nil {
return nil, err
}
resp, err := http.PostForm(c.addr+"call", values)
resp, err := http.PostForm(c.addr+reverseFuncMap["Call"], 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 *ctypes.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 *ClientHTTP) CallCode(code []byte, data []byte) (*ctypes.ResponseCall, error) {
values, err := argsToURLValues([]string{"code", "data"}, code, data)
if err != nil {
return nil, err
}
resp, err := http.PostForm(c.addr+reverseFuncMap["CallCode"], values)
if err != nil {
return nil, err
}
@ -124,7 +155,7 @@ func (c *ClientHTTP) DumpStorage(addr []byte) (*ctypes.ResponseDumpStorage, erro
if err != nil {
return nil, err
}
resp, err := http.PostForm(c.addr+"dump_storage", values)
resp, err := http.PostForm(c.addr+reverseFuncMap["DumpStorage"], values)
if err != nil {
return nil, err
}
@ -150,11 +181,11 @@ func (c *ClientHTTP) DumpStorage(addr []byte) (*ctypes.ResponseDumpStorage, erro
}
func (c *ClientHTTP) GenPrivAccount() (*ctypes.ResponseGenPrivAccount, error) {
values, err := argsToURLValues(nil, nil)
values, err := argsToURLValues(nil)
if err != nil {
return nil, err
}
resp, err := http.PostForm(c.addr+"gen_priv_account", values)
resp, err := http.PostForm(c.addr+reverseFuncMap["GenPrivAccount"], values)
if err != nil {
return nil, err
}
@ -184,7 +215,7 @@ func (c *ClientHTTP) GetAccount(address []byte) (*ctypes.ResponseGetAccount, err
if err != nil {
return nil, err
}
resp, err := http.PostForm(c.addr+"get_account", values)
resp, err := http.PostForm(c.addr+reverseFuncMap["GetAccount"], values)
if err != nil {
return nil, err
}
@ -214,7 +245,7 @@ func (c *ClientHTTP) GetBlock(height uint) (*ctypes.ResponseGetBlock, error) {
if err != nil {
return nil, err
}
resp, err := http.PostForm(c.addr+"get_block", values)
resp, err := http.PostForm(c.addr+reverseFuncMap["GetBlock"], values)
if err != nil {
return nil, err
}
@ -239,12 +270,12 @@ func (c *ClientHTTP) GetBlock(height uint) (*ctypes.ResponseGetBlock, error) {
return response.Result, nil
}
func (c *ClientHTTP) GetStorage(address []byte) (*ctypes.ResponseGetStorage, error) {
values, err := argsToURLValues([]string{"address"}, address)
func (c *ClientHTTP) GetStorage(address []byte, key []byte) (*ctypes.ResponseGetStorage, error) {
values, err := argsToURLValues([]string{"address", "key"}, address, key)
if err != nil {
return nil, err
}
resp, err := http.PostForm(c.addr+"get_storage", values)
resp, err := http.PostForm(c.addr+reverseFuncMap["GetStorage"], values)
if err != nil {
return nil, err
}
@ -270,11 +301,11 @@ func (c *ClientHTTP) GetStorage(address []byte) (*ctypes.ResponseGetStorage, err
}
func (c *ClientHTTP) ListAccounts() (*ctypes.ResponseListAccounts, error) {
values, err := argsToURLValues(nil, nil)
values, err := argsToURLValues(nil)
if err != nil {
return nil, err
}
resp, err := http.PostForm(c.addr+"list_accounts", values)
resp, err := http.PostForm(c.addr+reverseFuncMap["ListAccounts"], values)
if err != nil {
return nil, err
}
@ -300,11 +331,11 @@ func (c *ClientHTTP) ListAccounts() (*ctypes.ResponseListAccounts, error) {
}
func (c *ClientHTTP) ListValidators() (*ctypes.ResponseListValidators, error) {
values, err := argsToURLValues(nil, nil)
values, err := argsToURLValues(nil)
if err != nil {
return nil, err
}
resp, err := http.PostForm(c.addr+"list_validators", values)
resp, err := http.PostForm(c.addr+reverseFuncMap["ListValidators"], values)
if err != nil {
return nil, err
}
@ -330,11 +361,11 @@ func (c *ClientHTTP) ListValidators() (*ctypes.ResponseListValidators, error) {
}
func (c *ClientHTTP) NetInfo() (*ctypes.ResponseNetInfo, error) {
values, err := argsToURLValues(nil, nil)
values, err := argsToURLValues(nil)
if err != nil {
return nil, err
}
resp, err := http.PostForm(c.addr+"net_info", values)
resp, err := http.PostForm(c.addr+reverseFuncMap["NetInfo"], values)
if err != nil {
return nil, err
}
@ -364,7 +395,7 @@ func (c *ClientHTTP) SignTx(tx types.Tx, privAccounts []*account.PrivAccount) (*
if err != nil {
return nil, err
}
resp, err := http.PostForm(c.addr+"sign_tx", values)
resp, err := http.PostForm(c.addr+reverseFuncMap["SignTx"], values)
if err != nil {
return nil, err
}
@ -390,11 +421,11 @@ func (c *ClientHTTP) SignTx(tx types.Tx, privAccounts []*account.PrivAccount) (*
}
func (c *ClientHTTP) Status() (*ctypes.ResponseStatus, error) {
values, err := argsToURLValues(nil, nil)
values, err := argsToURLValues(nil)
if err != nil {
return nil, err
}
resp, err := http.PostForm(c.addr+"status", values)
resp, err := http.PostForm(c.addr+reverseFuncMap["Status"], values)
if err != nil {
return nil, err
}
@ -419,11 +450,11 @@ func (c *ClientHTTP) Status() (*ctypes.ResponseStatus, error) {
return response.Result, nil
}
func (c *ClientJSON) BlockchainInfo(minHeight uint) (*ctypes.ResponseBlockchainInfo, error) {
func (c *ClientJSON) BlockchainInfo(minHeight uint, maxHeight uint) (*ctypes.ResponseBlockchainInfo, error) {
request := rpc.RPCRequest{
JSONRPC: "2.0",
Method: "blockchain_info",
Params: []interface{}{minHeight},
Method: reverseFuncMap["BlockchainInfo"],
Params: []interface{}{minHeight, maxHeight},
Id: 0,
}
body, err := c.RequestResponse(request)
@ -449,7 +480,7 @@ func (c *ClientJSON) BlockchainInfo(minHeight uint) (*ctypes.ResponseBlockchainI
func (c *ClientJSON) BroadcastTx(tx types.Tx) (*ctypes.ResponseBroadcastTx, error) {
request := rpc.RPCRequest{
JSONRPC: "2.0",
Method: "broadcast_tx",
Method: reverseFuncMap["BroadcastTx"],
Params: []interface{}{tx},
Id: 0,
}
@ -473,11 +504,38 @@ func (c *ClientJSON) BroadcastTx(tx types.Tx) (*ctypes.ResponseBroadcastTx, erro
return response.Result, nil
}
func (c *ClientJSON) Call(address []byte) (*ctypes.ResponseCall, error) {
func (c *ClientJSON) Call(address []byte, data []byte) (*ctypes.ResponseCall, error) {
request := rpc.RPCRequest{
JSONRPC: "2.0",
Method: "call",
Params: []interface{}{address},
Method: reverseFuncMap["Call"],
Params: []interface{}{address, data},
Id: 0,
}
body, err := c.RequestResponse(request)
if err != nil {
return nil, err
}
var response struct {
Result *ctypes.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) CallCode(code []byte, data []byte) (*ctypes.ResponseCall, error) {
request := rpc.RPCRequest{
JSONRPC: "2.0",
Method: reverseFuncMap["CallCode"],
Params: []interface{}{code, data},
Id: 0,
}
body, err := c.RequestResponse(request)
@ -503,7 +561,7 @@ func (c *ClientJSON) Call(address []byte) (*ctypes.ResponseCall, error) {
func (c *ClientJSON) DumpStorage(addr []byte) (*ctypes.ResponseDumpStorage, error) {
request := rpc.RPCRequest{
JSONRPC: "2.0",
Method: "dump_storage",
Method: reverseFuncMap["DumpStorage"],
Params: []interface{}{addr},
Id: 0,
}
@ -530,8 +588,8 @@ func (c *ClientJSON) DumpStorage(addr []byte) (*ctypes.ResponseDumpStorage, erro
func (c *ClientJSON) GenPrivAccount() (*ctypes.ResponseGenPrivAccount, error) {
request := rpc.RPCRequest{
JSONRPC: "2.0",
Method: "gen_priv_account",
Params: []interface{}{nil},
Method: reverseFuncMap["GenPrivAccount"],
Params: []interface{}{},
Id: 0,
}
body, err := c.RequestResponse(request)
@ -557,7 +615,7 @@ func (c *ClientJSON) GenPrivAccount() (*ctypes.ResponseGenPrivAccount, error) {
func (c *ClientJSON) GetAccount(address []byte) (*ctypes.ResponseGetAccount, error) {
request := rpc.RPCRequest{
JSONRPC: "2.0",
Method: "get_account",
Method: reverseFuncMap["GetAccount"],
Params: []interface{}{address},
Id: 0,
}
@ -584,7 +642,7 @@ func (c *ClientJSON) GetAccount(address []byte) (*ctypes.ResponseGetAccount, err
func (c *ClientJSON) GetBlock(height uint) (*ctypes.ResponseGetBlock, error) {
request := rpc.RPCRequest{
JSONRPC: "2.0",
Method: "get_block",
Method: reverseFuncMap["GetBlock"],
Params: []interface{}{height},
Id: 0,
}
@ -608,11 +666,11 @@ func (c *ClientJSON) GetBlock(height uint) (*ctypes.ResponseGetBlock, error) {
return response.Result, nil
}
func (c *ClientJSON) GetStorage(address []byte) (*ctypes.ResponseGetStorage, error) {
func (c *ClientJSON) GetStorage(address []byte, key []byte) (*ctypes.ResponseGetStorage, error) {
request := rpc.RPCRequest{
JSONRPC: "2.0",
Method: "get_storage",
Params: []interface{}{address},
Method: reverseFuncMap["GetStorage"],
Params: []interface{}{address, key},
Id: 0,
}
body, err := c.RequestResponse(request)
@ -638,8 +696,8 @@ func (c *ClientJSON) GetStorage(address []byte) (*ctypes.ResponseGetStorage, err
func (c *ClientJSON) ListAccounts() (*ctypes.ResponseListAccounts, error) {
request := rpc.RPCRequest{
JSONRPC: "2.0",
Method: "list_accounts",
Params: []interface{}{nil},
Method: reverseFuncMap["ListAccounts"],
Params: []interface{}{},
Id: 0,
}
body, err := c.RequestResponse(request)
@ -665,8 +723,8 @@ func (c *ClientJSON) ListAccounts() (*ctypes.ResponseListAccounts, error) {
func (c *ClientJSON) ListValidators() (*ctypes.ResponseListValidators, error) {
request := rpc.RPCRequest{
JSONRPC: "2.0",
Method: "list_validators",
Params: []interface{}{nil},
Method: reverseFuncMap["ListValidators"],
Params: []interface{}{},
Id: 0,
}
body, err := c.RequestResponse(request)
@ -692,8 +750,8 @@ func (c *ClientJSON) ListValidators() (*ctypes.ResponseListValidators, error) {
func (c *ClientJSON) NetInfo() (*ctypes.ResponseNetInfo, error) {
request := rpc.RPCRequest{
JSONRPC: "2.0",
Method: "net_info",
Params: []interface{}{nil},
Method: reverseFuncMap["NetInfo"],
Params: []interface{}{},
Id: 0,
}
body, err := c.RequestResponse(request)
@ -719,7 +777,7 @@ func (c *ClientJSON) NetInfo() (*ctypes.ResponseNetInfo, error) {
func (c *ClientJSON) SignTx(tx types.Tx, privAccounts []*account.PrivAccount) (*ctypes.ResponseSignTx, error) {
request := rpc.RPCRequest{
JSONRPC: "2.0",
Method: "sign_tx",
Method: reverseFuncMap["SignTx"],
Params: []interface{}{tx, privAccounts},
Id: 0,
}
@ -746,8 +804,8 @@ func (c *ClientJSON) SignTx(tx types.Tx, privAccounts []*account.PrivAccount) (*
func (c *ClientJSON) Status() (*ctypes.ResponseStatus, error) {
request := rpc.RPCRequest{
JSONRPC: "2.0",
Method: "status",
Params: []interface{}{nil},
Method: reverseFuncMap["Status"],
Params: []interface{}{},
Id: 0,
}
body, err := c.RequestResponse(request)

View File

@ -1,3 +1,5 @@
// File generated by github.com/ebuchman/rpc-gen
package rpc
import (
@ -11,14 +13,15 @@ import (
)
type Client interface {
BlockchainInfo(minHeight uint) (*core.ResponseBlockchainInfo, error)
BlockchainInfo(minHeight uint, maxHeight uint) (*core.ResponseBlockchainInfo, error)
BroadcastTx(tx types.Tx) (*core.ResponseBroadcastTx, error)
Call(address []byte) (*core.ResponseCall, error)
Call(address []byte, data []byte) (*core.ResponseCall, error)
CallCode(code []byte, data []byte) (*core.ResponseCall, error)
DumpStorage(addr []byte) (*core.ResponseDumpStorage, error)
GenPrivAccount() (*core.ResponseGenPrivAccount, error)
GetAccount(address []byte) (*core.ResponseGetAccount, error)
GetBlock(height uint) (*core.ResponseGetBlock, error)
GetStorage(address []byte) (*core.ResponseGetStorage, error)
GetStorage(address []byte, key []byte) (*core.ResponseGetStorage, error)
ListAccounts() (*core.ResponseListAccounts, error)
ListValidators() (*core.ResponseListValidators, error)
NetInfo() (*core.ResponseNetInfo, error)
@ -26,12 +29,12 @@ type Client interface {
Status() (*core.ResponseStatus, error)
}
func (c *ClientHTTP) BlockchainInfo(minHeight uint) (*core.ResponseBlockchainInfo, error) {
values, err := argsToURLValues([]string{"minHeight"}, minHeight)
func (c *ClientHTTP) BlockchainInfo(minHeight uint, maxHeight uint) (*core.ResponseBlockchainInfo, error) {
values, err := argsToURLValues([]string{"minHeight", "maxHeight"}, minHeight, maxHeight)
if err != nil {
return nil, err
}
resp, err := http.PostForm(c.addr+"blockchain_info", values)
resp, err := http.PostForm(c.addr+reverseFuncMap["BlockchainInfo"], values)
if err != nil {
return nil, err
}
@ -61,7 +64,7 @@ func (c *ClientHTTP) BroadcastTx(tx types.Tx) (*core.ResponseBroadcastTx, error)
if err != nil {
return nil, err
}
resp, err := http.PostForm(c.addr+"broadcast_tx", values)
resp, err := http.PostForm(c.addr+reverseFuncMap["BroadcastTx"], values)
if err != nil {
return nil, err
}
@ -86,12 +89,42 @@ func (c *ClientHTTP) BroadcastTx(tx types.Tx) (*core.ResponseBroadcastTx, error)
return response.Result, nil
}
func (c *ClientHTTP) Call(address []byte) (*core.ResponseCall, error) {
values, err := argsToURLValues([]string{"address"}, address)
func (c *ClientHTTP) Call(address []byte, data []byte) (*core.ResponseCall, error) {
values, err := argsToURLValues([]string{"address", "data"}, address, data)
if err != nil {
return nil, err
}
resp, err := http.PostForm(c.addr+"call", values)
resp, err := http.PostForm(c.addr+reverseFuncMap["Call"], 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.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 *ClientHTTP) CallCode(code []byte, data []byte) (*core.ResponseCall, error) {
values, err := argsToURLValues([]string{"code", "data"}, code, data)
if err != nil {
return nil, err
}
resp, err := http.PostForm(c.addr+reverseFuncMap["CallCode"], values)
if err != nil {
return nil, err
}
@ -121,7 +154,7 @@ func (c *ClientHTTP) DumpStorage(addr []byte) (*core.ResponseDumpStorage, error)
if err != nil {
return nil, err
}
resp, err := http.PostForm(c.addr+"dump_storage", values)
resp, err := http.PostForm(c.addr+reverseFuncMap["DumpStorage"], values)
if err != nil {
return nil, err
}
@ -147,11 +180,11 @@ func (c *ClientHTTP) DumpStorage(addr []byte) (*core.ResponseDumpStorage, error)
}
func (c *ClientHTTP) GenPrivAccount() (*core.ResponseGenPrivAccount, error) {
values, err := argsToURLValues(nil, nil)
values, err := argsToURLValues(nil)
if err != nil {
return nil, err
}
resp, err := http.PostForm(c.addr+"gen_priv_account", values)
resp, err := http.PostForm(c.addr+reverseFuncMap["GenPrivAccount"], values)
if err != nil {
return nil, err
}
@ -181,7 +214,7 @@ func (c *ClientHTTP) GetAccount(address []byte) (*core.ResponseGetAccount, error
if err != nil {
return nil, err
}
resp, err := http.PostForm(c.addr+"get_account", values)
resp, err := http.PostForm(c.addr+reverseFuncMap["GetAccount"], values)
if err != nil {
return nil, err
}
@ -211,7 +244,7 @@ func (c *ClientHTTP) GetBlock(height uint) (*core.ResponseGetBlock, error) {
if err != nil {
return nil, err
}
resp, err := http.PostForm(c.addr+"get_block", values)
resp, err := http.PostForm(c.addr+reverseFuncMap["GetBlock"], values)
if err != nil {
return nil, err
}
@ -236,12 +269,12 @@ func (c *ClientHTTP) GetBlock(height uint) (*core.ResponseGetBlock, error) {
return response.Result, nil
}
func (c *ClientHTTP) GetStorage(address []byte) (*core.ResponseGetStorage, error) {
values, err := argsToURLValues([]string{"address"}, address)
func (c *ClientHTTP) GetStorage(address []byte, key []byte) (*core.ResponseGetStorage, error) {
values, err := argsToURLValues([]string{"address", "key"}, address, key)
if err != nil {
return nil, err
}
resp, err := http.PostForm(c.addr+"get_storage", values)
resp, err := http.PostForm(c.addr+reverseFuncMap["GetStorage"], values)
if err != nil {
return nil, err
}
@ -267,11 +300,11 @@ func (c *ClientHTTP) GetStorage(address []byte) (*core.ResponseGetStorage, error
}
func (c *ClientHTTP) ListAccounts() (*core.ResponseListAccounts, error) {
values, err := argsToURLValues(nil, nil)
values, err := argsToURLValues(nil)
if err != nil {
return nil, err
}
resp, err := http.PostForm(c.addr+"list_accounts", values)
resp, err := http.PostForm(c.addr+reverseFuncMap["ListAccounts"], values)
if err != nil {
return nil, err
}
@ -297,11 +330,11 @@ func (c *ClientHTTP) ListAccounts() (*core.ResponseListAccounts, error) {
}
func (c *ClientHTTP) ListValidators() (*core.ResponseListValidators, error) {
values, err := argsToURLValues(nil, nil)
values, err := argsToURLValues(nil)
if err != nil {
return nil, err
}
resp, err := http.PostForm(c.addr+"list_validators", values)
resp, err := http.PostForm(c.addr+reverseFuncMap["ListValidators"], values)
if err != nil {
return nil, err
}
@ -327,11 +360,11 @@ func (c *ClientHTTP) ListValidators() (*core.ResponseListValidators, error) {
}
func (c *ClientHTTP) NetInfo() (*core.ResponseNetInfo, error) {
values, err := argsToURLValues(nil, nil)
values, err := argsToURLValues(nil)
if err != nil {
return nil, err
}
resp, err := http.PostForm(c.addr+"net_info", values)
resp, err := http.PostForm(c.addr+reverseFuncMap["NetInfo"], values)
if err != nil {
return nil, err
}
@ -357,11 +390,11 @@ func (c *ClientHTTP) NetInfo() (*core.ResponseNetInfo, error) {
}
func (c *ClientHTTP) SignTx(tx types.Tx, privAccounts []*account.PrivAccount) (*core.ResponseSignTx, error) {
values, err := argsToURLValues([]string{"tx", "privAccounts"}, tx, privAccounts)
values, err := argsToURLValues([]string{"tx", "priv_accounts"}, tx, privAccounts)
if err != nil {
return nil, err
}
resp, err := http.PostForm(c.addr+"sign_tx", values)
resp, err := http.PostForm(c.addr+reverseFuncMap["SignTx"], values)
if err != nil {
return nil, err
}
@ -387,11 +420,11 @@ func (c *ClientHTTP) SignTx(tx types.Tx, privAccounts []*account.PrivAccount) (*
}
func (c *ClientHTTP) Status() (*core.ResponseStatus, error) {
values, err := argsToURLValues(nil, nil)
values, err := argsToURLValues(nil)
if err != nil {
return nil, err
}
resp, err := http.PostForm(c.addr+"status", values)
resp, err := http.PostForm(c.addr+reverseFuncMap["Status"], values)
if err != nil {
return nil, err
}
@ -416,11 +449,11 @@ func (c *ClientHTTP) Status() (*core.ResponseStatus, error) {
return response.Result, nil
}
func (c *ClientJSON) BlockchainInfo(minHeight uint) (*core.ResponseBlockchainInfo, error) {
func (c *ClientJSON) BlockchainInfo(minHeight uint, maxHeight uint) (*core.ResponseBlockchainInfo, error) {
request := RPCRequest{
JSONRPC: "2.0",
Method: "blockchain_info",
Params: []interface{}{minHeight},
Method: reverseFuncMap["BlockchainInfo"],
Params: []interface{}{minHeight, maxHeight},
Id: 0,
}
body, err := c.RequestResponse(request)
@ -446,7 +479,7 @@ func (c *ClientJSON) BlockchainInfo(minHeight uint) (*core.ResponseBlockchainInf
func (c *ClientJSON) BroadcastTx(tx types.Tx) (*core.ResponseBroadcastTx, error) {
request := RPCRequest{
JSONRPC: "2.0",
Method: "broadcast_tx",
Method: reverseFuncMap["BroadcastTx"],
Params: []interface{}{tx},
Id: 0,
}
@ -470,11 +503,38 @@ func (c *ClientJSON) BroadcastTx(tx types.Tx) (*core.ResponseBroadcastTx, error)
return response.Result, nil
}
func (c *ClientJSON) Call(address []byte) (*core.ResponseCall, error) {
func (c *ClientJSON) Call(address []byte, data []byte) (*core.ResponseCall, error) {
request := RPCRequest{
JSONRPC: "2.0",
Method: "call",
Params: []interface{}{address},
Method: reverseFuncMap["Call"],
Params: []interface{}{address, data},
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) CallCode(code []byte, data []byte) (*core.ResponseCall, error) {
request := RPCRequest{
JSONRPC: "2.0",
Method: reverseFuncMap["CallCode"],
Params: []interface{}{code, data},
Id: 0,
}
body, err := c.RequestResponse(request)
@ -500,7 +560,7 @@ func (c *ClientJSON) Call(address []byte) (*core.ResponseCall, error) {
func (c *ClientJSON) DumpStorage(addr []byte) (*core.ResponseDumpStorage, error) {
request := RPCRequest{
JSONRPC: "2.0",
Method: "dump_storage",
Method: reverseFuncMap["DumpStorage"],
Params: []interface{}{addr},
Id: 0,
}
@ -527,8 +587,8 @@ func (c *ClientJSON) DumpStorage(addr []byte) (*core.ResponseDumpStorage, error)
func (c *ClientJSON) GenPrivAccount() (*core.ResponseGenPrivAccount, error) {
request := RPCRequest{
JSONRPC: "2.0",
Method: "gen_priv_account",
Params: []interface{}{nil},
Method: reverseFuncMap["GenPrivAccount"],
Params: []interface{}{},
Id: 0,
}
body, err := c.RequestResponse(request)
@ -554,7 +614,7 @@ func (c *ClientJSON) GenPrivAccount() (*core.ResponseGenPrivAccount, error) {
func (c *ClientJSON) GetAccount(address []byte) (*core.ResponseGetAccount, error) {
request := RPCRequest{
JSONRPC: "2.0",
Method: "get_account",
Method: reverseFuncMap["GetAccount"],
Params: []interface{}{address},
Id: 0,
}
@ -581,7 +641,7 @@ func (c *ClientJSON) GetAccount(address []byte) (*core.ResponseGetAccount, error
func (c *ClientJSON) GetBlock(height uint) (*core.ResponseGetBlock, error) {
request := RPCRequest{
JSONRPC: "2.0",
Method: "get_block",
Method: reverseFuncMap["GetBlock"],
Params: []interface{}{height},
Id: 0,
}
@ -605,11 +665,11 @@ func (c *ClientJSON) GetBlock(height uint) (*core.ResponseGetBlock, error) {
return response.Result, nil
}
func (c *ClientJSON) GetStorage(address []byte) (*core.ResponseGetStorage, error) {
func (c *ClientJSON) GetStorage(address []byte, key []byte) (*core.ResponseGetStorage, error) {
request := RPCRequest{
JSONRPC: "2.0",
Method: "get_storage",
Params: []interface{}{address},
Method: reverseFuncMap["GetStorage"],
Params: []interface{}{address, key},
Id: 0,
}
body, err := c.RequestResponse(request)
@ -635,8 +695,8 @@ func (c *ClientJSON) GetStorage(address []byte) (*core.ResponseGetStorage, error
func (c *ClientJSON) ListAccounts() (*core.ResponseListAccounts, error) {
request := RPCRequest{
JSONRPC: "2.0",
Method: "list_accounts",
Params: []interface{}{nil},
Method: reverseFuncMap["ListAccounts"],
Params: []interface{}{},
Id: 0,
}
body, err := c.RequestResponse(request)
@ -662,8 +722,8 @@ func (c *ClientJSON) ListAccounts() (*core.ResponseListAccounts, error) {
func (c *ClientJSON) ListValidators() (*core.ResponseListValidators, error) {
request := RPCRequest{
JSONRPC: "2.0",
Method: "list_validators",
Params: []interface{}{nil},
Method: reverseFuncMap["ListValidators"],
Params: []interface{}{},
Id: 0,
}
body, err := c.RequestResponse(request)
@ -689,8 +749,8 @@ func (c *ClientJSON) ListValidators() (*core.ResponseListValidators, error) {
func (c *ClientJSON) NetInfo() (*core.ResponseNetInfo, error) {
request := RPCRequest{
JSONRPC: "2.0",
Method: "net_info",
Params: []interface{}{nil},
Method: reverseFuncMap["NetInfo"],
Params: []interface{}{},
Id: 0,
}
body, err := c.RequestResponse(request)
@ -716,7 +776,7 @@ func (c *ClientJSON) NetInfo() (*core.ResponseNetInfo, error) {
func (c *ClientJSON) SignTx(tx types.Tx, privAccounts []*account.PrivAccount) (*core.ResponseSignTx, error) {
request := RPCRequest{
JSONRPC: "2.0",
Method: "sign_tx",
Method: reverseFuncMap["SignTx"],
Params: []interface{}{tx, privAccounts},
Id: 0,
}
@ -743,8 +803,8 @@ func (c *ClientJSON) SignTx(tx types.Tx, privAccounts []*account.PrivAccount) (*
func (c *ClientJSON) Status() (*core.ResponseStatus, error) {
request := RPCRequest{
JSONRPC: "2.0",
Method: "status",
Params: []interface{}{nil},
Method: reverseFuncMap["Status"],
Params: []interface{}{},
Id: 0,
}
body, err := c.RequestResponse(request)