From 6339f1a468e1e698fb0de2ae976d0b1e6083b1d2 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Tue, 11 May 2021 13:24:13 +0300 Subject: [PATCH] [#493] node: Connect to main chain Establish client connection with main chain node on storage node startup. Client is configured simlarly to morph client. Signed-off-by: Leonard Lyubich --- cmd/neofs-node/config.go | 5 ++++ cmd/neofs-node/morph.go | 63 ++++++++++++++++++++++++++-------------- 2 files changed, 47 insertions(+), 21 deletions(-) diff --git a/cmd/neofs-node/config.go b/cmd/neofs-node/config.go index 31974ed1c..a34c5beeb 100644 --- a/cmd/neofs-node/config.go +++ b/cmd/neofs-node/config.go @@ -80,6 +80,9 @@ const ( cfgMorphNotifyRPCAddress = "morph.notification_endpoint" cfgMorphNotifyDialTimeout = "morph.dial_timeout" + cfgMainChainRPCAddress = "mainchain.rpc_endpoint" + cfgMainChainDialTimeout = "mainchain.dial_timeout" + // config keys for cfgAccounting cfgAccountingContract = "accounting.scripthash" cfgAccountingFee = "accounting.fee" @@ -219,6 +222,8 @@ type cfg struct { closers []func() cfgReputation cfgReputation + + mainChainClient *client.Client } type cfgGRPC struct { diff --git a/cmd/neofs-node/morph.go b/cmd/neofs-node/morph.go index 4d5127622..845ebb04d 100644 --- a/cmd/neofs-node/morph.go +++ b/cmd/neofs-node/morph.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "time" "github.com/nspcc-dev/neo-go/pkg/core/block" "github.com/nspcc-dev/neo-go/pkg/util" @@ -27,31 +28,51 @@ var ( func initMorphComponents(c *cfg) { var err error - addresses := c.viper.GetStringSlice(cfgMorphRPCAddress) - if len(addresses) == 0 { - fatalOnErr(errNoRPCEndpoints) - } - - crand := rand.New() // math/rand with cryptographic source - crand.Shuffle(len(addresses), func(i, j int) { - addresses[i], addresses[j] = addresses[j], addresses[i] - }) - - for i := range addresses { - c.cfgMorph.client, err = client.New(c.key, addresses[i]) - if err == nil { - c.log.Info("neo RPC connection established", - zap.String("endpoint", addresses[i])) - - break + fn := func(endpointCfg, dialTOCfg string, handler func(*client.Client)) { + addresses := c.viper.GetStringSlice(endpointCfg) + if len(addresses) == 0 { + fatalOnErr(errNoRPCEndpoints) } - c.log.Info("failed to establish neo RPC connection, trying another", - zap.String("endpoint", addresses[i]), - zap.String("error", err.Error())) + crand := rand.New() // math/rand with cryptographic source + crand.Shuffle(len(addresses), func(i, j int) { + addresses[i], addresses[j] = addresses[j], addresses[i] + }) + + var dialTimeout time.Duration + + if dialTOCfg != "" { + dialTimeout = c.viper.GetDuration(dialTOCfg) + } + + for i := range addresses { + cli, err := client.New(c.key, addresses[i], client.WithDialTimeout(dialTimeout)) + if err == nil { + c.log.Info("neo RPC connection established", + zap.String("endpoint", addresses[i])) + + handler(cli) + + break + } + + c.log.Info("failed to establish neo RPC connection, trying another", + zap.String("endpoint", addresses[i]), + zap.String("error", err.Error())) + } + + fatalOnErr(err) } - fatalOnErr(err) + // replace to a separate initialing block during refactoring + // since current function initializes sidechain components + fn(cfgMainChainRPCAddress, cfgMainChainDialTimeout, func(cli *client.Client) { + c.mainChainClient = cli + }) + + fn(cfgMorphRPCAddress, "", func(cli *client.Client) { + c.cfgMorph.client = cli + }) staticClient, err := client.NewStatic( c.cfgMorph.client,