From 896c749b92e220f8d0926b8491b0a485aab4e978 Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Fri, 23 Jul 2021 17:37:56 +0300 Subject: [PATCH] [#720] pkg/innerring: Check Notary availability automatically Do not read `without_notary` config value from env. Make morph client constructor return client without notary support. Enabling notary support should be done with public `EnableNotarySupport` method separately. Notary availability is deducted with client. Further, if notary is presented on chain its support is enabled at the corresponding client. Signed-off-by: Pavel Karpy --- pkg/innerring/innerring.go | 87 +++++++++++++++++++-------------- pkg/innerring/notary.go | 6 +-- pkg/morph/client/constructor.go | 18 +------ pkg/morph/client/notary.go | 4 +- 4 files changed, 56 insertions(+), 59 deletions(-) diff --git a/pkg/innerring/innerring.go b/pkg/innerring/innerring.go index 77aac0cd6..c4af538c5 100644 --- a/pkg/innerring/innerring.go +++ b/pkg/innerring/innerring.go @@ -303,7 +303,6 @@ func New(ctx context.Context, log *zap.Logger, cfg *viper.Viper) (*Server, error // parse notary support server.feeConfig = config.NewFeeConfig(cfg) - server.mainNotaryConfig, server.sideNotaryConfig = parseNotaryConfigs(cfg) // prepare inner ring node private key acc, err := utilConfig.LoadAccount( @@ -316,25 +315,6 @@ func New(ctx context.Context, log *zap.Logger, cfg *viper.Viper) (*Server, error server.key = acc.PrivateKey() - withoutMainNet := cfg.GetBool("without_mainnet") - - // get all script hashes of contracts - server.contracts, err = parseContracts( - cfg, - withoutMainNet, - server.mainNotaryConfig.disabled, - server.sideNotaryConfig.disabled, - ) - if err != nil { - return nil, err - } - - // parse default validators - server.predefinedValidators, err = parsePredefinedValidators(cfg) - if err != nil { - return nil, fmt.Errorf("ir: can't parse predefined validators list: %w", err) - } - morphChain := &chainParams{ log: log, cfg: cfg, @@ -348,18 +328,14 @@ func New(ctx context.Context, log *zap.Logger, cfg *viper.Viper) (*Server, error return nil, err } - // enable notary support in the client - var morphNotaryOpts []client.NotaryOption - if !server.sideNotaryConfig.disabled { - morphNotaryOpts = append(morphNotaryOpts, client.WithProxyContract(server.contracts.proxy)) - } - // create morph client - server.morphClient, err = createClient(ctx, morphChain, morphNotaryOpts...) + server.morphClient, err = createClient(ctx, morphChain) if err != nil { return nil, err } + withoutMainNet := cfg.GetBool("without_mainnet") + if withoutMainNet { // This works as long as event Listener starts listening loop once, // otherwise Server.Start will run two similar routines. @@ -376,21 +352,57 @@ func New(ctx context.Context, log *zap.Logger, cfg *viper.Viper) (*Server, error return nil, err } - // enable notary support in the client - var mainnetNotaryOpts []client.NotaryOption - if !server.mainNotaryConfig.disabled { - mainnetNotaryOpts = append(mainnetNotaryOpts, - client.WithProxyContract(server.contracts.processing), - client.WithAlphabetSource(server.morphClient.Committee)) - } - // create mainnet client - server.mainnetClient, err = createClient(ctx, mainnetChain, mainnetNotaryOpts...) + server.mainnetClient, err = createClient(ctx, mainnetChain) if err != nil { return nil, err } } + server.mainNotaryConfig, server.sideNotaryConfig = parseNotaryConfigs( + cfg, + server.morphClient.ProbeNotary(), + !withoutMainNet && server.mainnetClient.ProbeNotary(), // if mainnet disabled then notary flag must be disabled too + ) + + // get all script hashes of contracts + server.contracts, err = parseContracts( + cfg, + withoutMainNet, + server.mainNotaryConfig.disabled, + server.sideNotaryConfig.disabled, + ) + if err != nil { + return nil, err + } + + if !server.sideNotaryConfig.disabled { + // enable notary support in the side client + err = server.morphClient.EnableNotarySupport( + client.WithProxyContract(server.contracts.proxy), + ) + if err != nil { + return nil, fmt.Errorf("could not enable side chain notary support: %w", err) + } + } + + if !server.mainNotaryConfig.disabled { + // enable notary support in the main client + err = server.mainnetClient.EnableNotarySupport( + client.WithProxyContract(server.contracts.processing), + client.WithAlphabetSource(server.morphClient.Committee), + ) + if err != nil { + return nil, fmt.Errorf("could not enable main chain notary support: %w", err) + } + } + + // parse default validators + server.predefinedValidators, err = parsePredefinedValidators(cfg) + if err != nil { + return nil, fmt.Errorf("ir: can't parse predefined validators list: %w", err) + } + server.pubKey = server.key.PublicKey().Bytes() auditPool, err := ants.NewPool(cfg.GetInt("audit.task.exec_pool_size")) @@ -845,14 +857,13 @@ func createListener(ctx context.Context, p *chainParams) (event.Listener, error) return listener, err } -func createClient(ctx context.Context, p *chainParams, notaryOpts ...client.NotaryOption) (*client.Client, error) { +func createClient(ctx context.Context, p *chainParams) (*client.Client, error) { return client.New( p.key, p.cfg.GetString(p.name+".endpoint.client"), client.WithContext(ctx), client.WithLogger(p.log), client.WithDialTimeout(p.cfg.GetDuration(p.name+".dial_timeout")), - client.WithNotaryOptions(notaryOpts...), ) } diff --git a/pkg/innerring/notary.go b/pkg/innerring/notary.go index 146a7570b..2ae88b3df 100644 --- a/pkg/innerring/notary.go +++ b/pkg/innerring/notary.go @@ -73,18 +73,18 @@ func awaitNotaryDepositInClient(ctx context.Context, cli *client.Client, txHash return errDepositTimeout } -func parseNotaryConfigs(cfg *viper.Viper) (main, side *notaryConfig) { +func parseNotaryConfigs(cfg *viper.Viper, withSideNotary, withMainNotary bool) (main, side *notaryConfig) { main = new(notaryConfig) side = new(notaryConfig) - if cfg.GetBool("without_notary") { + if !withSideNotary { main.disabled = true side.disabled = true return } - main.disabled = cfg.GetBool("without_main_notary") + main.disabled = withMainNotary main.amount = fixedn.Fixed8(cfg.GetInt64("notary.main.deposit_amount")) main.duration = cfg.GetUint32("timers.main_notary") diff --git a/pkg/morph/client/constructor.go b/pkg/morph/client/constructor.go index 7d8726168..e3a7ed542 100644 --- a/pkg/morph/client/constructor.go +++ b/pkg/morph/client/constructor.go @@ -2,7 +2,6 @@ package client import ( "context" - "fmt" "time" "github.com/nspcc-dev/neo-go/pkg/core/native/nativenames" @@ -28,8 +27,6 @@ type cfg struct { gas util.Uint160 // native gas script-hash waitInterval time.Duration - - notaryOpts []NotaryOption } const ( @@ -47,6 +44,8 @@ func defaultConfig() *cfg { } // New creates, initializes and returns the Client instance. +// Notary support should be enabled with EnableNotarySupport client +// method separately. // // If private key is nil, it panics. // @@ -105,12 +104,6 @@ func New(key *keys.PrivateKey, endpoint string, opts ...Option) (*Client, error) waitInterval: cfg.waitInterval, } - if len(cfg.notaryOpts) != 0 { - if err := c.enableNotarySupport(cfg.notaryOpts...); err != nil { - return nil, fmt.Errorf("can't enable notary support: %w", err) - } - } - return c, nil } @@ -155,10 +148,3 @@ func WithLogger(logger *logger.Logger) Option { } } } - -// WithNotaryOptions enables notary support and sets notary options for the client. -func WithNotaryOptions(opts ...NotaryOption) Option { - return func(c *cfg) { - c.notaryOpts = opts - } -} diff --git a/pkg/morph/client/notary.go b/pkg/morph/client/notary.go index 27495b601..cd63a8d8e 100644 --- a/pkg/morph/client/notary.go +++ b/pkg/morph/client/notary.go @@ -65,10 +65,10 @@ func defaultNotaryConfig(c *Client) *notaryCfg { } } -// enableNotarySupport creates notary structure in client that provides +// EnableNotarySupport creates notary structure in client that provides // ability for client to get alphabet keys from committee or provided source // and use proxy contract script hash to create tx for notary contract. -func (c *Client) enableNotarySupport(opts ...NotaryOption) error { +func (c *Client) EnableNotarySupport(opts ...NotaryOption) error { cfg := defaultNotaryConfig(c) for _, opt := range opts {