From 90c38fc5e59c0e86520a2aed3ba8120382c59087 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Thu, 22 Oct 2020 17:19:07 +0300 Subject: [PATCH] [#40] morph/client: Implement read config method Signed-off-by: Leonard Lyubich --- pkg/morph/client/netmap/client.go | 19 +++++++++- pkg/morph/client/netmap/config.go | 61 +++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 pkg/morph/client/netmap/config.go diff --git a/pkg/morph/client/netmap/client.go b/pkg/morph/client/netmap/client.go index 41de084f0..544082749 100644 --- a/pkg/morph/client/netmap/client.go +++ b/pkg/morph/client/netmap/client.go @@ -37,7 +37,8 @@ type cfg struct { snapshotMethod, // get network map snapshot method name updateStateMethod, // update state method name for invocation innerRingListMethod, // IR list method name for invocation - epochMethod string // get epoch number method name + epochMethod, // get epoch number method name + configMethod string // get config value method name } const ( @@ -48,6 +49,7 @@ const ( defaultUpdateStateMethod = "updateState" // default update state method name defaultInnerRIngListMethod = "innerRingList" // default IR list method name defaultEpochMethod = "epoch" // default get epoch number method name + defaultConfigMethod = "config" // default get config value method name ) func defaultConfig() *cfg { @@ -59,6 +61,7 @@ func defaultConfig() *cfg { updateStateMethod: defaultUpdateStateMethod, innerRingListMethod: defaultInnerRIngListMethod, epochMethod: defaultEpochMethod, + configMethod: defaultConfigMethod, } } @@ -177,3 +180,17 @@ func WithEpochMethod(n string) Option { } } } + +// WithConfigMethod returns a client constructor option that +// specifies the method name of config value receiving operation. +// +// Ignores empty value. +// +// If option not provided, "config" is used. +func WithConfigMethod(n string) Option { + return func(c *cfg) { + if n != "" { + c.configMethod = n + } + } +} diff --git a/pkg/morph/client/netmap/config.go b/pkg/morph/client/netmap/config.go new file mode 100644 index 000000000..b9e861906 --- /dev/null +++ b/pkg/morph/client/netmap/config.go @@ -0,0 +1,61 @@ +package netmap + +import ( + "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" + "github.com/nspcc-dev/neofs-node/pkg/morph/client" + "github.com/pkg/errors" +) + +// ConfigArgs groups the arguments +// of get config value test invoke call. +type ConfigArgs struct { + key []byte +} + +// EpochValues groups the stack parameters +// returned by get epoch number test invoke. +type ConfigValues struct { + val interface{} +} + +// SetKey sets binary key to configuration parameter. +func (c *ConfigArgs) SetKey(v []byte) { + c.key = v +} + +// Value returns configuration value. +func (c ConfigValues) Value() interface{} { + return c.val +} + +// Config performs the test invoke of get config value +// method of NeoFS Netmap contract. +func (c *Client) Config(args ConfigArgs, assert func(stackitem.Item) (interface{}, error)) (*ConfigValues, error) { + items, err := c.client.TestInvoke( + c.configMethod, + args.key, + ) + if err != nil { + return nil, errors.Wrapf(err, + "could not perform test invocation (%s)", + c.configMethod) + } + + if ln := len(items); ln != 1 { + return nil, errors.Errorf("unexpected stack item count (%s): %d", + c.configMethod, ln) + } + + val, err := assert(items[0]) + if err != nil { + return nil, errors.Wrap(err, "value type assertion failed") + } + + return &ConfigValues{ + val: val, + }, nil +} + +func IntegerAssert(item stackitem.Item) (interface{}, error) { + return client.IntFromStackItem(item) +}