mirror of
https://github.com/tendermint/tendermint.git
synced 2026-09-20 06:54:41 +00:00
RPC refactor to separate core from core_client and the rest of RPC.
Other random changes.
This commit is contained in:
+14
-13
@@ -4,18 +4,19 @@ import (
|
||||
"fmt"
|
||||
"github.com/tendermint/tendermint/account"
|
||||
. "github.com/tendermint/tendermint/common"
|
||||
ctypes "github.com/tendermint/tendermint/rpc/core/types"
|
||||
)
|
||||
|
||||
func GenPrivAccount() (*ResponseGenPrivAccount, error) {
|
||||
return &ResponseGenPrivAccount{account.GenPrivAccount()}, nil
|
||||
func GenPrivAccount() (*ctypes.ResponseGenPrivAccount, error) {
|
||||
return &ctypes.ResponseGenPrivAccount{account.GenPrivAccount()}, nil
|
||||
}
|
||||
|
||||
func GetAccount(address []byte) (*ResponseGetAccount, error) {
|
||||
func GetAccount(address []byte) (*ctypes.ResponseGetAccount, error) {
|
||||
cache := mempoolReactor.Mempool.GetCache()
|
||||
return &ResponseGetAccount{cache.GetAccount(address)}, nil
|
||||
return &ctypes.ResponseGetAccount{cache.GetAccount(address)}, nil
|
||||
}
|
||||
|
||||
func GetStorage(address, slot []byte) (*ResponseGetStorage, error) {
|
||||
func GetStorage(address, slot []byte) (*ctypes.ResponseGetStorage, error) {
|
||||
state := consensusState.GetState()
|
||||
account := state.GetAccount(address)
|
||||
if account == nil {
|
||||
@@ -26,12 +27,12 @@ func GetStorage(address, slot []byte) (*ResponseGetStorage, error) {
|
||||
|
||||
_, value := storage.Get(RightPadWord256(slot).Bytes())
|
||||
if value == nil {
|
||||
return &ResponseGetStorage{slot, nil}, nil
|
||||
return &ctypes.ResponseGetStorage{slot, nil}, nil
|
||||
}
|
||||
return &ResponseGetStorage{slot, value.([]byte)}, nil
|
||||
return &ctypes.ResponseGetStorage{slot, value.([]byte)}, nil
|
||||
}
|
||||
|
||||
func ListAccounts() (*ResponseListAccounts, error) {
|
||||
func ListAccounts() (*ctypes.ResponseListAccounts, error) {
|
||||
var blockHeight uint
|
||||
var accounts []*account.Account
|
||||
state := consensusState.GetState()
|
||||
@@ -40,10 +41,10 @@ func ListAccounts() (*ResponseListAccounts, error) {
|
||||
accounts = append(accounts, value.(*account.Account))
|
||||
return false
|
||||
})
|
||||
return &ResponseListAccounts{blockHeight, accounts}, nil
|
||||
return &ctypes.ResponseListAccounts{blockHeight, accounts}, nil
|
||||
}
|
||||
|
||||
func DumpStorage(addr []byte) (*ResponseDumpStorage, error) {
|
||||
func DumpStorage(addr []byte) (*ctypes.ResponseDumpStorage, error) {
|
||||
state := consensusState.GetState()
|
||||
account := state.GetAccount(addr)
|
||||
if account == nil {
|
||||
@@ -51,11 +52,11 @@ func DumpStorage(addr []byte) (*ResponseDumpStorage, error) {
|
||||
}
|
||||
storageRoot := account.StorageRoot
|
||||
storage := state.LoadStorage(storageRoot)
|
||||
storageItems := []StorageItem{}
|
||||
storageItems := []ctypes.StorageItem{}
|
||||
storage.Iterate(func(key interface{}, value interface{}) bool {
|
||||
storageItems = append(storageItems, StorageItem{
|
||||
storageItems = append(storageItems, ctypes.StorageItem{
|
||||
key.([]byte), value.([]byte)})
|
||||
return false
|
||||
})
|
||||
return &ResponseDumpStorage{storageRoot, storageItems}, nil
|
||||
return &ctypes.ResponseDumpStorage{storageRoot, storageItems}, nil
|
||||
}
|
||||
|
||||
+5
-4
@@ -3,12 +3,13 @@ package core
|
||||
import (
|
||||
"fmt"
|
||||
. "github.com/tendermint/tendermint/common"
|
||||
ctypes "github.com/tendermint/tendermint/rpc/core/types"
|
||||
"github.com/tendermint/tendermint/types"
|
||||
)
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
func BlockchainInfo(minHeight, maxHeight uint) (*ResponseBlockchainInfo, error) {
|
||||
func BlockchainInfo(minHeight, maxHeight uint) (*ctypes.ResponseBlockchainInfo, error) {
|
||||
if maxHeight == 0 {
|
||||
maxHeight = blockStore.Height()
|
||||
} else {
|
||||
@@ -25,12 +26,12 @@ func BlockchainInfo(minHeight, maxHeight uint) (*ResponseBlockchainInfo, error)
|
||||
blockMetas = append(blockMetas, blockMeta)
|
||||
}
|
||||
|
||||
return &ResponseBlockchainInfo{blockStore.Height(), blockMetas}, nil
|
||||
return &ctypes.ResponseBlockchainInfo{blockStore.Height(), blockMetas}, nil
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
func GetBlock(height uint) (*ResponseGetBlock, error) {
|
||||
func GetBlock(height uint) (*ctypes.ResponseGetBlock, error) {
|
||||
if height == 0 {
|
||||
return nil, fmt.Errorf("height must be greater than 0")
|
||||
}
|
||||
@@ -40,5 +41,5 @@ func GetBlock(height uint) (*ResponseGetBlock, error) {
|
||||
|
||||
blockMeta := blockStore.LoadBlockMeta(height)
|
||||
block := blockStore.LoadBlock(height)
|
||||
return &ResponseGetBlock{blockMeta, block}, nil
|
||||
return &ctypes.ResponseGetBlock{blockMeta, block}, nil
|
||||
}
|
||||
|
||||
+3
-8
@@ -3,21 +3,16 @@ package core
|
||||
import (
|
||||
"fmt"
|
||||
. "github.com/tendermint/tendermint/common"
|
||||
ctypes "github.com/tendermint/tendermint/rpc/core/types"
|
||||
"github.com/tendermint/tendermint/state"
|
||||
"github.com/tendermint/tendermint/types"
|
||||
)
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
type Receipt struct {
|
||||
TxHash []byte
|
||||
CreatesContract uint8
|
||||
ContractAddr []byte
|
||||
}
|
||||
|
||||
// pass pointer?
|
||||
// Note: tx must be signed
|
||||
func BroadcastTx(tx types.Tx) (*ResponseBroadcastTx, error) {
|
||||
func BroadcastTx(tx types.Tx) (*ctypes.ResponseBroadcastTx, error) {
|
||||
err := mempoolReactor.BroadcastTx(tx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Error broadcasting transaction: %v", err)
|
||||
@@ -33,7 +28,7 @@ func BroadcastTx(tx types.Tx) (*ResponseBroadcastTx, error) {
|
||||
contractAddr = state.NewContractAddress(callTx.Input.Address, uint64(callTx.Input.Sequence))
|
||||
}
|
||||
}
|
||||
return &ResponseBroadcastTx{Receipt{txHash, createsContract, contractAddr}}, nil
|
||||
return &ctypes.ResponseBroadcastTx{ctypes.Receipt{txHash, createsContract, contractAddr}}, nil
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
+5
-4
@@ -3,13 +3,14 @@ package core
|
||||
import (
|
||||
"github.com/tendermint/tendermint/config"
|
||||
dbm "github.com/tendermint/tendermint/db"
|
||||
ctypes "github.com/tendermint/tendermint/rpc/core/types"
|
||||
sm "github.com/tendermint/tendermint/state"
|
||||
"github.com/tendermint/tendermint/types"
|
||||
)
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
func Status() (*ResponseStatus, error) {
|
||||
func Status() (*ctypes.ResponseStatus, error) {
|
||||
db := dbm.NewMemDB()
|
||||
genesisState := sm.MakeGenesisStateFromFile(db, config.App().GetString("GenesisFile"))
|
||||
genesisHash := genesisState.Hash()
|
||||
@@ -25,12 +26,12 @@ func Status() (*ResponseStatus, error) {
|
||||
latestBlockTime = latestBlockMeta.Header.Time.UnixNano()
|
||||
}
|
||||
|
||||
return &ResponseStatus{genesisHash, config.App().GetString("Network"), latestBlockHash, latestHeight, latestBlockTime}, nil
|
||||
return &ctypes.ResponseStatus{genesisHash, config.App().GetString("Network"), latestBlockHash, latestHeight, latestBlockTime}, nil
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
func NetInfo() (*ResponseNetInfo, error) {
|
||||
func NetInfo() (*ctypes.ResponseNetInfo, error) {
|
||||
listening := p2pSwitch.IsListening()
|
||||
network := config.App().GetString("Network")
|
||||
listeners := []string{}
|
||||
@@ -41,7 +42,7 @@ func NetInfo() (*ResponseNetInfo, error) {
|
||||
for _, peer := range p2pSwitch.Peers().List() {
|
||||
peers = append(peers, peer.String())
|
||||
}
|
||||
return &ResponseNetInfo{
|
||||
return &ctypes.ResponseNetInfo{
|
||||
Network: network,
|
||||
Listening: listening,
|
||||
Listeners: listeners,
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"github.com/tendermint/tendermint/rpc"
|
||||
)
|
||||
|
||||
/*
|
||||
TODO: support Call && GetStorage.
|
||||
*/
|
||||
|
||||
var Routes = map[string]*rpc.RPCFunc{
|
||||
"status": rpc.NewRPCFunc(Status, []string{}),
|
||||
"net_info": rpc.NewRPCFunc(NetInfo, []string{}),
|
||||
"blockchain": rpc.NewRPCFunc(BlockchainInfo, []string{"min_height", "max_height"}),
|
||||
"get_block": rpc.NewRPCFunc(GetBlock, []string{"height"}),
|
||||
"get_account": rpc.NewRPCFunc(GetAccount, []string{"address"}),
|
||||
"get_storage": rpc.NewRPCFunc(GetStorage, []string{"address", "storage"}),
|
||||
"call": rpc.NewRPCFunc(Call, []string{"address", "data"}),
|
||||
"list_validators": rpc.NewRPCFunc(ListValidators, []string{}),
|
||||
"dump_storage": rpc.NewRPCFunc(DumpStorage, []string{"address"}),
|
||||
"broadcast_tx": rpc.NewRPCFunc(BroadcastTx, []string{"tx"}),
|
||||
"list_accounts": rpc.NewRPCFunc(ListAccounts, []string{}),
|
||||
"unsafe/gen_priv_account": rpc.NewRPCFunc(GenPrivAccount, []string{}),
|
||||
"unsafe/sign_tx": rpc.NewRPCFunc(SignTx, []string{"tx", "privAccounts"}),
|
||||
}
|
||||
+5
-4
@@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
"github.com/tendermint/tendermint/account"
|
||||
. "github.com/tendermint/tendermint/common"
|
||||
ctypes "github.com/tendermint/tendermint/rpc/core/types"
|
||||
"github.com/tendermint/tendermint/state"
|
||||
"github.com/tendermint/tendermint/types"
|
||||
"github.com/tendermint/tendermint/vm"
|
||||
@@ -24,7 +25,7 @@ func toVMAccount(acc *account.Account) *vm.Account {
|
||||
|
||||
// Run a contract's code on an isolated and unpersisted state
|
||||
// Cannot be used to create new contracts
|
||||
func Call(address, data []byte) (*ResponseCall, error) {
|
||||
func Call(address, data []byte) (*ctypes.ResponseCall, error) {
|
||||
|
||||
st := consensusState.GetState() // performs a copy
|
||||
cache := mempoolReactor.Mempool.GetCache()
|
||||
@@ -48,12 +49,12 @@ func Call(address, data []byte) (*ResponseCall, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &ResponseCall{Return: ret}, nil
|
||||
return &ctypes.ResponseCall{Return: ret}, nil
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
func SignTx(tx types.Tx, privAccounts []*account.PrivAccount) (*ResponseSignTx, error) {
|
||||
func SignTx(tx types.Tx, privAccounts []*account.PrivAccount) (*ctypes.ResponseSignTx, error) {
|
||||
// more checks?
|
||||
|
||||
for i, privAccount := range privAccounts {
|
||||
@@ -85,5 +86,5 @@ func SignTx(tx types.Tx, privAccounts []*account.PrivAccount) (*ResponseSignTx,
|
||||
rebondTx := tx.(*types.RebondTx)
|
||||
rebondTx.Signature = privAccounts[0].Sign(rebondTx).(account.SignatureEd25519)
|
||||
}
|
||||
return &ResponseSignTx{tx}, nil
|
||||
return &ctypes.ResponseSignTx{tx}, nil
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package core
|
||||
package core_types
|
||||
|
||||
import (
|
||||
"github.com/tendermint/tendermint/account"
|
||||
@@ -55,6 +55,12 @@ type ResponseBroadcastTx struct {
|
||||
Receipt Receipt
|
||||
}
|
||||
|
||||
type Receipt struct {
|
||||
TxHash []byte
|
||||
CreatesContract uint8
|
||||
ContractAddr []byte
|
||||
}
|
||||
|
||||
type ResponseStatus struct {
|
||||
GenesisHash []byte
|
||||
Network string
|
||||
@@ -1,12 +1,13 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
ctypes "github.com/tendermint/tendermint/rpc/core/types"
|
||||
sm "github.com/tendermint/tendermint/state"
|
||||
)
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
func ListValidators() (*ResponseListValidators, error) {
|
||||
func ListValidators() (*ctypes.ResponseListValidators, error) {
|
||||
var blockHeight uint
|
||||
var bondedValidators []*sm.Validator
|
||||
var unbondingValidators []*sm.Validator
|
||||
@@ -22,5 +23,5 @@ func ListValidators() (*ResponseListValidators, error) {
|
||||
return false
|
||||
})
|
||||
|
||||
return &ResponseListValidators{blockHeight, bondedValidators, unbondingValidators}, nil
|
||||
return &ctypes.ResponseListValidators{blockHeight, bondedValidators, unbondingValidators}, nil
|
||||
}
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
package rpc
|
||||
package core_client
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"github.com/tendermint/tendermint/binary"
|
||||
"github.com/tendermint/tendermint/rpc"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
@@ -18,7 +19,7 @@ type Response struct {
|
||||
Error string
|
||||
}
|
||||
|
||||
//go:generate go-rpc-gen -interface Client -pkg core -type *ClientHTTP,*ClientJSON -exclude pipe.go -out-pkg rpc
|
||||
//go:generate go-rpc-gen -interface Client -dir ../core -pkg core -type *ClientHTTP,*ClientJSON -exclude pipe.go -out-pkg core_client
|
||||
|
||||
type ClientJSON struct {
|
||||
addr string
|
||||
@@ -99,7 +100,7 @@ func (c *ClientHTTP) RequestResponse(method string, values url.Values) (*Respons
|
||||
return response, nil
|
||||
}
|
||||
|
||||
func (c *ClientJSON) RequestResponse(s RPCRequest) (b []byte, err error) {
|
||||
func (c *ClientJSON) RequestResponse(s rpc.RPCRequest) (b []byte, err error) {
|
||||
b = binary.JSONBytes(s)
|
||||
buf := bytes.NewBuffer(b)
|
||||
resp, err := http.Post(c.addr, "text/json", buf)
|
||||
@@ -165,6 +166,7 @@ func argsToURLValues(argNames []string, args ...interface{}) (url.Values, error)
|
||||
|
||||
/*rpc-gen:imports:
|
||||
github.com/tendermint/tendermint/binary
|
||||
github.com/tendermint/tendermint/rpc
|
||||
net/http
|
||||
io/ioutil
|
||||
fmt
|
||||
@@ -173,7 +175,7 @@ fmt
|
||||
// Template functions to be filled in
|
||||
|
||||
/*rpc-gen:template:*ClientJSON func (c *ClientJSON) {{name}}({{args.def}}) ({{response}}) {
|
||||
request := RPCRequest{
|
||||
request := rpc.RPCRequest{
|
||||
JSONRPC: "2.0",
|
||||
Method: {{lowername}},
|
||||
Params: []interface{}{ {{args.ident}} },
|
||||
@@ -0,0 +1,769 @@
|
||||
package core_client
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/tendermint/tendermint/account"
|
||||
"github.com/tendermint/tendermint/binary"
|
||||
"github.com/tendermint/tendermint/rpc"
|
||||
ctypes "github.com/tendermint/tendermint/rpc/core/types"
|
||||
"github.com/tendermint/tendermint/types"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type Client interface {
|
||||
BlockchainInfo(minHeight uint) (*ctypes.ResponseBlockchainInfo, error)
|
||||
BroadcastTx(tx types.Tx) (*ctypes.ResponseBroadcastTx, error)
|
||||
Call(address []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)
|
||||
ListAccounts() (*ctypes.ResponseListAccounts, error)
|
||||
ListValidators() (*ctypes.ResponseListValidators, error)
|
||||
NetInfo() (*ctypes.ResponseNetInfo, error)
|
||||
SignTx(tx types.Tx, privAccounts []*account.PrivAccount) (*ctypes.ResponseSignTx, error)
|
||||
Status() (*ctypes.ResponseStatus, error)
|
||||
}
|
||||
|
||||
func (c *ClientHTTP) BlockchainInfo(minHeight uint) (*ctypes.ResponseBlockchainInfo, error) {
|
||||
values, err := argsToURLValues([]string{"minHeight"}, minHeight)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := http.PostForm(c.addr+"blockchain_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 *ctypes.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 *ClientHTTP) BroadcastTx(tx types.Tx) (*ctypes.ResponseBroadcastTx, error) {
|
||||
values, err := argsToURLValues([]string{"tx"}, tx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := http.PostForm(c.addr+"broadcast_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 *ctypes.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 *ClientHTTP) Call(address []byte) (*ctypes.ResponseCall, error) {
|
||||
values, err := argsToURLValues([]string{"address"}, address)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := http.PostForm(c.addr+"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) DumpStorage(addr []byte) (*ctypes.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 *ctypes.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) GenPrivAccount() (*ctypes.ResponseGenPrivAccount, error) {
|
||||
values, err := argsToURLValues(nil, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := http.PostForm(c.addr+"gen_priv_account", 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.ResponseGenPrivAccount `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) GetAccount(address []byte) (*ctypes.ResponseGetAccount, error) {
|
||||
values, err := argsToURLValues([]string{"address"}, address)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := http.PostForm(c.addr+"get_account", 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.ResponseGetAccount `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) GetBlock(height uint) (*ctypes.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 *ctypes.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) (*ctypes.ResponseGetStorage, error) {
|
||||
values, err := argsToURLValues([]string{"address"}, address)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := http.PostForm(c.addr+"get_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 *ctypes.ResponseGetStorage `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() (*ctypes.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 *ctypes.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) ListValidators() (*ctypes.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 *ctypes.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 *ClientHTTP) NetInfo() (*ctypes.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 *ctypes.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) (*ctypes.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 *ctypes.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() (*ctypes.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 *ctypes.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) (*ctypes.ResponseBlockchainInfo, error) {
|
||||
request := rpc.RPCRequest{
|
||||
JSONRPC: "2.0",
|
||||
Method: "blockchain_info",
|
||||
Params: []interface{}{minHeight},
|
||||
Id: 0,
|
||||
}
|
||||
body, err := c.RequestResponse(request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var response struct {
|
||||
Result *ctypes.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) (*ctypes.ResponseBroadcastTx, error) {
|
||||
request := rpc.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 *ctypes.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) (*ctypes.ResponseCall, error) {
|
||||
request := rpc.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 *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) DumpStorage(addr []byte) (*ctypes.ResponseDumpStorage, error) {
|
||||
request := rpc.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 *ctypes.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() (*ctypes.ResponseGenPrivAccount, error) {
|
||||
request := rpc.RPCRequest{
|
||||
JSONRPC: "2.0",
|
||||
Method: "gen_priv_account",
|
||||
Params: []interface{}{nil},
|
||||
Id: 0,
|
||||
}
|
||||
body, err := c.RequestResponse(request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var response struct {
|
||||
Result *ctypes.ResponseGenPrivAccount `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) GetAccount(address []byte) (*ctypes.ResponseGetAccount, error) {
|
||||
request := rpc.RPCRequest{
|
||||
JSONRPC: "2.0",
|
||||
Method: "get_account",
|
||||
Params: []interface{}{address},
|
||||
Id: 0,
|
||||
}
|
||||
body, err := c.RequestResponse(request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var response struct {
|
||||
Result *ctypes.ResponseGetAccount `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) GetBlock(height uint) (*ctypes.ResponseGetBlock, error) {
|
||||
request := rpc.RPCRequest{
|
||||
JSONRPC: "2.0",
|
||||
Method: "get_block",
|
||||
Params: []interface{}{height},
|
||||
Id: 0,
|
||||
}
|
||||
body, err := c.RequestResponse(request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var response struct {
|
||||
Result *ctypes.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) (*ctypes.ResponseGetStorage, error) {
|
||||
request := rpc.RPCRequest{
|
||||
JSONRPC: "2.0",
|
||||
Method: "get_storage",
|
||||
Params: []interface{}{address},
|
||||
Id: 0,
|
||||
}
|
||||
body, err := c.RequestResponse(request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var response struct {
|
||||
Result *ctypes.ResponseGetStorage `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() (*ctypes.ResponseListAccounts, error) {
|
||||
request := rpc.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 *ctypes.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() (*ctypes.ResponseListValidators, error) {
|
||||
request := rpc.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 *ctypes.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() (*ctypes.ResponseNetInfo, error) {
|
||||
request := rpc.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 *ctypes.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) (*ctypes.ResponseSignTx, error) {
|
||||
request := rpc.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 *ctypes.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() (*ctypes.ResponseStatus, error) {
|
||||
request := rpc.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 *ctypes.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
|
||||
}
|
||||
+42
-65
@@ -1,59 +1,34 @@
|
||||
package rpc
|
||||
|
||||
/*
|
||||
TODO: support Call && GetStorage.
|
||||
*/
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/tendermint/tendermint/binary"
|
||||
"github.com/tendermint/tendermint/rpc/core"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// 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{}),
|
||||
"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"}),
|
||||
}
|
||||
|
||||
func initHandlers() {
|
||||
func RegisterRPCFuncs(mux *http.ServeMux, funcMap map[string]*RPCFunc) {
|
||||
// HTTP endpoints
|
||||
for funcName, funcInfo := range funcMap {
|
||||
http.HandleFunc("/"+funcName, toHTTPHandler(funcInfo))
|
||||
for funcName, rpcFunc := range funcMap {
|
||||
mux.HandleFunc("/"+funcName, makeHTTPHandler(rpcFunc))
|
||||
}
|
||||
|
||||
// JSONRPC endpoints
|
||||
http.HandleFunc("/", JSONRPCHandler)
|
||||
mux.HandleFunc("/", makeJSONRPCHandler(funcMap))
|
||||
}
|
||||
|
||||
//-------------------------------------
|
||||
|
||||
// holds all type information for each function
|
||||
type FuncWrapper struct {
|
||||
f reflect.Value // function from "rpc/core"
|
||||
type RPCFunc struct {
|
||||
f reflect.Value // underlying rpc function
|
||||
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{
|
||||
func NewRPCFunc(f interface{}, args []string) *RPCFunc {
|
||||
return &RPCFunc{
|
||||
f: reflect.ValueOf(f),
|
||||
args: funcArgTypes(f),
|
||||
returns: funcReturnTypes(f),
|
||||
@@ -85,39 +60,41 @@ func funcReturnTypes(f interface{}) []reflect.Type {
|
||||
// rpc.json
|
||||
|
||||
// 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 request RPCRequest
|
||||
err := json.Unmarshal(b, &request)
|
||||
if err != nil {
|
||||
WriteRPCResponse(w, NewRPCResponse(nil, err.Error()))
|
||||
return
|
||||
}
|
||||
func makeJSONRPCHandler(funcMap map[string]*RPCFunc) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
b, _ := ioutil.ReadAll(r.Body)
|
||||
var request RPCRequest
|
||||
err := json.Unmarshal(b, &request)
|
||||
if err != nil {
|
||||
WriteRPCResponse(w, NewRPCResponse(nil, err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
funcInfo := funcMap[request.Method]
|
||||
if funcInfo == nil {
|
||||
WriteRPCResponse(w, NewRPCResponse(nil, "RPC method unknown: "+request.Method))
|
||||
return
|
||||
rpcFunc := funcMap[request.Method]
|
||||
if rpcFunc == nil {
|
||||
WriteRPCResponse(w, NewRPCResponse(nil, "RPC method unknown: "+request.Method))
|
||||
return
|
||||
}
|
||||
args, err := jsonParamsToArgs(rpcFunc, request.Params)
|
||||
if err != nil {
|
||||
WriteRPCResponse(w, NewRPCResponse(nil, err.Error()))
|
||||
return
|
||||
}
|
||||
returns := rpcFunc.f.Call(args)
|
||||
response, err := unreflectResponse(returns)
|
||||
if err != nil {
|
||||
WriteRPCResponse(w, NewRPCResponse(nil, err.Error()))
|
||||
return
|
||||
}
|
||||
WriteRPCResponse(w, NewRPCResponse(response, ""))
|
||||
}
|
||||
args, err := jsonParamsToArgs(funcInfo, request.Params)
|
||||
if err != nil {
|
||||
WriteRPCResponse(w, NewRPCResponse(nil, err.Error()))
|
||||
return
|
||||
}
|
||||
returns := funcInfo.f.Call(args)
|
||||
response, err := unreflectResponse(returns)
|
||||
if err != nil {
|
||||
WriteRPCResponse(w, NewRPCResponse(nil, err.Error()))
|
||||
return
|
||||
}
|
||||
WriteRPCResponse(w, NewRPCResponse(response, ""))
|
||||
}
|
||||
|
||||
// covert a list of interfaces to properly typed values
|
||||
func jsonParamsToArgs(funcInfo *FuncWrapper, params []interface{}) ([]reflect.Value, error) {
|
||||
func jsonParamsToArgs(rpcFunc *RPCFunc, params []interface{}) ([]reflect.Value, error) {
|
||||
values := make([]reflect.Value, len(params))
|
||||
for i, p := range params {
|
||||
ty := funcInfo.args[i]
|
||||
ty := rpcFunc.args[i]
|
||||
v, err := _jsonObjectToArg(ty, p)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -143,14 +120,14 @@ func _jsonObjectToArg(ty reflect.Type, object interface{}) (reflect.Value, error
|
||||
// rpc.http
|
||||
|
||||
// convert from a function name to the http handler
|
||||
func toHTTPHandler(funcInfo *FuncWrapper) func(http.ResponseWriter, *http.Request) {
|
||||
func makeHTTPHandler(rpcFunc *RPCFunc) func(http.ResponseWriter, *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
args, err := httpParamsToArgs(funcInfo, r)
|
||||
args, err := httpParamsToArgs(rpcFunc, r)
|
||||
if err != nil {
|
||||
WriteRPCResponse(w, NewRPCResponse(nil, err.Error()))
|
||||
return
|
||||
}
|
||||
returns := funcInfo.f.Call(args)
|
||||
returns := rpcFunc.f.Call(args)
|
||||
response, err := unreflectResponse(returns)
|
||||
if err != nil {
|
||||
WriteRPCResponse(w, NewRPCResponse(nil, err.Error()))
|
||||
@@ -162,9 +139,9 @@ func toHTTPHandler(funcInfo *FuncWrapper) func(http.ResponseWriter, *http.Reques
|
||||
|
||||
// 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
|
||||
func httpParamsToArgs(rpcFunc *RPCFunc, r *http.Request) ([]reflect.Value, error) {
|
||||
argTypes := rpcFunc.args
|
||||
argNames := rpcFunc.argNames
|
||||
|
||||
var err error
|
||||
values := make([]reflect.Value, len(argNames))
|
||||
|
||||
+9
-16
@@ -8,18 +8,20 @@ import (
|
||||
"runtime/debug"
|
||||
"time"
|
||||
|
||||
"github.com/tendermint/tendermint/alert"
|
||||
"github.com/tendermint/tendermint/binary"
|
||||
. "github.com/tendermint/tendermint/common"
|
||||
"github.com/tendermint/tendermint/config"
|
||||
)
|
||||
|
||||
func StartHTTPServer() {
|
||||
initHandlers()
|
||||
|
||||
log.Info(Fmt("Starting RPC HTTP server on %s", config.App().GetString("RPC.HTTP.ListenAddr")))
|
||||
func StartHTTPServer(listenAddr string, funcMap map[string]*RPCFunc) {
|
||||
log.Info(Fmt("Starting RPC HTTP server on %s", listenAddr))
|
||||
mux := http.NewServeMux()
|
||||
RegisterRPCFuncs(mux, funcMap)
|
||||
go func() {
|
||||
log.Crit("RPC HTTPServer stopped", "result", http.ListenAndServe(config.App().GetString("RPC.HTTP.ListenAddr"), RecoverAndLogHandler(http.DefaultServeMux)))
|
||||
res := http.ListenAndServe(
|
||||
listenAddr,
|
||||
RecoverAndLogHandler(mux),
|
||||
)
|
||||
log.Crit("RPC HTTPServer stopped", "result", res)
|
||||
}()
|
||||
}
|
||||
|
||||
@@ -97,12 +99,3 @@ func (w *ResponseWriterWrapper) WriteHeader(status int) {
|
||||
w.Status = status
|
||||
w.ResponseWriter.WriteHeader(status)
|
||||
}
|
||||
|
||||
// Stick it as a deferred statement in gouroutines to prevent the program from crashing.
|
||||
func Recover(daemonName string) {
|
||||
if e := recover(); e != nil {
|
||||
stack := string(debug.Stack())
|
||||
errorString := fmt.Sprintf("[%s] %s\n%s", daemonName, e, stack)
|
||||
alert.Alert(errorString)
|
||||
}
|
||||
}
|
||||
|
||||
+10
-10
@@ -7,7 +7,7 @@ import (
|
||||
"github.com/tendermint/tendermint/binary"
|
||||
. "github.com/tendermint/tendermint/common"
|
||||
"github.com/tendermint/tendermint/merkle"
|
||||
"github.com/tendermint/tendermint/rpc/core"
|
||||
ctypes "github.com/tendermint/tendermint/rpc/core/types"
|
||||
"github.com/tendermint/tendermint/state"
|
||||
"github.com/tendermint/tendermint/types"
|
||||
"io/ioutil"
|
||||
@@ -27,10 +27,10 @@ func TestHTTPStatus(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var response struct {
|
||||
Result core.ResponseStatus `json:"result"`
|
||||
Error string `json:"error"`
|
||||
Id string `json:"id"`
|
||||
JSONRPC string `json:"jsonrpc"`
|
||||
Result ctypes.ResponseStatus `json:"result"`
|
||||
Error string `json:"error"`
|
||||
Id string `json:"id"`
|
||||
JSONRPC string `json:"jsonrpc"`
|
||||
}
|
||||
binary.ReadJSON(&response, body, &err)
|
||||
if err != nil {
|
||||
@@ -55,10 +55,10 @@ func TestHTTPGenPriv(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var response struct {
|
||||
Result core.ResponseGenPrivAccount `json:"result"`
|
||||
Error string `json:"error"`
|
||||
Id string `json:"id"`
|
||||
JSONRPC string `json:"jsonrpc"`
|
||||
Result ctypes.ResponseGenPrivAccount `json:"result"`
|
||||
Error string `json:"error"`
|
||||
Id string `json:"id"`
|
||||
JSONRPC string `json:"jsonrpc"`
|
||||
}
|
||||
binary.ReadJSON(&response, body, &err)
|
||||
if err != nil {
|
||||
@@ -130,7 +130,7 @@ func TestHTTPBroadcastTx(t *testing.T) {
|
||||
func TestHTTPGetStorage(t *testing.T) {
|
||||
priv := state.LoadPrivValidator(".tendermint/priv_validator.json")
|
||||
_ = priv
|
||||
//core.SetPrivValidator(priv)
|
||||
//ctypes.SetPrivValidator(priv)
|
||||
|
||||
byteAddr, _ := hex.DecodeString(userAddr)
|
||||
var byteKey [64]byte
|
||||
|
||||
+13
-13
@@ -9,7 +9,7 @@ import (
|
||||
. "github.com/tendermint/tendermint/common"
|
||||
"github.com/tendermint/tendermint/config"
|
||||
"github.com/tendermint/tendermint/rpc"
|
||||
"github.com/tendermint/tendermint/rpc/core"
|
||||
ctypes "github.com/tendermint/tendermint/rpc/core/types"
|
||||
"github.com/tendermint/tendermint/types"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
@@ -40,10 +40,10 @@ func TestJSONStatus(t *testing.T) {
|
||||
}
|
||||
|
||||
var response struct {
|
||||
Result core.ResponseStatus `json:"result"`
|
||||
Error string `json:"error"`
|
||||
Id string `json:"id"`
|
||||
JSONRPC string `json:"jsonrpc"`
|
||||
Result ctypes.ResponseStatus `json:"result"`
|
||||
Error string `json:"error"`
|
||||
Id string `json:"id"`
|
||||
JSONRPC string `json:"jsonrpc"`
|
||||
}
|
||||
binary.ReadJSON(&response, body, &err)
|
||||
if err != nil {
|
||||
@@ -80,10 +80,10 @@ func TestJSONGenPriv(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var response struct {
|
||||
Result core.ResponseGenPrivAccount `json:"result"`
|
||||
Error string `json:"error"`
|
||||
Id string `json:"id"`
|
||||
JSONRPC string `json:"jsonrpc"`
|
||||
Result ctypes.ResponseGenPrivAccount `json:"result"`
|
||||
Error string `json:"error"`
|
||||
Id string `json:"id"`
|
||||
JSONRPC string `json:"jsonrpc"`
|
||||
}
|
||||
binary.ReadJSON(&response, body, &err)
|
||||
if err != nil {
|
||||
@@ -143,10 +143,10 @@ func TestJSONBroadcastTx(t *testing.T) {
|
||||
b := w.Bytes()
|
||||
|
||||
var response struct {
|
||||
Result core.ResponseBroadcastTx `json:"result"`
|
||||
Error string `json:"error"`
|
||||
Id string `json:"id"`
|
||||
JSONRPC string `json:"jsonrpc"`
|
||||
Result ctypes.ResponseBroadcastTx `json:"result"`
|
||||
Error string `json:"error"`
|
||||
Id string `json:"id"`
|
||||
JSONRPC string `json:"jsonrpc"`
|
||||
}
|
||||
requestResponse(t, "broadcast_tx", url.Values{"tx": {string(b)}}, &response)
|
||||
if response.Error != "" {
|
||||
|
||||
+27
-27
@@ -6,11 +6,11 @@ import (
|
||||
"github.com/tendermint/tendermint/account"
|
||||
"github.com/tendermint/tendermint/binary"
|
||||
"github.com/tendermint/tendermint/config"
|
||||
"github.com/tendermint/tendermint/daemon"
|
||||
"github.com/tendermint/tendermint/logger"
|
||||
nm "github.com/tendermint/tendermint/node"
|
||||
"github.com/tendermint/tendermint/p2p"
|
||||
"github.com/tendermint/tendermint/rpc"
|
||||
"github.com/tendermint/tendermint/rpc/core"
|
||||
ctypes "github.com/tendermint/tendermint/rpc/core/types"
|
||||
cclient "github.com/tendermint/tendermint/rpc/core_client"
|
||||
"github.com/tendermint/tendermint/state"
|
||||
"github.com/tendermint/tendermint/types"
|
||||
"io/ioutil"
|
||||
@@ -23,7 +23,7 @@ var (
|
||||
rpcAddr = "127.0.0.1:8089"
|
||||
requestAddr = "http://" + rpcAddr + "/"
|
||||
|
||||
node *daemon.Node
|
||||
node *nm.Node
|
||||
|
||||
mempoolCount = 0
|
||||
|
||||
@@ -42,7 +42,7 @@ func decodeHex(hexStr string) []byte {
|
||||
|
||||
func newNode(ready chan struct{}) {
|
||||
// Create & start node
|
||||
node = daemon.NewNode()
|
||||
node = nm.NewNode()
|
||||
l := p2p.NewDefaultListener("tcp", config.App().GetString("ListenAddr"), false)
|
||||
node.AddListener(l)
|
||||
node.Start()
|
||||
@@ -85,12 +85,12 @@ func init() {
|
||||
}
|
||||
|
||||
func getAccount(t *testing.T, typ string, addr []byte) *account.Account {
|
||||
var client rpc.Client
|
||||
var client cclient.Client
|
||||
switch typ {
|
||||
case "JSONRPC":
|
||||
client = rpc.NewClient(requestAddr, "JSONRPC")
|
||||
client = cclient.NewClient(requestAddr, "JSONRPC")
|
||||
case "HTTP":
|
||||
client = rpc.NewClient(requestAddr, "HTTP")
|
||||
client = cclient.NewClient(requestAddr, "HTTP")
|
||||
}
|
||||
ac, err := client.GetAccount(addr)
|
||||
if err != nil {
|
||||
@@ -130,7 +130,7 @@ func getAccount(t *testing.T, typ string, addr []byte) *account.Account {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var response struct {
|
||||
Result core.ResponseGetAccount `json:"result"`
|
||||
Result ctypes.ResponseGetAccount `json:"result"`
|
||||
Error string `json:"error"`
|
||||
Id string `json:"id"`
|
||||
JSONRPC string `json:"jsonrpc"`
|
||||
@@ -242,10 +242,10 @@ func signTx(t *testing.T, typ string, fromAddr, toAddr, data []byte, key [64]byt
|
||||
}
|
||||
|
||||
var response struct {
|
||||
Result core.ResponseSignTx `json:"result"`
|
||||
Error string `json:"error"`
|
||||
Id string `json:"id"`
|
||||
JSONRPC string `json:"jsonrpc"`
|
||||
Result ctypes.ResponseSignTx `json:"result"`
|
||||
Error string `json:"error"`
|
||||
Id string `json:"id"`
|
||||
JSONRPC string `json:"jsonrpc"`
|
||||
}
|
||||
requestResponse(t, "unsafe/sign_tx", url.Values{"tx": {string(b)}, "privAccounts": {string(w.Bytes())}}, &response)
|
||||
if response.Error != "" {
|
||||
@@ -255,7 +255,7 @@ func signTx(t *testing.T, typ string, fromAddr, toAddr, data []byte, key [64]byt
|
||||
return result.Tx, privAcc
|
||||
}
|
||||
|
||||
func broadcastTx(t *testing.T, typ string, fromAddr, toAddr, data []byte, key [64]byte, amt, gaslim, fee uint64) (types.Tx, core.Receipt) {
|
||||
func broadcastTx(t *testing.T, typ string, fromAddr, toAddr, data []byte, key [64]byte, amt, gaslim, fee uint64) (types.Tx, ctypes.Receipt) {
|
||||
tx, _ := signTx(t, typ, fromAddr, toAddr, data, key, amt, gaslim, fee)
|
||||
|
||||
n, w := new(int64), new(bytes.Buffer)
|
||||
@@ -267,10 +267,10 @@ func broadcastTx(t *testing.T, typ string, fromAddr, toAddr, data []byte, key [6
|
||||
b := w.Bytes()
|
||||
|
||||
var response struct {
|
||||
Result core.ResponseBroadcastTx `json:"result"`
|
||||
Error string `json:"error"`
|
||||
Id string `json:"id"`
|
||||
JSONRPC string `json:"jsonrpc"`
|
||||
Result ctypes.ResponseBroadcastTx `json:"result"`
|
||||
Error string `json:"error"`
|
||||
Id string `json:"id"`
|
||||
JSONRPC string `json:"jsonrpc"`
|
||||
}
|
||||
requestResponse(t, "broadcast_tx", url.Values{"tx": {string(b)}}, &response)
|
||||
if response.Error != "" {
|
||||
@@ -279,13 +279,13 @@ func broadcastTx(t *testing.T, typ string, fromAddr, toAddr, data []byte, key [6
|
||||
return tx, response.Result.Receipt
|
||||
}
|
||||
|
||||
func dumpStorage(t *testing.T, addr []byte) core.ResponseDumpStorage {
|
||||
func dumpStorage(t *testing.T, addr []byte) ctypes.ResponseDumpStorage {
|
||||
addrString := "\"" + hex.EncodeToString(addr) + "\""
|
||||
var response struct {
|
||||
Result core.ResponseDumpStorage `json:"result"`
|
||||
Error string `json:"error"`
|
||||
Id string `json:"id"`
|
||||
JSONRPC string `json:"jsonrpc"`
|
||||
Result ctypes.ResponseDumpStorage `json:"result"`
|
||||
Error string `json:"error"`
|
||||
Id string `json:"id"`
|
||||
JSONRPC string `json:"jsonrpc"`
|
||||
}
|
||||
requestResponse(t, "dump_storage", url.Values{"address": {addrString}}, &response)
|
||||
if response.Error != "" {
|
||||
@@ -298,10 +298,10 @@ func getStorage(t *testing.T, addr, slot []byte) []byte {
|
||||
addrString := "\"" + hex.EncodeToString(addr) + "\""
|
||||
slotString := "\"" + hex.EncodeToString(slot) + "\""
|
||||
var response struct {
|
||||
Result core.ResponseGetStorage `json:"result"`
|
||||
Error string `json:"error"`
|
||||
Id string `json:"id"`
|
||||
JSONRPC string `json:"jsonrpc"`
|
||||
Result ctypes.ResponseGetStorage `json:"result"`
|
||||
Error string `json:"error"`
|
||||
Id string `json:"id"`
|
||||
JSONRPC string `json:"jsonrpc"`
|
||||
}
|
||||
requestResponse(t, "get_storage", url.Values{"address": {addrString}, "storage": {slotString}}, &response)
|
||||
if response.Error != "" {
|
||||
|
||||
Reference in New Issue
Block a user