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"
|
2020-05-28 19:30:48 +00:00
|
|
|
"sort"
|
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
|
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
|
|
|
wifMu *sync.Mutex
|
|
|
|
wif *keys.WIF
|
|
|
|
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-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
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
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-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-04-29 15:39:24 +00:00
|
|
|
err = errors.Wrap(err, "JSON decoding")
|
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
|
|
|
|
}
|
2020-05-28 19:30:48 +00:00
|
|
|
|
|
|
|
// unspentsToInputs uses UnspentBalances to create a slice of inputs for a new
|
|
|
|
// transcation containing the required amount of asset.
|
|
|
|
func unspentsToInputs(utxos state.UnspentBalances, required util.Fixed8) ([]transaction.Input, util.Fixed8, error) {
|
|
|
|
var (
|
|
|
|
num, i uint16
|
|
|
|
selected = util.Fixed8(0)
|
|
|
|
)
|
|
|
|
sort.Sort(utxos)
|
|
|
|
|
|
|
|
for _, us := range utxos {
|
|
|
|
if selected >= required {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
selected += us.Value
|
|
|
|
num++
|
|
|
|
}
|
|
|
|
if selected < required {
|
|
|
|
return nil, util.Fixed8(0), errors.New("cannot compose inputs for transaction; check sender balance")
|
|
|
|
}
|
|
|
|
|
|
|
|
inputs := make([]transaction.Input, 0, num)
|
|
|
|
for i = 0; i < num; i++ {
|
|
|
|
inputs = append(inputs, transaction.Input{
|
|
|
|
PrevHash: utxos[i].Tx,
|
|
|
|
PrevIndex: utxos[i].Index,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return inputs, selected, nil
|
|
|
|
}
|