[#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 <carpawell@nspcc.ru>
This commit is contained in:
Pavel Karpy 2021-07-23 17:37:56 +03:00 committed by Alex Vanin
parent 28aa0f521e
commit 896c749b92
4 changed files with 56 additions and 59 deletions

View file

@ -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...),
)
}

View file

@ -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")

View file

@ -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
}
}

View file

@ -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 {