Browse Source

rpc-gen update

pull/83/head
Ethan Buchman 10 years ago
parent
commit
293aa31f64
3 changed files with 118 additions and 56 deletions
  1. +19
    -15
      rpc/core_client/client.go
  2. +97
    -39
      rpc/core_client/client_methods.go
  3. +2
    -2
      rpc/test/helpers.go

+ 19
- 15
rpc/core_client/client.go View File

@ -16,21 +16,25 @@ import (
// 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",
"NameRegEntry": "name_reg_entry",
"GenPrivAccount": "unsafe/gen_priv_account",
"SignTx": "unsafe/sign_tx",
"Status": "status",
"NetInfo": "net_info",
"BlockchainInfo": "blockchain",
"Genesis": "genesis",
"GetBlock": "get_block",
"GetAccount": "get_account",
"GetStorage": "get_storage",
"Call": "call",
"CallCode": "call_code",
"ListValidators": "list_validators",
"DumpConsensusState": "dump_consensus_state",
"DumpStorage": "dump_storage",
"BroadcastTx": "broadcast_tx",
"ListUnconfirmedTxs": "list_unconfirmed_txs",
"ListAccounts": "list_accounts",
"GetName": "get_name",
"ListNames": "list_names",
"GenPrivAccount": "unsafe/gen_priv_account",
"SignTx": "unsafe/sign_tx",
}
/*


+ 97
- 39
rpc/core_client/client_methods.go View File

@ -24,11 +24,12 @@ type Client interface {
Genesis() (*string, error)
GetAccount(address []byte) (*ctypes.ResponseGetAccount, error)
GetBlock(height uint) (*ctypes.ResponseGetBlock, error)
GetName(name string) (*types.NameRegEntry, error)
GetStorage(address []byte, key []byte) (*ctypes.ResponseGetStorage, error)
ListAccounts() (*ctypes.ResponseListAccounts, error)
ListNames() (*ctypes.ResponseListNames, error)
ListUnconfirmedTxs() (*ctypes.ResponseListUnconfirmedTxs, error)
ListValidators() (*ctypes.ResponseListValidators, error)
NameRegEntry(name string) (*ctypes.ResponseNameRegEntry, error)
NetInfo() (*ctypes.ResponseNetInfo, error)
SignTx(tx types.Tx, privAccounts []*account.PrivAccount) (*ctypes.ResponseSignTx, error)
Status() (*ctypes.ResponseStatus, error)
@ -334,6 +335,36 @@ func (c *ClientHTTP) GetBlock(height uint) (*ctypes.ResponseGetBlock, error) {
return response.Result, nil
}
func (c *ClientHTTP) GetName(name string) (*types.NameRegEntry, error) {
values, err := argsToURLValues([]string{"name"}, name)
if err != nil {
return nil, err
}
resp, err := http.PostForm(c.addr+reverseFuncMap["GetName"], 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 *types.NameRegEntry `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, key []byte) (*ctypes.ResponseGetStorage, error) {
values, err := argsToURLValues([]string{"address", "key"}, address, key)
if err != nil {
@ -394,12 +425,12 @@ func (c *ClientHTTP) ListAccounts() (*ctypes.ResponseListAccounts, error) {
return response.Result, nil
}
func (c *ClientHTTP) ListUnconfirmedTxs() (*ctypes.ResponseListUnconfirmedTxs, error) {
func (c *ClientHTTP) ListNames() (*ctypes.ResponseListNames, error) {
values, err := argsToURLValues(nil)
if err != nil {
return nil, err
}
resp, err := http.PostForm(c.addr+reverseFuncMap["ListUnconfirmedTxs"], values)
resp, err := http.PostForm(c.addr+reverseFuncMap["ListNames"], values)
if err != nil {
return nil, err
}
@ -409,10 +440,10 @@ func (c *ClientHTTP) ListUnconfirmedTxs() (*ctypes.ResponseListUnconfirmedTxs, e
return nil, err
}
var response struct {
Result *ctypes.ResponseListUnconfirmedTxs `json:"result"`
Error string `json:"error"`
Id string `json:"id"`
JSONRPC string `json:"jsonrpc"`
Result *ctypes.ResponseListNames `json:"result"`
Error string `json:"error"`
Id string `json:"id"`
JSONRPC string `json:"jsonrpc"`
}
binary.ReadJSON(&response, body, &err)
if err != nil {
@ -424,12 +455,12 @@ func (c *ClientHTTP) ListUnconfirmedTxs() (*ctypes.ResponseListUnconfirmedTxs, e
return response.Result, nil
}
func (c *ClientHTTP) ListValidators() (*ctypes.ResponseListValidators, error) {
func (c *ClientHTTP) ListUnconfirmedTxs() (*ctypes.ResponseListUnconfirmedTxs, error) {
values, err := argsToURLValues(nil)
if err != nil {
return nil, err
}
resp, err := http.PostForm(c.addr+reverseFuncMap["ListValidators"], values)
resp, err := http.PostForm(c.addr+reverseFuncMap["ListUnconfirmedTxs"], values)
if err != nil {
return nil, err
}
@ -439,10 +470,10 @@ func (c *ClientHTTP) ListValidators() (*ctypes.ResponseListValidators, error) {
return nil, err
}
var response struct {
Result *ctypes.ResponseListValidators `json:"result"`
Error string `json:"error"`
Id string `json:"id"`
JSONRPC string `json:"jsonrpc"`
Result *ctypes.ResponseListUnconfirmedTxs `json:"result"`
Error string `json:"error"`
Id string `json:"id"`
JSONRPC string `json:"jsonrpc"`
}
binary.ReadJSON(&response, body, &err)
if err != nil {
@ -454,12 +485,12 @@ func (c *ClientHTTP) ListValidators() (*ctypes.ResponseListValidators, error) {
return response.Result, nil
}
func (c *ClientHTTP) NameRegEntry(name string) (*ctypes.ResponseNameRegEntry, error) {
values, err := argsToURLValues([]string{"name"}, name)
func (c *ClientHTTP) ListValidators() (*ctypes.ResponseListValidators, error) {
values, err := argsToURLValues(nil)
if err != nil {
return nil, err
}
resp, err := http.PostForm(c.addr+reverseFuncMap["NameRegEntry"], values)
resp, err := http.PostForm(c.addr+reverseFuncMap["ListValidators"], values)
if err != nil {
return nil, err
}
@ -469,10 +500,10 @@ func (c *ClientHTTP) NameRegEntry(name string) (*ctypes.ResponseNameRegEntry, er
return nil, err
}
var response struct {
Result *ctypes.ResponseNameRegEntry `json:"result"`
Error string `json:"error"`
Id string `json:"id"`
JSONRPC string `json:"jsonrpc"`
Result *ctypes.ResponseListValidators `json:"result"`
Error string `json:"error"`
Id string `json:"id"`
JSONRPC string `json:"jsonrpc"`
}
binary.ReadJSON(&response, body, &err)
if err != nil {
@ -844,6 +875,33 @@ func (c *ClientJSON) GetBlock(height uint) (*ctypes.ResponseGetBlock, error) {
return response.Result, nil
}
func (c *ClientJSON) GetName(name string) (*types.NameRegEntry, error) {
request := rpctypes.RPCRequest{
JSONRPC: "2.0",
Method: reverseFuncMap["GetName"],
Params: []interface{}{name},
Id: 0,
}
body, err := c.RequestResponse(request)
if err != nil {
return nil, err
}
var response struct {
Result *types.NameRegEntry `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, key []byte) (*ctypes.ResponseGetStorage, error) {
request := rpctypes.RPCRequest{
JSONRPC: "2.0",
@ -898,10 +956,10 @@ func (c *ClientJSON) ListAccounts() (*ctypes.ResponseListAccounts, error) {
return response.Result, nil
}
func (c *ClientJSON) ListUnconfirmedTxs() (*ctypes.ResponseListUnconfirmedTxs, error) {
func (c *ClientJSON) ListNames() (*ctypes.ResponseListNames, error) {
request := rpctypes.RPCRequest{
JSONRPC: "2.0",
Method: reverseFuncMap["ListUnconfirmedTxs"],
Method: reverseFuncMap["ListNames"],
Params: []interface{}{},
Id: 0,
}
@ -910,10 +968,10 @@ func (c *ClientJSON) ListUnconfirmedTxs() (*ctypes.ResponseListUnconfirmedTxs, e
return nil, err
}
var response struct {
Result *ctypes.ResponseListUnconfirmedTxs `json:"result"`
Error string `json:"error"`
Id string `json:"id"`
JSONRPC string `json:"jsonrpc"`
Result *ctypes.ResponseListNames `json:"result"`
Error string `json:"error"`
Id string `json:"id"`
JSONRPC string `json:"jsonrpc"`
}
binary.ReadJSON(&response, body, &err)
if err != nil {
@ -925,10 +983,10 @@ func (c *ClientJSON) ListUnconfirmedTxs() (*ctypes.ResponseListUnconfirmedTxs, e
return response.Result, nil
}
func (c *ClientJSON) ListValidators() (*ctypes.ResponseListValidators, error) {
func (c *ClientJSON) ListUnconfirmedTxs() (*ctypes.ResponseListUnconfirmedTxs, error) {
request := rpctypes.RPCRequest{
JSONRPC: "2.0",
Method: reverseFuncMap["ListValidators"],
Method: reverseFuncMap["ListUnconfirmedTxs"],
Params: []interface{}{},
Id: 0,
}
@ -937,10 +995,10 @@ func (c *ClientJSON) ListValidators() (*ctypes.ResponseListValidators, error) {
return nil, err
}
var response struct {
Result *ctypes.ResponseListValidators `json:"result"`
Error string `json:"error"`
Id string `json:"id"`
JSONRPC string `json:"jsonrpc"`
Result *ctypes.ResponseListUnconfirmedTxs `json:"result"`
Error string `json:"error"`
Id string `json:"id"`
JSONRPC string `json:"jsonrpc"`
}
binary.ReadJSON(&response, body, &err)
if err != nil {
@ -952,11 +1010,11 @@ func (c *ClientJSON) ListValidators() (*ctypes.ResponseListValidators, error) {
return response.Result, nil
}
func (c *ClientJSON) NameRegEntry(name string) (*ctypes.ResponseNameRegEntry, error) {
func (c *ClientJSON) ListValidators() (*ctypes.ResponseListValidators, error) {
request := rpctypes.RPCRequest{
JSONRPC: "2.0",
Method: reverseFuncMap["NameRegEntry"],
Params: []interface{}{name},
Method: reverseFuncMap["ListValidators"],
Params: []interface{}{},
Id: 0,
}
body, err := c.RequestResponse(request)
@ -964,10 +1022,10 @@ func (c *ClientJSON) NameRegEntry(name string) (*ctypes.ResponseNameRegEntry, er
return nil, err
}
var response struct {
Result *ctypes.ResponseNameRegEntry `json:"result"`
Error string `json:"error"`
Id string `json:"id"`
JSONRPC string `json:"jsonrpc"`
Result *ctypes.ResponseListValidators `json:"result"`
Error string `json:"error"`
Id string `json:"id"`
JSONRPC string `json:"jsonrpc"`
}
binary.ReadJSON(&response, body, &err)
if err != nil {


+ 2
- 2
rpc/test/helpers.go View File

@ -212,11 +212,11 @@ func callContract(t *testing.T, client cclient.Client, address, data, expected [
// get the namereg entry
func getNameRegEntry(t *testing.T, typ string, name string) *types.NameRegEntry {
client := clients[typ]
r, err := client.NameRegEntry(name)
entry, err := client.GetName(name)
if err != nil {
t.Fatal(err)
}
return r.Entry
return entry
}
//--------------------------------------------------------------------------------


Loading…
Cancel
Save