[#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 <carpawell@nspcc.ru>
This commit is contained in:
Pavel Karpy 2021-05-05 23:54:08 +03:00 committed by Alex Vanin
parent dddbf0368c
commit 7319ca5a00
3 changed files with 40 additions and 3 deletions

View file

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

View file

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

View file

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