rpc/client: split out client packages (#4628)

* rpc/client: initial split into directories

* lite2: split out test package

* rpc/client: simplify client constructurs

* updated docs

* updated changelog
This commit is contained in:
Erik Grinaker
2020-04-02 13:25:30 +00:00
committed by GitHub
parent 6c88d2ba1f
commit fdf9c7ae64
19 changed files with 159 additions and 145 deletions
+3 -3
View File
@@ -6,7 +6,7 @@ import (
"log"
"github.com/tendermint/tendermint/abci/example/kvstore"
"github.com/tendermint/tendermint/rpc/client"
rpchttp "github.com/tendermint/tendermint/rpc/client/http"
ctypes "github.com/tendermint/tendermint/rpc/core/types"
rpctest "github.com/tendermint/tendermint/rpc/test"
)
@@ -19,7 +19,7 @@ func ExampleHTTP_simple() {
// Create our RPC client
rpcAddr := rpctest.GetConfig().RPC.ListenAddress
c, err := client.NewHTTP(rpcAddr, "/websocket")
c, err := rpchttp.New(rpcAddr, "/websocket")
if err != nil {
log.Fatal(err)
}
@@ -72,7 +72,7 @@ func ExampleHTTP_batching() {
// Create our RPC client
rpcAddr := rpctest.GetConfig().RPC.ListenAddress
c, err := client.NewHTTP(rpcAddr, "/websocket")
c, err := rpchttp.New(rpcAddr, "/websocket")
if err != nil {
log.Fatal(err)
}
@@ -1,4 +1,4 @@
package client
package http
import (
"context"
@@ -15,8 +15,9 @@ import (
"github.com/tendermint/tendermint/libs/log"
tmpubsub "github.com/tendermint/tendermint/libs/pubsub"
"github.com/tendermint/tendermint/libs/service"
rpcclient "github.com/tendermint/tendermint/rpc/client"
ctypes "github.com/tendermint/tendermint/rpc/core/types"
rpcclient "github.com/tendermint/tendermint/rpc/lib/client"
rpcclientlib "github.com/tendermint/tendermint/rpc/lib/client"
"github.com/tendermint/tendermint/types"
)
@@ -40,7 +41,7 @@ the example for more details.
Example:
c, err := NewHTTP("http://192.168.1.10:26657", "/websocket")
c, err := New("http://192.168.1.10:26657", "/websocket")
if err != nil {
// handle error
}
@@ -61,7 +62,7 @@ Example:
*/
type HTTP struct {
remote string
rpc *rpcclient.JSONRPCClient
rpc *rpcclientlib.JSONRPCClient
*baseRPCClient
*WSEvents
@@ -78,7 +79,7 @@ type HTTP struct {
// batch, but ordering of transactions in the batch cannot be guaranteed in such
// an example.
type BatchHTTP struct {
rpcBatch *rpcclient.JSONRPCRequestBatch
rpcBatch *rpcclientlib.JSONRPCRequestBatch
*baseRPCClient
}
@@ -86,17 +87,17 @@ type BatchHTTP struct {
// non-batch) must conform. Acts as an additional code-level sanity check to
// make sure the implementations stay coherent.
type rpcClient interface {
ABCIClient
HistoryClient
NetworkClient
SignClient
StatusClient
rpcclient.ABCIClient
rpcclient.HistoryClient
rpcclient.NetworkClient
rpcclient.SignClient
rpcclient.StatusClient
}
// baseRPCClient implements the basic RPC method logic without the actual
// underlying RPC call functionality, which is provided by `caller`.
type baseRPCClient struct {
caller rpcclient.JSONRPCCaller
caller rpcclientlib.JSONRPCCaller
}
var _ rpcClient = (*HTTP)(nil)
@@ -106,35 +107,35 @@ var _ rpcClient = (*baseRPCClient)(nil)
//-----------------------------------------------------------------------------
// HTTP
// NewHTTP takes a remote endpoint in the form <protocol>://<host>:<port> and
// New takes a remote endpoint in the form <protocol>://<host>:<port> and
// the websocket path (which always seems to be "/websocket")
// An error is returned on invalid remote. The function panics when remote is nil.
func NewHTTP(remote, wsEndpoint string) (*HTTP, error) {
httpClient, err := rpcclient.DefaultHTTPClient(remote)
func New(remote, wsEndpoint string) (*HTTP, error) {
httpClient, err := rpcclientlib.DefaultHTTPClient(remote)
if err != nil {
return nil, err
}
return NewHTTPWithClient(remote, wsEndpoint, httpClient)
return NewWithClient(remote, wsEndpoint, httpClient)
}
// Create timeout enabled http client
func NewHTTPWithTimeout(remote, wsEndpoint string, timeout uint) (*HTTP, error) {
httpClient, err := rpcclient.DefaultHTTPClient(remote)
func NewWithTimeout(remote, wsEndpoint string, timeout uint) (*HTTP, error) {
httpClient, err := rpcclientlib.DefaultHTTPClient(remote)
if err != nil {
return nil, err
}
httpClient.Timeout = time.Duration(timeout) * time.Second
return NewHTTPWithClient(remote, wsEndpoint, httpClient)
return NewWithClient(remote, wsEndpoint, httpClient)
}
// NewHTTPWithClient allows for setting a custom http client (See NewHTTP).
// NewWithClient allows for setting a custom http client (See New).
// An error is returned on invalid remote. The function panics when remote is nil.
func NewHTTPWithClient(remote, wsEndpoint string, client *http.Client) (*HTTP, error) {
func NewWithClient(remote, wsEndpoint string, client *http.Client) (*HTTP, error) {
if client == nil {
panic("nil http.Client provided")
}
rc, err := rpcclient.NewJSONRPCClientWithHTTPClient(remote, client)
rc, err := rpcclientlib.NewJSONRPCClientWithHTTPClient(remote, client)
if err != nil {
return nil, err
}
@@ -157,7 +158,7 @@ func NewHTTPWithClient(remote, wsEndpoint string, client *http.Client) (*HTTP, e
return httpClient, nil
}
var _ Client = (*HTTP)(nil)
var _ rpcclient.Client = (*HTTP)(nil)
// SetLogger sets a logger.
func (c *HTTP) SetLogger(l log.Logger) {
@@ -224,13 +225,13 @@ func (c *baseRPCClient) ABCIInfo() (*ctypes.ResultABCIInfo, error) {
}
func (c *baseRPCClient) ABCIQuery(path string, data bytes.HexBytes) (*ctypes.ResultABCIQuery, error) {
return c.ABCIQueryWithOptions(path, data, DefaultABCIQueryOptions)
return c.ABCIQueryWithOptions(path, data, rpcclient.DefaultABCIQueryOptions)
}
func (c *baseRPCClient) ABCIQueryWithOptions(
path string,
data bytes.HexBytes,
opts ABCIQueryOptions) (*ctypes.ResultABCIQuery, error) {
opts rpcclient.ABCIQueryOptions) (*ctypes.ResultABCIQuery, error) {
result := new(ctypes.ResultABCIQuery)
_, err := c.caller.Call("abci_query",
map[string]interface{}{"path": path, "data": data, "height": opts.Height, "prove": opts.Prove},
@@ -440,7 +441,7 @@ type WSEvents struct {
cdc *amino.Codec
remote string
endpoint string
ws *rpcclient.WSClient
ws *rpcclientlib.WSClient
mtx sync.RWMutex
subscriptions map[string]chan ctypes.ResultEvent // query -> chan
@@ -456,7 +457,7 @@ func newWSEvents(cdc *amino.Codec, remote, endpoint string) (*WSEvents, error) {
w.BaseService = *service.NewBaseService(nil, "WSEvents", w)
var err error
w.ws, err = rpcclient.NewWSClient(w.remote, w.endpoint, rpcclient.OnReconnect(func() {
w.ws, err = rpcclientlib.NewWSClient(w.remote, w.endpoint, rpcclientlib.OnReconnect(func() {
// resubscribe immediately
w.redoSubscriptionsAfter(0 * time.Second)
}))
@@ -1,4 +1,4 @@
package client
package local
import (
"context"
@@ -11,6 +11,7 @@ import (
tmpubsub "github.com/tendermint/tendermint/libs/pubsub"
tmquery "github.com/tendermint/tendermint/libs/pubsub/query"
nm "github.com/tendermint/tendermint/node"
rpcclient "github.com/tendermint/tendermint/rpc/client"
"github.com/tendermint/tendermint/rpc/core"
ctypes "github.com/tendermint/tendermint/rpc/core/types"
rpctypes "github.com/tendermint/tendermint/rpc/lib/types"
@@ -49,7 +50,7 @@ type Local struct {
// you can only have one node per process. So make sure test cases
// don't run in parallel, or try to simulate an entire network in
// one process...
func NewLocal(node *nm.Node) *Local {
func New(node *nm.Node) *Local {
node.ConfigureRPC()
return &Local{
EventBus: node.EventBus(),
@@ -58,7 +59,7 @@ func NewLocal(node *nm.Node) *Local {
}
}
var _ Client = (*Local)(nil)
var _ rpcclient.Client = (*Local)(nil)
// SetLogger allows to set a logger on the client.
func (c *Local) SetLogger(l log.Logger) {
@@ -74,13 +75,13 @@ func (c *Local) ABCIInfo() (*ctypes.ResultABCIInfo, error) {
}
func (c *Local) ABCIQuery(path string, data bytes.HexBytes) (*ctypes.ResultABCIQuery, error) {
return c.ABCIQueryWithOptions(path, data, DefaultABCIQueryOptions)
return c.ABCIQueryWithOptions(path, data, rpcclient.DefaultABCIQueryOptions)
}
func (c *Local) ABCIQueryWithOptions(
path string,
data bytes.HexBytes,
opts ABCIQueryOptions) (*ctypes.ResultABCIQuery, error) {
opts rpcclient.ABCIQueryOptions) (*ctypes.ResultABCIQuery, error) {
return core.ABCIQuery(c.ctx, path, data, opts.Height, opts.Prove)
}
+11 -9
View File
@@ -23,15 +23,17 @@ import (
mempl "github.com/tendermint/tendermint/mempool"
"github.com/tendermint/tendermint/privval"
"github.com/tendermint/tendermint/rpc/client"
rpchttp "github.com/tendermint/tendermint/rpc/client/http"
rpclocal "github.com/tendermint/tendermint/rpc/client/local"
ctypes "github.com/tendermint/tendermint/rpc/core/types"
rpcclient "github.com/tendermint/tendermint/rpc/lib/client"
rpctest "github.com/tendermint/tendermint/rpc/test"
"github.com/tendermint/tendermint/types"
)
func getHTTPClient() *client.HTTP {
func getHTTPClient() *rpchttp.HTTP {
rpcAddr := rpctest.GetConfig().RPC.ListenAddress
c, err := client.NewHTTP(rpcAddr, "/websocket")
c, err := rpchttp.New(rpcAddr, "/websocket")
if err != nil {
panic(err)
}
@@ -39,9 +41,9 @@ func getHTTPClient() *client.HTTP {
return c
}
func getHTTPClientWithTimeout(timeout uint) *client.HTTP {
func getHTTPClientWithTimeout(timeout uint) *rpchttp.HTTP {
rpcAddr := rpctest.GetConfig().RPC.ListenAddress
c, err := client.NewHTTPWithTimeout(rpcAddr, "/websocket", timeout)
c, err := rpchttp.NewWithTimeout(rpcAddr, "/websocket", timeout)
if err != nil {
panic(err)
}
@@ -49,8 +51,8 @@ func getHTTPClientWithTimeout(timeout uint) *client.HTTP {
return c
}
func getLocalClient() *client.Local {
return client.NewLocal(node)
func getLocalClient() *rpclocal.Local {
return rpclocal.New(node)
}
// GetClients returns a slice of clients for table-driven tests
@@ -63,7 +65,7 @@ func GetClients() []client.Client {
func TestNilCustomHTTPClient(t *testing.T) {
require.Panics(t, func() {
_, _ = client.NewHTTPWithClient("http://example.com", "/websocket", nil)
_, _ = rpchttp.NewWithClient("http://example.com", "/websocket", nil)
})
require.Panics(t, func() {
_, _ = rpcclient.NewJSONRPCClientWithHTTPClient("http://example.com", nil)
@@ -72,7 +74,7 @@ func TestNilCustomHTTPClient(t *testing.T) {
func TestCustomHTTPClient(t *testing.T) {
remote := rpctest.GetConfig().RPC.ListenAddress
c, err := client.NewHTTPWithClient(remote, "/websocket", http.DefaultClient)
c, err := rpchttp.NewWithClient(remote, "/websocket", http.DefaultClient)
require.Nil(t, err)
status, err := c.Status()
require.NoError(t, err)
@@ -701,7 +703,7 @@ func TestBatchedJSONRPCCalls(t *testing.T) {
testBatchedJSONRPCCalls(t, c)
}
func testBatchedJSONRPCCalls(t *testing.T, c *client.HTTP) {
func testBatchedJSONRPCCalls(t *testing.T, c *rpchttp.HTTP) {
k1, v1, tx1 := MakeTxKV()
k2, v2, tx2 := MakeTxKV()
+3 -2
View File
@@ -253,9 +253,10 @@ paths:
https://godoc.org/github.com/tendermint/tendermint/libs/pubsub/query.
```go
import rpchttp "github.com/tendermint/rpc/client/http"
import "github.com/tendermint/tendermint/types"
client := client.NewHTTP("tcp:0.0.0.0:26657", "/websocket")
client := rpchttp.New("tcp:0.0.0.0:26657", "/websocket")
err := client.Start()
if err != nil {
handle error
@@ -309,7 +310,7 @@ paths:
operationId: unsubscribe
description: |
```go
client := client.NewHTTP("tcp:0.0.0.0:26657", "/websocket")
client := rpchttp.New("tcp:0.0.0.0:26657", "/websocket")
err := client.Start()
if err != nil {
handle error