From 7319ca5a00aa8ac368f76495b4ae23f046f24162 Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Wed, 5 May 2021 23:54:08 +0300 Subject: [PATCH] [#504] morph/client: Add more global config value getters Including: - typo fix for `StringFromStackItem` error msg - EigenTrust alpha getter. - renaming local var in reading uin64 values from global cfg function Signed-off-by: Pavel Karpy --- pkg/morph/client/netmap/config.go | 4 +++ pkg/morph/client/netmap/wrapper/config.go | 37 +++++++++++++++++++++-- pkg/morph/client/util.go | 2 +- 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/pkg/morph/client/netmap/config.go b/pkg/morph/client/netmap/config.go index b9e861906..52bd1b369 100644 --- a/pkg/morph/client/netmap/config.go +++ b/pkg/morph/client/netmap/config.go @@ -59,3 +59,7 @@ func (c *Client) Config(args ConfigArgs, assert func(stackitem.Item) (interface{ func IntegerAssert(item stackitem.Item) (interface{}, error) { return client.IntFromStackItem(item) } + +func StringAssert(item stackitem.Item) (interface{}, error) { + return client.StringFromStackItem(item) +} diff --git a/pkg/morph/client/netmap/wrapper/config.go b/pkg/morph/client/netmap/wrapper/config.go index 4900eead1..b74cc99d3 100644 --- a/pkg/morph/client/netmap/wrapper/config.go +++ b/pkg/morph/client/netmap/wrapper/config.go @@ -1,6 +1,8 @@ package wrapper import ( + "strconv" + "github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap" "github.com/pkg/errors" ) @@ -12,6 +14,7 @@ const ( epochDurationConfig = "EpochDuration" containerFeeConfig = "ContainerFee" etIterationsConfig = "EigenTrustIterations" + etAlphaConfig = "EigenTrustAlpha" irCandidateFeeConfig = "InnerRingCandidateFee" withdrawFeeConfig = "WithdrawFee" ) @@ -81,6 +84,17 @@ func (w *Wrapper) EigenTrustIterations() (uint64, error) { return iterations, nil } +// EigenTrustAlpha returns global configuration value of alpha parameter. +// It receives the alpha as a string and tries to convert it to float. +func (w *Wrapper) EigenTrustAlpha() (float64, error) { + strAlpha, err := w.readStringConfig(etAlphaConfig) + if err != nil { + return 0, errors.Wrapf(err, "(%T) could not get eigen trust alpha", w) + } + + return strconv.ParseFloat(strAlpha, 64) +} + // InnerRingCandidateFee returns global configuration value of fee paid by // node to be in inner ring candidates list. func (w *Wrapper) InnerRingCandidateFee() (uint64, error) { @@ -114,10 +128,29 @@ func (w *Wrapper) readUInt64Config(key string) (uint64, error) { v := vals.Value() - sz, ok := v.(int64) + numeric, ok := v.(int64) if !ok { return 0, errors.Errorf("(%T) invalid value type %T", w, v) } - return uint64(sz), nil + return uint64(numeric), nil +} + +func (w *Wrapper) readStringConfig(key string) (string, error) { + args := netmap.ConfigArgs{} + args.SetKey([]byte(key)) + + vals, err := w.client.Config(args, netmap.StringAssert) + if err != nil { + return "", err + } + + v := vals.Value() + + str, ok := v.(string) + if !ok { + return "", errors.Errorf("(%T) invalid value type %T", w, v) + } + + return str, nil } diff --git a/pkg/morph/client/util.go b/pkg/morph/client/util.go index da61e276c..883614242 100644 --- a/pkg/morph/client/util.go +++ b/pkg/morph/client/util.go @@ -87,7 +87,7 @@ func ArrayFromStackItem(param stackitem.Item) ([]stackitem.Item, error) { // StringFromStackItem receives string value from the value of a smart contract parameter. func StringFromStackItem(param stackitem.Item) (string, error) { if param.Type() != stackitem.ByteArrayT { - return "", errors.Errorf("chain/client: %s is not an integer type", param.Type()) + return "", errors.Errorf("chain/client: %s is not an string type", param.Type()) } return stackitem.ToString(param)