2022-07-21 19:39:53 +00:00
|
|
|
package rpcclient
|
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"
|
2022-02-21 13:06:43 +00:00
|
|
|
"sync"
|
2018-03-05 08:53:09 +00:00
|
|
|
"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"
|
2022-07-22 16:09:29 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/neorpc"
|
2022-08-01 10:37:07 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/invoker"
|
2020-09-25 16:32:53 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2022-02-18 17:28:13 +00:00
|
|
|
"go.uber.org/atomic"
|
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
|
2021-05-12 20:17:03 +00:00
|
|
|
// Number of blocks after which cache is expired.
|
2020-04-15 06:50:13 +00:00
|
|
|
cacheTimeout = 100
|
2018-03-05 08:53:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Client represents the middleman for executing JSON RPC calls
|
2022-02-22 13:49:38 +00:00
|
|
|
// to remote NEO RPC nodes. Client is thread-safe and can be used from
|
|
|
|
// multiple goroutines.
|
2018-03-05 08:53:09 +00:00
|
|
|
type Client struct {
|
2022-02-21 10:13:39 +00:00
|
|
|
cli *http.Client
|
|
|
|
endpoint *url.URL
|
|
|
|
ctx context.Context
|
2022-10-12 12:23:32 +00:00
|
|
|
// ctxCancel is a cancel function aimed to send closing signal to the users of
|
|
|
|
// ctx.
|
|
|
|
ctxCancel func()
|
|
|
|
opts Options
|
|
|
|
requestF func(*neorpc.Request) (*neorpc.Response, error)
|
2022-02-21 10:13:39 +00:00
|
|
|
|
2022-08-01 10:37:07 +00:00
|
|
|
// reader is an Invoker that has no signers and uses current state,
|
|
|
|
// it's used to implement various getters. It'll be removed eventually,
|
|
|
|
// but for now it keeps Client's API compatibility.
|
|
|
|
reader *invoker.Invoker
|
|
|
|
|
2022-02-21 13:06:43 +00:00
|
|
|
cacheLock sync.RWMutex
|
2022-04-20 18:30:09 +00:00
|
|
|
// cache stores RPC node related information the client is bound to.
|
2022-02-21 10:13:39 +00:00
|
|
|
// cache is mostly filled in during Init(), but can also be updated
|
|
|
|
// during regular Client lifecycle.
|
|
|
|
cache cache
|
2022-02-18 17:28:13 +00:00
|
|
|
|
|
|
|
latestReqID *atomic.Uint64
|
2022-04-20 18:30:09 +00:00
|
|
|
// getNextRequestID returns an ID to be used for the subsequent request creation.
|
|
|
|
// It is defined on Client, so that our testing code can override this method
|
|
|
|
// for the sake of more predictable request IDs generation behavior.
|
2022-02-18 17:28:13 +00:00
|
|
|
getNextRequestID func() uint64
|
2018-03-05 08:53:09 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 12:01:57 +00:00
|
|
|
// Options defines options for the RPC client.
|
2022-04-20 18:30:09 +00:00
|
|
|
// All values are optional. If any duration is not specified,
|
2020-04-29 15:15:26 +00:00
|
|
|
// 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
|
2021-09-02 08:25:57 +00:00
|
|
|
// Limit total number of connections per host. No limit by default.
|
|
|
|
MaxConnsPerHost int
|
2018-03-05 08:53:09 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 20:17:03 +00:00
|
|
|
// cache stores cache values for the RPC client methods.
|
2020-04-15 06:50:13 +00:00
|
|
|
type cache struct {
|
2022-02-21 10:13:39 +00:00
|
|
|
initDone bool
|
|
|
|
network netmode.Magic
|
|
|
|
stateRootInHeader bool
|
2020-04-15 06:50:13 +00:00
|
|
|
calculateValidUntilBlock calculateValidUntilBlockCache
|
2020-09-25 16:32:53 +00:00
|
|
|
nativeHashes map[string]util.Uint160
|
2020-04-15 06:50:13 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// calculateValidUntilBlockCache stores a cached number of validators and
|
2021-05-12 20:17:03 +00:00
|
|
|
// cache expiration value in blocks.
|
2020-04-15 06:50:13 +00:00
|
|
|
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
|
2022-09-07 16:09:53 +00:00
|
|
|
// initialize stateroot setting for the network the client is operating on if
|
|
|
|
// you plan using GetBlock*.
|
2020-02-17 12:01:57 +00:00
|
|
|
func New(ctx context.Context, endpoint string, opts Options) (*Client, error) {
|
2022-02-21 15:30:07 +00:00
|
|
|
cl := new(Client)
|
|
|
|
err := initClient(ctx, cl, endpoint, opts)
|
2018-03-05 08:53:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-02-21 15:30:07 +00:00
|
|
|
return cl, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func initClient(ctx context.Context, cl *Client, endpoint string, opts Options) error {
|
|
|
|
url, err := url.Parse(endpoint)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-03-05 08:53:09 +00:00
|
|
|
|
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,
|
2021-09-02 08:25:57 +00:00
|
|
|
MaxConnsPerHost: opts.MaxConnsPerHost,
|
2020-04-29 14:19:31 +00:00
|
|
|
},
|
|
|
|
Timeout: opts.RequestTimeout,
|
2018-03-05 08:53:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(@antdm): Enable SSL.
|
2021-05-12 16:10:31 +00:00
|
|
|
// if opts.Cert != "" && opts.Key != "" {
|
|
|
|
// }
|
2018-03-05 08:53:09 +00:00
|
|
|
|
2022-10-12 12:23:32 +00:00
|
|
|
cancelCtx, cancel := context.WithCancel(ctx)
|
|
|
|
cl.ctx = cancelCtx
|
|
|
|
cl.ctxCancel = cancel
|
2022-02-21 15:30:07 +00:00
|
|
|
cl.cli = httpClient
|
|
|
|
cl.endpoint = url
|
|
|
|
cl.cache = cache{
|
|
|
|
nativeHashes: make(map[string]util.Uint160),
|
2020-04-29 15:04:05 +00:00
|
|
|
}
|
2022-02-21 15:30:07 +00:00
|
|
|
cl.latestReqID = atomic.NewUint64(0)
|
2022-02-18 17:28:13 +00:00
|
|
|
cl.getNextRequestID = (cl).getRequestID
|
2020-04-29 19:51:43 +00:00
|
|
|
cl.opts = opts
|
|
|
|
cl.requestF = cl.makeHTTPRequest
|
2022-08-01 10:37:07 +00:00
|
|
|
cl.reader = invoker.New(cl, nil)
|
2022-02-21 15:30:07 +00:00
|
|
|
return nil
|
2018-03-05 08:53:09 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 17:28:13 +00:00
|
|
|
func (c *Client) getRequestID() uint64 {
|
|
|
|
return c.latestReqID.Inc()
|
|
|
|
}
|
|
|
|
|
2021-09-08 10:02:14 +00:00
|
|
|
// Init sets magic of the network client connected to, stateRootInHeader option
|
|
|
|
// and native NEO, GAS and Policy contracts scripthashes. This method should be
|
2022-02-21 10:41:14 +00:00
|
|
|
// called before any header- or block-related requests in order to deserialize
|
|
|
|
// responses properly.
|
2020-10-14 15:13:20 +00:00
|
|
|
func (c *Client) Init() error {
|
|
|
|
version, err := c.GetVersion()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to get network magic: %w", err)
|
|
|
|
}
|
2022-05-06 10:56:09 +00:00
|
|
|
natives, err := c.GetNativeContracts()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to get native contracts: %w", err)
|
|
|
|
}
|
2022-02-21 13:06:43 +00:00
|
|
|
|
|
|
|
c.cacheLock.Lock()
|
|
|
|
defer c.cacheLock.Unlock()
|
|
|
|
|
2022-02-21 10:13:39 +00:00
|
|
|
c.cache.network = version.Protocol.Network
|
|
|
|
c.cache.stateRootInHeader = version.Protocol.StateRootInHeader
|
2022-05-06 10:56:09 +00:00
|
|
|
for _, ctr := range natives {
|
|
|
|
c.cache.nativeHashes[ctr.Manifest.Name] = ctr.Hash
|
2020-12-13 16:08:47 +00:00
|
|
|
}
|
2022-05-06 10:56:09 +00:00
|
|
|
|
2022-02-21 10:13:39 +00:00
|
|
|
c.cache.initDone = true
|
2020-10-14 15:13:20 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-07-01 20:07:37 +00:00
|
|
|
// Close closes unused underlying networks connections.
|
|
|
|
func (c *Client) Close() {
|
2022-10-12 12:23:32 +00:00
|
|
|
c.ctxCancel()
|
2022-07-01 20:07:37 +00:00
|
|
|
c.cli.CloseIdleConnections()
|
|
|
|
}
|
|
|
|
|
2022-07-07 17:03:10 +00:00
|
|
|
func (c *Client) performRequest(method string, p []interface{}, v interface{}) error {
|
|
|
|
if p == nil {
|
|
|
|
p = []interface{}{} // neo-project/neo-modules#742
|
|
|
|
}
|
2022-07-22 16:09:29 +00:00
|
|
|
var r = neorpc.Request{
|
|
|
|
JSONRPC: neorpc.JSONRPCVersion,
|
2022-07-07 15:33:23 +00:00
|
|
|
Method: method,
|
2022-07-07 17:03:10 +00:00
|
|
|
Params: p,
|
2022-07-07 15:33:23 +00:00
|
|
|
ID: c.getNextRequestID(),
|
2020-04-29 15:39:24 +00:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-07-22 16:09:29 +00:00
|
|
|
func (c *Client) makeHTTPRequest(r *neorpc.Request) (*neorpc.Response, error) {
|
2018-11-26 15:56:45 +00:00
|
|
|
var (
|
|
|
|
buf = new(bytes.Buffer)
|
2022-07-22 16:09:29 +00:00
|
|
|
raw = new(neorpc.Response)
|
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()
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// The node might send us a proper JSON anyway, so look there first and if
|
|
|
|
// it parses, 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
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Ping attempts to create a connection to the endpoint
|
|
|
|
// and returns an error if there is any.
|
2018-03-05 08:53:09 +00:00
|
|
|
func (c *Client) Ping() error {
|
|
|
|
conn, err := net.DialTimeout("tcp", c.endpoint.Host, defaultDialTimeout)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_ = conn.Close()
|
|
|
|
return nil
|
|
|
|
}
|
2022-10-12 12:23:32 +00:00
|
|
|
|
|
|
|
// Context returns client instance context.
|
|
|
|
func (c *Client) Context() context.Context {
|
|
|
|
return c.ctx
|
|
|
|
}
|