2020-02-17 12:01:57 +00:00
|
|
|
package client
|
2018-03-05 08:53:09 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2020-08-06 14:44:08 +00:00
|
|
|
"errors"
|
2018-03-05 08:53:09 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"time"
|
2018-12-21 09:32:18 +00:00
|
|
|
|
2020-06-18 06:43:37 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpc/request"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpc/response"
|
2018-03-05 08:53:09 +00:00
|
|
|
)
|
|
|
|
|
2019-11-22 09:08:33 +00:00
|
|
|
const (
|
2018-03-05 08:53:09 +00:00
|
|
|
defaultDialTimeout = 4 * time.Second
|
|
|
|
defaultRequestTimeout = 4 * time.Second
|
|
|
|
defaultClientVersion = "2.0"
|
2020-04-15 06:50:13 +00:00
|
|
|
// number of blocks after which cache is expired
|
|
|
|
cacheTimeout = 100
|
2018-03-05 08:53:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Client represents the middleman for executing JSON RPC calls
|
|
|
|
// to remote NEO RPC nodes.
|
|
|
|
type Client struct {
|
2020-04-29 15:04:05 +00:00
|
|
|
cli *http.Client
|
|
|
|
endpoint *url.URL
|
2020-10-14 15:13:20 +00:00
|
|
|
network netmode.Magic
|
|
|
|
initDone bool
|
2020-04-29 15:04:05 +00:00
|
|
|
ctx context.Context
|
2020-04-29 19:51:43 +00:00
|
|
|
opts Options
|
|
|
|
requestF func(*request.Raw) (*response.Raw, error)
|
2020-04-29 15:04:05 +00:00
|
|
|
cache cache
|
2018-03-05 08:53:09 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 12:01:57 +00:00
|
|
|
// Options defines options for the RPC client.
|
2020-04-29 15:15:26 +00:00
|
|
|
// All values are optional. If any duration is not specified
|
|
|
|
// a default of 4 seconds will be used.
|
2020-02-17 12:01:57 +00:00
|
|
|
type Options struct {
|
2020-04-29 15:15:26 +00:00
|
|
|
// Cert is a client-side certificate, it doesn't work at the moment along
|
|
|
|
// with the other two options below.
|
2020-04-29 14:19:31 +00:00
|
|
|
Cert string
|
|
|
|
Key string
|
|
|
|
CACert string
|
|
|
|
DialTimeout time.Duration
|
|
|
|
RequestTimeout time.Duration
|
2018-03-05 08:53:09 +00:00
|
|
|
}
|
|
|
|
|
2020-04-15 06:50:13 +00:00
|
|
|
// cache stores cache values for the RPC client methods
|
|
|
|
type cache struct {
|
|
|
|
calculateValidUntilBlock calculateValidUntilBlockCache
|
|
|
|
}
|
|
|
|
|
|
|
|
// calculateValidUntilBlockCache stores cached number of validators and
|
|
|
|
// cache expiration value in blocks
|
|
|
|
type calculateValidUntilBlockCache struct {
|
|
|
|
validatorsCount uint32
|
|
|
|
expiresAt uint32
|
|
|
|
}
|
|
|
|
|
2020-10-14 15:13:20 +00:00
|
|
|
// New returns a new Client ready to use. You should call Init method to
|
|
|
|
// initialize network magic the client is operating on.
|
2020-02-17 12:01:57 +00:00
|
|
|
func New(ctx context.Context, endpoint string, opts Options) (*Client, error) {
|
2018-03-05 08:53:09 +00:00
|
|
|
url, err := url.Parse(endpoint)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-04-29 14:19:31 +00:00
|
|
|
if opts.DialTimeout <= 0 {
|
|
|
|
opts.DialTimeout = defaultDialTimeout
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts.RequestTimeout <= 0 {
|
|
|
|
opts.RequestTimeout = defaultRequestTimeout
|
|
|
|
}
|
|
|
|
|
|
|
|
httpClient := &http.Client{
|
|
|
|
Transport: &http.Transport{
|
|
|
|
DialContext: (&net.Dialer{
|
|
|
|
Timeout: opts.DialTimeout,
|
|
|
|
}).DialContext,
|
|
|
|
},
|
|
|
|
Timeout: opts.RequestTimeout,
|
2018-03-05 08:53:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(@antdm): Enable SSL.
|
|
|
|
if opts.Cert != "" && opts.Key != "" {
|
|
|
|
}
|
|
|
|
|
2020-04-29 15:04:05 +00:00
|
|
|
cl := &Client{
|
|
|
|
ctx: ctx,
|
|
|
|
cli: httpClient,
|
|
|
|
endpoint: url,
|
|
|
|
}
|
2020-04-29 19:51:43 +00:00
|
|
|
cl.opts = opts
|
|
|
|
cl.requestF = cl.makeHTTPRequest
|
2020-04-29 15:04:05 +00:00
|
|
|
return cl, nil
|
2018-03-05 08:53:09 +00:00
|
|
|
}
|
|
|
|
|
2020-10-14 15:13:20 +00:00
|
|
|
// Init sets magic of the network client connected to. This method should be called
|
|
|
|
// before any transaction-, header- or block-related requests in order to deserialize
|
|
|
|
// responses properly.
|
|
|
|
func (c *Client) Init() error {
|
|
|
|
version, err := c.GetVersion()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to get network magic: %w", err)
|
|
|
|
}
|
|
|
|
c.network = version.Magic
|
|
|
|
c.initDone = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-14 12:02:38 +00:00
|
|
|
func (c *Client) performRequest(method string, p request.RawParams, v interface{}) error {
|
2020-04-29 15:39:24 +00:00
|
|
|
var r = request.Raw{
|
|
|
|
JSONRPC: request.JSONRPCVersion,
|
|
|
|
Method: method,
|
|
|
|
RawParams: p.Values,
|
|
|
|
ID: 1,
|
|
|
|
}
|
|
|
|
|
2020-04-29 19:51:43 +00:00
|
|
|
raw, err := c.requestF(&r)
|
2020-04-29 15:39:24 +00:00
|
|
|
|
|
|
|
if raw != nil && raw.Error != nil {
|
|
|
|
return raw.Error
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
2020-04-29 19:51:43 +00:00
|
|
|
} else if raw == nil || raw.Result == nil {
|
|
|
|
return errors.New("no result returned")
|
2020-04-29 15:39:24 +00:00
|
|
|
}
|
|
|
|
return json.Unmarshal(raw.Result, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) makeHTTPRequest(r *request.Raw) (*response.Raw, error) {
|
2018-11-26 15:56:45 +00:00
|
|
|
var (
|
|
|
|
buf = new(bytes.Buffer)
|
2020-04-29 15:39:24 +00:00
|
|
|
raw = new(response.Raw)
|
2018-11-26 15:56:45 +00:00
|
|
|
)
|
2018-03-05 08:53:09 +00:00
|
|
|
|
2018-11-26 15:56:45 +00:00
|
|
|
if err := json.NewEncoder(buf).Encode(r); err != nil {
|
2020-04-29 15:39:24 +00:00
|
|
|
return nil, err
|
2018-03-05 08:53:09 +00:00
|
|
|
}
|
2018-11-26 15:56:45 +00:00
|
|
|
|
|
|
|
req, err := http.NewRequest("POST", c.endpoint.String(), buf)
|
2018-03-05 08:53:09 +00:00
|
|
|
if err != nil {
|
2020-04-29 15:39:24 +00:00
|
|
|
return nil, err
|
2018-03-05 08:53:09 +00:00
|
|
|
}
|
2020-04-29 14:19:31 +00:00
|
|
|
resp, err := c.cli.Do(req)
|
2018-03-05 08:53:09 +00:00
|
|
|
if err != nil {
|
2020-04-29 15:39:24 +00:00
|
|
|
return nil, err
|
2018-03-05 08:53:09 +00:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2020-02-20 17:24:31 +00:00
|
|
|
// The node might send us proper JSON anyway, so look there first and if
|
|
|
|
// it parses, then it has more relevant data than HTTP error code.
|
2020-02-20 18:08:22 +00:00
|
|
|
err = json.NewDecoder(resp.Body).Decode(raw)
|
2020-04-29 15:39:24 +00:00
|
|
|
if err != nil {
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
err = fmt.Errorf("HTTP %d/%s", resp.StatusCode, http.StatusText(resp.StatusCode))
|
2020-02-20 18:08:22 +00:00
|
|
|
} else {
|
2020-08-06 14:44:08 +00:00
|
|
|
err = fmt.Errorf("JSON decoding: %w", err)
|
2020-02-20 18:08:22 +00:00
|
|
|
}
|
2018-03-05 08:53:09 +00:00
|
|
|
}
|
2020-04-29 15:39:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return raw, nil
|
2018-03-05 08:53:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ping attempts to create a connection to the endpoint.
|
|
|
|
// and returns an error if there is one.
|
|
|
|
func (c *Client) Ping() error {
|
|
|
|
conn, err := net.DialTimeout("tcp", c.endpoint.Host, defaultDialTimeout)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_ = conn.Close()
|
|
|
|
return nil
|
|
|
|
}
|