2016-01-12 16:50:06 -05:00
|
|
|
package rpcclient
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2016-01-13 18:37:35 -05:00
|
|
|
"encoding/json"
|
2017-01-02 09:50:20 -08:00
|
|
|
"fmt"
|
2016-01-12 16:50:06 -05:00
|
|
|
"io/ioutil"
|
2016-02-18 21:07:49 +00:00
|
|
|
"net"
|
2016-01-12 16:50:06 -05:00
|
|
|
"net/http"
|
2016-08-10 01:12:01 -04:00
|
|
|
"strings"
|
2019-04-17 11:10:12 -04:00
|
|
|
"sync"
|
2016-01-12 16:50:06 -05:00
|
|
|
|
2017-03-09 19:00:05 +04:00
|
|
|
"github.com/pkg/errors"
|
2019-06-05 17:23:53 +08:00
|
|
|
|
2019-01-28 14:13:17 +02:00
|
|
|
amino "github.com/tendermint/go-amino"
|
2017-10-16 16:01:32 +02:00
|
|
|
|
2017-04-26 19:57:33 -04:00
|
|
|
types "github.com/tendermint/tendermint/rpc/lib/types"
|
2016-01-12 16:50:06 -05:00
|
|
|
)
|
|
|
|
|
2018-06-27 13:03:47 +02:00
|
|
|
const (
|
|
|
|
protoHTTP = "http"
|
|
|
|
protoHTTPS = "https"
|
|
|
|
protoWSS = "wss"
|
|
|
|
protoWS = "ws"
|
|
|
|
protoTCP = "tcp"
|
|
|
|
)
|
|
|
|
|
2019-07-31 23:45:24 +04:00
|
|
|
//-------------------------------------------------------------
|
|
|
|
|
|
|
|
// HTTPClient is a common interface for JSON-RPC clients.
|
2017-03-07 18:34:54 +04:00
|
|
|
type HTTPClient interface {
|
2019-07-31 23:45:24 +04:00
|
|
|
// Call calls the given method with the params and returns a result.
|
2017-03-08 10:26:13 +04:00
|
|
|
Call(method string, params map[string]interface{}, result interface{}) (interface{}, error)
|
2019-07-31 23:45:24 +04:00
|
|
|
// Codec returns an amino codec used.
|
2018-04-05 15:45:11 -07:00
|
|
|
Codec() *amino.Codec
|
2019-07-31 23:45:24 +04:00
|
|
|
// SetCodec sets an amino codec.
|
2018-04-05 21:19:14 -07:00
|
|
|
SetCodec(*amino.Codec)
|
2017-03-07 18:34:54 +04:00
|
|
|
}
|
|
|
|
|
2019-07-31 23:45:24 +04:00
|
|
|
// JSONRPCCaller implementers can facilitate calling the JSON-RPC endpoint.
|
|
|
|
type JSONRPCCaller interface {
|
|
|
|
Call(method string, params map[string]interface{}, result interface{}) (interface{}, error)
|
2019-04-17 11:10:12 -04:00
|
|
|
}
|
|
|
|
|
2019-07-31 23:45:24 +04:00
|
|
|
//-------------------------------------------------------------
|
2019-04-17 11:10:12 -04:00
|
|
|
|
2019-07-31 23:45:24 +04:00
|
|
|
// JSONRPCClient is a JSON-RPC client, which sends POST HTTP requests to the
|
|
|
|
// remote server.
|
|
|
|
//
|
|
|
|
// Request values are amino encoded. Response is expected to be amino encoded.
|
|
|
|
// New amino codec is used if no other codec was set using SetCodec.
|
2017-03-08 01:22:35 +04:00
|
|
|
type JSONRPCClient struct {
|
2016-08-10 01:12:01 -04:00
|
|
|
address string
|
|
|
|
client *http.Client
|
2018-04-05 15:45:11 -07:00
|
|
|
cdc *amino.Codec
|
2016-01-12 16:50:06 -05:00
|
|
|
|
2019-07-31 23:45:24 +04:00
|
|
|
nextReqID uint
|
2019-04-17 11:10:12 -04:00
|
|
|
}
|
|
|
|
|
2019-07-31 23:45:24 +04:00
|
|
|
var _ HTTPClient = (*JSONRPCClient)(nil)
|
|
|
|
|
2019-04-17 11:10:12 -04:00
|
|
|
// Both JSONRPCClient and JSONRPCRequestBatch can facilitate calls to the JSON
|
|
|
|
// RPC endpoint.
|
|
|
|
var _ JSONRPCCaller = (*JSONRPCClient)(nil)
|
|
|
|
var _ JSONRPCCaller = (*JSONRPCRequestBatch)(nil)
|
|
|
|
|
2017-10-17 09:59:05 +02:00
|
|
|
// NewJSONRPCClient returns a JSONRPCClient pointed at the given address.
|
2017-03-08 01:22:35 +04:00
|
|
|
func NewJSONRPCClient(remote string) *JSONRPCClient {
|
2016-08-10 01:12:01 -04:00
|
|
|
address, client := makeHTTPClient(remote)
|
2017-03-08 01:22:35 +04:00
|
|
|
return &JSONRPCClient{
|
2016-08-10 01:12:01 -04:00
|
|
|
address: address,
|
|
|
|
client: client,
|
2018-04-05 15:45:11 -07:00
|
|
|
cdc: amino.NewCodec(),
|
2016-01-12 16:50:06 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-31 23:45:24 +04:00
|
|
|
// Call issues a POST HTTP request. Requests are JSON encoded. Content-Type:
|
|
|
|
// text/json.
|
2017-03-08 01:22:35 +04:00
|
|
|
func (c *JSONRPCClient) Call(method string, params map[string]interface{}, result interface{}) (interface{}, error) {
|
2019-07-31 23:45:24 +04:00
|
|
|
id := c.nextRequestID()
|
|
|
|
|
|
|
|
request, err := types.MapToRequest(c.cdc, id, method, params)
|
2017-05-03 16:13:58 +02:00
|
|
|
if err != nil {
|
2019-07-31 23:45:24 +04:00
|
|
|
return nil, errors.Wrap(err, "failed to encode params")
|
2016-01-12 16:50:06 -05:00
|
|
|
}
|
2019-07-31 23:45:24 +04:00
|
|
|
|
2017-03-07 18:34:54 +04:00
|
|
|
requestBytes, err := json.Marshal(request)
|
|
|
|
if err != nil {
|
2019-07-31 23:45:24 +04:00
|
|
|
return nil, errors.Wrap(err, "failed to marshal request")
|
2017-03-07 18:34:54 +04:00
|
|
|
}
|
2019-07-31 23:45:24 +04:00
|
|
|
|
2016-01-12 16:50:06 -05:00
|
|
|
requestBuf := bytes.NewBuffer(requestBytes)
|
2016-08-10 01:12:01 -04:00
|
|
|
httpResponse, err := c.client.Post(c.address, "text/json", requestBuf)
|
2016-01-12 16:50:06 -05:00
|
|
|
if err != nil {
|
2019-07-31 23:45:24 +04:00
|
|
|
return nil, errors.Wrap(err, "Post failed")
|
2016-01-12 16:50:06 -05:00
|
|
|
}
|
2017-10-03 18:49:20 -04:00
|
|
|
defer httpResponse.Body.Close() // nolint: errcheck
|
2017-09-21 09:55:06 -04:00
|
|
|
|
2016-01-12 16:50:06 -05:00
|
|
|
responseBytes, err := ioutil.ReadAll(httpResponse.Body)
|
|
|
|
if err != nil {
|
2019-07-31 23:45:24 +04:00
|
|
|
return nil, errors.Wrap(err, "failed to read response body")
|
2016-01-12 16:50:06 -05:00
|
|
|
}
|
2019-07-31 23:45:24 +04:00
|
|
|
|
|
|
|
return unmarshalResponseBytes(c.cdc, responseBytes, id, result)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *JSONRPCClient) Codec() *amino.Codec { return c.cdc }
|
|
|
|
func (c *JSONRPCClient) SetCodec(cdc *amino.Codec) { c.cdc = cdc }
|
|
|
|
|
|
|
|
func (c *JSONRPCClient) nextRequestID() types.JSONRPCIntID {
|
|
|
|
id := c.nextReqID
|
|
|
|
c.nextReqID++
|
|
|
|
return types.JSONRPCIntID(id)
|
2019-04-17 11:10:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewRequestBatch starts a batch of requests for this client.
|
|
|
|
func (c *JSONRPCClient) NewRequestBatch() *JSONRPCRequestBatch {
|
|
|
|
return &JSONRPCRequestBatch{
|
|
|
|
requests: make([]*jsonRPCBufferedRequest, 0),
|
|
|
|
client: c,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *JSONRPCClient) sendBatch(requests []*jsonRPCBufferedRequest) ([]interface{}, error) {
|
|
|
|
reqs := make([]types.RPCRequest, 0, len(requests))
|
|
|
|
results := make([]interface{}, 0, len(requests))
|
|
|
|
for _, req := range requests {
|
|
|
|
reqs = append(reqs, req.request)
|
|
|
|
results = append(results, req.result)
|
|
|
|
}
|
2019-07-31 23:45:24 +04:00
|
|
|
|
2019-04-17 11:10:12 -04:00
|
|
|
// serialize the array of requests into a single JSON object
|
|
|
|
requestBytes, err := json.Marshal(reqs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-07-31 23:45:24 +04:00
|
|
|
|
2019-04-17 11:10:12 -04:00
|
|
|
httpResponse, err := c.client.Post(c.address, "text/json", bytes.NewBuffer(requestBytes))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer httpResponse.Body.Close() // nolint: errcheck
|
|
|
|
|
|
|
|
responseBytes, err := ioutil.ReadAll(httpResponse.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-04-05 15:45:11 -07:00
|
|
|
|
2019-07-31 23:45:24 +04:00
|
|
|
// collect ids to check them in unmarshalResponseBytesArray
|
|
|
|
ids := make([]types.JSONRPCIntID, len(requests))
|
|
|
|
for i, req := range requests {
|
|
|
|
ids[i] = req.request.ID.(types.JSONRPCIntID)
|
|
|
|
}
|
2016-01-12 16:50:06 -05:00
|
|
|
|
2019-07-31 23:45:24 +04:00
|
|
|
return unmarshalResponseBytesArray(c.cdc, responseBytes, ids, results)
|
2018-04-05 21:19:14 -07:00
|
|
|
}
|
|
|
|
|
2016-02-18 21:07:49 +00:00
|
|
|
//-------------------------------------------------------------
|
|
|
|
|
2019-07-31 23:45:24 +04:00
|
|
|
// jsonRPCBufferedRequest encapsulates a single buffered request, as well as
|
|
|
|
// its anticipated response structure.
|
|
|
|
type jsonRPCBufferedRequest struct {
|
|
|
|
request types.RPCRequest
|
|
|
|
result interface{} // The result will be deserialized into this object.
|
|
|
|
}
|
|
|
|
|
|
|
|
// JSONRPCRequestBatch allows us to buffer multiple request/response structures
|
|
|
|
// into a single batch request. Note that this batch acts like a FIFO queue,
|
|
|
|
// and is thread-safe.
|
|
|
|
type JSONRPCRequestBatch struct {
|
|
|
|
client *JSONRPCClient
|
|
|
|
|
|
|
|
mtx sync.Mutex
|
|
|
|
requests []*jsonRPCBufferedRequest
|
|
|
|
}
|
|
|
|
|
2019-04-17 11:10:12 -04:00
|
|
|
// Count returns the number of enqueued requests waiting to be sent.
|
|
|
|
func (b *JSONRPCRequestBatch) Count() int {
|
|
|
|
b.mtx.Lock()
|
|
|
|
defer b.mtx.Unlock()
|
|
|
|
return len(b.requests)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *JSONRPCRequestBatch) enqueue(req *jsonRPCBufferedRequest) {
|
|
|
|
b.mtx.Lock()
|
|
|
|
defer b.mtx.Unlock()
|
|
|
|
b.requests = append(b.requests, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear empties out the request batch.
|
|
|
|
func (b *JSONRPCRequestBatch) Clear() int {
|
|
|
|
b.mtx.Lock()
|
|
|
|
defer b.mtx.Unlock()
|
|
|
|
return b.clear()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *JSONRPCRequestBatch) clear() int {
|
|
|
|
count := len(b.requests)
|
|
|
|
b.requests = make([]*jsonRPCBufferedRequest, 0)
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send will attempt to send the current batch of enqueued requests, and then
|
|
|
|
// will clear out the requests once done. On success, this returns the
|
|
|
|
// deserialized list of results from each of the enqueued requests.
|
|
|
|
func (b *JSONRPCRequestBatch) Send() ([]interface{}, error) {
|
|
|
|
b.mtx.Lock()
|
|
|
|
defer func() {
|
|
|
|
b.clear()
|
|
|
|
b.mtx.Unlock()
|
|
|
|
}()
|
|
|
|
return b.client.sendBatch(b.requests)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call enqueues a request to call the given RPC method with the specified
|
|
|
|
// parameters, in the same way that the `JSONRPCClient.Call` function would.
|
|
|
|
func (b *JSONRPCRequestBatch) Call(method string, params map[string]interface{}, result interface{}) (interface{}, error) {
|
2019-07-31 23:45:24 +04:00
|
|
|
id := b.client.nextRequestID()
|
2017-09-21 09:55:06 -04:00
|
|
|
|
2019-07-31 23:45:24 +04:00
|
|
|
request, err := types.MapToRequest(b.client.cdc, id, method, params)
|
2016-01-12 16:50:06 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-04-05 15:45:11 -07:00
|
|
|
|
2019-07-31 23:45:24 +04:00
|
|
|
b.enqueue(&jsonRPCBufferedRequest{request: request, result: result})
|
2016-01-12 16:50:06 -05:00
|
|
|
|
2016-10-10 03:22:34 -04:00
|
|
|
return result, nil
|
2016-01-12 16:50:06 -05:00
|
|
|
}
|
|
|
|
|
2019-07-31 23:45:24 +04:00
|
|
|
//-------------------------------------------------------------
|
2019-04-17 11:10:12 -04:00
|
|
|
|
2019-07-31 23:45:24 +04:00
|
|
|
// TODO: Deprecate support for IP:PORT or /path/to/socket
|
|
|
|
func makeHTTPDialer(remoteAddr string) (string, string, func(string, string) (net.Conn, error)) {
|
|
|
|
// protocol to use for http operations, to support both http and https
|
|
|
|
clientProtocol := protoHTTP
|
2019-04-17 11:10:12 -04:00
|
|
|
|
2019-07-31 23:45:24 +04:00
|
|
|
parts := strings.SplitN(remoteAddr, "://", 2)
|
|
|
|
var protocol, address string
|
|
|
|
if len(parts) == 1 {
|
|
|
|
// default to tcp if nothing specified
|
|
|
|
protocol, address = protoTCP, remoteAddr
|
|
|
|
} else if len(parts) == 2 {
|
|
|
|
protocol, address = parts[0], parts[1]
|
|
|
|
} else {
|
|
|
|
// return a invalid message
|
|
|
|
msg := fmt.Sprintf("Invalid addr: %s", remoteAddr)
|
|
|
|
return clientProtocol, msg, func(_ string, _ string) (net.Conn, error) {
|
|
|
|
return nil, errors.New(msg)
|
2019-04-17 11:10:12 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-31 23:45:24 +04:00
|
|
|
// accept http as an alias for tcp and set the client protocol
|
|
|
|
switch protocol {
|
|
|
|
case protoHTTP, protoHTTPS:
|
|
|
|
clientProtocol = protocol
|
|
|
|
protocol = protoTCP
|
|
|
|
case protoWS, protoWSS:
|
|
|
|
clientProtocol = protocol
|
2019-04-17 11:10:12 -04:00
|
|
|
}
|
|
|
|
|
2019-07-31 23:45:24 +04:00
|
|
|
// replace / with . for http requests (kvstore domain)
|
|
|
|
trimmedAddress := strings.Replace(address, "/", ".", -1)
|
|
|
|
return clientProtocol, trimmedAddress, func(proto, addr string) (net.Conn, error) {
|
|
|
|
return net.Dial(protocol, address)
|
2016-01-12 16:50:06 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-31 23:45:24 +04:00
|
|
|
// We overwrite the http.Client.Dial so we can do http over tcp or unix.
|
|
|
|
// remoteAddr should be fully featured (eg. with tcp:// or unix://)
|
|
|
|
func makeHTTPClient(remoteAddr string) (string, *http.Client) {
|
|
|
|
protocol, address, dialer := makeHTTPDialer(remoteAddr)
|
|
|
|
return protocol + "://" + address, &http.Client{
|
|
|
|
Transport: &http.Transport{
|
|
|
|
// Set to true to prevent GZIP-bomb DoS attacks
|
|
|
|
DisableCompression: true,
|
|
|
|
Dial: dialer,
|
|
|
|
},
|
2016-01-12 16:50:06 -05:00
|
|
|
}
|
|
|
|
}
|