2020-07-24 13:54:03 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
2022-02-10 11:58:14 +00:00
|
|
|
lru "github.com/hashicorp/golang-lru"
|
2021-08-05 14:17:54 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
2020-07-24 13:54:03 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2021-09-21 14:30:45 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpc/client"
|
2020-07-24 13:54:03 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/wallet"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/util/logger"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Option is a client configuration change function.
|
|
|
|
type Option func(*cfg)
|
|
|
|
|
|
|
|
// groups the configurations with default values.
|
|
|
|
type cfg struct {
|
|
|
|
ctx context.Context // neo-go client context
|
|
|
|
|
|
|
|
dialTimeout time.Duration // client dial timeout
|
|
|
|
|
|
|
|
logger *logger.Logger // logging component
|
|
|
|
|
2021-02-25 16:50:10 +00:00
|
|
|
waitInterval time.Duration
|
2021-08-05 14:17:54 +00:00
|
|
|
|
|
|
|
signer *transaction.Signer
|
2021-08-26 07:59:02 +00:00
|
|
|
|
|
|
|
extraEndpoints []string
|
2021-09-21 14:30:45 +00:00
|
|
|
|
2021-12-15 18:04:36 +00:00
|
|
|
maxConnPerHost int
|
|
|
|
|
2021-09-21 14:30:45 +00:00
|
|
|
singleCli *client.Client // neo-go client for single client mode
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
|
2021-02-25 16:50:10 +00:00
|
|
|
const (
|
2021-12-15 18:04:36 +00:00
|
|
|
defaultDialTimeout = 5 * time.Second
|
|
|
|
defaultWaitInterval = 500 * time.Millisecond
|
|
|
|
defaultMaxConnPerHost = 10
|
2021-02-25 16:50:10 +00:00
|
|
|
)
|
2020-07-24 13:54:03 +00:00
|
|
|
|
|
|
|
func defaultConfig() *cfg {
|
|
|
|
return &cfg{
|
2021-02-25 16:50:10 +00:00
|
|
|
ctx: context.Background(),
|
|
|
|
dialTimeout: defaultDialTimeout,
|
|
|
|
logger: zap.L(),
|
|
|
|
waitInterval: defaultWaitInterval,
|
2021-08-05 14:17:54 +00:00
|
|
|
signer: &transaction.Signer{
|
|
|
|
Scopes: transaction.Global,
|
|
|
|
},
|
2021-12-15 18:04:36 +00:00
|
|
|
maxConnPerHost: defaultMaxConnPerHost,
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates, initializes and returns the Client instance.
|
2021-07-23 14:37:56 +00:00
|
|
|
// Notary support should be enabled with EnableNotarySupport client
|
|
|
|
// method separately.
|
2020-07-24 13:54:03 +00:00
|
|
|
//
|
2021-05-31 08:55:40 +00:00
|
|
|
// If private key is nil, it panics.
|
2020-07-24 13:54:03 +00:00
|
|
|
//
|
|
|
|
// Other values are set according to provided options, or by default:
|
|
|
|
// * client context: Background;
|
|
|
|
// * dial timeout: 5s;
|
|
|
|
// * blockchain network type: netmode.PrivNet;
|
|
|
|
// * logger: zap.L().
|
|
|
|
//
|
|
|
|
// If desired option satisfies the default value, it can be omitted.
|
|
|
|
// If multiple options of the same config value are supplied,
|
|
|
|
// the option with the highest index in the arguments will be used.
|
2021-05-31 08:55:40 +00:00
|
|
|
func New(key *keys.PrivateKey, endpoint string, opts ...Option) (*Client, error) {
|
2020-07-24 13:54:03 +00:00
|
|
|
if key == nil {
|
2021-05-31 08:55:40 +00:00
|
|
|
panic("empty private key")
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// build default configuration
|
|
|
|
cfg := defaultConfig()
|
|
|
|
|
|
|
|
// apply options
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(cfg)
|
|
|
|
}
|
|
|
|
|
2021-09-21 14:30:45 +00:00
|
|
|
if cfg.singleCli != nil {
|
|
|
|
return &Client{
|
2022-03-15 11:33:09 +00:00
|
|
|
cache: newClientCache(),
|
2021-09-21 14:30:45 +00:00
|
|
|
singleClient: blankSingleClient(cfg.singleCli, wallet.NewAccountFromPrivateKey(key), cfg),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-08-26 07:59:02 +00:00
|
|
|
endpoints := append(cfg.extraEndpoints, endpoint)
|
2021-06-01 09:29:32 +00:00
|
|
|
|
2021-08-26 07:59:02 +00:00
|
|
|
return &Client{
|
2022-03-15 11:33:09 +00:00
|
|
|
cache: newClientCache(),
|
2021-08-26 07:59:02 +00:00
|
|
|
multiClient: &multiClient{
|
2022-03-15 11:33:09 +00:00
|
|
|
cfg: *cfg,
|
|
|
|
account: wallet.NewAccountFromPrivateKey(key),
|
|
|
|
endpoints: endpoints,
|
|
|
|
clients: make(map[string]*Client, len(endpoints)),
|
2021-08-26 07:59:02 +00:00
|
|
|
},
|
|
|
|
}, nil
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
|
2022-03-15 11:33:09 +00:00
|
|
|
func newClientCache() cache {
|
2022-02-10 11:58:14 +00:00
|
|
|
c, _ := lru.New(100) // returns error only if size is negative
|
|
|
|
return cache{
|
|
|
|
txHeights: c,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-24 13:54:03 +00:00
|
|
|
// WithContext returns a client constructor option that
|
|
|
|
// specifies the neo-go client context.
|
|
|
|
//
|
2021-09-21 14:30:45 +00:00
|
|
|
// Ignores nil value. Has no effect if WithSingleClient
|
|
|
|
// is provided.
|
2020-07-24 13:54:03 +00:00
|
|
|
//
|
|
|
|
// If option not provided, context.Background() is used.
|
|
|
|
func WithContext(ctx context.Context) Option {
|
|
|
|
return func(c *cfg) {
|
|
|
|
if ctx != nil {
|
|
|
|
c.ctx = ctx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithDialTimeout returns a client constructor option
|
|
|
|
// that specifies neo-go client dial timeout duration.
|
|
|
|
//
|
2021-09-21 14:30:45 +00:00
|
|
|
// Ignores non-positive value. Has no effect if WithSingleClient
|
|
|
|
// is provided.
|
2020-07-24 13:54:03 +00:00
|
|
|
//
|
|
|
|
// If option not provided, 5s timeout is used.
|
|
|
|
func WithDialTimeout(dur time.Duration) Option {
|
|
|
|
return func(c *cfg) {
|
|
|
|
if dur > 0 {
|
|
|
|
c.dialTimeout = dur
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithLogger returns a client constructor option
|
|
|
|
// that specifies the component for writing log messages.
|
|
|
|
//
|
|
|
|
// Ignores nil value.
|
|
|
|
//
|
|
|
|
// If option not provided, zap.L() is used.
|
|
|
|
func WithLogger(logger *logger.Logger) Option {
|
|
|
|
return func(c *cfg) {
|
|
|
|
if logger != nil {
|
|
|
|
c.logger = logger
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-05 14:17:54 +00:00
|
|
|
|
|
|
|
// WithSigner returns a client constructor option
|
|
|
|
// that specifies the signer and the scope of the transaction.
|
|
|
|
//
|
|
|
|
// Ignores nil value.
|
|
|
|
//
|
|
|
|
// If option not provided, signer with global scope is used.
|
|
|
|
func WithSigner(signer *transaction.Signer) Option {
|
|
|
|
return func(c *cfg) {
|
|
|
|
if signer != nil {
|
|
|
|
c.signer = signer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-26 07:59:02 +00:00
|
|
|
|
|
|
|
// WithExtraEndpoints returns a client constructor option
|
|
|
|
// that specifies additional Neo rpc endpoints.
|
2021-09-21 14:30:45 +00:00
|
|
|
//
|
|
|
|
// Has no effect if WithSingleClient is provided.
|
2021-08-26 07:59:02 +00:00
|
|
|
func WithExtraEndpoints(endpoints []string) Option {
|
|
|
|
return func(c *cfg) {
|
|
|
|
c.extraEndpoints = append(c.extraEndpoints, endpoints...)
|
|
|
|
}
|
|
|
|
}
|
2021-09-21 14:30:45 +00:00
|
|
|
|
2021-12-15 18:04:36 +00:00
|
|
|
// WithMaxConnectionPerHost returns a client constructor
|
|
|
|
// option that specifies Neo client's maximum opened
|
|
|
|
// connection per one host.
|
|
|
|
func WithMaxConnectionPerHost(m int) Option {
|
|
|
|
return func(c *cfg) {
|
|
|
|
c.maxConnPerHost = m
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-21 14:30:45 +00:00
|
|
|
// WithSingleClient returns a client constructor option
|
|
|
|
// that specifies single neo-go client and forces Client
|
|
|
|
// to use it and only it for requests.
|
|
|
|
//
|
|
|
|
// Passed client must already be initialized.
|
|
|
|
func WithSingleClient(cli *client.Client) Option {
|
|
|
|
return func(c *cfg) {
|
|
|
|
c.singleCli = cli
|
|
|
|
}
|
|
|
|
}
|