mirror of
https://github.com/tendermint/tendermint.git
synced 2026-09-19 14:34:17 +00:00
rpc: clean up the RPCFunc constructor signature (#7586)
Instead of taking a comma-separated string of parameter names, take each parameter name as a separate argument. Now that we no longer have an extra flag for caching, this fits nicely into a variadic trailer. * Update all usage of NewRPCFunc and NewWSRPCFunc.
This commit is contained in:
@@ -38,16 +38,16 @@ func Routes(cfg config.RPCConfig, s state.Store, bs state.BlockStore, es []index
|
|||||||
Logger: logger,
|
Logger: logger,
|
||||||
}
|
}
|
||||||
return core.RoutesMap{
|
return core.RoutesMap{
|
||||||
"blockchain": server.NewRPCFunc(env.BlockchainInfo, "minHeight,maxHeight"),
|
"blockchain": server.NewRPCFunc(env.BlockchainInfo, "minHeight", "maxHeight"),
|
||||||
"consensus_params": server.NewRPCFunc(env.ConsensusParams, "height"),
|
"consensus_params": server.NewRPCFunc(env.ConsensusParams, "height"),
|
||||||
"block": server.NewRPCFunc(env.Block, "height"),
|
"block": server.NewRPCFunc(env.Block, "height"),
|
||||||
"block_by_hash": server.NewRPCFunc(env.BlockByHash, "hash"),
|
"block_by_hash": server.NewRPCFunc(env.BlockByHash, "hash"),
|
||||||
"block_results": server.NewRPCFunc(env.BlockResults, "height"),
|
"block_results": server.NewRPCFunc(env.BlockResults, "height"),
|
||||||
"commit": server.NewRPCFunc(env.Commit, "height"),
|
"commit": server.NewRPCFunc(env.Commit, "height"),
|
||||||
"validators": server.NewRPCFunc(env.Validators, "height,page,per_page"),
|
"validators": server.NewRPCFunc(env.Validators, "height", "page", "per_page"),
|
||||||
"tx": server.NewRPCFunc(env.Tx, "hash,prove"),
|
"tx": server.NewRPCFunc(env.Tx, "hash", "prove"),
|
||||||
"tx_search": server.NewRPCFunc(env.TxSearch, "query,prove,page,per_page,order_by"),
|
"tx_search": server.NewRPCFunc(env.TxSearch, "query", "prove", "page", "per_page", "order_by"),
|
||||||
"block_search": server.NewRPCFunc(env.BlockSearch, "query,page,per_page,order_by"),
|
"block_search": server.NewRPCFunc(env.BlockSearch, "query", "page", "per_page", "order_by"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+16
-16
@@ -31,14 +31,14 @@ func NewRoutesMap(svc RPCService, opts *RouteOptions) RoutesMap {
|
|||||||
// subscribe/unsubscribe are reserved for websocket events.
|
// subscribe/unsubscribe are reserved for websocket events.
|
||||||
"subscribe": rpc.NewWSRPCFunc(svc.Subscribe, "query"),
|
"subscribe": rpc.NewWSRPCFunc(svc.Subscribe, "query"),
|
||||||
"unsubscribe": rpc.NewWSRPCFunc(svc.Unsubscribe, "query"),
|
"unsubscribe": rpc.NewWSRPCFunc(svc.Unsubscribe, "query"),
|
||||||
"unsubscribe_all": rpc.NewWSRPCFunc(svc.UnsubscribeAll, ""),
|
"unsubscribe_all": rpc.NewWSRPCFunc(svc.UnsubscribeAll),
|
||||||
|
|
||||||
// info API
|
// info API
|
||||||
"health": rpc.NewRPCFunc(svc.Health, ""),
|
"health": rpc.NewRPCFunc(svc.Health),
|
||||||
"status": rpc.NewRPCFunc(svc.Status, ""),
|
"status": rpc.NewRPCFunc(svc.Status),
|
||||||
"net_info": rpc.NewRPCFunc(svc.NetInfo, ""),
|
"net_info": rpc.NewRPCFunc(svc.NetInfo),
|
||||||
"blockchain": rpc.NewRPCFunc(svc.BlockchainInfo, "minHeight,maxHeight"),
|
"blockchain": rpc.NewRPCFunc(svc.BlockchainInfo, "minHeight", "maxHeight"),
|
||||||
"genesis": rpc.NewRPCFunc(svc.Genesis, ""),
|
"genesis": rpc.NewRPCFunc(svc.Genesis),
|
||||||
"genesis_chunked": rpc.NewRPCFunc(svc.GenesisChunked, "chunk"),
|
"genesis_chunked": rpc.NewRPCFunc(svc.GenesisChunked, "chunk"),
|
||||||
"header": rpc.NewRPCFunc(svc.Header, "height"),
|
"header": rpc.NewRPCFunc(svc.Header, "height"),
|
||||||
"header_by_hash": rpc.NewRPCFunc(svc.HeaderByHash, "hash"),
|
"header_by_hash": rpc.NewRPCFunc(svc.HeaderByHash, "hash"),
|
||||||
@@ -48,15 +48,15 @@ func NewRoutesMap(svc RPCService, opts *RouteOptions) RoutesMap {
|
|||||||
"commit": rpc.NewRPCFunc(svc.Commit, "height"),
|
"commit": rpc.NewRPCFunc(svc.Commit, "height"),
|
||||||
"check_tx": rpc.NewRPCFunc(svc.CheckTx, "tx"),
|
"check_tx": rpc.NewRPCFunc(svc.CheckTx, "tx"),
|
||||||
"remove_tx": rpc.NewRPCFunc(svc.RemoveTx, "txkey"),
|
"remove_tx": rpc.NewRPCFunc(svc.RemoveTx, "txkey"),
|
||||||
"tx": rpc.NewRPCFunc(svc.Tx, "hash,prove"),
|
"tx": rpc.NewRPCFunc(svc.Tx, "hash", "prove"),
|
||||||
"tx_search": rpc.NewRPCFunc(svc.TxSearch, "query,prove,page,per_page,order_by"),
|
"tx_search": rpc.NewRPCFunc(svc.TxSearch, "query", "prove", "page", "per_page", "order_by"),
|
||||||
"block_search": rpc.NewRPCFunc(svc.BlockSearch, "query,page,per_page,order_by"),
|
"block_search": rpc.NewRPCFunc(svc.BlockSearch, "query", "page", "per_page", "order_by"),
|
||||||
"validators": rpc.NewRPCFunc(svc.Validators, "height,page,per_page"),
|
"validators": rpc.NewRPCFunc(svc.Validators, "height", "page", "per_page"),
|
||||||
"dump_consensus_state": rpc.NewRPCFunc(svc.DumpConsensusState, ""),
|
"dump_consensus_state": rpc.NewRPCFunc(svc.DumpConsensusState),
|
||||||
"consensus_state": rpc.NewRPCFunc(svc.GetConsensusState, ""),
|
"consensus_state": rpc.NewRPCFunc(svc.GetConsensusState),
|
||||||
"consensus_params": rpc.NewRPCFunc(svc.ConsensusParams, "height"),
|
"consensus_params": rpc.NewRPCFunc(svc.ConsensusParams, "height"),
|
||||||
"unconfirmed_txs": rpc.NewRPCFunc(svc.UnconfirmedTxs, "limit"),
|
"unconfirmed_txs": rpc.NewRPCFunc(svc.UnconfirmedTxs, "limit"),
|
||||||
"num_unconfirmed_txs": rpc.NewRPCFunc(svc.NumUnconfirmedTxs, ""),
|
"num_unconfirmed_txs": rpc.NewRPCFunc(svc.NumUnconfirmedTxs),
|
||||||
|
|
||||||
// tx broadcast API
|
// tx broadcast API
|
||||||
"broadcast_tx_commit": rpc.NewRPCFunc(svc.BroadcastTxCommit, "tx"),
|
"broadcast_tx_commit": rpc.NewRPCFunc(svc.BroadcastTxCommit, "tx"),
|
||||||
@@ -64,14 +64,14 @@ func NewRoutesMap(svc RPCService, opts *RouteOptions) RoutesMap {
|
|||||||
"broadcast_tx_async": rpc.NewRPCFunc(svc.BroadcastTxAsync, "tx"),
|
"broadcast_tx_async": rpc.NewRPCFunc(svc.BroadcastTxAsync, "tx"),
|
||||||
|
|
||||||
// abci API
|
// abci API
|
||||||
"abci_query": rpc.NewRPCFunc(svc.ABCIQuery, "path,data,height,prove"),
|
"abci_query": rpc.NewRPCFunc(svc.ABCIQuery, "path", "data", "height", "prove"),
|
||||||
"abci_info": rpc.NewRPCFunc(svc.ABCIInfo, ""),
|
"abci_info": rpc.NewRPCFunc(svc.ABCIInfo),
|
||||||
|
|
||||||
// evidence API
|
// evidence API
|
||||||
"broadcast_evidence": rpc.NewRPCFunc(svc.BroadcastEvidence, "evidence"),
|
"broadcast_evidence": rpc.NewRPCFunc(svc.BroadcastEvidence, "evidence"),
|
||||||
}
|
}
|
||||||
if u, ok := svc.(RPCUnsafe); ok && opts.Unsafe {
|
if u, ok := svc.(RPCUnsafe); ok && opts.Unsafe {
|
||||||
out["unsafe_flush_mempool"] = rpc.NewRPCFunc(u.UnsafeFlushMempool, "")
|
out["unsafe_flush_mempool"] = rpc.NewRPCFunc(u.UnsafeFlushMempool)
|
||||||
}
|
}
|
||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -55,7 +55,7 @@
|
|||||||
// Define some routes
|
// Define some routes
|
||||||
//
|
//
|
||||||
// var Routes = map[string]*rpcserver.RPCFunc{
|
// var Routes = map[string]*rpcserver.RPCFunc{
|
||||||
// "status": rpcserver.NewRPCFunc(Status, "arg", false),
|
// "status": rpcserver.NewRPCFunc(Status, "arg"),
|
||||||
// }
|
// }
|
||||||
//
|
//
|
||||||
// An rpc function:
|
// An rpc function:
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ import (
|
|||||||
|
|
||||||
func testMux() *http.ServeMux {
|
func testMux() *http.ServeMux {
|
||||||
funcMap := map[string]*RPCFunc{
|
funcMap := map[string]*RPCFunc{
|
||||||
"c": NewRPCFunc(func(ctx context.Context, s string, i int) (string, error) { return "foo", nil }, "s,i"),
|
"c": NewRPCFunc(func(ctx context.Context, s string, i int) (string, error) { return "foo", nil }, "s", "i"),
|
||||||
"block": NewRPCFunc(func(ctx context.Context, h int) (string, error) { return "block", nil }, "height"),
|
"block": NewRPCFunc(func(ctx context.Context, h int) (string, error) { return "block", nil }, "height"),
|
||||||
}
|
}
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
|
|||||||
@@ -135,7 +135,7 @@ func TestParseJSONArray(t *testing.T) {
|
|||||||
|
|
||||||
func TestParseJSONRPC(t *testing.T) {
|
func TestParseJSONRPC(t *testing.T) {
|
||||||
demo := func(ctx context.Context, height int, name string) {}
|
demo := func(ctx context.Context, height int, name string) {}
|
||||||
call := NewRPCFunc(demo, "height,name")
|
call := NewRPCFunc(demo, "height", "name")
|
||||||
|
|
||||||
cases := []struct {
|
cases := []struct {
|
||||||
raw string
|
raw string
|
||||||
@@ -172,7 +172,7 @@ func TestParseJSONRPC(t *testing.T) {
|
|||||||
|
|
||||||
func TestParseURI(t *testing.T) {
|
func TestParseURI(t *testing.T) {
|
||||||
demo := func(ctx context.Context, height int, name string) {}
|
demo := func(ctx context.Context, height int, name string) {}
|
||||||
call := NewRPCFunc(demo, "height,name")
|
call := NewRPCFunc(demo, "height", "name")
|
||||||
|
|
||||||
cases := []struct {
|
cases := []struct {
|
||||||
raw []string
|
raw []string
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package server
|
|||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/tendermint/tendermint/libs/log"
|
"github.com/tendermint/tendermint/libs/log"
|
||||||
)
|
)
|
||||||
@@ -35,26 +34,22 @@ type RPCFunc struct {
|
|||||||
|
|
||||||
// NewRPCFunc wraps a function for introspection.
|
// NewRPCFunc wraps a function for introspection.
|
||||||
// f is the function, args are comma separated argument names
|
// f is the function, args are comma separated argument names
|
||||||
func NewRPCFunc(f interface{}, args string) *RPCFunc {
|
func NewRPCFunc(f interface{}, argNames ...string) *RPCFunc {
|
||||||
return newRPCFunc(f, args, false)
|
return newRPCFunc(f, argNames, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewWSRPCFunc wraps a function for introspection and use in the websockets.
|
// NewWSRPCFunc wraps a function for introspection and use in the websockets.
|
||||||
func NewWSRPCFunc(f interface{}, args string) *RPCFunc {
|
func NewWSRPCFunc(f interface{}, argNames ...string) *RPCFunc {
|
||||||
return newRPCFunc(f, args, true)
|
return newRPCFunc(f, argNames, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
func newRPCFunc(f interface{}, args string, ws bool) *RPCFunc {
|
func newRPCFunc(f interface{}, argNames []string, wsOnly bool) *RPCFunc {
|
||||||
var argNames []string
|
|
||||||
if args != "" {
|
|
||||||
argNames = strings.Split(args, ",")
|
|
||||||
}
|
|
||||||
return &RPCFunc{
|
return &RPCFunc{
|
||||||
f: reflect.ValueOf(f),
|
f: reflect.ValueOf(f),
|
||||||
args: funcArgTypes(f),
|
args: funcArgTypes(f),
|
||||||
returns: funcReturnTypes(f),
|
returns: funcReturnTypes(f),
|
||||||
argNames: argNames,
|
argNames: argNames,
|
||||||
ws: ws,
|
ws: wsOnly,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ func TestWebsocketManagerHandler(t *testing.T) {
|
|||||||
|
|
||||||
func newWSServer(t *testing.T, logger log.Logger) *httptest.Server {
|
func newWSServer(t *testing.T, logger log.Logger) *httptest.Server {
|
||||||
funcMap := map[string]*RPCFunc{
|
funcMap := map[string]*RPCFunc{
|
||||||
"c": NewWSRPCFunc(func(ctx context.Context, s string, i int) (string, error) { return "foo", nil }, "s,i"),
|
"c": NewWSRPCFunc(func(ctx context.Context, s string, i int) (string, error) { return "foo", nil }, "s", "i"),
|
||||||
}
|
}
|
||||||
wm := NewWebsocketManager(logger, funcMap)
|
wm := NewWebsocketManager(logger, funcMap)
|
||||||
|
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var routes = map[string]*rpcserver.RPCFunc{
|
var routes = map[string]*rpcserver.RPCFunc{
|
||||||
"hello_world": rpcserver.NewRPCFunc(HelloWorld, "name,num"),
|
"hello_world": rpcserver.NewRPCFunc(HelloWorld, "name", "num"),
|
||||||
}
|
}
|
||||||
|
|
||||||
func HelloWorld(ctx context.Context, name string, num int) (Result, error) {
|
func HelloWorld(ctx context.Context, name string, num int) (Result, error) {
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var rpcFuncMap = map[string]*rs.RPCFunc{
|
var rpcFuncMap = map[string]*rs.RPCFunc{
|
||||||
"c": rs.NewRPCFunc(func(s string, i int) (string, int) { return "foo", 200 }, "s,i"),
|
"c": rs.NewRPCFunc(func(s string, i int) (string, int) { return "foo", 200 }, "s", "i"),
|
||||||
}
|
}
|
||||||
var mux *http.ServeMux
|
var mux *http.ServeMux
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user