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 {
|
|
|
|
// The underlying http client. It's never a good practice to use
|
|
|
|
// the http.DefaultClient, therefore we will role our own.
|
2019-02-12 19:03:21 +00:00
|
|
|
cliMu *sync.Mutex
|
|
|
|
cli *http.Client
|
|
|
|
endpoint *url.URL
|
|
|
|
ctx context.Context
|
|
|
|
version string
|
|
|
|
wifMu *sync.Mutex
|
2019-08-27 13:29:42 +00:00
|
|
|
wif *keys.WIF
|
2019-02-12 19:03:21 +00:00
|
|
|
balancerMu *sync.Mutex
|
2020-02-17 11:54:53 +00:00
|
|
|
balancer request.BalanceGetter
|
2020-04-15 06:50:13 +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.
|
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 {
|
2019-02-12 19:03:21 +00:00
|
|
|
Cert string
|
|
|
|
Key string
|
|
|
|
CACert string
|
|
|
|
DialTimeout time.Duration
|
|
|
|
Client *http.Client
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts.Version == "" {
|
|
|
|
opts.Version = defaultClientVersion
|
|
|
|
}
|
|
|
|
|
2019-02-12 19:03:21 +00:00
|
|
|
if opts.Client == nil {
|
|
|
|
opts.Client = &http.Client{
|
|
|
|
Transport: &http.Transport{
|
|
|
|
DialContext: (&net.Dialer{
|
|
|
|
Timeout: opts.DialTimeout,
|
|
|
|
}).DialContext,
|
|
|
|
},
|
|
|
|
}
|
2018-03-05 08:53:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(@antdm): Enable SSL.
|
|
|
|
if opts.Cert != "" && opts.Key != "" {
|
|
|
|
}
|
|
|
|
|
2019-02-12 19:03:21 +00:00
|
|
|
if opts.Client.Timeout == 0 {
|
|
|
|
opts.Client.Timeout = defaultRequestTimeout
|
|
|
|
}
|
|
|
|
|
2018-03-05 08:53:09 +00:00
|
|
|
return &Client{
|
2019-02-12 19:03:21 +00:00
|
|
|
ctx: ctx,
|
|
|
|
cli: opts.Client,
|
|
|
|
cliMu: new(sync.Mutex),
|
|
|
|
balancerMu: new(sync.Mutex),
|
|
|
|
wifMu: new(sync.Mutex),
|
|
|
|
endpoint: url,
|
|
|
|
version: opts.Version,
|
2018-03-05 08:53:09 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-09-03 14:51:37 +00:00
|
|
|
// Balancer is a getter for balance field.
|
2020-02-17 11:54:53 +00:00
|
|
|
func (c *Client) Balancer() request.BalanceGetter {
|
2019-02-12 19:03:21 +00:00
|
|
|
c.balancerMu.Lock()
|
|
|
|
defer c.balancerMu.Unlock()
|
|
|
|
return c.balancer
|
|
|
|
}
|
|
|
|
|
2019-09-03 14:51:37 +00:00
|
|
|
// SetBalancer is a setter for balance field.
|
2020-02-17 11:54:53 +00:00
|
|
|
func (c *Client) SetBalancer(b request.BalanceGetter) {
|
2019-02-12 19:03:21 +00:00
|
|
|
c.balancerMu.Lock()
|
|
|
|
defer c.balancerMu.Unlock()
|
|
|
|
|
|
|
|
if b != nil {
|
|
|
|
c.balancer = b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-03 14:51:37 +00:00
|
|
|
// Client is a getter for client field.
|
2019-02-12 19:03:21 +00:00
|
|
|
func (c *Client) Client() *http.Client {
|
|
|
|
c.cliMu.Lock()
|
|
|
|
defer c.cliMu.Unlock()
|
|
|
|
return c.cli
|
|
|
|
}
|
|
|
|
|
2019-09-03 14:51:37 +00:00
|
|
|
// SetClient is a setter for client field.
|
2019-02-12 19:03:21 +00:00
|
|
|
func (c *Client) SetClient(cli *http.Client) {
|
|
|
|
c.cliMu.Lock()
|
|
|
|
defer c.cliMu.Unlock()
|
|
|
|
|
|
|
|
if cli != nil {
|
|
|
|
c.cli = cli
|
|
|
|
}
|
2018-12-21 09:32:18 +00:00
|
|
|
}
|
|
|
|
|
2019-11-19 17:16:15 +00:00
|
|
|
// CalculateInputs creates input transactions for the specified amount of given
|
|
|
|
// asset belonging to specified address. This implementation uses GetUnspents
|
2020-04-14 12:36:29 +00:00
|
|
|
// 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
|
|
|
|
}
|
2019-02-12 19:03:21 +00:00
|
|
|
resp, err := c.Client().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
|
|
|
|
}
|