2020-07-24 13:54:03 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-07-18 13:41:35 +00:00
|
|
|
"errors"
|
2021-02-09 17:52:10 +00:00
|
|
|
"fmt"
|
2020-07-24 13:54:03 +00:00
|
|
|
"time"
|
|
|
|
|
2023-05-03 12:58:21 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
|
2024-06-24 14:12:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/metrics"
|
2023-06-08 13:37:46 +00:00
|
|
|
morphmetrics "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/metrics"
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/logger"
|
2022-12-30 09:47:18 +00:00
|
|
|
lru "github.com/hashicorp/golang-lru/v2"
|
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"
|
2022-07-28 16:22:32 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient"
|
2022-08-29 15:32:54 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/actor"
|
2021-02-09 17:52:10 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2020-07-24 13:54:03 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/wallet"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Option is a client configuration change function.
|
|
|
|
type Option func(*cfg)
|
|
|
|
|
2022-03-31 11:17:01 +00:00
|
|
|
// Callback is a function that is going to be called
|
|
|
|
// on certain Client's state.
|
|
|
|
type Callback func()
|
|
|
|
|
2020-07-24 13:54:03 +00:00
|
|
|
// groups the configurations with default values.
|
|
|
|
type cfg struct {
|
|
|
|
dialTimeout time.Duration // client dial timeout
|
|
|
|
|
|
|
|
logger *logger.Logger // logging component
|
|
|
|
|
2023-06-08 13:37:46 +00:00
|
|
|
metrics morphmetrics.Register
|
|
|
|
|
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
|
|
|
|
2022-07-18 13:41:35 +00:00
|
|
|
endpoints []Endpoint
|
2021-09-21 14:30:45 +00:00
|
|
|
|
2022-03-31 11:17:01 +00:00
|
|
|
inactiveModeCb Callback
|
2022-10-12 17:12:35 +00:00
|
|
|
|
|
|
|
switchInterval time.Duration
|
2023-05-26 09:15:50 +00:00
|
|
|
|
|
|
|
morphCacheMetrics metrics.MorphCacheMetrics
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
|
2021-02-25 16:50:10 +00:00
|
|
|
const (
|
2022-10-12 16:47:33 +00:00
|
|
|
defaultDialTimeout = 5 * time.Second
|
|
|
|
defaultWaitInterval = 500 * time.Millisecond
|
2021-02-25 16:50:10 +00:00
|
|
|
)
|
2020-07-24 13:54:03 +00:00
|
|
|
|
2023-10-31 11:56:55 +00:00
|
|
|
var ErrNoHealthyEndpoint = errors.New("no healthy endpoint")
|
2023-05-03 12:58:21 +00:00
|
|
|
|
2020-07-24 13:54:03 +00:00
|
|
|
func defaultConfig() *cfg {
|
|
|
|
return &cfg{
|
2021-02-25 16:50:10 +00:00
|
|
|
dialTimeout: defaultDialTimeout,
|
2022-09-28 07:41:01 +00:00
|
|
|
logger: &logger.Logger{Logger: zap.L()},
|
2023-06-08 13:37:46 +00:00
|
|
|
metrics: morphmetrics.NoopRegister{},
|
2021-02-25 16:50:10 +00:00
|
|
|
waitInterval: defaultWaitInterval,
|
2021-08-05 14:17:54 +00:00
|
|
|
signer: &transaction.Signer{
|
|
|
|
Scopes: transaction.Global,
|
|
|
|
},
|
2023-06-14 09:12:47 +00:00
|
|
|
morphCacheMetrics: &morphmetrics.NoopMorphCacheMetrics{},
|
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:
|
2022-08-15 16:20:20 +00:00
|
|
|
// - client context: Background;
|
|
|
|
// - dial timeout: 5s;
|
|
|
|
// - blockchain network type: netmode.PrivNet;
|
|
|
|
// - signer with the global scope;
|
|
|
|
// - wait interval: 500ms;
|
2022-09-28 07:41:01 +00:00
|
|
|
// - logger: &logger.Logger{Logger: zap.L()}.
|
2023-06-08 13:37:46 +00:00
|
|
|
// - metrics: metrics.NoopRegister
|
2020-07-24 13:54:03 +00:00
|
|
|
//
|
|
|
|
// 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.
|
2023-05-03 12:58:21 +00:00
|
|
|
// If the list of endpoints provided - uses first alive.
|
|
|
|
// If there are no healthy endpoint - returns ErrNoHealthyEndpoint.
|
2023-04-05 13:58:32 +00:00
|
|
|
func New(ctx context.Context, key *keys.PrivateKey, 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
|
|
|
}
|
|
|
|
|
2022-08-29 15:32:54 +00:00
|
|
|
acc := wallet.NewAccountFromPrivateKey(key)
|
|
|
|
accAddr := key.GetScriptHash()
|
|
|
|
|
2020-07-24 13:54:03 +00:00
|
|
|
// build default configuration
|
|
|
|
cfg := defaultConfig()
|
|
|
|
|
|
|
|
// apply options
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(cfg)
|
|
|
|
}
|
|
|
|
|
2022-07-18 13:41:35 +00:00
|
|
|
if len(cfg.endpoints) == 0 {
|
|
|
|
return nil, errors.New("no endpoints were provided")
|
|
|
|
}
|
|
|
|
|
2021-02-09 17:52:10 +00:00
|
|
|
cli := &Client{
|
2023-05-26 09:15:50 +00:00
|
|
|
cache: newClientCache(cfg.morphCacheMetrics),
|
2023-05-30 07:14:37 +00:00
|
|
|
logger: cfg.logger,
|
2023-06-08 13:37:46 +00:00
|
|
|
metrics: cfg.metrics,
|
2023-05-30 07:14:37 +00:00
|
|
|
acc: acc,
|
|
|
|
accAddr: accAddr,
|
|
|
|
cfg: *cfg,
|
|
|
|
closeChan: make(chan struct{}),
|
2021-02-09 17:52:10 +00:00
|
|
|
}
|
|
|
|
|
2022-08-02 10:14:13 +00:00
|
|
|
cli.endpoints.init(cfg.endpoints)
|
|
|
|
|
2022-08-29 15:32:54 +00:00
|
|
|
var err error
|
2022-11-03 12:44:59 +00:00
|
|
|
var act *actor.Actor
|
2024-10-09 08:34:36 +00:00
|
|
|
var endpoint Endpoint
|
|
|
|
for cli.endpoints.curr, endpoint = range cli.endpoints.list {
|
|
|
|
cli.client, act, err = cli.newCli(ctx, endpoint)
|
2021-02-09 17:52:10 +00:00
|
|
|
if err != nil {
|
2024-10-09 08:34:36 +00:00
|
|
|
cli.logger.Warn(logs.FrostFSIRCouldntCreateRPCClientForEndpoint,
|
|
|
|
zap.Error(err), zap.String("endpoint", endpoint.Address))
|
|
|
|
} else {
|
|
|
|
cli.logger.Info(logs.FrostFSIRCreatedRPCClientForEndpoint,
|
|
|
|
zap.String("endpoint", endpoint.Address))
|
|
|
|
if cli.endpoints.curr > 0 && cli.cfg.switchInterval != 0 {
|
|
|
|
cli.switchIsActive.Store(true)
|
|
|
|
go cli.switchToMostPrioritized(ctx)
|
2023-05-03 12:58:21 +00:00
|
|
|
}
|
2024-10-09 08:34:36 +00:00
|
|
|
break
|
2021-02-09 17:52:10 +00:00
|
|
|
}
|
2021-09-21 14:30:45 +00:00
|
|
|
}
|
2024-10-09 08:34:36 +00:00
|
|
|
if cli.client == nil {
|
|
|
|
return nil, ErrNoHealthyEndpoint
|
|
|
|
}
|
2022-11-03 12:44:59 +00:00
|
|
|
cli.setActor(act)
|
2021-09-21 14:30:45 +00:00
|
|
|
|
2023-05-11 14:05:24 +00:00
|
|
|
go cli.closeWaiter(ctx)
|
2021-06-01 09:29:32 +00:00
|
|
|
|
2021-02-09 17:52:10 +00:00
|
|
|
return cli, nil
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
|
2024-06-10 08:48:14 +00:00
|
|
|
func (c *Client) newCli(ctx context.Context, endpoint Endpoint) (*rpcclient.WSClient, *actor.Actor, error) {
|
|
|
|
cfg, err := endpoint.MTLSConfig.parse()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("read mtls certificates: %w", err)
|
|
|
|
}
|
|
|
|
cli, err := rpcclient.NewWS(ctx, endpoint.Address, rpcclient.WSOptions{
|
2023-06-01 14:21:03 +00:00
|
|
|
Options: rpcclient.Options{
|
2024-06-10 08:48:14 +00:00
|
|
|
DialTimeout: c.cfg.dialTimeout,
|
|
|
|
TLSClientConfig: cfg,
|
2023-06-01 14:21:03 +00:00
|
|
|
},
|
2022-08-29 15:32:54 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2022-11-03 12:44:59 +00:00
|
|
|
return nil, nil, fmt.Errorf("WS client creation: %w", err)
|
2022-08-29 15:32:54 +00:00
|
|
|
}
|
|
|
|
|
2022-11-21 16:13:00 +00:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
cli.Close()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2022-08-29 15:32:54 +00:00
|
|
|
err = cli.Init()
|
|
|
|
if err != nil {
|
2022-11-03 12:44:59 +00:00
|
|
|
return nil, nil, fmt.Errorf("WS client initialization: %w", err)
|
2022-08-29 15:32:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
act, err := newActor(cli, c.acc, c.cfg)
|
|
|
|
if err != nil {
|
2022-11-03 12:44:59 +00:00
|
|
|
return nil, nil, fmt.Errorf("RPC actor creation: %w", err)
|
2022-08-29 15:32:54 +00:00
|
|
|
}
|
|
|
|
|
2022-11-03 12:44:59 +00:00
|
|
|
return cli, act, nil
|
2022-08-29 15:32:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newActor(ws *rpcclient.WSClient, acc *wallet.Account, cfg cfg) (*actor.Actor, error) {
|
|
|
|
return actor.New(ws, []actor.SignerAccount{{
|
|
|
|
Signer: transaction.Signer{
|
|
|
|
Account: acc.PrivateKey().PublicKey().GetScriptHash(),
|
|
|
|
Scopes: cfg.signer.Scopes,
|
|
|
|
AllowedContracts: cfg.signer.AllowedContracts,
|
|
|
|
AllowedGroups: cfg.signer.AllowedGroups,
|
|
|
|
},
|
|
|
|
Account: acc,
|
|
|
|
}})
|
|
|
|
}
|
|
|
|
|
2023-05-26 09:15:50 +00:00
|
|
|
func newClientCache(morphCacheMetrics metrics.MorphCacheMetrics) cache {
|
2022-12-30 09:47:18 +00:00
|
|
|
c, _ := lru.New[util.Uint256, uint32](100) // returns error only if size is negative
|
2022-02-10 11:58:14 +00:00
|
|
|
return cache{
|
|
|
|
txHeights: c,
|
2023-05-26 09:15:50 +00:00
|
|
|
metrics: morphCacheMetrics,
|
2022-02-10 11:58:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-24 13:54:03 +00:00
|
|
|
// 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.
|
|
|
|
//
|
2022-09-28 07:41:01 +00:00
|
|
|
// If option not provided, &logger.Logger{Logger: zap.L()} is used.
|
2020-07-24 13:54:03 +00:00
|
|
|
func WithLogger(logger *logger.Logger) Option {
|
|
|
|
return func(c *cfg) {
|
|
|
|
if logger != nil {
|
|
|
|
c.logger = logger
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-05 14:17:54 +00:00
|
|
|
|
2023-06-08 13:37:46 +00:00
|
|
|
// WithMetrics returns a client constructor option
|
|
|
|
// that specifies the component for reporting metrics.
|
|
|
|
//
|
|
|
|
// Ignores nil value.
|
|
|
|
//
|
|
|
|
// If option not provided, NoopMetrics is used.
|
|
|
|
func WithMetrics(metrics morphmetrics.Register) Option {
|
|
|
|
return func(c *cfg) {
|
|
|
|
if metrics != nil {
|
|
|
|
c.metrics = metrics
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2022-07-18 13:41:35 +00:00
|
|
|
// WithEndpoints returns a client constructor option
|
2021-08-26 07:59:02 +00:00
|
|
|
// that specifies additional Neo rpc endpoints.
|
2022-07-18 13:41:35 +00:00
|
|
|
func WithEndpoints(endpoints ...Endpoint) Option {
|
2021-08-26 07:59:02 +00:00
|
|
|
return func(c *cfg) {
|
2022-07-18 13:41:35 +00:00
|
|
|
c.endpoints = append(c.endpoints, endpoints...)
|
2021-08-26 07:59:02 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-21 14:30:45 +00:00
|
|
|
|
2022-03-31 11:17:01 +00:00
|
|
|
// WithConnLostCallback return a client constructor option
|
|
|
|
// that specifies a callback that is called when Client
|
|
|
|
// unsuccessfully tried to connect to all the specified
|
|
|
|
// endpoints.
|
|
|
|
func WithConnLostCallback(cb Callback) Option {
|
|
|
|
return func(c *cfg) {
|
|
|
|
c.inactiveModeCb = cb
|
|
|
|
}
|
|
|
|
}
|
2022-10-12 17:12:35 +00:00
|
|
|
|
|
|
|
// WithSwitchInterval returns a client constructor option
|
|
|
|
// that specifies a wait interval b/w attempts to reconnect
|
|
|
|
// to an RPC node with the highest priority.
|
|
|
|
func WithSwitchInterval(i time.Duration) Option {
|
|
|
|
return func(c *cfg) {
|
|
|
|
c.switchInterval = i
|
|
|
|
}
|
|
|
|
}
|
2023-05-26 09:15:50 +00:00
|
|
|
|
|
|
|
func WithMorphCacheMetrics(morphCacheMetrics metrics.MorphCacheMetrics) Option {
|
|
|
|
return func(c *cfg) {
|
|
|
|
c.morphCacheMetrics = morphCacheMetrics
|
|
|
|
}
|
|
|
|
}
|