2020-02-17 12:01:57 +00:00
|
|
|
package client
|
2018-03-05 08:53:09 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2019-02-12 19:03:21 +00:00
|
|
|
"sync"
|
2018-03-05 08:53:09 +00:00
|
|
|
"time"
|
2018-12-21 09:32:18 +00:00
|
|
|
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpc/request"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpc/response"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2018-12-21 09:32:18 +00:00
|
|
|
"github.com/pkg/errors"
|
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
|
|
|
|
ctx context.Context
|
|
|
|
version string
|
|
|
|
wifMu *sync.Mutex
|
|
|
|
wif *keys.WIF
|
|
|
|
balancer request.BalanceGetter
|
|
|
|
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.
|
2018-03-05 08:53:09 +00:00
|
|
|
// All Values are optional. If any duration is not specified
|
|
|
|
// a default of 3 seconds will be used.
|
2020-02-17 12:01:57 +00:00
|
|
|
type Options struct {
|
2020-04-29 15:04:05 +00:00
|
|
|
// Balancer is an implementation of request.BalanceGetter interface,
|
|
|
|
// if not set then the default Client's implementation will be used, but
|
|
|
|
// it relies on server support for `getunspents` RPC call which is
|
|
|
|
// standard for neo-go, but only implemented as a plugin for C# node. So
|
|
|
|
// you can override it here to use NeoScanServer for example.
|
|
|
|
Balancer request.BalanceGetter
|
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
|
|
|
// Version is the version of the client that will be send
|
|
|
|
// along with the request body. If no version is specified
|
|
|
|
// the default version (currently 2.0) will be used.
|
|
|
|
Version string
|
|
|
|
}
|
|
|
|
|
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-02-17 12:01:57 +00:00
|
|
|
// New returns a new Client ready to use.
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-03-05 08:53:09 +00:00
|
|
|
if opts.Version == "" {
|
|
|
|
opts.Version = defaultClientVersion
|
|
|
|
}
|
|
|
|
|
2020-04-29 14:19:31 +00:00
|
|
|
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,
|
|
|
|
wifMu: new(sync.Mutex),
|
|
|
|
endpoint: url,
|
|
|
|
version: opts.Version,
|
|
|
|
}
|
|
|
|
if opts.Balancer == nil {
|
|
|
|
opts.Balancer = cl
|
|
|
|
}
|
|
|
|
cl.balancer = opts.Balancer
|
|
|
|
return cl, nil
|
2018-03-05 08:53:09 +00:00
|
|
|
}
|
|
|
|
|
2019-09-03 14:51:37 +00:00
|
|
|
// WIF returns WIF structure associated with the client.
|
2019-08-27 13:29:42 +00:00
|
|
|
func (c *Client) WIF() keys.WIF {
|
2019-02-12 19:03:21 +00:00
|
|
|
c.wifMu.Lock()
|
|
|
|
defer c.wifMu.Unlock()
|
2019-08-27 13:29:42 +00:00
|
|
|
return keys.WIF{
|
2019-02-12 19:03:21 +00:00
|
|
|
Version: c.wif.Version,
|
|
|
|
Compressed: c.wif.Compressed,
|
|
|
|
PrivateKey: c.wif.PrivateKey,
|
|
|
|
S: c.wif.S,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-21 09:32:18 +00:00
|
|
|
// SetWIF decodes given WIF and adds some wallet
|
|
|
|
// data to client. Useful for RPC calls that require an open wallet.
|
|
|
|
func (c *Client) SetWIF(wif string) error {
|
2019-02-12 19:03:21 +00:00
|
|
|
c.wifMu.Lock()
|
|
|
|
defer c.wifMu.Unlock()
|
2019-08-27 13:29:42 +00:00
|
|
|
decodedWif, err := keys.WIFDecode(wif, 0x00)
|
2018-12-21 09:32:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "Failed to decode WIF; failed to add WIF to client ")
|
|
|
|
}
|
2019-02-12 19:03:21 +00:00
|
|
|
c.wif = decodedWif
|
2018-12-21 09:32:18 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-29 15:04:05 +00:00
|
|
|
// CalculateInputs implements request.BalanceGetter interface and returns inputs
|
|
|
|
// array for the specified amount of given asset belonging to specified address.
|
|
|
|
// This implementation uses GetUnspents JSON-RPC call internally, so make sure
|
|
|
|
// your RPC server supports that.
|
2019-11-19 17:16:15 +00:00
|
|
|
func (c *Client) CalculateInputs(address string, asset util.Uint256, cost util.Fixed8) ([]transaction.Input, util.Fixed8, error) {
|
2019-11-28 16:06:09 +00:00
|
|
|
var utxos state.UnspentBalances
|
2019-11-19 17:16:15 +00:00
|
|
|
|
|
|
|
resp, err := c.GetUnspents(address)
|
2020-02-20 18:08:22 +00:00
|
|
|
if err != nil {
|
2019-11-19 17:16:15 +00:00
|
|
|
return nil, util.Fixed8(0), errors.Wrapf(err, "cannot get balance for address %v", address)
|
|
|
|
}
|
2020-02-20 18:08:22 +00:00
|
|
|
for _, ubi := range resp.Balance {
|
2019-11-19 17:16:15 +00:00
|
|
|
if asset.Equals(ubi.AssetHash) {
|
|
|
|
utxos = ubi.Unspents
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return unspentsToInputs(utxos, cost)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-01-14 12:02:38 +00:00
|
|
|
func (c *Client) performRequest(method string, p request.RawParams, v interface{}) error {
|
2018-11-26 15:56:45 +00:00
|
|
|
var (
|
2020-01-14 12:02:38 +00:00
|
|
|
r = request.Raw{
|
|
|
|
JSONRPC: c.version,
|
|
|
|
Method: method,
|
|
|
|
RawParams: p.Values,
|
|
|
|
ID: 1,
|
2018-11-26 15:56:45 +00:00
|
|
|
}
|
|
|
|
buf = new(bytes.Buffer)
|
2020-02-20 18:08:22 +00:00
|
|
|
raw = &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 {
|
2018-03-05 08:53:09 +00:00
|
|
|
return err
|
|
|
|
}
|
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 {
|
|
|
|
return err
|
|
|
|
}
|
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 {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
if err == nil {
|
|
|
|
if raw.Error != nil {
|
|
|
|
err = raw.Error
|
|
|
|
} else {
|
|
|
|
err = json.Unmarshal(raw.Result, v)
|
|
|
|
}
|
|
|
|
} else if resp.StatusCode != http.StatusOK {
|
2020-02-20 17:24:31 +00:00
|
|
|
err = fmt.Errorf("HTTP %d/%s", resp.StatusCode, http.StatusText(resp.StatusCode))
|
2020-02-20 18:08:22 +00:00
|
|
|
} else {
|
|
|
|
err = errors.Wrap(err, "JSON decoding")
|
2018-03-05 08:53:09 +00:00
|
|
|
}
|
|
|
|
|
2020-02-20 17:24:31 +00:00
|
|
|
return err
|
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
|
|
|
|
}
|